Hello all! I am in week 3 of learning how to program in openGL and I am having a tough time making a program interactive. There are several parts to my assignment but I am getting stuck on my switch statement and I am not sure that I am approaching this correctly… the goal is to have a user input what shape they would like drawn to the screen…‘t’ for triangle etc. I have been progressively adding functionality so as I have completed steps, I have been able to successfully draw each of the shapes, and have been able to get a rotating cube who’s rotation direction can be altered by mouse clicks. So my problem is here…the prgm is exiting with a 255 status. It appears that when I type in a t to draw a triangle or whatever shape, it pops up on the screen for less than a second and then just freaks out…
So I am not sure that I am handling my display correctly? and what each switch statement actually does…I also am not too sure as to whether or not I am supposed to have a ‘special’ key function defined as well that actually handles the switch cases…any ideas or hints?
Thanks so much!!
void init()
{
glClearColor(1.0,1.0,1.0,0.0); // set white background color
glColor3f(1.0f, 0.0f, 0.0f); // set the drawing color
glLineWidth(4.0); // a ?dot? is 4 by 4 pixels
}
//--------------- setWindow ---------------------
void setWindow(double left, double right, double bottom, double top)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0, 1.0, -1.0, 1.0);
}
//---------------- setViewport ------------------
void setViewport(int left, int right, int bottom, int top)
{
glViewport(left, bottom, right - left, top - bottom);
}
void displaySquare(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(0.5, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
void displayTriangle(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(-0.5, -0.5);
glVertex2f(0.0, 0.5);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}
void displayPentagon(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(0.0, 1.0);
glVertex2f(-0.951, 0.309);
glVertex2f(-0.588, -0.809);
glVertex2f(0.588, -0.809);
glVertex2f(0.951, 0.309);
glEnd();
glFlush();
}
void displayHexagon(){
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex2f(1.0, 0.0);
glVertex2f(0.5, 0.866); //0.866 = sqrt(3) / 2
glVertex2f(-0.5, 0.866);
glVertex2f(-1.0, 0.0);
glVertex2f(-0.5, - 0.866);
glVertex2f(0.5, - 0.866);
glEnd();
glFlush();
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay-CIRCLE>>>>>>>>>>>>>>>>>
void myCircle()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
const float PI = 3.1415926535;
double radius = 1.0;
int i;
int sides = 100;
for(i = 0; i < 360; i += 360/sides)
{
double angle = PI * i / 180;
glVertex2d(cos(angle), sin(angle));
}
glEnd();
glFlush();
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay-implicit CIRCLE>>>>>>>>>>>>>>>>>
void myImplicitCircle(){
glClear(GL_COLOR_BUFFER_BIT);
setWindow(-9.0, 9.0, -0.3, 1.0);
setViewport(0, 250, 0, 250);
glBegin(GL_LINE_LOOP);
double radius = 1.0;
double x, y;
int i;
{
for(x = -radius; x <= radius; x += 0.001) // draw the plot
{ double temp = (radius * radius) - (x * x);
y = sqrt(temp);
glVertex2f(x, y);
}
for(x = radius; x >= -radius; x -= 0.001) // draw the plot
{ double temp = (radius * radius) - (x * x);
y = sqrt(temp);
glVertex2f(x, -y);
}
}
glEnd();
setViewport(175, 500, 175, 500);
glBegin(GL_LINE_LOOP);
radius = 0.75;
{
for(x = radius; x >= -radius; x -= 0.001) // draw the plot
{ double temp = (radius * radius) - (x * x);
y = sqrt(temp);
glVertex2f(x, y);
}
for(x = -radius; x <= radius; x += 0.001) // draw the plot
{ double temp = (radius * radius) - (x * x);
y = sqrt(temp);
glVertex2f(x, -y);
}
}
glEnd();
glFlush();
}
//<<<<<<<<<<<<<<<<<<<<<<<< SPHERE>>>>>>>>>>>>>>>>>
void mySphere(){
glClear(GL_COLOR_BUFFER_BIT);
setWindow(-9.0, 9.0, -0.3, 1.0);
setViewport(0, 500, 0, 500);
int color = 0;
float x, y, z;
float phi, phir, phir20, theta, thetar, theta20;
float c = 0.01745329252;
float radius = 0.75;
for (phi = -80.0; phi <= 80.0; phi += 20.0) {
phir = phi * c;
phir20 = c * (phi + 20);
glBegin(GL_LINE_STRIP);
for (theta = -180.0; theta <= 180.0; theta += 20.0) {
if (color) {
glColor3f(1,0,0);
} else {
glColor3f(1,1,1);
}
thetar = c * theta;
x = sin(thetar) * cos(phir);
y = cos(thetar) * cos(phir);
z = sin(phir);
glVertex3d(x, y, z);
color = 1 - color;
}
}
glEnd();
glFlush();
}
void spinCube()
{
theta[axis] += .05;
if(theta[axis] > 360.0) theta[axis] -= 360.0;
glutPostRedisplay();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state==GLUT_DOWN)
axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
//glutSolidCube(1.0);
glutSwapBuffers();
}
//<<<<<<<<<<<<<<<<<<<<<<<< myKeyboard >>>>>>>>>>>>>>
void myKeyboard(unsigned char theKey, int mouseX, int mouseY)
{
switch(theKey)
{
case 'c':
case 'C':
//draw cube
glutSolidCube(1.0);
glutPostRedisplay();
break;
case 'h':
case 'H':
//draw hexagon
displayHexagon();
glutPostRedisplay();
break;
case 'r':
case 'R':
//draw rectangle
displaySquare();
glutPostRedisplay();
break;
case 'T':
case 't':
//draw triangle
displayTriangle();
glutPostRedisplay();
break;
case 'Q':
case 'q':
exit(-1); //terminate the program
case 'x':
case 'X':
//draw circle
myImplicitCircle();
glutPostRedisplay();
break;
default:
break; // do nothing
}
}
//<<<<<<<<<<<<<<<<<<<<<<<< myDisplay >>>>>>>>>>>>>>>>>
/*void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear the screen
setWindow(-9.0, 9.0, -0.3, 1.0); // set the window
setViewport(0, 800, 0, 600); // set the viewport
glBegin(GL_LINE_STRIP);
GLfloat x;
for(x = -8.0; x < 8.0; x += 0.1) // draw the plot
glVertex2f(x, sin(3.14159 * x) / (3.14159 * x));
glEnd();
glFlush(); // send all output to display
}
*/
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow("Implicit Circle");
//glutDisplayFunc(displaySquare);
//glutDisplayFunc(displayTriangle);
//glutDisplayFunc(displayPentagon);
//glutDisplayFunc(displayHexagon);
//glutDisplayFunc(myCircle);
//glutDisplayFunc(myImplicitCircle);
//glutDisplayFunc(mySphere);
glutDisplayFunc(display);
//glutIdleFunc(spinCube);
glutMouseFunc(mouse);
glutKeyboardFunc(myKeyboard);
glEnable(GL_DEPTH_TEST);
init();
glutMainLoop();
}