First let me say I’ve tried figuring this out on my own, reading a ton of stuff googling forever. I just can’t seem to solve my problem. When I render a single textured object my code works. When I add a textured object to the object vector seen in my code, it crashes. I know my object class is intact and working correctly, I’ve reconstructed my object files from the loaded objects. So I know data is being stored correctly. On top of that for some reason it always misses parts of my non textured objects.
What I’m trying to do here is create segmented objects with each segment being in a display list. Each segment also can have multiple textures/materials.
Please please please have a look at my code and just brush over it. It’s been 4 days since I made any progress at all on it.
Here is my rendering code
#include "render.h"
render::render()
{
theta = 47;
}
render::~render()
{
tFactory.DeleteAll();
}
void render::LoadObject(vector<BaseObject>::iterator& tempObject)
{ofstream outfile;
outfile.open("blah.out");
vector<point>::iterator v = tempObject->GetVBegin();
vector<point>::iterator vn = tempObject->GetVnBegin();
vector<uvPoint>::iterator vt = tempObject->GetVtBegin();
int y = 0;
tFactory.LoadTextures(tempObject->GetMaterialLoc());
for(vector<group>::iterator x = tempObject->GetGroupsBegin(); x != tempObject->GetGroupsEnd() ; x++)
{
x->groupCompileIndex = glGenLists(1);
glNewList(x->groupCompileIndex, GL_COMPILE_AND_EXECUTE);
glPushMatrix();
glBegin(GL_TRIANGLES);
for(int i = 0 ; i < x->faces.size() ; i++)
{
if(i == 0)
{
tFactory.SetUpTexture(x->materials[y].materialName, outfile);
}
else if(i == x->materials[y].polyIndex)
{
y++;
tFactory.SetUpTexture(x->materials[y].materialName, outfile);
}
if(x->faces[i].vtIndex != NULL)
{
glTexCoord2f(vt[x->faces[i].vtIndex[0]-1].x, vt[x->faces[i].vtIndex[0]-1].y);
}
glNormal3f(vn[x->faces[i].vnIndex[0]-1].x, vn[x->faces[i].vnIndex[0]-1].y, vn[x->faces[i].vnIndex[0]-1].z);
glVertex3f(v[x->faces[i].vIndex[0]-1].x, v[x->faces[i].vIndex[0]-1].y, v[x->faces[i].vIndex[0]-1].z);
if(x->faces[i].vtIndex != NULL)
{
glTexCoord2f(vt[x->faces[i].vtIndex[1]-1].x, vt[x->faces[i].vtIndex[1]-1].y);
}
glNormal3f(vn[x->faces[i].vnIndex[1]-1].x, vn[x->faces[i].vnIndex[1]-1].y, vn[x->faces[i].vnIndex[1]-1].z);
glVertex3f(v[x->faces[i].vIndex[1]-1].x, v[x->faces[i].vIndex[1]-1].y, v[x->faces[i].vIndex[1]-1].z);
if(x->faces[i].vtIndex != NULL)
{
glTexCoord2f(vt[x->faces[i].vtIndex[2]-1].x, vt[x->faces[i].vtIndex[2]-1].y);
}
glNormal3f(vn[x->faces[i].vnIndex[2]-1].x, vn[x->faces[i].vnIndex[2]-1].y, vn[x->faces[i].vnIndex[2]-1].z);
glVertex3f(v[x->faces[i].vIndex[2]-1].x, v[x->faces[i].vIndex[2]-1].y, v[x->faces[i].vIndex[2]-1].z);
}
glEnd();
glEndList();
y = 0;
}
outfile.close();
}
void render::LoadTextures(string fileName)
{
tFactory.LoadTextures(fileName);
}
void render::LightSetup()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
}
void render::SetupBlending(int& bType) const //antialiasing
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
switch(bType)
{
case GG_AA_X2:
{
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_FASTEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
}
case GG_AA_X4:
{
glEnable(GL_POLYGON_SMOOTH);
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
}
case GG_MS:
{
glEnable(GL_MULTISAMPLE);
}
}
}
void render::Init(int bType)
{
glEnable(GL_TEXTURE_2D);
SetupBlending(bType);
glMatrixMode(GL_PROJECTION);
gluPerspective(theta, 1, .9, 500);
glMatrixMode(GL_TEXTURE);
glRotatef(180.0, 1.0, 0.0, 0.0); //Flips textures due to how SDL loads them.
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 0.0, 0.0);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
LightSetup();
}
void render::Display(vector<BaseObject>& objects)
{
for(vector<BaseObject>::iterator i = objects.begin() ; i != objects.end() ; i++)
{glTranslatef(4.0, 0, 0);
if(!i->Loaded())
{
LoadObject(i);
i->ChangeLoadState(true);
}
else
{
for(vector<group>::iterator x = i->GetGroupsBegin() ; x != i->GetGroupsEnd() ; x++)
{
glCallList(x->groupCompileIndex);
}
}
}
}
void render::FreeLists(vector<BaseObject>& objects)
{
for(vector<BaseObject>::iterator i = objects.begin() ; i != objects.end() ; i++)
{
for(vector<group>::iterator x = i->GetGroupsBegin() ; x != i->GetGroupsEnd() ; x++)
{
glDeleteLists(x->groupCompileIndex, 1);
}
}
tFactory.DeleteAll();
}
and here is my texture factory
#include "textureFactory.h"
textureFactory::textureFactory()
{
}
textureFactory::~textureFactory()
{
textureList.clear();
}
bool textureFactory::TextureLoaded(string textureName) const
{
if(textureList.empty())
{
return false;
}
else if(textureList.find(textureName) != textureList.end())
{
return true;
}
else
{
return false;
}
}
void textureFactory::TextureFormat(GLenum& textureFormat, int& numOfColors, SDL_Surface*& picture)
{
numOfColors = picture->format->BytesPerPixel;
if (numOfColors == 4)
{
if (picture->format->Rmask == 0x000000ff)
{
textureFormat = GL_RGBA;
}
else
{
textureFormat = GL_BGRA;
}
}
else if (numOfColors == 3)
{
if (picture->format->Rmask == 0x000000ff)
{
textureFormat = GL_RGB;
}
else
{
textureFormat = GL_BGR;
}
}
}
void textureFactory::LoadTextures(string fileName)
{
string garbage, tempMaterial, map;
int numOfColors;
GLenum textureFormat = GL_RGB;
SDL_Surface* picture = NULL;
textureNode tempTexture;
tempTexture.isUvMap = false;
ifstream infile;
infile.open(fileName.c_str());
//ofstream outfile;
//outfile.open("blah.out");
infile >> garbage;
while(infile)
{
if(garbage == "newmtl")
{
infile >> tempMaterial;
//outfile << tempMaterial << endl;
textureList.insert(make_pair(tempMaterial, tempTexture));
infile >> garbage >> textureList[tempMaterial].ns >> garbage;
infile >> textureList[tempMaterial].ka.x >> textureList[tempMaterial].ka.y >> textureList[tempMaterial].ka.z;
infile >> garbage >> textureList[tempMaterial].kd.x >> textureList[tempMaterial].kd.y >> textureList[tempMaterial].kd.z;
infile >> garbage >> textureList[tempMaterial].ks.x >> textureList[tempMaterial].ks.y >> textureList[tempMaterial].ks.z >> garbage;
if(garbage == "map_Kd")
{
textureList[tempMaterial].isUvMap = true;
infile >> map >> garbage;
picture = SDL_LoadBMP(map.c_str());
TextureFormat(textureFormat, numOfColors, picture);
glGenTextures(1, &textureList[tempMaterial].textureIndex);
glBindTexture(GL_TEXTURE_2D, textureList[tempMaterial].textureIndex);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
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_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, numOfColors, picture->w, picture->h, 0, textureFormat, GL_UNSIGNED_BYTE, picture->pixels);
//gluBuild2DMipmaps(GL_TEXTURE_2D, numOfColors, picture->w, picture->h, textureFormat, GL_UNSIGNED_BYTE, picture->pixels);
if(picture)
{
SDL_FreeSurface(picture);
picture = NULL;
}
}
infile >> textureList[tempMaterial].d;
}
infile >> garbage;
}
infile.close();
//outfile.close();
}
void textureFactory::DeleteTexture(string textureName)
{
if(textureList.find(textureName) != textureList.end() && !textureList.empty())
{
glDeleteTextures(1, &textureList.find(textureName)->second.textureIndex);
textureList.erase(textureList.find(textureName));
}
}
void textureFactory::DeleteAll()
{
for(map<string, textureNode>::iterator i = textureList.begin() ; i != textureList.end() ; i++)
{
glDeleteTextures(1, &i->second.textureIndex);
}
textureList.clear();
}
void textureFactory::SetUpTexture(string textureName, ofstream& outfile)
{
if(textureList.find(textureName) != textureList.end() && !textureList.empty())
{
float temp[4];
glMaterialf(GL_FRONT, GL_SHININESS, textureList[textureName].ns);
temp[0] = textureList[textureName].ka.x;
temp[1] = textureList[textureName].ka.y;
temp[2] = textureList[textureName].ka.z;
temp[3] = textureList[textureName].d;
glMaterialfv(GL_FRONT, GL_AMBIENT, temp);
temp[0] = textureList[textureName].kd.x;
temp[1] = textureList[textureName].kd.y;
temp[2] = textureList[textureName].kd.z;
temp[3] = textureList[textureName].d;
glMaterialfv(GL_FRONT, GL_DIFFUSE, temp);
temp[0] = textureList[textureName].ks.x;
temp[1] = textureList[textureName].ks.y;
temp[2] = textureList[textureName].ks.z;
temp[3] = textureList[textureName].d;
glMaterialfv(GL_FRONT, GL_SPECULAR, temp);
if(textureList[textureName].isUvMap)
{
glBindTexture(GL_TEXTURE_2D, textureList[textureName].textureIndex);
}
}
}
I just can’t seem to trace my problem. I know it’s a texturing/materials problem. Also, if I use GL_COMPILE on the non textured object, it will draw the missing material but mess up somewhere else. I’m just at such a loss of what to do or how to debug this problem. I’m new to openGL I’m a littl eout of my league. Also I don’t have much time t debug since I’m in iraq. Please please please help :’(
Gauge