Please can someone tell me whats wrong with this code?
typedef struct
{
GLubyte* texture_buffer;
}PPMtexture;
GLint textura;
int n,i = 0;
int h,w;
FILE *fp;
PPMtexture *ptr;
static GLuint texName;
void PPMtex(char* filename)
{
char header[32];
int sizeX, sizeY, range;
fprintf(stderr, "Opening PPM texture file: %s
", filename);
// Read in the header.
fp = fopen(filename,"rb");
fscanf( fp, "%s
%d %d
%d
", header, &sizeX, &sizeY, &range );
if (strcmp(header, “P6”))
{
fprintf(stderr, "Error1: texture %s is in incorrect format.
", filename);
fclose(fp);
exit(-1);
}
if (range != 255)
{
fprintf(stderr, "Error2: texture %s is in incorrect format.
", filename);
fclose(fp);
exit(-1);
}
h=sizeY;
w=sizeX;
// (cod original) texture_buffer = new GLubyte[sizeX*sizeY*3];
// reserva espaço para sizeX*sizeY*3 GLubytes
ptr = malloc (sizeof (PPMtexture));
(ptr->texture_buffer) = calloc (sizeX*sizeY*3, sizeof(GLubyte));
fread((ptr->texture_buffer),1,sizeX*sizeY*3,fp);
fclose(fp);
}
void makeTex()
{
glClearColor(0,0,0,0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
PPMtex("proj.ppm");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w,h, 0, GL_RGB, GL_UNSIGNED_BYTE, (ptr->texture_buffer));
fp = fopen("1.txt","wb");
fwrite((ptr->texture_buffer),1,w*h*3,fp);
fclose(fp);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, texName);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f( -3, -3, 0);
glTexCoord2f(1,0);
glVertex3f( 3,-3, 0);
glTexCoord2f(1,1);
glVertex3f( 3,3, 0);
glTexCoord2f(0,1);
glVertex3f(-3,3, 0);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
}
The result is always the same… no textures, everything else in my code is working (no textures). Im using format p6 (binary) in my ppm files. I think the problem is problably on the calloc, but it seems correct to me, the read and write can also be the prob… Please i really need to make this.
Thanks, JML.