Texturing Problem ( Basic )

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()
{
	glClearColor(1,1,1,1);
	glClear(GL_COLOR_BUFFER_BIT);

	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);
	fclose(file);

	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
	glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	gluBuild2DMipmaps(GL_TEXTURE_2D,3,w,h, GL_RGB ,GL_UNSIGNED_BYTE ,data);

	free(data);		
	glBegin(GL_QUADS);
		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);
	glEnd();

	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 );

Well, I would be appreciated for your help guys …

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.

Yeah, Your right. It’s better to figuring out the concepts first.