• Advertisement
Sign in to follow this  

Textures in OpenGL

This topic is 3768 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Alrighty..so here's my initialization function:
int VideoInit( int width, int height )
{
	if( video_surface != NULL ) return 1;
	
	SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
	SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
	SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
	
	video_surface = SDL_SetVideoMode( width, height, 0, SDL_OPENGL );
	
	if( video_surface == NULL ) return -1;
	
	SDL_WM_SetCaption( "OpenGL test", NULL );
	
	glEnable( GL_TEXTURE_2D );
	
	glClearColor( 0.0, 0.0, 0.0, 0.0 );
	glMatrixMode( GL_PROJECTION );
	glLoadIdentity( );
	glOrtho( 0.0, (GLdouble) width, (GLdouble) height, 0.0, -0.1, 0.1 );
	glMatrixMode( GL_MODELVIEW );
	glLoadIdentity( );
	
	video_x = 0;
	video_y = 0;
	video_width  = width;
	video_height = height;
	
	return 0;
}


And here's my function to draw a texture:
void DrawTexture( Texture *tex, int x, int y )
{
	glBindTexture( GL_TEXTURE_2D, (GLint) tex->handle );
	
	glBegin( GL_QUADS );
		glTexCoord2i( 0, 0 ); glVertex2i( x, y );
		glTexCoord2i( 1, 0 ); glVertex2i( x + tex->width, y );
		glTexCoord2i( 1, 1 ); glVertex2i( x + tex->width, y + tex->height );
		glTexCoord2i( 0, 1 ); glVertex2i( x, y + tex->height );
	glEnd( );
}


I've had this problem before, but that was years ago so I forgot the solution. The texture is appearing in the correct place, but instead of the image, it's all white. Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Do you do anything with the recently generated texture handle OpenGL gives you before generating another one? I had done that once and it would give me the same handle all the time (if I didn't do anything between generation calls, of course) and there would be the same (the last one create) texture everywhere.

Share this post


Link to post
Share on other sites
Your image-loading library is not loading the pixel data into the texture. OpenGL uses a white, opaque texture surrogate for any declared texture, which must have pixel data loaded into it.

Share this post


Link to post
Share on other sites
Here's my texture loading function:


Texture* LoadBitmap( const char *filename )
{
Texture *tex;
BitmapFileHeader bfh;
BitmapInfoHeader bih;
FILE *file;
char *bits;
int x;
int y;

/* Open file */
file = fopen( filename, "r" );

if( file == NULL ) return NULL;

/* Check header */
fread( &bfh, 14, 1, file );
fread( &bih, 40, 1, file );

if( feof( file ) == 1 ) return NULL;

if( ( bfh.sig[0] != 'B' ) || ( bfh.sig[1] != 'M' ) ) return NULL;
if( bih.structsize != 40 ) return NULL;
if( bih.planes != 1 ) return NULL;
if( bih.bpp != 24 ) return NULL;
if( bih.compression != 0 ) return NULL;

/* Setup texture */
tex = malloc( sizeof(Texture) );
tex->width = bih.width;
tex->height = bih.height;
tex->box.left = 0;
tex->box.right = bih.width;
tex->box.top = 0;
tex->box.bottom = bih.height;

/* Load bits */
bits = malloc( bih.width * bih.height * 3 );

for( y = bih.height - 1; y >= 0; y-- )
{
for( x = 0; x < bih.width; x++ )
{
fread( &bits[ ( ( y * bih.width ) + x ) * 3 ], 3, 1, file );
}

fseek( file, 4 - ( ( bih.width * 3 ) % 4 ), SEEK_CUR );
}

/* Register texture */
glGenTextures( 1, (GLint*) &tex->handle );

if( tex->handle == NULL )
{
free( bits );
return NULL;
}

glBindTexture( GL_TEXTURE_2D, (GLint) tex->handle );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, bih.width, bih.height, 0, GL_RGB, GL_BYTE,
bits );

free( bits );
return tex;
}

Share this post


Link to post
Share on other sites
Thanks for all the feedback. It turns out i just needed to use glTexParameteri. Don't use any of this code though, because I discovered zillions of other errors too.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement