PDA

View Full Version : TEXTURES, ppm format P6, help please



JOSE ML
11-29-2001, 11:32 AM
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\n", filename);

// Read in the header.
fp = fopen(filename,"rb");
fscanf( fp, "%s\n%d %d\n%d\n", header, &sizeX, &sizeY, &range );
if (strcmp(header, "P6"))
{
fprintf(stderr, "Error1: texture %s is in incorrect format.\n", filename);
fclose(fp);
exit(-1);
}
if (range != 255)
{
fprintf(stderr, "Error2: texture %s is in incorrect format.\n", 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.

Bob
11-29-2001, 12:29 PM
Did you run your code through a debugger? If not, do it and try to locate the problem. Do you check for error messages? If not, do it. You assume the file is opened correctly (check for NULL-pointer), you assume memory is allocated correctly (again, check NULL-pointer), you assume the file is read correctly (check number of bytes returned by fread), you assume you use OpenGL correctly (use glGetError in various places). Assuming things is the worst thing you can do.

Apart from that, are you perhaps using non-power of two texture sizes?

dorbie
11-29-2001, 08:01 PM
Bob's comment about non power of two textures is saying that your texture dimensions must be 2 to the power of n.

i.e. 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, etc.

So you can have textures which are 256x512 for example, but you cannot have a texture which is 135x432. This is just not supported in basic OpenGL. Because your dimensions are determined by the size of the PPM, there is no way of telling if the texture you get is a legal size. YOU must ensure that image on disk is the correct size.