Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Texture wont show on quad.


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 13 February 2013 - 01:49 PM

I made this thread in For Beginners, but noone there was able to help.

 

 

I was having trouble loading the image, but have since fixed that. Now I cant seem to get the image to actually show on the screen. Instead its just the blue rectangle I have drawn with no texture on it.

 

#include "SDL.h"
#include "SDL_image.h"
#include "SDL_opengl.h"
#include <math.h>
#include <stdio.h>

int loadImage();
void drawImage();

extern SDL_Event event;

GLuint texture = NULL;
GLenum texture_format=NULL;
GLint nofcolors;

bool initGL()
{
    //Initialize Projection Matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    //Initialize Modelview Matrix
    glOrtho(0, 1200, 700, 0, -1, 1);
	glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    //Initialize clear color
   glClearColor(1.0,.4,0.0,0.0);
   glEnable( GL_TEXTURE_2D);
    //Check for error
    GLenum error = glGetError();
    if( error != GL_NO_ERROR )
    {
        printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
        return false;
    }

    return true;
}

bool init()
{
    //Initialize SDL
    if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    {
        return false;
    }

	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1);

    //Create Window
    if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
    {
        return false;
    }

    //Enable unicode
    SDL_EnableUNICODE( SDL_TRUE );

	


    //Initialize OpenGL
    if( initGL() == false )
    {
        return false;
    }

    //Set caption
    SDL_WM_SetCaption( "Project Birth Game Window. Guns&Player.", NULL );

    return true;
}




int main( int argc, char *argv[] )
{
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //The frame rate regulator
    Timer fps;
	fps.start();
	loadImage(); 


	int x = 0, y = 0;
	//Wait for user exit
	while( quit == false )
	{
        //Start the frame timer
        
		
        //While there are events to handle
		while( SDL_PollEvent( &event ) )
		{
			if( event.type == SDL_QUIT )
			{
                quit = true;
            }
			
			else if( event.type == SDL_KEYDOWN )
            {
                //Handle keypress with current mouse position
                
				SDL_GetMouseState( &x, &y );
                
			}
		}

	
		drawImage(); 

       
        update(); 
		
		SDL_GL_SwapBuffers();
		
        //Cap the frame rate
        if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
        {
            SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
        }
	}

	//Clean up
	clean_up();

	return 0;
}




int loadImage()
{


	SDL_Surface* surface;
	
	
	if((surface = IMG_Load("Avatar Basic Character Model.jpg")))
	{
	
		

	//get number of channels in the SDL surface
	nofcolors = surface -> format -> BytesPerPixel;
	
   //contains an alpha channel
	if(nofcolors == 4)
	{
		if ( surface -> format -> Rmask == 0x000000ff)
			texture_format = GL_RGBA;
		else
			texture_format = GL_BGRA;
	}
	else if ( nofcolors == 3) //no alpha channel
	{
		if(surface ->format->Rmask==0x000000ff)
			texture_format=GL_RGB;
		else
			texture_format=GL_BGR;
	}

	//Have Opengl generate a texture object handle for us
	glGenTextures(1, &texture);

	//Bind the texture object
	glBindTexture( GL_TEXTURE_2D, texture);

	//Set the texture's stretching properties
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glPixelStorei(GL_UNPACK_ALIGNMENT,1);

	
	glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface -> w, surface ->h, 0, texture_format, GL_UNSIGNED_BYTE, surface -> pixels );
	
	}
	else //could not load image
		return -1;


if (surface)
{
	SDL_FreeSurface(surface);
}

return 1; 

}


void drawImage()
{

	//Clear the screen before drawing 
	glClear( GL_COLOR_BUFFER_BIT);

	//Bind the texture to which subsequent calls refer to
	glBindTexture( GL_TEXTURE_2D, texture);

	glPushMatrix();
	glTranslatef(300,300,0.0f);
	glBegin(GL_QUADS);
	//Set color
	glColor4f(0.0,0.0,1.0,0.0);

	
	glTexCoord2i(1,1);
	glVertex2f(150.0,0);//bottom right
	
	glTexCoord2i(0,1);
	glVertex2f(150.0,-50);//top right
	
	glTexCoord2i(0,0);
	glVertex2f(0.0,-50);//top left
	
	glTexCoord2i(1,0);
	glVertex2f(0.0,0);//bottom left
    

	glEnd();
	
	glPopMatrix();

	
}

 

At first I was having issues with loading the image, but thats no longer an issue. Any idea on why the image wont show on the quad? Perhaps an issue with binding?

 



Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 8454

Like
0Likes
Like

Posted 13 February 2013 - 05:03 PM

The code works as it is for me. Check for OpenGL errors in your code.



#3 GDsnakes   Members   -  Reputation: 145

Like
0Likes
Like

Posted 13 February 2013 - 05:19 PM

The code works as it is for me. Check for OpenGL errors in your code.

The image shows on screen for you? How can that be?



#4 fastcall22   Crossbones+   -  Reputation: 4389

Like
0Likes
Like

Posted 13 February 2013 - 05:37 PM

I see an IMG_Load for a JPG file, but I don't see IMG_Init anywhere in your code. Have you initialized SDL_Image properly?


EDIT:
References 'n' stuff.

c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#5 Brother Bob   Moderators   -  Reputation: 8454

Like
0Likes
Like

Posted 13 February 2013 - 05:37 PM

Probably because there's nothing wrong with the code itself, but the environment in which you run it. For example, you may be trying to load a non-power of two texture in an environment that can only handle power of two textures. As I said, check for errors.



#6 L. Spiro   Crossbones+   -  Reputation: 14032

Like
0Likes
Like

Posted 13 February 2013 - 06:20 PM

Also this is not related to your question but you may find it helpful anyway to know that when using C++ you should use the C++ headers instead of the C headers.

 

#include <math.h> #include <cmath>

#include <stdio.h> #include <cstdio>

 

 

L. Spiro


It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#7 BGB   Crossbones+   -  Reputation: 1554

Like
1Likes
Like

Posted 14 February 2013 - 12:28 AM

Also this is not related to your question but you may find it helpful anyway to know that when using C++ you should use the C++ headers instead of the C headers.
 
#include <math.h> #include <cmath>
#include <stdio.h> #include <cstdio>
 
 
L. Spiro

while fair enough, it is not made clear here that the code in question is C++, rather than C99 (clarification: C code looking like that given will generally be accepted by GCC, which implements C99 support, well, along with much of C11). (this comment is N/A if in-fact the OP is using C++).

Edited by cr88192, 14 February 2013 - 12:58 AM.


#8 L. Spiro   Crossbones+   -  Reputation: 14032

Like
0Likes
Like

Posted 14 February 2013 - 04:23 PM

Something else to mention to the original topic-poster: Fixed your random-style indentations. You have random indentations all over the place. Why? It is just shooting yourself in the foot.
There are guidelines for indenting that, time-tested and mother-approved, will help both you and anyone else read your code. You should adhere to indentation guidelines strictly.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS