第四弹 3D物体无光照绘制

来源:互联网 发布:家琦外贸淘宝 垃圾 编辑:程序博客网 时间:2024/04/29 06:33
  1 /*************************************************************************  2     > File Name: frame.cpp  3     > Author:   4     > Mail:   5     > Created Time: 2015年11月21日 星期六 15时28分01秒  6  ************************************************************************/  7   8 #include<iostream>  9 #include<GL/glut.h> 10 using namespace std; 11 static GLfloat spin=0.0; 12 void init(void) 13 { 14     glClearColor(0.0,0.0,0.0,0.0); 15     glShadeModel(GL_SMOOTH); 16 } 17  18 void display(void) 19 { 20     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 21     glPushMatrix(); 22     glColor3f(1.0,1.0,1.0); 23  24     gluLookAt(-5.0,5.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); 25  26     glLoadIdentity(); 27     glRotatef(spin,0.0,1.0,0.0); 28     //glutSolidTeapot(20); 29     glScalef(20.0f,20.0f,20.0f); 30     glBegin(GL_TRIANGLES); 31         //front 32         glColor3f(1.0,0.0,0.0); 33         glVertex3f(0.0f,1.0f,0.0f); 34         glColor3f(0.0,1.0,0.0); 35         glVertex3f(-2.0f,0.0f,0.0f); 36         glColor3f(0.0,0.0,1.0); 37         glVertex3f(2.0f,0.0f,0.0f); 38         //left 39         glColor3f(1.0,0.0,0.0); 40         glVertex3f(0.0f,1.0f,0.0f); 41         glColor3f(1.0,0.0,1.0); 42         glVertex3f(0.0f,0.0f,3.0f); 43         glColor3f(0.0,0.0,1.0); 44         glVertex3f(2.0f,0.0f,0.0f); 45  46         //right 47         glColor3f(1.0,0.0,0.0); 48         glVertex3f(0.0f,1.0f,0.0f); 49         glColor3f(1.0,0.0,1.0); 50         glVertex3f(0.0f,0.0f,3.0f); 51         glColor3f(0.0,1.0,0.0); 52         glVertex3f(-2.0f,0.0f,0.0f); 53         //bottom 54         glColor3f(0.0,1.0,0.0); 55         glVertex3f(-2.0f,0.0f,0.0f); 56         glColor3f(0.0,0.0,1.0); 57         glVertex3f(2.0f,0.0f,0.0f); 58         glColor3f(1.0,0.0,1.0); 59         glVertex3f(0.0f,0.0f,3.0f); 60     glEnd(); 61     glPopMatrix(); 62     glutSwapBuffers(); 63 } 64  65 void spinDisplay(void) 66 { 67     spin=spin+2.0; 68     if(spin>360) 69         spin=spin-360.0; 70     glutPostRedisplay(); 71 } 72  73 void reshape(int w,int h) 74 { 75     glViewport(0,0,(GLsizei)w,(GLsizei)h); 76     glMatrixMode(GL_PROJECTION); 77     glLoadIdentity(); 78     glOrtho(-50.0,50.0,-50.0,50.0,-1.0,1.0); 79     glMatrixMode(GL_MODELVIEW); 80     glLoadIdentity(); 81 } 82  83 void mouse(int button,int state,int x,int y) 84 { 85     switch(button) 86     { 87         case GLUT_LEFT_BUTTON: 88             if(state == GLUT_DOWN) 89                 glutIdleFunc(spinDisplay); 90             break; 91         case GLUT_MIDDLE_BUTTON: 92             if(state == GLUT_DOWN) 93                 glutIdleFunc(NULL); 94             break; 95         default: 96             break; 97     } 98 } 99 100 int main(int argc,char ** argv)101 {102     glutInit(&argc,argv);103     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);104     glutInitWindowSize(400,400);105     glutInitWindowPosition(100,100);106     glutCreateWindow("OpenGL test");107     init();108 109     glutDisplayFunc(display);110     glutReshapeFunc(reshape);111     glutMouseFunc(mouse);112 113     glutMainLoop();114     return 0;115 }

效果图:

 

0 0
原创粉丝点击