Click to See Complete Forum and Search --> : Textures and Materials


mytakamineg240
November 1st, 2006, 03:58 PM
I am having some trouble using openGL and GLUT in C++. I'm not sure if this is the right forum, so feel free to ignore this. I am trying to set up textures and call them during rendering. I have coded this, but it doesn't seem to be working. I get shaded objects, but no textures. The textures are read in from .ppm files.


void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

vector<int> tempFace;
vector<int> tempTex;
int corner;
int texCorner;
VERT tempVert;
TEXVERT tempVT;

glEnable(GL_TEXTURE_2D);
for(int objLoop = 0; objLoop < objList.size(); objLoop++) //Loops through the list of face structures
{
tempFace = objList.at(objLoop).face;
tempTex = objList.at(objLoop).texVerts;

if(tempTex.size() != 0)
{
if(tempFace.size() == 3)
glBegin(GL_TRIANGLES);
else
glBegin(GL_POLYGON);

glBindTexture(GL_TEXTURE_2D, texName[objList.at(objLoop).material]);
glMaterialfv(GL_FRONT, GL_AMBIENT, mtlList.at(objList.at(objLoop).material).ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mtlList.at(objList.at(objLoop).material).diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mtlList.at(objList.at(objLoop).material).specular);
glMaterialf(GL_FRONT, GL_SHININESS, mtlList.at(objList.at(objLoop).material).shinyness);

for(int facePos = 0; facePos < tempFace.size(); facePos++) //Loops through the vertices of a face
{
// glColor3f(0.0, 0.0, 1.0);
corner = tempFace.at(facePos) - 1; //need to decrement by 1, since array indicies start at 0
texCorner = tempTex.at(facePos) -1;
tempVert = vertices.at(corner);
tempVT = textureVertices.at(texCorner);

glTexCoord2f(tempVT.u, tempVT.v);
glVertex3f(tempVert.a, tempVert.b, tempVert.c);
}
glEnd();
}
else
{
// glColor3f(1.0, 0.0, 1.0);
glBegin(GL_POLYGON);
for(int facePos = 0; facePos < tempFace.size(); facePos++) //Loops through the vertices of a face
{
int corner = tempFace.at(facePos) - 1; //need to decrement by 1, since array indicies start at 0
tempVert = vertices.at(corner);

glVertex3f(tempVert.a, tempVert.b, tempVert.c);
}
glEnd();
}
}
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}


This is my draw fuction. Is this wrong?
Full source code is attached.

Thanks.

JohnyDog
November 1st, 2006, 05:45 PM
Can you also post the object file and texture ?
Anyway:

for(int i = 0; i < mtlList.size(); i++)
{
glBindTexture(GL_TEXTURE_2D, texName[1]);

should probably be

for(int i = 0; i < mtlList.size(); i++)
{
glBindTexture(GL_TEXTURE_2D, texName[i]);


Apart from this, the code seems ok otherwise.

mytakamineg240
November 2nd, 2006, 03:59 PM
Thanks for the reply. Unfortunatly, that was not my problem. I have managed to do little more than turn the ground yellow. I have made a few other minor changes(i.e. I moved the glBindTexture call in the draw loop.

I would appreciate a look over my code if you wouldn't mind.

mytakamineg240
November 2nd, 2006, 04:05 PM
For some reason, I can't post more than one attachment(may be my browser) at a time, so if you'd like to see the mtl file, obj file, make file, etc. email me and I'll send it.
wsmurph1@uncc.edu

Thanks so much
Stephen

JohnyDog
November 3rd, 2006, 12:41 AM
Okay, first, there's missing filtering settings for the textures:

glBindTexture(GL_TEXTURE_2D, texName[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
if(mtlList.at(i).map != "")


second, there seems to be more (non GL-related) problems in your code either with material or texture coordinates reading. For example if i force one texture for all objects by changing:

if(currentMaterial != texName[objList[objLoop].material -1])
{
glBindTexture(GL_TEXTURE_2D, 3);
glMaterialfv(GL_FRONT, GL_AMBIENT, mtlList[objList[objLoop].material].ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mtlList[objList[objLoop].material].diffuse);


you can see that all objects are now indeed textured.

mytakamineg240
November 3rd, 2006, 09:32 AM
I appreciate the help. It really helps me out, as I am really new to openGL and C++. I think I can probably figure out from here where the problem is.

Thanks,
Stephen

mytakamineg240
November 4th, 2006, 02:00 PM
I figured it out.
Changed:


glBindTexture(GL_TEXTURE_2D, texName[objList[objLoop].material -1]);
to
glBindTexture(GL_TEXTURE_2D, objList[objLoop].material - 1);


Everything is nicely textured now.

Thanks everyone!