#### Archived

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

# Variables Getting Overwritten?

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

## 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 on other sites
I didnt even read ur post i guess.

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

##### Share on other sites
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

and the vertex info is defined with

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.

becomes

##### Share on other sites
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 on other sites
Thanks for your help, aside from the color vs colour remark. It''s just a difference in dialects.

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

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

}
}

glEnd();

rtri+= 50.0f * TimeElapsed;

return;
}

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

1. 1
Rutin
33
2. 2
3. 3
4. 4
5. 5

• 13
• 9
• 9
• 9
• 14
• ### Forum Statistics

• Total Topics
633330
• Total Posts
3011385
• ### Who's Online (See full list)

There are no registered users currently online

×