计算机图形学实验四
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <gl/glut.h> static GLsizei iMode = 1; static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; int winWidth = 400, winHeight = 200; int iPointNum = 0; int x1=0,x2=0,y1=0,y2=0; GLuint Cube; void initial(void) { glClearColor(1.0, 1.0, 1.0, 1.0); glMatrixMode (GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-3.0, 3.0, -3.0,3.0); Cube=glGenLists(1); glNewList(Cube,GL_COMPILE); glBegin(GL_TRIANGLE_FAN); glShadeModel(GL_FLAT); glColor3f(1.0,0.0,0.0); glVertex3f(1,1,1); glVertex3f(2,0,0); glVertex3f(2,1,0); glEnd(); glBegin(GL_TRIANGLE_FAN); glShadeModel(GL_FLAT); glColor3f(1.0,1.0,0.0); glVertex3f(1,1,1); glVertex3f(0,1,0); glVertex3f(2,1,0); glEnd(); glBegin(GL_TRIANGLE_FAN); glShadeModel(GL_FLAT); glColor3f(0.0,0.0,1.0); glVertex3f(1,1,1); glVertex3f(0,1,0); glVertex3f(2,0,0); glEnd(); glBegin(GL_TRIANGLE_FAN); glShadeModel(GL_FLAT); glColor3f(0.0,1.0,0.0); glVertex3f(0,1,0); glVertex3f(2,0,0); glVertex3f(2,1,0); glEnd(); glEndList(); } void triangle (GLsizei mode) { if(mode == 1) { glPolygonMode(GL_FRONT_AND_BACK,GL_LINE); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-5.0,5.0,-5.0,5.0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); switch(iMode){ case 1:glCallList(Cube);break; case 2:glRotatef(-90.0,1.0,0.0,0.0);glCallList(Cube);break; case 3:glRotatef(90.0,0.0,1.0,0.0);glCallList(Cube);break; } glPopMatrix(); } else { glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,1,1.0f,100.0f); gluLookAt(0,0,-10,0,0,0,0,1,0); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glRotatef(xRot,1.0f,0.0f,0.0f); glRotatef(yRot,0.0f,1.0f,0.0f); glCallList(Cube); glPopMatrix(); glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f); } } void Display(void) { glClear(GL_COLOR_BUFFER_BIT); glViewport(0, 0, 200, 200); triangle(1); glViewport(200, 0, 200, 200); triangle(2); glFlush(); } void PassiveMouseMove (GLint xMouse, GLint yMouse) { if(iPointNum == 1) { x2 = xMouse; y2 = winHeight - yMouse; if(y2<=y1) xRot -= 0.05f; if(y2>=y1) xRot += 0.05f; if(x2>=x1) yRot -= 0.05f; if(x2<=x1) yRot += 0.05f; if(xRot > 356.0f) xRot = 0.0f; if(xRot < -1.0f) xRot = 355.0f; if(yRot > 356.0f) yRot = 0.0f; if(yRot < -1.0f) yRot = 355.0f; glutPostRedisplay(); } } void ProcessMenu(int value) { iMode = value; glutPostRedisplay(); } void MousePlot(GLint button, GLint action, GLint xMouse, GLint yMouse) { if(button == GLUT_LEFT_BUTTON && action == GLUT_DOWN) { if(iPointNum == 0 || iPointNum == 2){ iPointNum = 1; x1 = xMouse; y1 = winHeight - yMouse; } else{ iPointNum = 2; x2 = xMouse; y2 = winHeight - yMouse; glutPostRedisplay(); } } if(button == GLUT_RIGHT_BUTTON && action == GLUT_DOWN){ iPointNum = 0; glutPostRedisplay(); } } void main() { glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowPosition(100, 100); glutInitWindowSize(400,200); glutCreateWindow("实验四-三视图与旋转"); int nMainMenu = glutCreateMenu(ProcessMenu); glutAddMenuEntry("正视图",1); glutAddMenuEntry("俯视图", 2); glutAddMenuEntry("侧视图", 3); glutAttachMenu(GLUT_RIGHT_BUTTON); glutMouseFunc(MousePlot); glutPassiveMotionFunc(PassiveMouseMove); initial(); glutDisplayFunc(Display); glutMainLoop(); }