PDA

View Full Version : Textures not working



Esben
12-08-2009, 02:05 AM
Hi,

As I wrote in another thread, I am porting my OpenGL application from Mac to Windows. I am experiencing the following problem:

I have a PPM image, which I am loading into the following variable:



GLubyte texels[512][512][3];


In my init function, I am setting up a texture, using automatic generation of texture coordinates:



// texture image
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 512, 512, 0, GL_RGB,GL_UNSIGNED_BYTE, texels);

// set drawing mode to GL_DECAL so that the textured polygons are
//drawn using the colors from the texture map, and not polygon colors
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

// enable automatic texture coords generation
// (in two dimensions as the texture map is 2D)
glEnable( GL_TEXTURE_GEN_S);
glEnable( GL_TEXTURE_GEN_T);

// generate texture coords automatically, s is in the x-direction...
GLfloat x_dir[] = { 1, 0, 0 };
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_S, GL_OBJECT_PLANE, x_dir);

// .. t is in the z-direction
GLfloat z_dir[] = { 0, 0, 1 };
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
glTexGenfv(GL_T, GL_OBJECT_PLANE, z_dir);

// repeat the texture in both directions
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

// filtering (not totally sure what this is for)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);


Then, when I draw the polygons that I want to texture on, I specify



glEnable( GL_TEXTURE_2D);


before drawing the polygon and



glDisable( GL_TEXTURE_2D);


afterwards. On Mac this worked perfectly but now (on Windows) the textures are not used - instead, my polygons are just green. If I print out the values of the texels variable, they seem alright (they are values from 0 to 255) so I don't think it's the loading of the file that's gone wrong. What can I do?

ZbuffeR
12-08-2009, 04:28 AM
GL_RGB is 3 bytes, and may be problematic with the 4 byte alignment which is the default with GL.

It is easier to set it to "tightly packed" like this :
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ALIGNMENT, 1);

More details on the following page, search for "pixelstore" : http://www.opengl.org/resources/features/KilgardTechniques/oglpitfall/
Almost obsolete but still useful.



Can you describe exactly how "texels" array is build and filled in ?

Esben
12-08-2009, 04:51 AM
Sure, first I open the PPM file (it is plain text) as an ifstream and then a use the values from the file to build the texels array. This is the for-loop that populates the array:



for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
for (int c = 0; c < 3; c++)
{
getline(file, line);
texels[i][j][c] = atoi(line.c_str());
}
}
}


If I print them as integers, I can see that the texels variable now holds RGB-triples in the inner array, such as: (78,192,63)

ZbuffeR
12-08-2009, 06:05 AM
texels[i][j][c] <- this may be a problem, depending on alignement.

Try this :
for (int t = 0; t<height*width*3 ;t++) {
getline(file, line);
texels[t] = atoi(line.c_str());
}

Esben
12-13-2009, 09:15 AM
What do you mean by "allignment"? If you mean the order in which the RGB-values are specified, they should be alright. However, I tried you suggestion, and it gives the same result. So now I have a single-dimensional texels array of length height x width x 3... Should I still create the texture image like this?



glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, height, width,
0, GL_RGB, GL_UNSIGNED_BYTE, texels);


Anything else I can try?