您的位置:首页 > 其它

第五弹 带光照茶壶绘制

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 }


效果图:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: