Texture wont show on quad.

Started by
6 comments, last by L. Spiro 11 years, 2 months ago

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?

Advertisement

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

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?

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.

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.

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

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++).
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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

This topic is closed to new replies.

Advertisement