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