Hello everybody!
I’m trying to write program rendering cone, that can be rotated using direction keys. I approximate cone by triangles and use GL_TRIANGLES_FAN to defien vertexes. If all the vertexes have the same color everuthing is ok. But if I alternate color of vertexes (red-green-red… and so on) strange behaviour is observable: on every even(n%2 == 0) render I see a cone and on every odd(n%2 == 1) render I see a flat circle. What am I doing wrong?
Unfortunately , I can’t add links to screenshots, but hope explanation is understandable.
System: Windows 7 Maximum (6.1.7600)
OpenGL: version 4.3.0; vendor NVIDIA; renderer GeForce GTX 560
FreeGLUT: 2.8.1-1.mp for MSVC
IDE: Microsoft Visual Studio 2008 Version 9.0.21022.8 RTM
Source code:
#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;
long 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 = %d
", renderNum, xRot, yRot);
glBegin(GL_TRIANGLE_FAN);
{
int icolor = 0;
glVertex3f(0.0f,0.0f,HEIGHT);
for(GLfloat angle = 0.0f; angle <= 2*GL_PI; angle += (GL_PI / 10))
{
glColor3fv(icolor++ % 2? vColorRED : vColorGRN);
glVertex2f(RADIUS * cos(angle), RADIUS * sin(angle));
}
}
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
", glGetString(GL_VERSION));
printf("Vendor: %s
", glGetString(GL_VENDOR));
printf("Renderer: %s
", glGetString(GL_RENDERER));
glutMainLoop();
return 0;
}