Hi All,
I tried to use the attached OpenGL code to draw a Star-shape figure on my Microsoft Visual C++ .NET 2002-Windows XP Pro PC. I did 'Build’in Debug Mode successfully. I clicked the ‘! Start without Debug’ and it did not generate the Star-shape figure on my console and did not have any error messges. Please help and tell me what it is wrong with my coding in this program and how to correct the problem.
Thanks in advance,
Scott Chang
// —StarOGLkeys.cpp—
// —Rotating Star by stricking keys—
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>
#include <gl/glut.h>
// Rotation amounts
static GLfloat xRot = 0.0f;
static GLfloat yRot = 0.0f;
// Called to draw scene
void RenderScene(void)
{
// Clear the window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f);
glRotatef(yRot, 0.0f, 1.0f, 0.0f);
}
//Add Star Data here*
//******Here’s Where We Do All The Drawing
SetupRC()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_LINE_LOOP); // Drawing a star by using 10 vertices for the outer rims of a star
glVertex3f (0, 0.5, 0); //Specify the next 10 vertices
glVertex3f (-0.2, 0.2, 0); //Specify the next 10 vertices
glVertex3f (-0.5, 0.1, 0);
glVertex3f (-0.2, -0.1, 0);
glVertex3f (-0.3, -0.5, 0);
glVertex3f (0, -0.2, 0);
glVertex3f (0.3, -0.5, 0);
glVertex3f (0.2, -0.1, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0.2, 0.2, 0);
glVertex3f (0, 0.5, 0);
glEnd(); // Finished drawing the star shape
glColor3f (0.0, 1.0, 0.0); //Set the color to green
glBegin(GL_LINES); //Draw 10 lines radiating from (0,0,0) to each vertice
glVertex3f (0, 0.5, 0); //1st vertix to connect the origin to form a line
glVertex3f (0, 0, 0);
glVertex3f (-0.2, 0.2, 0); //2nd vertix
glVertex3f (0, 0, 0);
glVertex3f (-0.5, 0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (-0.2, -0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (-0.3, -0.5, 0);
glVertex3f (0, 0, 0);
glVertex3f (0, -0.2, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.3, -0.5, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.2, -0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0, 0, 0);
glVertex3f (0.2, 0.2, 0);
glVertex3f (0, 0, 0);
glColor3f (0.5, 0.5, 1.0); //Set the color to blue
glVertex3f (0, 0.5, 0);// last vertix
glVertex3f (0, 0, 0);
glEnd(); // Finished drawing the last line
glColor3f (1.0, 0.0, 0.0); //Set the color to red
glBegin(GL_TRIANGLES);
glVertex3f (0, 0, 0);
glVertex3f (0.5, 0.1, 0);
glVertex3f (0.2, 0.2, 0);
glEnd();
//return 0;
}
// Restore transformations
// glPopMatrix();
// Flush drawing commands
// glutSwapBuffers();
//Stricking Keys here*
void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_UP)
xRot-= 5.0f;
if(key == GLUT_KEY_DOWN)
xRot += 5.0f;
if(key == GLUT_KEY_LEFT)
yRot -= 5.0f;
if(key == GLUT_KEY_RIGHT)
yRot += 5.0f;
if(key > 356.0f)
xRot = 0.0f;
if(key < -1.0f)
xRot = 355.0f;
if(key > 356.0f)
yRot = 0.0f;
if(key < -1.0f)
yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow(“StarOGLkeys-GLUT Shapes”);
SetupRC();
return 0;
}