Question: Create a 3D drawing using OpenGL and C or C++ for the following code for a 3D Benab: #include #include void render(); void onKeyPress(unsigned char
Create a 3D drawing using OpenGL and C or C++ for the following code for a 3D Benab:
#include
void render(); void onKeyPress(unsigned char key, int x, int y);
void drawAxes(); void drawBenabRoof(); void drawBenabBase();
int x_rotation = 0, y_rotation = 0, z_rotation = 0; // angle of rotation
int main(int argc, char **argv) // entry point into program; arguments are used during //initialization and can be accessed during the execution of the program { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE); // sets the initial display mode; GLUT_SINGLE - single buffered window
glutInitWindowSize(800, 800); glutInitWindowPosition(100, 100); //experiment with changing the window position glutCreateWindow("3D Benab");
glutDisplayFunc(render); //sets the display callback for the current window; may need to display changes to the display during the course of the program glutKeyboardFunc(onKeyPress); // sets the keyboard callback for the current window; will listen for keyboard input
glutMainLoop(); //tells the program to enter the GLUT event processing loop. //(This just means the program sits and waits for things to happen, //such as window refreshes, window resizes, mouse clicks, key presses, etc.). glutMainLoop never exits, so it should always be the last line of the main program.
return 0; }
void render() { glPushMatrix(); //main push
glRotatef((GLfloat)x_rotation, 1, 0, 0); //angle, rotate in x glRotatef((GLfloat)y_rotation, 0, 1, 0); //angle, rotate in y glRotatef((GLfloat)z_rotation, 0, 0, 1); //angle, rotate in z
glClear(GL_COLOR_BUFFER_BIT);
drawAxes(); //drawBenabRoof(); //drawBenabBase();
glPopMatrix();
glFlush(); }
void onKeyPress(unsigned char key, int x, int y) { switch (key) { case 'x': x_rotation++; glutPostRedisplay(); break; case 'y': y_rotation++; glutPostRedisplay(); break; case 'z': z_rotation++; glutPostRedisplay(); break; default: break; } }
void drawAxes() { glBegin(GL_LINES); glColor3f(1, 0, 0); //Red x-axis glVertex3f(-10.0, 0.0, 0.0); //end point glVertex3f(10.0, 0.0, 0.0); //end point
glColor3f(0, 1, 0); //Green y-axis glVertex3f(0.0, -10.0, 0.0); //end point glVertex3f(0.0, 10.0, 0.0); //end point
glColor3f(0, 0, 1); //Blue z-axis glVertex3f(0.0, 0.0, -10.0); //end point glVertex3f(0.0, 0.0, 10.0); //end point glEnd(); }
void drawBenabRoof() { glColor3f(1, 0, 0); //colour of roof glutWireCone(1, 1, 100, 100); //base radius, height, slices stacks
}
void drawBenabBase() { GLUquadricObj *quadratic = gluNewQuadric();
glColor3f(0, 1, 0); //colour of base gluCylinder(quadratic, 0.8, 1, 0.8, 100, 100); //new quadric object, base radius, top radius, height, slices, stacks
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
