Question
Create a 3D drawing using OpenGL and C or C++ for the following code for a 3D house: #include #include using namespace std; float ORG[3]
Create a 3D drawing using OpenGL and C or C++ for the following code for a 3D house:
#include
using namespace std;
float ORG[3] = {0,0,0}; float XP[3] = {10,0,0}, XN[3] = {-10,0,0}, YP[3] = {0,10,0}, YN[3] = {0,-10,0}, ZP[3] = {0, 0, 10}, ZN[3] = {0, 0, -10};
int x_rotation, y_rotation, z_rotation;
void drawHouse(void);
void displayMe(void) { glPushMatrix();
glRotatef((GLfloat) x_rotation, 1, 0, 0); glRotatef((GLfloat) y_rotation, 0, 1, 0); glRotatef((GLfloat) z_rotation, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINES); glColor3f(1,0,0); //Red x-axis glVertex3fv(XN); glVertex3fv(XP);
glColor3f(0,1,0); //Green y-axis glVertex3fv(YN); glVertex3fv(YP);
glColor3f(0,0,1); //Blue z-axis glVertex3fv(ZN); glVertex3fv(ZP);
glEnd();
drawHouse(); glPopMatrix(); glFlush(); }
void drawHouse(){ glBegin(GL_QUADS);//front wall glColor3f(0, 1, 0); glVertex3f(0.6,-0.4, -0.4); //bottom right glVertex3f(0.6, 0.4, -0.4); //top right glVertex3f(-0.6,0.4, -0.4); //top left glVertex3f(-0.6, -0.4, -0.4); //bottom left glEnd();
glBegin(GL_QUADS);//back wall glColor3f(1, 0, 1); glVertex3f(0.6,-0.4, 0.4); //bottom right glVertex3f(0.6, 0.4, 0.4); //top right glVertex3f(-0.6,0.4, 0.4); //top left glVertex3f(-0.6, -0.4, 0.4); //bottom left glEnd();
glBegin(GL_QUADS);//back window glColor3f(0, 0, 0); glVertex3f(0.4,-0.2, 0.4); //bottom right glVertex3f(0.4, 0.2, 0.4); //top right glVertex3f(0.1,0.2, 0.4); //top left glVertex3f(0.1, -0.2, 0.4); //bottom left glEnd();
glBegin(GL_QUADS); glColor3f(0, 0, 1);//side1 glVertex3f(-0.6, -0.4, -0.4); //bottom right glVertex3f(-0.6,0.4, -0.4); //top right glVertex3f(-0.6,0.4, 0.4); //top left glVertex3f(-0.6, -0.4, 0.4); //bottom left glEnd();
glBegin(GL_QUADS);//side2 glColor3f(0, 0, 1); glVertex3f(0.6,-0.4, 0.4); //bottom right glVertex3f(0.6, 0.4, 0.4); //top right glVertex3f(0.6, 0.4, -0.4); //top left glVertex3f(0.6,-0.4, -0.4); //bottom left glEnd();
glBegin(GL_TRIANGLES);//roof back glColor3f(1, 0, 0); glVertex3f(0.6, 0.4, -0.4); //bottom right glVertex3f(0.0, 0.7, 0.0); //top glVertex3f(-0.6,0.4, -0.4); //bottom left glEnd();
glBegin(GL_TRIANGLES);//roof front glColor3f(1, 0, 0); glVertex3f(0.6, 0.4, 0.4); //bottom right glVertex3f(0.0, 0.7, 0.0); //top glVertex3f(-0.6,0.4, 0.4); //bottom left glEnd();
glBegin(GL_TRIANGLES);//roof side1 glColor3f(1, 0, 0); glVertex3f(-0.6,0.4, 0.4); //bottom right glVertex3f(0.0, 0.7, 0.0); //top glVertex3f(-0.6,0.4, -0.4); //bottom left glEnd();
glBegin(GL_TRIANGLES);//roof side2 glColor3f(1, 0, 0); glVertex3f(0.6, 0.4, -0.4); //bottom right glVertex3f(0.0, 0.7, 0.0); //top glVertex3f(0.6, 0.4, 0.4); //bottom left glEnd();
glBegin(GL_POLYGON);//front window glColor3f(1, 1, 1); glVertex3f(0.4,-0.2, -0.4); //bottom right glVertex3f(0.4, 0.2, -0.4); //top right glVertex3f(0.1,0.2, -0.4); //top left glVertex3f(0.1, -0.2, -0.4); //bottom left glEnd();
glBegin(GL_POLYGON);//front door glColor3f(0, 0, 0); glVertex3f(-0.4,-0.4, -0.4); //bottom right glVertex3f(-0.4, 0.0, -0.4); //top right glVertex3f(-0.1,0.0, -0.4); //top left glVertex3f(-0.1, -0.4, -0.4); //bottom left glEnd();
glBegin(GL_QUADS); glColor3f(1, 1, 1);//bottom glVertex3f(0.6,-0.4, -0.4); //bottom right glVertex3f(0.6,-0.4, 0.4); //top right glVertex3f(-0.6, -0.4, 0.4); //top left glVertex3f(-0.6, -0.4, -0.4); //bottom left glEnd();
}
void keyboard(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;
} } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE); glutInitWindowSize(800, 800); glutInitWindowPosition(100, 100); glutCreateWindow("Week 3"); glutDisplayFunc(displayMe); glutKeyboardFunc(keyboard); glutMainLoop();
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started