• Advertisement

Archived

This topic is now archived and is closed to further replies.

Variables Getting Overwritten?

This topic is 5063 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello. I have the following header: #ifndef PRIMITIVES_H_ #define PRIMITIVES_H_ #include <gl\gl.h> #include <gl\glu.h> #include <gl\glaux.h> typedef struct Vertex { float x; float y; float z; } Vertex; typedef struct Face { Vertex Vertices[3]; } Face; typedef struct RGBColor { float r; float g; float b; } RGBColor; typedef struct Quad { RGBColor QuadColor; Vertex Vertices[4]; } Quad; struct BoxPrimitive { Quad Quads[6]; RGBColor Color; Vertex Position; Vertex Rotation; short ClassID; BoxPrimitive *nextaddr; void render(void); void SetColor(float r, float g, float b); void SetPosition(float x, float y, float z); void SetRotation(float x, float y, float z); void Translate(float x, float y, float z); void Rotate(float x, float y, float z); }; int CreateBox(float LengthX, float LengthY, float LengthZ); void RenderAllBoxes(); extern BoxPrimitive *list; #endif // PRIMITIVES_H_ Basically, it''s a fancy way of drawing and manipulating boxes. Any how, BoxPrimitive is a linked list and the points of the cubes are calculated the CreateBox function and stored in Quads which in turn are stored in Vertices. However, void BoxPrimitive::SetColor(float r, float g, float b) { // Convert to 0-1 instead of 0-255 r = r / 255; g = g / 255; b = b / 255; Color.r = r; Color.g = g; Color.b = b; return; } is a function that sets the color. When I call this function, some how the values stored in the points become overwritten and the cube appears extremely distorted on the screen. I have been seeking the answer to this problem for days, considering this is my first attempt at C++ programming. Please help me.

Share this post


Link to post
Share on other sites
Advertisement
I didnt even read ur post i guess.

[edited by - honayboyz on April 13, 2004 9:09:36 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Ok, first up (if you''re not already) I suggest you get rid of the linked list stuff and concentrate on getting 1 cube working.

You define the colour in 2 places, in the BoxPrimitive class and also for each Quad. Are you setting the colour for both of these? The BoxPrimitive::SetColor code you showed only sets the colour for the whole primitive. Make sure you define the colour for each individual quad using

Quad[quad_no].QuadColor.r=1.0f;
Quad[quad_no].QuadColor.g=1.0f;
Quad[quad_no].QuadColor.b=1.0f;

and the vertex info is defined with

Quad[quad_no].vertices[vertex_no]=whatever ;

It doesn''t look like you have any weird pointer errors in the code from what you have shown. Maybe what I have said will help prompt some thoughts. I''d suggest you step through it with the debugger and watch what''s happening to your variables.

For future reference, if you set quads/vertices etc to use classes instead of structs, you can use the constructor to make your program easier to read (and less to type!) i.e.

Quad[quad_no].QuadColor.r=1.0f;
Quad[quad_no].QuadColor.g=1.0f;
Quad[quad_no].QuadColor.b=1.0f;

becomes

Quad[quad_no].QuadColor=RGBColor(1.0f,1.0f,1.0f);

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
And learn to spell "COLOUR"! There is a "U". Not in the OpenGL commands of course (e.g. glColor3f) cos they were invented by Americans who can''t spell. No excuse not to spell it right in your own routines though.

That is unless you''re a yank...

Share this post


Link to post
Share on other sites
Really, really strange. When I set the Color or Rotation variables, the vertices get messed up... I even got rid of the linked list stuff.

Share this post


Link to post
Share on other sites
Also, I used QuadColor as a test to see if each side was rendering correctly by assigning each side a different color, while Color is just general and affects the whole cube. Right now I''m manually setting the color because that tends to mess it up too, along with setting the rotation.

void BoxPrimitive::render()
{
int i, j;

glTranslatef(Position.x,Position.y,Position.z);

BoxPrimitive::SetRotation(0, rtri, 0);

glRotatef(Rotation.x,1.0f,0.0f,0.0f);
glRotatef(Rotation.y,0.0f,1.0f,0.0f);
glRotatef(Rotation.z,0.0f,0.0f,1.0f);

glBegin(GL_QUADS);

glColor3f(Color.r, Color.g, Color.b);

for (i = 1; i <= 6; i++)
{
for(j = 1; j <= 4; j++)
{
if (i == 1)
glColor3f(1.0f, 0.0f, 0.0f);
if (i == 2)
glColor3f(0.0f, 1.0f, 0.0f);
if (i == 3)
glColor3f(0.0f, 0.0f, 1.0f);
if (i == 4)
glColor3f(1.0f, 1.0f, 0.0f);
if (i == 5)
glColor3f(1.0f, 0.0f, 1.0f);
if (i == 6)
glColor3f(0.0f, 1.0f, 1.0f);

glVertex3f(Quads.Vertices[j].x,Quads[i].Vertices[j].y,Quads[i].Vertices[j].z);
}
}

glEnd();

rtri+= 50.0f * TimeElapsed;

return;
}

Share this post


Link to post
Share on other sites
Weird. Somehow the y value of quad 6 vertex 1 becomes approximate 580,000 and fluctuates based on how the rotation vertex is set. I have nothing weird in my code either...

Share this post


Link to post
Share on other sites
Similar results occur when instead of using a Vertex struct to store rotation information, I use individual floats such as float Rotationx, float Rotationy, and float Rotationz. If I ignore setting the value of rotation all together, the structure works fine.

Share this post


Link to post
Share on other sites
Same thing happens when I use a class and when I use coded declarations instead of allocating it dynamicly using the new keyword.

Share this post


Link to post
Share on other sites
Problem solved. By making Rotation and Position private, it's fine. But I never attempt to change it outside of SetRotation, SetPosition, Rotate, and Translate. Weird...

[edited by - TheChuckster on April 13, 2004 8:40:46 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by TheChuckster
Problem solved. By making Rotation and Position private, it''s fine. But I never attempt to change it outside of SetRotation, SetPosition, Rotate, and Translate. Weird...

[edited by - TheChuckster on April 13, 2004 8:40:46 PM]


I didn''t really pay attentian to what''s going on, but is there a chance you had a pointer/array in some other file that wrote to the wrong location?

<-- that''s still a link if you didn''t notice
"Gay marriage will encourage people to be gay, in the same way that hanging around tall people will make you tall." - Grizwald

Share this post


Link to post
Share on other sites
quote:
Original post by TheChuckster

for (i = 1; i <= 6; i++)
{
for(j = 1; j <= 4; j++)
{


Here's your problem.

You're counting starting with 1. Arrays are numbered starting with zero.

So in these for() loops you're skipping the first element of the arrays, and worse than that, you're writing data after the array ends which could be over-writing who knows what. (Probably whatever you declared next.)

Those lines should read :
quote:
for (i = 0; i < 6; i++)
{
for(j = 0; j < 4; j++)
{


Hope this helps!

-Andy

[edited by - AndyL on April 14, 2004 1:51:41 AM]

Share this post


Link to post
Share on other sites

  • Advertisement