glClear() tampering with data?

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:

[b] {

   *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.


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


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);



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

[b]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 {
//Material and Face fields
int numOfFaces;//gets altered at glClear()
face* faces;//gets altered at glClear()
model();//private constructor: called in factory classes


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

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?

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.

numOfLines = count;
P3Sys* verts = new P3Sys[numOfLines];
mdl.faces = new face[numOfLines];
input.seekg(0, ios::beg);
count = 0;
//verticies reading loop
while (input.good()) {
if (retr != ’
') {
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
y: %f
z: %f
“, verts[count].getVal(1), verts[count].getVal(2), verts[count].getVal(3));
if (flag.ABLB_READOVERLINE == false) {
input.seekg(0, ios_base::beg);
material lastMaterial;//last material, stored in each face
char it[] = “none”;
lastMaterial.materialName = it;
count = 0;
//faces reading loop
while (input.good()) {
	if (retr == '

') {
} else {

	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 &lt;&lt; verts[vertNum].getVal(1) &lt;&lt; '

’ << verts[vertNum].getVal(2) << ’
’ << verts[vertNum].getVal(3) << ’
’ ;
mdl.faces[count].mat = lastMaterial;
mdl.faces[count].Vert = new P3Sys[count2];
mdl.faces[count].Vert = t_Verts;
mdl.faces[count].numOfVerts = count2;
if (retr == ‘u’ || retr == ‘U’) {
readString(input, flag);
lastMaterial.materialName = readString(input, flag);
lastMaterial = getMaterial(lastMaterial.materialName, mats, numOfMats);
if (flag.ABLB_READOVERLINE == false) {
mdl.numOfFaces = count;
cout << "

for (count = 0; count < mdl.numOfFaces; count++) {
cout << "Face " << count + 1 << ":
" << mdl.faces[count].numOfVerts << " Vertecies
Material: " << mdl.faces[count].mat.materialName << ’
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 ) {
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));
//the four vertex display
if (this->faces[numOfFaces].numOfVerts == 4 ) {
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;
glVertex3f(point.getVal(1), point.getVal(2), point.getVal(3));

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

OK, I can see you are parsing the OBJ file and rendering in immediate mode.
With the glClear command in, you are saying that
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.

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.

Now that is something interesting :slight_smile:

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) ?

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.

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?

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?

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.

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.

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.