PDA

View Full Version : Stuck with 1 texture



celem
11-09-2010, 05:55 AM
Heya folks, I'm an OpenGL beginner and i'm currently playing about with code from various tutorials.

What im trying to do just now is create the basics of a tile engine, using C++ (MVSC++ 08) and OpenGL

The issue I have is that while i'm able to load a .raw image into my program and make a texture from it, I encounter some problems when trying to work with multiple textures, they are all identical.

So, I assign an array of 3 GLuint's, i then assign each element in the array to textures returned by the LoadTextureRAW() method.


GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;

// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;

// allocate buffer
width = 64;
height = 64;
data = (BYTE*)malloc( width * height * 3 );

// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );

// allocate a texture name
glGenTextures( 1, &texture );

// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );

// when texture area is small, bilinear filter the closest MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first MIP map
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );

// build our texture MIP maps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width,
height, GL_RGB, GL_UNSIGNED_BYTE, data );

// free buffer
free( data );

return texture;

}
GLuint texture_id[3];
....
texture_id[0] = LoadTextureRAW( "blue.raw", TRUE );
texture_id[1] = LoadTextureRAW( "red.raw", TRUE );
texture_id[2] = LoadTextureRAW( "green.raw", TRUE );
.....


The problem is that this code should put blue.raw onto my quad, replacing texture_id[0] with 1 or 2 should then paint red.raw and green.raw respectively.


glBegin(GL_QUADS);
glBindTexture( GL_TEXTURE_2D, texture_id[0]);
glColor3d(1,1,1);
glTexCoord2d(0.0,0.0); glVertex3d(float(x-0.1), float(y+0.1), 0.0f);
glTexCoord2d(1.0,0.0); glVertex3d(float(x+0.1), float(y+0.1), 0.0f);
glTexCoord2d(1.0,1.0); glVertex3d(float(x+0.1), float(y-0.1), 0.0f);
glTexCoord2d(0.0,1.0); glVertex3d(float(x-0.1), float(y-0.1), 0.0f);
glEnd();

What I actually get is green.raw painted at every location.

Can anyone spot what im doing wrong?
Can up the entire code if need be, these should be most relevant sections, most of the rest is just Window Handling

celem
11-09-2010, 06:22 AM
A little further prodding at this code has shown me that the problem seems to be where I declare

texture_id[0] = LoadTextureRAW( "blue.raw", TRUE );
texture_id[1] = LoadTextureRAW( "red.raw", TRUE );
texture_id[2] = LoadTextureRAW( "green.raw", TRUE );


Every texture will get green.raw because its the last line given, if I reorder these lines then the last called is the texture used everywhere.

FIXED: Moving the glBind line in my draw routine to above the glBegin line has fixed the issue

mhagain
11-09-2010, 03:50 PM
You should move this one out of the creation function too;

// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
glTexEnv doesn't work the same as glTexparameter; it's global state rather than per-texture.