473,383 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

where are my objects???

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[i];
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
Jul 19 '05 #1
3 1670
> 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
Jul 19 '05 #2
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" <ga********@hotmail.com> wrote in message
news:Xz*********************@news4.srv.hcvlny.cv.n et...
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[i];
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

Jul 19 '05 #3
> > void Model::computeVerticesNormals(void) {
for(int i=0; i<vertices.size(); i++) {

// for each vertex....
Vertex *v = vertices[i];
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
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
1
by: Bae,Hyun-jik | last post by:
Where are __value objects allocated? I guess __value object is same to basic __value objects such as int,char,float, but C# compiler demands using new keyword even to __value classes. Please...
3
by: Smithers | last post by:
In Windows Forms MDI applications... I'm wondering if it is standard practice to create DAL and business objects as static classes. The only alternative (please enlighten me if I'm wrong about...
9
by: Emin | last post by:
Dear Experts, I have a fairly simple query in which adding a where clause slows things down by at least a factor of 100. The following is the slow version of the query ...
1
by: Edward K Ream | last post by:
Hello all. I'm tracking down memory leaks in my app. To do this I wrote a script to show the numbers of each different type of object. But it doesn't show strings! Here is the script: import...
34
by: linq936 | last post by:
Hi, I just did the following test: 1:void main(void){ 2: int p = 1; 3: int* pp = &p; 4: int c = p * pp; 5: int d = p + pp; 6:}
1
by: ismailc | last post by:
Hi, I imported an css file for my dropdowns, but it does not align with the other objects in my xslt. I checked the imported css file for dropdown & it's aligned left but somehow I other...
8
by: junky_fellow | last post by:
Guys, Consider the following snippet of code: int main(VOID) { static ushort fractionalValue={ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 250, 333, 666, 750, 0, 0 };
10
by: mirandacascade | last post by:
Question toward the bottom of this post....background information immediately below. Access 97 SQL Server 2000 Please note: although the subject line uses the word 'bloat', this post is NOT...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.