Jump to content
  • Advertisement
Sign in to follow this  
Ne_cro

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.

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

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 );
    glBegin( GL_QUADS );
    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 this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by waryams
set 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 this post


Link to post
Share on other sites
Quote:
Original post by waryams
Sorry, 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Ne_cro
The 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 this post


Link to post
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

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!