//MAKEFILE # To use this Makefile, just type make. That's it. CC = gcc LDLIBS = -L /usr/X11R6/lib -lGL -lGLU -lglut -lXmu -lX11 -lXi -lm INCLUDE = -I /usr/X11R6/include -I /usr/X11R6/include/GL executable: $(CC) -g $(INCLUDE) -o $I primitives $I primitives.c $(LDLIBS) -Wall ------------------------------------------------------------------------- //primitives.h #define SYSTEM_ZERO 0 #define SYSTEM_ONE 1 #define SYSTEM_TWO 2 /*type definitions*/ typedef GLfloat xyPoint[2]; typedef int xyPointInt[2]; typedef GLdouble xyzPoint[3]; typedef GLfloat color3f[3]; typedef GLfloat color4f[4]; typedef GLfloat matrix[16]; ------------------------------------------------------------------------- //primitives.c #include #include #include #include #include #include #include #include "primitives.h" #define AXIS 0 #define PYRAMID 1 #define HOUSE 2 void setWindowVals(xyPoint size, xyPoint pos, char * name); void initSystem(color4f winColor, color3f drawColor, xyPoint startDisp, xyPoint endDisp, float ptSize); void keyboardReader(unsigned char key, int x, int y); void display(); void defineMatrix(); int selected = AXIS; int showAll = 0; matrix * coordinateSystem; matrix system_one = {1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0}; matrix system_two = {1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0}; matrix system_zero = {1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0,0.0,0.0,0.0,1.0}; /* void initializeSystem() * This function initializes the matrices*/ void initializeSystem(){ int counter = 0; for (counter = 0; counter < 16; counter++){ if((counter == 0) || (counter == 5) || (counter == 10) || (counter == 15)) { system_one[counter] = 1.0; system_two[counter] = 1.0; system_zero[counter] = 1.0; } else { system_one[counter] = 0.0; system_two[counter] = 0.0; system_zero[counter] = 0.0; } } } /* int main(int argc, char **argv) * This function initializes the board, glut, the window, the GL system * the menu, the gl matrices, and then calls the glutMainLoop*/ int main(int argc, char **argv){ xyPoint startDisplay = {0.0,0.0};//location to start the display at xyPoint endDisplay = {500.0,500.0};//location to finish the display at color4f winBGColor = {0.6, 0.6, 0.6, 1.0};//window background color color3f drawColor = {1.0, 1.0, 1.0};//color we are drawing in xyPoint windowSize = {500, 500};//size of the window xyPoint windowLoc = {0, 0};//location on screen of the window float ptsize = 0.5;//the point size char * windowName = "Primitives";//name of the window /*initiate interaction with the local window system*/ glutInit(&argc, argv); /*initialize the window variables*/ setWindowVals(windowSize, windowLoc, windowName); /*initialize the system*/ initSystem(winBGColor, drawColor, startDisplay, endDisplay, ptsize); //coordinateSystem = &system_zero; defineMatrix(); initializeSystem(); /*set the initial position of the pyramid*/ glLoadMatrixf(system_one); glTranslatef(0, -50, 0); glRotatef(-45, 1, 0, 0); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); /*set the initial position of the house*/ glLoadMatrixf(system_two); glTranslatef(-20, -50, 0); glRotatef(-45, 1, 0, 0); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); glutMainLoop(); } /* setWindowVals(xyPoint size, xyPoint pos, char * name) * Set the variables for the window*/ void setWindowVals(xyPoint size, xyPoint pos, char * name){ /*set number and type of buffers*/ glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); /*set the window size*/ glutInitWindowSize(size[0], size[1]); /*position the window*/ glutInitWindowPosition(pos[0], pos[1]); /*actually create the window*/ glutCreateWindow(name); } /* initSystem(color4f winColor, color3f drawColor, xyPoint startDisp, xyPoint endDisp, float ptSize) * This function initializes all of display variables*/ void initSystem(color4f winColor, color3f drawColor, xyPoint startDisp, xyPoint endDisp, float ptSize){ /*set the background color*/ glClearColor(winColor[0], winColor[1], winColor[2], winColor[3]); /*clear the window*/ glClear(GL_COLOR_BUFFER_BIT); /*set the drawing color*/ glColor3f(drawColor[0], drawColor[1], drawColor[2]); glMatrixMode(GL_PROJECTION); glLoadIdentity(); /*set the portion of the world to display*/ glOrtho(-150.0, 150.0, -150.0, 150.0, -150.0, 150.0); glMatrixMode(GL_MODELVIEW); /*set the point size*/ glPointSize(ptSize); /*set the callback functions*/ glutDisplayFunc(display); glutKeyboardFunc(keyboardReader); } /* void keyboardReader(unsigned char key, int x, int y) * this function handles all keyboard interaction*/ void keyboardReader(unsigned char key, int x, int y){ if(selected != AXIS){ glLoadMatrixf(*coordinateSystem); } /*select the coordinate system*/ if (key == '0'){ selected = AXIS; coordinateSystem = &system_zero; display(); } if (key == '1'){ selected = PYRAMID; coordinateSystem = &system_one; display(); } if (key == '2'){ selected = HOUSE; coordinateSystem = &system_two; display(); } /*rotate around X axis*/ if ((key == 'X') || (key == 'x')){ if(selected == AXIS){ /*create the modified origin matrix*/ glLoadIdentity(); glRotatef(10, 1, 0, 0); /*multiply it times the current matrix*/ glMultMatrixf(system_one); /*save matrix*/ glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glRotatef(10, 1, 0, 0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glRotatef(10, 1, 0, 0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*rotate around Y axis*/ if ((key == 'Y') || (key == 'y')){ if(selected == AXIS){ glLoadIdentity(); glRotatef(10, 0, 1, 0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glRotatef(10, 0, 1, 0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glRotatef(10, 0, 1, 0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*rotate around Z axis*/ if ((key == 'Z') || (key == 'z')){ if(selected == AXIS){ glLoadIdentity(); glRotatef(10, 0, 0, 1); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glRotatef(10, 0, 0, 1); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glRotatef(10, 0, 0, 1); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object up along Y axis*/ if ((key == 'U') || (key == 'u')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(0.0, 5.0, 0.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(0.0, 5.0, 0.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(0.0, 5.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object down along y Axis*/ if ((key == 'D') || (key == 'd')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(0.0, -5.0, 0.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(0.0, -5.0, 0.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(0.0, -5.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object left along x Axis*/ if ((key == 'L') || (key == 'l')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(-5.0, 0.0, 0.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(-5.0, 0.0, 0.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(-5.0, 0.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object right along x Axis*/ if ((key == 'R') || (key == 'r')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(5.0, 0.0, 0.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(5.0, 0.0, 0.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(5.0, 0.0, 0.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object back along z Axis*/ if ((key == 'B') || (key == 'b')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(0.0, 0.0, -5.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(0.0, 0.0, -5.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(0.0, 0.0, -5.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*move object forward along z Axis*/ if ((key == 'F') || (key == 'f')){ if(selected == AXIS){ glLoadIdentity(); glTranslatef(0.0, 0.0, 5.0); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glTranslatef(0.0, 0.0, 5.0); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glTranslatef(0.0, 0.0, 5.0); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*scale object size up*/ if (key == '+'){ if(selected == AXIS){ glLoadIdentity(); glScalef(1.5,1.5,1.5); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glScalef(1.5,1.5,1.5); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glScalef(1.5,1.5,1.5); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*scale object size down*/ if (key == '-'){ if(selected == AXIS){ glLoadIdentity(); glScalef(0.66,0.66,0.66); glMultMatrixf(system_one); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glScalef(0.66,0.66,0.66); glMultMatrixf(system_two); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); } else { glScalef(0.66,0.66,0.66); glGetFloatv(GL_MODELVIEW_MATRIX, *coordinateSystem); } display(); } /*reset the system*/ if ((key == 'C') || (key == 'c')){ initializeSystem(); display(); } /*quit*/ if ((key == 'Q') || (key == 'q')){ printf("Quit\n"); exit(1); } /*show object axes*/ if ((key == 'A') || (key == 'a')){ if (showAll == 0) { showAll = 1; } else { showAll = 0; } display(); } } /* void display() * This function handles the drawing of the objects and coordinate lines*/ void display(){ glClear(GL_COLOR_BUFFER_BIT); /*draw the coordinate system*/ glMatrixMode(GL_MODELVIEW); glLoadMatrixf(system_zero); glColor3f(0.0,0.0,1.0); /*draw the X axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(100.0, 0.0, 0.0); glEnd(); /*draw the Y axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 100.0, 0.0); glEnd(); /*draw the fake Z axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(-30.0, -30.0, 0.0); glEnd(); /*color the object appropriately*/ if(selected == PYRAMID){ glColor3f(1.0,0.0,0.0); } else{ glColor3f(0.0, 0.0, 0.0); } glMatrixMode(GL_MODELVIEW); glLoadMatrixf(system_one); if(showAll == 1){ /*draw the X axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(100.0, 0.0, 0.0); glEnd(); /*draw the Y axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 100.0, 0.0); glEnd(); /*draw the Z axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 100.0); glEnd(); } /*position the cone at the center of the axes*/ glTranslatef(0, 0, -15/2); glutWireCone(15.0, 15.0, 4.0, 3.0); glTranslatef(0, 0, 15/2); /*color the object appropriately*/ if(selected == HOUSE){ glColor3f(1.0,0.0,0.0); } else{ glColor3f(0.0, 0.0, 0.0); } glMatrixMode(GL_MODELVIEW); glLoadMatrixf(system_two); if(showAll == 1){ /*draw the X axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(100.0, 0.0, 0.0); glEnd(); /*draw the Y axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 100.0, 0.0); glEnd(); /*draw the Z axis*/ glBegin(GL_LINE_LOOP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 100.0); glEnd(); } /*position the cone on top of the cube, and the axes at the center*/ glutWireCube(15.0 * sqrt(2)); glTranslatef(0, 0, (15 * sqrt(2))/2); glRotatef(45, 0, 0, 1); glutWireCone(15.0, 15.0, 4.0, 3.0); /*draw the image*/ glutSwapBuffers(); } /* void defineMatrix() * This function loads the identity matrix into each stored matrix*/ void defineMatrix(){ glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glGetFloatv(GL_MODELVIEW_MATRIX, system_zero); glLoadIdentity(); glGetFloatv(GL_MODELVIEW_MATRIX, system_one); glLoadIdentity(); glGetFloatv(GL_MODELVIEW_MATRIX, system_two); }