#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
#define GL_PI 3.1415f
#define ROTAION_STEP 5.0f
#define FULL_CIRCLE 360.0f
#define HEIGHT 90.0f
#define RADIUS 45.0f
GLfloat vColorRED[3] = {1.0f, 0.0f, 0.0f};
GLfloat vColorGRN[3] = {0.0f, 1.0f, 0.0f};
GLfloat xRot = 315.0f;
GLfloat yRot = 0.0f;
int renderNum = 0;
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
printf("renderNum = %d, xRot = %f, yRot = %f\n", renderNum, xRot, yRot);
glBegin(GL_TRIANGLE_FAN);
{
int icolor = 1;
glVertex3f(0.0f,0.0f,HEIGHT);
for(GLfloat angle = 0.0f; angle <= 2*GL_PI; angle += (GL_PI / 10))
{
glColor3fv(icolor++ % 2? vColorRED : vColorGRN);
glVertex3f(RADIUS * cos(angle), RADIUS * sin(angle), 0.0f); // <---------------------------- This simple change is the answer to how to make it work correctly
}
}
glEnd();
glPopMatrix();
glutSwapBuffers();
renderNum++;
}
void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glColor3fv(vColorGRN);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glPolygonMode(GL_BACK,GL_LINE);
}
void SpecialKeys(int key, int x, int y)
{
switch(key)
{
case GLUT_KEY_UP: xRot-= ROTAION_STEP; break;
case GLUT_KEY_DOWN: xRot+= ROTAION_STEP; break;
case GLUT_KEY_LEFT: yRot-= ROTAION_STEP; break;
case GLUT_KEY_RIGHT: yRot+= ROTAION_STEP; break;
}
if(xRot > FULL_CIRCLE - ROTAION_STEP)
xRot = 0.0f;
if(xRot < 0.0f)
xRot = FULL_CIRCLE - ROTAION_STEP;
if(yRot > FULL_CIRCLE - ROTAION_STEP)
yRot = 0.0f;
if(yRot < 0.0f)
yRot = FULL_CIRCLE - ROTAION_STEP;
glutPostRedisplay();
}
void ChangeSize(int w, int h)
{
GLfloat nRange = 100;
if(h == 0) h = 1;
if(w == 0) w = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glutCreateWindow("Rotating Cone Example");
glutPositionWindow(300, 300);
glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutDisplayFunc(RenderScene);
SetupRC();
printf("Version: %s\n", glGetString(GL_VERSION));
printf("Vendor: %s\n", glGetString(GL_VENDOR));
printf("Renderer: %s\n", glGetString(GL_RENDERER));
glutMainLoop();
return 0;
}