opengl 光照
- 绘制带有光照效果的场景;
- 场景包含3个茶壶,通过键盘1,2,3可以分别选中三个茶壶,然后用鼠标对每个茶壶进行分别的旋转;
- 使用点光源,点光源为白色,以白色的球体表示;
- 使用3个不同的点光源
i. 光源1的坐标设置在世界坐标系中,并围绕着三个茶壶的中心进行圆周运动;
ii. 光源2 的坐标 设置在观察坐标系,不发生变化;
iii. 光源3的坐标设置在茶壶1的物体坐标系中,当茶壶1被选中进行旋转时,光源3要跟随茶壶1 做相同的旋转;
1 #include <windows.h> 2 #include <GL/glut.h> 3 #include <GL/glext.h> 4 #include <GL/SOIL.h> 5 #include <cstdio> 6 GLfloat r1 = 0.0f; 7 GLfloat r2 = 0.0f; 8 GLfloat r3 = 0.0f; 9 GLfloat rlight1 = 0.0f; 10 11 GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f}; 12 GLfloat low_shiniess[] = {5.0}; // 镜面反射 13 14 GLfloat material1[] = {1.0f,0.0f,0.0f,1.0f}; 15 GLfloat material2[] = {0.0f,1.0f,0.0f,1.0f}; 16 GLfloat material3[] = {0.0f,0.0f,1.0f,1.0f}; 17 18 19 GLfloat light_position1[] = {0.0f,0.0f,4.0f,1.0f}; 20 GLfloat light_diffuse1[] = {1.0f,1.0f,1.0f,1.0f}; //白光 21 22 GLfloat light_position2[] = {1.0f,-2.0f,1.0f,1.0f}; //红光 23 GLfloat light_diffuse2[] = {1.0f,0.0f,0.0f,1.0f}; 24 25 GLfloat light_position3[] = {0.0f,0.0f,2.0f,1.0f}; 26 GLfloat light_diffuse3[] = {0.0f,0.0f,1.0f,1.0f};// 蓝光 27 GLfloat light_specular3[] = {1.0f,1.0f,1.0f,1.0f}; 28 //漫射光 29 void init () 30 { 31 glShadeModel(GL_SMOOTH); 32 glClearColor(0.0,0.0,0.0,0.0); 33 glClearDepth(1.0); 34 glEnable(GL_DEPTH_TEST); 35 glDepthFunc(GL_LEQUAL); 36 glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); 37 glEnable(GL_LIGHTING); 38 //glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); 39 //glMaterialfv(GL_FRONT,GL_SHININESS,low_shiniess); 40 } 41 42 void reshape(int w,int h) 43 { 44 glViewport(0,0,(GLsizei)w,(GLsizei)h); 45 glMatrixMode(GL_PROJECTION); 46 glLoadIdentity(); 47 gluPerspective(45.0,(GLfloat)w/(GLfloat)h,0.1,100.0); 48 glMatrixMode(GL_MODELVIEW); 49 glLoadIdentity(); 50 gluLookAt(0.0f,0.0f,10.0f, 51 0.0f,0.0f,0.0f, 52 0.0f,1.0f,0.0f); 53 54 55 } 56 void display() 57 { 58 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 59 glPushMatrix(); 60 glRotated(rlight1,0.0f,1.0f,0.0f); 61 glLightfv(GL_LIGHT0,GL_POSITION,light_position1); 62 glLightfv(GL_LIGHT0,GL_DIFFUSE,light_diffuse1); //世界坐标系光源 63 glPopMatrix(); 64 65 glPushMatrix(); 66 glDisable(GL_LIGHTING); 67 glRotated(rlight1,0.0f,1.0f,0.0f); 68 glTranslatef(0.0f,0.0f,4.0f); 69 glColor3f(1.0f,1.0f,1.0f); //光源1 白光 的小球 70 glutSolidSphere(0.05,10,10); 71 glEnable(GL_LIGHTING); 72 glPopMatrix(); 73 74 glLightfv(GL_LIGHT1,GL_POSITION,light_position2); 75 glLightfv(GL_LIGHT1,GL_DIFFUSE,light_diffuse2); //观察坐标系光源 76 77 glPushMatrix(); 78 glDisable(GL_LIGHTING); 79 glTranslatef(1.0f,-2.0f,1.0f); //光源2 红光的小球 80 glColor3f(1.0f,0.0f,0.0f); 81 glutSolidSphere(0.05,10,10); 82 glEnable(GL_LIGHTING); 83 glPopMatrix(); 84 85 86 87 //teapot1 88 glPushMatrix(); 89 glTranslatef(0.0f,2.0f,0.0f); 90 glRotatef(r1,0.0f,1.0f,0.0f); 91 //glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,material1); 92 glLightfv(GL_LIGHT2,GL_POSITION,light_position3); 93 glLightfv(GL_LIGHT2,GL_DIFFUSE,light_diffuse3); 94 glLightfv(GL_LIGHT2,GL_SPECULAR,light_specular3); // 95 glLightf(GL_LIGHT2,GL_SPOT_CUTOFF,45.0f); 96 glPushMatrix(); 97 glDisable(GL_LIGHTING); 98 glTranslated(0.0f,0.0f,2.0f); 99 glRotatef(r1,0.0f,1.0f,0.0f); 100 glColor3f(0.0f,0.0f,1.0f); //蓝色光源的小球 101 glutSolidSphere(0.05,10,10); 102 glEnable(GL_LIGHTING); 103 glPopMatrix(); 104 glutSolidTeapot(1.0f); 105 glPopMatrix(); 106 107 108 109 //teapot2 110 glPushMatrix(); 111 glTranslatef(-2.5f,-1.0f,0.0f); 112 glRotatef(r2,1.0f,0.0f,0.0f); 113 //glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,material2); 114 //glColor3f(1.0f,0.0f,0.0f); 115 glutSolidTeapot(1.0f); 116 glPopMatrix(); 117 118 //teapot3 119 glPushMatrix(); 120 glTranslatef(2.5f,-1.0f,0.0f); 121 glRotatef(r3,0.0f,0.0f,1.0f); 122 //glMaterialfv(GL_FRONT,GL_AMBIENT_AND_DIFFUSE,material3); 123 //glColor3f(1.0f,1.0f,0.0f); 124 glutSolidTeapot(1.0f); 125 glPopMatrix(); 126 127 glFlush(); 128 } 129 void mouse_move1(int x,int y) 130 { 131 r1+=10.0f; 132 glutPostRedisplay(); 133 } 134 void mouse_move2(int x,int y) 135 { 136 r2+=10.0f; 137 glutPostRedisplay(); 138 } 139 void mouse_move3(int x,int y) 140 { 141 r3+=10.0f; 142 glutPostRedisplay(); 143 } 144 void rotate1() 145 { 146 rlight1+=1.0f; 147 if(rlight1>360.0) 148 rlight1 = rlight1-360; 149 glutPostRedisplay(); 150 } 151 void keyboard(unsigned char key,int x,int y) 152 { 153 switch(key) 154 { 155 case '1': 156 glutMotionFunc(mouse_move1); 157 break; 158 case '2': 159 glutMotionFunc(mouse_move2); 160 break; 161 case '3': 162 glutMotionFunc(mouse_move3); 163 break; 164 case 'r': 165 glutIdleFunc(rotate1); 166 break; 167 case 's': 168 glutIdleFunc(NULL); 169 break; 170 case 'a': // light1 171 glDisable(GL_LIGHT1); 172 glDisable(GL_LIGHT2); 173 glEnable(GL_LIGHT0); 174 glutPostRedisplay(); 175 break; 176 case 'b': //light2 177 glDisable(GL_LIGHT0); 178 glDisable(GL_LIGHT2); 179 glEnable(GL_LIGHT1); 180 glutPostRedisplay(); 181 break; 182 case 'c': //light2 183 glDisable(GL_LIGHT1); 184 glDisable(GL_LIGHT0); 185 glEnable(GL_LIGHT2); 186 glutPostRedisplay(); 187 break; 188 } 189 } 190 191 /*void mouse(int button,int state,int x,int y) 192 { 193 switch(button){ 194 case GLUT_LEFT_BUTTON: 195 if(state == GLUT_DOWN) 196 break; 197 case GLUT_RIGHT_BUTTON: 198 if(state == GLUT_DOWN) 199 break; 200 } 201 }*/ 202 int main(int argc,char **argv) 203 { 204 printf("keyboard 1,2,3 choose the teapot mousemove control\n"); 205 printf("keyboard r light1 rotated, s stop"); 206 glutInit(&argc,argv); 207 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 208 glutInitWindowSize(900,600); 209 glutInitWindowPosition(400,100); 210 glutCreateWindow("hello world"); 211 init(); 212 glutDisplayFunc(display); 213 glutReshapeFunc(reshape); 214 glutKeyboardFunc(keyboard); 215 //glutMouseFunc(mouse); 216 glutMainLoop(); 217 return 0; 218 }