Archived

This topic is now archived and is closed to further replies.

cbmeredith

Having problems with STL vectors.

Recommended Posts

cbmeredith    122
Basically what I have done is that I''ve created a class called C3DObject. My Model class store a vector of C3DObjects. Now this all seems to work fine, but when I try and do something like this... class C3DObject { public: int numFaces; }; vector pObjects; // Fill in the vector // This is where problem arises. pObjects[].numFaces the compiler doesn''t know what''s going on. Anyone got any ideas? I''ve been trying to fix this for the last 4 hours, and still have no idea what the problem is.

Share this post


Link to post
Share on other sites
Sneftel    1788
put it in [ source ] and [ /source ] tags (with the brackets, without the spaces).


How appropriate. You fight like a cow.

Share this post


Link to post
Share on other sites
cbmeredith    122
Take a look at the code tell me if you see anything wrong


3DModel.h


#include <stdio.h>
#include <vector>
#include <fstream.h>
#include <math.h>
#include "3DMath.h"
#include "main.h"

#ifndef _3DMODEL_H
#define _3DMODEL_H

using namespace std;

#define VERTEX 0
#define TEXTURE 1
#define NORMAL 2


struct tFace {
vector<int> vertIndex;
vector<int> normIndex;
};


class C3DObject {

public:
int numVerts;
int numFaces;
int numNorms;
char strName[255];
CVector3 *pVerts;
CVector3 *pNormals;
tFace *pFaces;

};

class C3DModel {

public:
C3DModel();
~C3DModel();
int LoadOBJModel(char *strFilename);
int CreateDisplayList();
// DrawObject(...)

void DrawModel();
// DestroyModel(...)



private:
FILE* pFilePointer;
int numObjects;
bool b_justReadAFace, b_fileContainsNormals, b_hasDisplayList;
GLuint displayList;
vector<C3DObject> pObjects;

int LoadOBJFileData();
int LoadOBJVertexData();
int ReadOBJVertex();
int ReadOBJNormal();
int LoadOBJFaceData();
int FillInObjectData();
int ComputeNormals();

};


#endif




3DModel.cpp where I''m having problems


int C3DModel::CreateDisplayList()
{
if(numObjects == 0) // Do we have any objects to create a display list with?

return 1;

displayList = glGenLists(1);
glNewList(displayList, GL_COMPILE);
glColor3f(1.0f, 0.0f, 0.0f);

for(int i=0;i<pObjects.size();i++) {
for(int j=0;j<pObjects[i].numFaces;j++) {

glBegin(GL_POLYGON);
for(int k=0;k<pObjects[i].pFaces[j].vertIndex.size();k++) {
// ############# BLOWS UP RIGHT HERE ###################

// When i=1 it blows up. Only blows up for normals

// If I comment out normals it works fine.

if(b_fileContainsNormals) {
glNormal3f(pObjects[i].pNormals[pObjects[i].pFaces[j].normIndex[k]].x,
pObjects[i].pNormals[pObjects[i].pFaces[j].normIndex[k]].y,
pObjects[i].pNormals[pObjects[i].pFaces[j].normIndex[k]].z);
}

glVertex3f(pObjects[i].pVerts[pObjects[i].pFaces[j].vertIndex[k]].x,
pObjects[i].pVerts[pObjects[i].pFaces[j].vertIndex[k]].y,
pObjects[i].pVerts[pObjects[i].pFaces[j].vertIndex[k]].z);
}
glEnd();
}
}
glEndList();

b_hasDisplayList = true;
return 1;
}




Share this post


Link to post
Share on other sites
GekkoCube    116
it could be possible that the variables you use to keep count of the faces and stuff do not coincide.

insteady of using separate variables, try using the vector''s size().

example:

vector m_vList;

m_vList.push_back(5);

cout << "size = " << m_vList.size() << endl;

Share this post


Link to post
Share on other sites