View Full Version : Texturing Problem ( Basic )

Ehsan Nekooee
08-27-2009, 02:56 PM
Hi everyone

I tried to code my very first texture code thing. But I don't know
Why always I just get a distorted image! here is my code:

GLuint loadTexture()

int w, h;
GLbyte * data;
FILE * file;

w = 750 ;
h = 704 ;

GLuint texture;

data = (GLbyte*) malloc ( h * w * 3 * 2 );
file = fopen("image.raw", "r");
if (file == NULL) return 0;
fread(data , h * w * 3 ,1 ,file);

glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
gluBuild2DMipmaps(GL_TEXTURE_2D,3,w,h, GL_RGB ,GL_UNSIGNED_BYTE ,data);

glTexCoord2d(0.0,0.0); glVertex2d(0.0,0.0);
glTexCoord2d(1.0,0.0); glVertex2d(100.0,0.0);
glTexCoord2d(1.0,1.0); glVertex2d(100.0,100.0);
glTexCoord2d(0.0,1.0); glVertex2d(0.0,100.0);

return texture;

And one more thing, there were no 2 in the tutorial I was following but It didn't work without 2 in the line below. why is that 2 for?

data = (GLbyte*) malloc ( h * w * 3 * 2 );

Ehsan Nekooee
08-28-2009, 01:32 AM
Well, I would be appreciated for your help guys ...

08-28-2009, 01:34 AM
key to understand this is to understand this rather then copy n paste. basically any image has dimension, pixelsize (or bitdepth) and an special order of the channels (RGBA/ABGR). the later two components can be described as the pixel format (RGBA_8888/RGBA_5551). when you malloc you need to ensure the correct pixel data size of the image thus width * height * pixelsize f.e. 512 * 512 * 4 (RGBA ala 8bit per channel) equals 1048576 bytes. without knowing how the data is stored you will get wrong channels order (swapped colors) or distorted data what you have now. advice you to forget about .raw and use some image formats like TGA or BMP since they are easy to read. all the image data you listed above is stored in the header of the image files so you can easily retrieve them, malloc, read and upload to OpenGL.

Ehsan Nekooee
08-28-2009, 01:47 AM
Yeah, Your right. It's better to figuring out the concepts first.