where are my objects???

  • Thread starter Francesco Gallarotti
  • Start date
F

Francesco Gallarotti

I have a class Model that contains a private

vector<Vertex*> vertices

Each Vertex object has some info in it, and among those there is a

Vector *normal

In a piece of code (inside a parser that reads data from a file and builds a
data structure) I have this loop:

void Model::computeVerticesNormals(void) {
for(int i=0; i<vertices.size(); i++) {

// for each vertex....
Vertex *v = vertices;
int numOfTriangles = v->getNumberOfTriangles();
float x=0.0f, y=0.0f, z=0.0f;

// ....goes through the list of incident triangles
// (i.e. triangles that share the same vertex)
for(int j=0; j<numOfTriangles; j++) {
Triangle *t = v->getTriangle(j);
Vertex *n = t->getNormal();
x = x+n->getX();
y = y+n->getY();
z = z+n->getZ();
}

// averages the normals of each triangle
// to get the normal at the vertex
x = x/numOfTriangles;
y = y/numOfTriangles;
z = z/numOfTriangles;

Vertex *result = new Vertex(x,y,z);
result->normalize();

// set the vertex normal
v->setNormal(result);

// reads back the vertex normal from the vertex itself
// to see if the correct value has been saved
Vertex *test = v->getNormal();
cout<< i << ") " << *test << endl;
}
}


This is the ouput produced:

Computing normals at vertices...
0) (0,0,1)
1) (0.467037,-0.494024,0.73336)
2) (-0.611279,-0.146755,0.77769)
3) (0.184617,0.627597,0.756332)
4) (0.220298,-0.969669,-0.105883)
5) (-0.947068,0.281957,0.153498)
6) (0.720865,0.691571,-0.0456409)
7) (0.215026,-0.96971,-0.115873)
8) (-0.93456,0.277214,0.223049)
9) (0.71061,0.695713,-0.104963)
10) (0.176013,-0.961244,-0.212202)
11) (-0.901334,0.272784,0.336431)
12) (0.702211,0.703715,-0.108096)
13) (0.132502,-0.956589,-0.259579)
14) (-0.835799,0.253162,0.487185)
15) (0.660714,0.724194,-0.197484)
16) (0.0889998,-0.951882,-0.293258)
17) (-0.775485,0.236146,0.585541)
18) (0.624514,0.743401,-0.239451)
.................
.................

which shows that the normals have been successfully saved in the Vertex
objects.

When this part of code ends, the code goes into some other code (different
..cpp file) that is in charge of displaying the geometry on the screen using
openGL. The part of code that surprises me is the following:

// for each vertex
int numOfVertices = my_model->getNumOfVertices();
cout << "vertices = " << numOfVertices <<endl;
for(int j=0; j<numOfVertices; j++) {

// prints out the content of the vertex (x,y,z coords)
Vertex* v=my_model->getVertex(j);
cout << "vertex["<<j<<"]" <<*v << endl;
// and the address of the normal (which should be a valid address)
Vertex* n=v->getNormal();
cout << "normal["<<j<<"]" <<n << endl;
}

instead the output that I am getting is:

vertices = 234
vertex[0](-1.01545,-0.479097,-17.894)
normal[0]CDCDCDCD
vertex[1](-1.17015,0.165296,-17.894)
normal[1]CDCDCDCD
vertex[2](-0.380034,-0.667315,-17.894)
normal[2]CDCDCDCD
vertex[3](-1.49616,-0.935275,-17.894)
normal[3]CDCDCDCD
vertex[4](-0.616068,0.00116807,-11.7246)
normal[4]CDCDCDCD
vertex[5](0.17405,-0.831445,-11.7246)
normal[5]CDCDCDCD
vertex[6](-0.942071,-1.09939,-11.7246)
normal[6]CDCDCDCD
vertex[7](0.32804,-0.27849,-7.19337)
normal[7]CDCDCDCD
vertex[8](1.11816,-1.1111,-7.19337)
normal[8]CDCDCDCD
vertex[9](0.00204068,-1.37905,-7.19337)
normal[9]CDCDCDCD
vertex[10](1.4273,-0.604105,-2.42307)
normal[10]CDCDCDCD
.............
.............

where CDCDCDCD is a way for VC6 to say that the address is not valid

Where are my normals? They were there a fraction of a second before... where
are they now???
Any suggestions???
I am totally lost on this one. Maybe some of you had similar problems before
and you can suggest a debugging plan or something....

thanks

Francesco
 
J

Jonathan Mcdougall

Where are my normals?

Should ask them, they know better than anyone.

Please post some real code describing your problem
and every function which interacts with the problematic
code along with object declarations.


Jonathan
 
M

Moonlit

Hi,

It is hard to tell from this code. Just a gamble and probably wrong but are
you trying to read a 3DS file with andrea's ingegneri's code?

Regards, Ron AF Greve.


Francesco Gallarotti said:
I have a class Model that contains a private

vector<Vertex*> vertices

Each Vertex object has some info in it, and among those there is a

Vector *normal

In a piece of code (inside a parser that reads data from a file and builds a
data structure) I have this loop:

void Model::computeVerticesNormals(void) {
for(int i=0; i<vertices.size(); i++) {

// for each vertex....
Vertex *v = vertices;
int numOfTriangles = v->getNumberOfTriangles();
float x=0.0f, y=0.0f, z=0.0f;

// ....goes through the list of incident triangles
// (i.e. triangles that share the same vertex)
for(int j=0; j<numOfTriangles; j++) {
Triangle *t = v->getTriangle(j);
Vertex *n = t->getNormal();
x = x+n->getX();
y = y+n->getY();
z = z+n->getZ();
}

// averages the normals of each triangle
// to get the normal at the vertex
x = x/numOfTriangles;
y = y/numOfTriangles;
z = z/numOfTriangles;

Vertex *result = new Vertex(x,y,z);
result->normalize();

// set the vertex normal
v->setNormal(result);

// reads back the vertex normal from the vertex itself
// to see if the correct value has been saved
Vertex *test = v->getNormal();
cout<< i << ") " << *test << endl;
}
}


This is the ouput produced:

Computing normals at vertices...
0) (0,0,1)
1) (0.467037,-0.494024,0.73336)
2) (-0.611279,-0.146755,0.77769)
3) (0.184617,0.627597,0.756332)
4) (0.220298,-0.969669,-0.105883)
5) (-0.947068,0.281957,0.153498)
6) (0.720865,0.691571,-0.0456409)
7) (0.215026,-0.96971,-0.115873)
8) (-0.93456,0.277214,0.223049)
9) (0.71061,0.695713,-0.104963)
10) (0.176013,-0.961244,-0.212202)
11) (-0.901334,0.272784,0.336431)
12) (0.702211,0.703715,-0.108096)
13) (0.132502,-0.956589,-0.259579)
14) (-0.835799,0.253162,0.487185)
15) (0.660714,0.724194,-0.197484)
16) (0.0889998,-0.951882,-0.293258)
17) (-0.775485,0.236146,0.585541)
18) (0.624514,0.743401,-0.239451)
................
................

which shows that the normals have been successfully saved in the Vertex
objects.

When this part of code ends, the code goes into some other code (different
.cpp file) that is in charge of displaying the geometry on the screen using
openGL. The part of code that surprises me is the following:

// for each vertex
int numOfVertices = my_model->getNumOfVertices();
cout << "vertices = " << numOfVertices <<endl;
for(int j=0; j<numOfVertices; j++) {

// prints out the content of the vertex (x,y,z coords)
Vertex* v=my_model->getVertex(j);
cout << "vertex["<<j<<"]" <<*v << endl;
// and the address of the normal (which should be a valid address)
Vertex* n=v->getNormal();
cout << "normal["<<j<<"]" <<n << endl;
}

instead the output that I am getting is:

vertices = 234
vertex[0](-1.01545,-0.479097,-17.894)
normal[0]CDCDCDCD
vertex[1](-1.17015,0.165296,-17.894)
normal[1]CDCDCDCD
vertex[2](-0.380034,-0.667315,-17.894)
normal[2]CDCDCDCD
vertex[3](-1.49616,-0.935275,-17.894)
normal[3]CDCDCDCD
vertex[4](-0.616068,0.00116807,-11.7246)
normal[4]CDCDCDCD
vertex[5](0.17405,-0.831445,-11.7246)
normal[5]CDCDCDCD
vertex[6](-0.942071,-1.09939,-11.7246)
normal[6]CDCDCDCD
vertex[7](0.32804,-0.27849,-7.19337)
normal[7]CDCDCDCD
vertex[8](1.11816,-1.1111,-7.19337)
normal[8]CDCDCDCD
vertex[9](0.00204068,-1.37905,-7.19337)
normal[9]CDCDCDCD
vertex[10](1.4273,-0.604105,-2.42307)
normal[10]CDCDCDCD
............
............

where CDCDCDCD is a way for VC6 to say that the address is not valid

Where are my normals? They were there a fraction of a second before... where
are they now???
Any suggestions???
I am totally lost on this one. Maybe some of you had similar problems before
and you can suggest a debugging plan or something....

thanks

Francesco
 
F

Francesco Gallarotti

void Model::computeVerticesNormals(void) {
for(int i=0; i<vertices.size(); i++) {

// for each vertex....
Vertex *v = vertices;
int numOfTriangles = v->getNumberOfTriangles();
float x=0.0f, y=0.0f, z=0.0f;

// ....goes through the list of incident triangles
// (i.e. triangles that share the same vertex)
for(int j=0; j<numOfTriangles; j++) {
Triangle *t = v->getTriangle(j);
Vertex *n = t->getNormal();
x = x+n->getX();
y = y+n->getY();
z = z+n->getZ();
}

// averages the normals of each triangle
// to get the normal at the vertex
x = x/numOfTriangles;
y = y/numOfTriangles;
z = z/numOfTriangles;

Vertex *result = new Vertex(x,y,z); <<<<<<<<< creating Vertex result object here
result->normalize();

// set the vertex normal
v->setNormal(result); <<<<<<<<< making the Vertex v point to it


Apparently the problem are dangling pointers.
What seems to be happening here is that i am creating objects of type Vertex
inside this function and then, after the function terminates, the objects
are gone and the pointers to them are not pointing to anything anymore.
I am not sure if this is completely correct but this seems to be the
problem.
// reads back the vertex normal from the vertex itself
// to see if the correct value has been saved
Vertex *test = v->getNormal();
cout<< i << ") " << *test << endl;
}
}


This is the ouput produced:

Computing normals at vertices...
0) (0,0,1)
1) (0.467037,-0.494024,0.73336)
2) (-0.611279,-0.146755,0.77769)
3) (0.184617,0.627597,0.756332)
4) (0.220298,-0.969669,-0.105883)
5) (-0.947068,0.281957,0.153498)
6) (0.720865,0.691571,-0.0456409)
7) (0.215026,-0.96971,-0.115873)
8) (-0.93456,0.277214,0.223049)
9) (0.71061,0.695713,-0.104963)
10) (0.176013,-0.961244,-0.212202)
11) (-0.901334,0.272784,0.336431)
12) (0.702211,0.703715,-0.108096)
13) (0.132502,-0.956589,-0.259579)
14) (-0.835799,0.253162,0.487185)
15) (0.660714,0.724194,-0.197484)
16) (0.0889998,-0.951882,-0.293258)
17) (-0.775485,0.236146,0.585541)
18) (0.624514,0.743401,-0.239451)
................
................

which shows that the normals have been successfully saved in the Vertex
objects.

When this part of code ends, the code goes into some other code (different
.cpp file) that is in charge of displaying the geometry on the screen using
openGL. The part of code that surprises me is the following:

// for each vertex
int numOfVertices = my_model->getNumOfVertices();
cout << "vertices = " << numOfVertices <<endl;
for(int j=0; j<numOfVertices; j++) {

// prints out the content of the vertex (x,y,z coords)
Vertex* v=my_model->getVertex(j);
cout << "vertex["<<j<<"]" <<*v << endl;
// and the address of the normal (which should be a valid address)
Vertex* n=v->getNormal();
cout << "normal["<<j<<"]" <<n << endl;
}

instead the output that I am getting is:

vertices = 234
vertex[0](-1.01545,-0.479097,-17.894)
normal[0]CDCDCDCD
vertex[1](-1.17015,0.165296,-17.894)
normal[1]CDCDCDCD
vertex[2](-0.380034,-0.667315,-17.894)
normal[2]CDCDCDCD
vertex[3](-1.49616,-0.935275,-17.894)
normal[3]CDCDCDCD
vertex[4](-0.616068,0.00116807,-11.7246)
normal[4]CDCDCDCD
vertex[5](0.17405,-0.831445,-11.7246)
normal[5]CDCDCDCD
vertex[6](-0.942071,-1.09939,-11.7246)
normal[6]CDCDCDCD
vertex[7](0.32804,-0.27849,-7.19337)
normal[7]CDCDCDCD
vertex[8](1.11816,-1.1111,-7.19337)
normal[8]CDCDCDCD
vertex[9](0.00204068,-1.37905,-7.19337)
normal[9]CDCDCDCD
vertex[10](1.4273,-0.604105,-2.42307)
normal[10]CDCDCDCD
............
............

where CDCDCDCD is a way for VC6 to say that the address is not valid

Where are my normals? They were there a fraction of a second before... where
are they now???
Any suggestions???
I am totally lost on this one. Maybe some of you had similar problems before
and you can suggest a debugging plan or something....

thanks

Francesco

anyway thanks to everybody for spending your time trying to help.
I hope I had a good idea to change my code to fix this problem.

Francesco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top