PDA

View Full Version : glClear() tampering with data?



ablbebxb
08-14-2011, 10:08 PM
Hello,
I have been having some trouble with the glClear command randomly altering some model data I have stored in a class (I know its the glClear command from debugging to pinpoint the problem). This has puzzled me for a bit as the data being altered is private and the command should not interact with the model at all anyway. I'm not sure how anyone can help me much without some code, so here is my displayFunc so far:

{

/**
*This line seems to alter the code, in my debugger I put breakpoints
*before, on and after this line. The debugger indicated that the data was
*normal preceding the line, being defined on the line, and was corrupted
*after it.
*/
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();


glTranslatef(xPos, yPos, zPos);
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);

glPushMatrix();

GLfloat light1Pos[] = {0.0f, 0.0f, 10.0f, 1.0f};
GLfloat light1Diff[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light1Diff);
glLightfv(GL_LIGHT0, GL_POSITION, light1Pos);

tank->draw(0.0f, 0.0f, 0.0f);

glPopMatrix();

glutSwapBuffers();
}

and the only thing being altered is the model data, so here is the .h for that class as well.

struct material {
P3Sys materialColor;//gets altered at glClear
char* materialName;//gets altered at glClear
};

struct face {
int numOfVerts;//number of verticies used by the face
P3Sys* Vert;//gets altered at glClear
P3Sys* Normal;//gets altered at glClear
material mat;//gets altered at glClear
};

class model {
public:
//Material and Face fields
int numOfFaces;//gets altered at glClear()
face* faces;//gets altered at glClear()
model();//private constructor: called in factory classes

~model();

void draw(float x, float y, float z);//draws the model at the given x, y, and z coord

static model* objModel(char* objFile, char* mtlFile);//static object model factory
};

I would appreciate any help or advice on how to get the model data to be left alone

BionicBytes
08-15-2011, 01:19 AM
There can be no way that glclear is erasing or corrupting your model data. The problem must lie in your code somewhere.

Where's your code that creates vertex arrays/buffers ?
Where's your model drawing code ?

What GFX card do you have incidentally?

ablbebxb
08-15-2011, 06:13 AM
Alright, heres my loading code that defines the vertecies and faces. flag.Ablb_READOVERLINE is just a flag system I have setup, it gets defined as true after a read that ends on a newline and mdl is the model being defined.


{
input.open(objFile);
numOfLines = count;
P3Sys* verts = new P3Sys[numOfLines];
mdl.faces = new face[numOfLines];
input.seekg(0, ios::beg);
count = 0;
//verticies reading loop
flag.ABLB_READOVERLINE = false;
while (input.good()) {
input.get(retr);
if (retr != '\n') {
flag.ABLB_READOVERLINE = false;
}
if (retr == 'v' || retr == 'V') {
int pos = input.tellg();
input.seekg(pos + 1);
for (int a = 1; a <= 3; a ++) {
verts[count].setVal(a, readFloat(input, flag));
}
printf("x: %f\ny: %f\nz: %f\n", verts[count].getVal(1), verts[count].getVal(2), verts[count].getVal(3));
count++;
}
if (flag.ABLB_READOVERLINE == false) {
skipLine(input);
}
printf("\n");
/**
NOTE FOR DEBUGGING: MAKE SURE THIS LINE IS NOT SKIPPING ANY NECCESARY LINES IN THE FILE
*/
}
input.seekg(0, ios_base::beg);
material lastMaterial;//last material, stored in each face
{
char it[] = "none";
lastMaterial.materialName = it;
}
count = 0;
input.close();

input.open(objFile);
//faces reading loop
while (input.good()) {
input.get(retr);
if (retr == '\n') {
flag.ABLB_READOVERLINE = true;
} else {
flag.ABLB_READOVERLINE = false;
}

if (retr == 'f' || retr == 'F') {
int pos = input.tellg();
input.seekg(pos + 1);
P3Sys* t_Verts = new P3Sys[5];//a temporary array of estimated maximum size
int vertNum = 0;//an int used to read the integer to find the correct vertex
int count2 = 0;//sub-counter
while (flag.ABLB_READOVERLINE == false) {
vertNum = readInt(input, flag);
t_Verts[count2] = verts[vertNum];
cout << verts[vertNum].getVal(1) << '\n' << verts[vertNum].getVal(2) << '\n' << verts[vertNum].getVal(3) << '\n' ;
count2++;
}
mdl.faces[count].mat = lastMaterial;
mdl.faces[count].Vert = new P3Sys[count2];
mdl.faces[count].Vert = t_Verts;
mdl.faces[count].numOfVerts = count2;
count++;
}
if (retr == 'u' || retr == 'U') {
readString(input, flag);
lastMaterial.materialName = readString(input, flag);
lastMaterial = getMaterial(lastMaterial.materialName, mats, numOfMats);
}
if (flag.ABLB_READOVERLINE == false) {
skipLine(input);
}
}
mdl.numOfFaces = count;
cout << "\n\n\n\n\n";
for (count = 0; count < mdl.numOfFaces; count++) {
cout << "Face " << count + 1 << ":\n\t" << mdl.faces[count].numOfVerts << " Vertecies\n\tMaterial: " << mdl.faces[count].mat.materialName << '\n';
}
input.close();
assert(!(input.is_open()));
return &mdl;
}


Here is my drawing code.


for (int a = 0; a < this->numOfFaces; a++) {
//the three vertex display
if (this->faces[a].numOfVerts == 3 ) {
glBegin(GL_TRIANGLES);
P3Sys color = this->faces[a].mat.materialColor;
glColor3f(color.getVal(1), color.getVal(2), color.getVal(3));
for (int b = 0; b < 3; b++) {
P3Sys point = this->faces[a].Vert[B];
glVertex3f(point.getVal(1), point.getVal(2), point.getVal(3));
}
glEnd();
}
//the four vertex display
if (this->faces[numOfFaces].numOfVerts == 4 ) {
glBegin(GL_QUADS);
P3Sys color = this->faces[a].mat.materialColor;
glColor3f(color.getVal(1), color.getVal(2), color.getVal(3));
for (int b = 0; b < 4; b++) {
P3Sys point = this->faces[a].Vert[B];
glVertex3f(point.getVal(1), point.getVal(2), point.getVal(3));
}
glEnd();
}
}

and my graphics card is the AMD Raedon HD 6490M.

BionicBytes
08-15-2011, 09:40 AM
OK, I can see you are parsing the OBJ file and rendering in immediate mode.
With the glClear command in, you are saying that
faces[a].Vert[B]
gets overwritten with garbage?

I don't have a mobile laptop using a Radeon, but I do have a GeForce laptop and a Radeon 4850 desktop; none of which have ever experienced this kind of issue before.

ablbebxb
08-15-2011, 05:35 PM
Yes, thats whats happening, the data is fine before the call as my model is global, but after the call to glClear the data is replaced with random numbers. I don't think the issue is with the graphics card, though, because the corrupted model is still displayed, more or less.

Avithohol
08-16-2011, 12:45 AM
Now that is something interesting :)

I know it sounds weird but have you tried to comment out glClear,
you should see the tank. I know it will look like chaotic, but at least you could see that the geometry last longer than one frame.

Do you have option to try the code on another machine (in school, workplace, friend's pc) ?

ablbebxb
08-17-2011, 05:01 AM
Hmmm, this problem may go deeper than I first thought, even when I ran it without the glClear(), the glBegin() corrupted the data later on.

BionicBytes
08-17-2011, 06:31 AM
Your draw code makes use of 'this'. Is this a pointer to mdl.faces?

if (this->faces[a].numOfVerts == 3

Are you using any manged code. Could that be interfering with your array structure? Are you dynamically resizing the array structures at any time thus causing the memory to be reallocated - but your pointers are referencing the wrong data/location?

ablbebxb
08-19-2011, 08:49 AM
yes, the draw function is a method in the model class.

I do also have a few memory allocations, but I'm not sure I understand what you mean when you say the pointer could get changed to a different point in memory?

BionicBytes
08-19-2011, 12:02 PM
In some languages dynamically resizing an array causes a new block of memory to be assigned and the original contents copied to it.
Therefore any pointers to the dynamic array are now invalid.
In your case you were using *this* which I presume is a pointer to mdl.faces
If faces grows dynamically then maybe the *this* pointer is invalid.

ablbebxb
08-20-2011, 09:00 PM
I guess that could be a reason for the corruption of the faces array, but unless the this keyword can screw up like that I don't see why the numOfFaces int gets modified.

I just realized I should specify that I'm using c++ as well, sorry I forgot to mention that.

g0bl1n
08-20-2011, 11:54 PM
Is there any way you could make numOfFaces const? If you did then it couldn't change. Your code looks fine to me, the only problem I saw was that you check for a 3 faced and then a 4 faced, I personally would have an else in there just to be sure, but there is no reason why it would do both...just a preference I guess. Maybe try making a copy of the variable in the beginning (a const copy). Although this troubles me:
"cout << "Face " << count + 1..."

When right above you set mdl.numOfFaces=count;
Should this be count+1?

Anyways, there my 2 cents... Don't think I was of any help, but there it is... GL on your MDL stuff, that's the model format GMod uses right? That would be pretty cool to have, there's a lot of models on the GMod site game ready, and a lot of variety too.

Goblin