# glVertex and variables unwillingly modified

This is my first attempt at an OpenGL application, and I’m using GLUT. I’m writing a program that, among other things, will draw a triangle to the screen, but it’s acting… eh… funny. OpenGL seems to be modifying my variables after it’s called, and I’m not sure how to prevent that.

Here’s the function in question, with some printf’s added to see what was going on:

``````
void triangle(void)		// Draw the triangle to screen
{
float r, a, x, y;

r = sc / sb;
a = M_PI_2 - naA;
x = r * sin(a);
y = r * cos(a);

printf("x = %f
", x);
printf("y = %f
", y);

glColor3f(.0625,.1875,.125);

glBegin(GL_TRIANGLES);
glVertex2f(0,0);
glVertex2f(1,0);
glVertex2f(x,y);
glEnd();
}

``````

First, I attempt to draw a 120 degree isosceles triangle with legs of 1 unit.

In the terminal this yields:

x = 1.500000
y = 0.866025

Which is correct. However, this isn’t what OpenGL draws, and if I attempt to resize the window, thereby recalling the function, the terminal will spit out:

x = 0.866025
y = 0.500000

Which is what is actually being drawn.

Now, I could cast the x and y variables as const floats in the glVertex2f call, which would have the effect of the triangle initially draw correctly… that is until I resize the window.

What am I missing?

Well, I noticed another variable was changing after a reshape that was affecting this. This seems to have been solved by getting all calculations out of the display function and done before initializing GLUT; although, the calculations in the above function are still there.