# OpenGL Texture problem

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

## Recommended Posts

Recently i started learning opengl, however i got stuck at textures, i dont know if im learning the right way, but its complicated for me. Im trying to apply a texture on to a square but everytime i compile and run, the texture appears in a different color than it should be. I dont know what im doing wrong, perhaps someone could point me in the right direction. Here is the function that loads the texture:
GLuint LoadTexture( const char * filename, int width, int height ){
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
data = (unsigned char *)malloc( width * height * 3 );
fread( data, width * height * 3, 1, file );
fclose( file );
glGenTextures( 1, &texture );
glBindTexture( GL_TEXTURE_2D, texture );
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
free( data );
return texture;
}


And here is the function that applies the texture onto the square:
void tile(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
texture = LoadTexture( "texture.bmp", 64, 64 );
glEnable( GL_TEXTURE_2D );
glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(0.2,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(0.2,0.2);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,0.2);
glEnd();
glFlush();
glutSwapBuffers();
}


I would appreciate any help. Ne_cro

##### Share on other sites
set glColor to pure white when you draw the texture. eg -

glColor4ub(255,255,255,255)

##### Share on other sites
Quote:
 Original post by waryamsset glColor to pure white when you draw the texture. eg - glColor4ub(255,255,255,255)

Actually i thought of that before, anyways i tried what you said but i still get the same result unfortunately.
I even tried to change between GL_DECAL and GL_MODULATE, but its still no use.

##### Share on other sites
Sorry, didn't notice the GL_DECAL =/

##### Share on other sites
Quote:
 Original post by waryamsSorry, didn't notice the GL_DECAL =/See if this thread helps.

Thanks alot i think i found what my problem was. bmp format uses BGR and not RGB. On that thread they suggested to use GL_BGR which i tried and didnt work and i guess its because its only supported in older versions of opengl.

The only solution i guess would be is to use raw images (haven't tried that yet) but i would like someone to show me how to use bmp files instead, without using glaux or glut.

Btw thanks for the help waryams :)

EDIT: Does anyone know a site where i can download raw textures?

Ne_cro

##### Share on other sites
Just to let you know, Targa is pretty much the same as BMP, but also allows transparency if you want it.

##### Share on other sites
Quote:
 Original post by Ne_croThe only solution i guess would be is to use raw images

I just tried to load a raw format texture and all i got was black and white lines. Im starting to think that i might be doing all of this wrong :( its really frustrating.

[Edited by - Ne_cro on May 25, 2007 10:02:30 PM]

##### Share on other sites
Yay i fixed it :) My problem was that bmp use BGR and not RGB. To the people that have the same problem, i had to modify my code like this:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR_EXT, GL_UNSIGNED_BYTE, data);

notice that i used GL_BGR_EXT instead of GL_RGB. This tells opengl that the pixel data is stored as blue then green and then red.

To use GL_BGR_EXT simply include <gl/glext.h>

Ne_cro

1. 1
Rutin
19
2. 2
3. 3
JoeJ
15
4. 4
5. 5

• 18
• 19
• 10
• 13
• 17
• ### Forum Statistics

• Total Topics
631694
• Total Posts
3001751
×