第五弹 带光照茶壶绘制
2015-12-01 18:38
357 查看
1 /************************************************************************* 2 > File Name: frame.cpp 3 > Author:Aerk 4 > Mail: 1134675716@qq.com 5 > Created Time: 2015年11月22日 星期日 16时57分01秒 6 ************************************************************************/ 7 8 #include<iostream> 9 #include<GL/glut.h> 10 using namespace std; 11 static GLfloat spin=0.0; 12 static GLfloat controlValue=10.0; 13 void init(void) 14 { 15 GLfloat mat_specular[]={1.0,1.0,1.0,1.0}; 16 GLfloat mat_shininess[]={50.0}; 17 18 GLfloat light_position[]={200.0,200.0,200.0}; 19 GLfloat white_light[]={1.0,0.0,1.0,0.8}; 20 GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0}; 21 22 glClearColor(0.0,0.0,0.0,0.0); 23 glShadeModel(GL_SMOOTH); 24 25 glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); 26 glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess); 27 glLightfv(GL_LIGHT0,GL_POSITION,light_position); 28 glLightfv(GL_LIGHT0,GL_SPECULAR,white_light); 29 glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light); 30 31 glEnable(GL_LIGHTING); 32 glEnable(GL_LIGHT0); 33 glEnable(GL_DEPTH_TEST); 34 } 35 36 void display(void) 37 { 38 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 39 glLoadIdentity(); 40 //gluLookAt(20.0,20.0,10.0,0.0,0.0,0.0,0.0,1.0,0.0); 41 glRotatef(spin,1.0f,0.0f,0.0f); 42 //glutSolidSphere(30.0,20,16); 43 glutSolidTeapot(50.0); 44 45 //glFlush(); 46 glutSwapBuffers(); 47 } 48 49 void spinDisplay(void) 50 { 51 spin=spin+2.0; 52 if(spin>360) 53 spin=spin-360.0; 54 glutPostRedisplay(); 55 } 56 57 void reshape(int w,int h) 58 { 59 glViewport(0,0,(GLsizei)w,(GLsizei)h); 60 glMatrixMode(GL_PROJECTION); 61 glLoadIdentity(); 62 63 if(w<=h) 64 glOrtho(-200,200,-200*(GLfloat)h/(GLfloat)w,200*(GLfloat)h/(GLfloat)w,-100.0,100.0); 65 else 66 glOrtho(-200*(GLfloat)w/(GLfloat)h,200*(GLfloat)w/(GLfloat)h,-200,200,-100.0,100.0); 67 glMatrixMode(GL_MODELVIEW); 68 glLoadIdentity(); 69 } 70 71 void mouse(int button,int state,int x,int y) 72 { 73 switch(button) 74 { 75 case GLUT_LEFT_BUTTON: 76 if(state == GLUT_DOWN) 77 glutIdleFunc(spinDisplay); 78 break; 79 case GLUT_MIDDLE_BUTTON: 80 if(state == GLUT_DOWN) 81 glutIdleFunc(NULL); 82 break; 83 default: 84 controlValue+=10.0; 85 glutPostRedisplay(); 86 break; 87 } 88 } 89 90 int main(int argc,char ** argv) 91 { 92 glutInit(&argc,argv); 93 glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); 94 glutInitWindowSize(400,400); 95 glutInitWindowPosition(100,100); 96 glutCreateWindow("OpenGL test"); 97 init(); 98 99 glutDisplayFunc(display); 100 glutReshapeFunc(reshape); 101 glutMouseFunc(mouse); 102 103 glutMainLoop(); 104 return 0; 105 }
效果图:
相关文章推荐
- 第四弹 3D物体无光照绘制
- 第三弹 不同方向旋转的三角形和矩形
- 第二弹 给绘制的物体设置颜色
- Java与c/c++运行时的不同
- windows修改hosts文件
- 第一弹 创建窗体,并绘制一个三角形一个矩形
- Poco之ftp获取文件列表以及下载文件
- poco异步等待ActiveResult
- Poco版信号槽
- C++指针例
- QTcpsocket 实现FTP
- iOS9 通讯录(无界面的和有界面的)
- networkRequest
- Poco之ftp目录切换与创建
- Poco库之XML操作
- Qt之json解析
- [ACM]模拟大数加法
- github避免每次输入账户密码
- coder
- Android的按钮单击事件及监听器的实现方式