Advertisement

Problem with 3d engine structures...

Started by September 04, 2000 10:48 PM
4 comments, last by Martee 24 years, 3 months ago
I have started to write a 3d engine in C++ (how original...), and I''m already having problems with my data structures. Here''s the code (note that these aren''t final or anything, they''re just for testing):
    
class VERTEX{
public:
	float x,y,z;
	int Color;
};

class POLY{
public:
	VERTEX* vtx;
	int NumVtx;
	int Color;
};

class SECTOR{
public:
	POLY* poly;
	int PolyCount;
};

int main(void)
{
        SECTOR s;
        .
	.
        .
	s.poly[0].vtx[0].x=5.0f;
        // crashes when i run it

}
    
It will compile, but it crashes when I run it. Did I miss something? I can do ''s.poly[0].NumVtx=3;'', or ''s.PolyCount=1;'', and both of those will compile and run fine. Martee Magnum Games
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I''m not too sure, but your using pointers in your classes. So, you would need to dereference them.

You use:
s.poly[0].vtx[0].x = 5.0f;

Should it be?
s->ploy[0]->vtx[0].x = 5.0f;

not sure, maybe this will help and get you thinking...I have a question for you. How do you get your code in that neat white box???
Advertisement
I think it should be:
s->poly[0]->vtx[0]->x=5.0f;

Damnit, I wanted to be the first one to reply!! BY 1 minute!

Oh well. ComputrJam, use [ source ] and [ /source ] (no spaces).

Edited by - Zipster on September 4, 2000 12:01:36 AM
Thank you! I''ve been trying to figure that out forever. lol
You haven''t allocated any memory for your pointer members vtx and poly. Building upon your structures, you should be doing this...

    class VERTEX{public:   float x, y, z;   int colour;};class POLY{public:   POLY() : vtx(NULL) {}   void SetNbVertices(int num)   {      if (vtx)         delete [] vtx;      vtx = new VERTEX[num];      numvtx = num;   }   VERTEX *vtx;   int numvtx;   int colour;};[/source]And do the same for your sector. Then when you create your polygons, just do...[source]   POLY poly;   poly.SetNumVertices(3);   poly.vtx[0] = ...    


Or follow your sector example. I won''t lecture you on your class arrangement, you''ll discover everything in due time. But for now everything''s fine.
Thanx for the help guys

Martee
Magnum Games
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers

This topic is closed to new replies.

Advertisement