LIBS += -lGL -lGLU -lglut -lGLEW
1 #include <GL/glut.h> 2 #include <iostream> 3 using namespace std; 4 GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; 5 6 GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 雾气的模式 7 8 GLuint fogfilter= 0; // 使用哪一种雾气 9 10 GLfloat fogColor[4]= {0.5f, 0.5f, 0.5f, 1.0f}; // 雾的颜色设为白色 11 void init(void) 12 { 13 14 glClearColor(0.5f,0.5f,0.5f,1.0f); 15 16 GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; 17 GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 }; 18 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 19 GLfloat mat_shininess[] = { 60.0 }; 20 21 GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0}; 22 // GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; 23 24 GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; 25 GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 }; 26 GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 }; 27 GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; 28 29 GLfloat spot_direction[]={ 1.0,1.0,-1.0}; 30 31 GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 }; 32 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); 33 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); 34 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 35 glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess); 36 // glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); 37 38 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); 39 glLightfv(GL_LIGHT0, GL_POSITION,light0_position); 40 41 glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); 42 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); 43 glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular); 44 glLightfv(GL_LIGHT1, GL_POSITION,light1_position); 45 46 glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0); 47 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); 48 49 glEnable(GL_LIGHTING); 50 glEnable(GL_LIGHT0); 51 glEnable(GL_LIGHT1); 52 glDepthFunc(GL_LESS); 53 glEnable(GL_DEPTH_TEST); 54 glFogi(GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 55 56 glFogfv(GL_FOG_COLOR, fogColor); // 设置雾的颜色 57 58 glFogf(GL_FOG_DENSITY, 0.55f); // 设置雾的密度 59 60 glHint(GL_FOG_HINT, GL_NICEST); // 设置系统如何计算雾气 61 62 glFogf(GL_FOG_START, -10.0f); // 雾气的开始位置 63 64 glFogf(GL_FOG_END, 10.0f); // 雾气的结束位置 65 66 glEnable(GL_FOG); // 使用雾气 67 } 68 void display(void) 69 { 70 GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; 71 GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 }; 72 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; 73 GLfloat mat_shininess[] = { 60.0 }; 74 75 GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0}; 76 // GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; 77 78 GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; 79 GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 }; 80 GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 }; 81 GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; 82 83 GLfloat spot_direction[]={ 1.0,1.0,-1.0}; 84 85 GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 }; 86 glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); 87 glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); 88 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 89 glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess); 90 // glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); 91 92 glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); 93 glLightfv(GL_LIGHT0, GL_POSITION,light0_position); 94 95 glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); 96 glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); 97 glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular); 98 glLightfv(GL_LIGHT1, GL_POSITION,light1_position); 99 100 glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0); 101 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); 102 103 glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); 104 105 glEnable(GL_LIGHTING); 106 glEnable(GL_LIGHT0); 107 glEnable(GL_LIGHT1); 108 glDepthFunc(GL_LESS); 109 glEnable(GL_DEPTH_TEST); 110 111 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 112 glPushMatrix(); 113 glTranslatef(0.5,0,0); 114 glutSolidSphere(1.0,20,16); 115 glPopMatrix(); 116 glFlush(); 117 } 118 void reshape(int w,int h) 119 { 120 glViewport(0,0,(GLsizei)w,(GLsizei)h); 121 glMatrixMode(GL_PROJECTION); 122 glLoadIdentity(); 123 if(w<=h) 124 glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0); 125 else 126 glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0); 127 glMatrixMode(GL_MODELVIEW); 128 glLoadIdentity(); 129 } 130 void keyAction(unsigned char key,int x,int y) 131 { 132 switch(key) { 133 case 'q' : 134 light0_position[0]=light0_position[0]+1; 135 break; 136 case 'w' : 137 light0_position[1]=light0_position[1]+1; 138 break; 139 case 'e' : 140 light0_position[2]=light0_position[2]+1; 141 break; 142 case 'a' : 143 light0_position[0]=light0_position[0]-1; 144 break; 145 case 's' : 146 light0_position[1]=light0_position[1]-1; 147 break; 148 case 'd' : 149 light0_position[2]=light0_position[2]-1; 150 break; 151 152 case 't': 153 fogfilter++; 154 fogfilter=fogfilter%3; 155 glFogi (GL_FOG_MODE, fogMode[fogfilter]); 156 break; 157 } 158 glutPostRedisplay(); 159 } 160 int main(int argc,char **argv) 161 { 162 glutInit(&argc,argv); 163 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); 164 glutInitWindowSize(500,500); 165 glutInitWindowPosition(100,100); 166 glutCreateWindow("hello,GL"); 167 init(); 168 glutDisplayFunc(display); 169 glutReshapeFunc(reshape); 170 glutKeyboardFunc(keyAction); 171 glutMainLoop(); 172 return 0; 173 }
#include <GL/glut.h> #include <iostream> using namespace std; GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLuint fogMode[]= { GL_EXP, GL_EXP2, GL_LINEAR }; // 雾气的模式 GLuint fogfilter= 0; // 使用哪一种雾气 GLfloat fogColor[4]= {0.5f, 0.5f, 0.5f, 1.0f}; // 雾的颜色设为白色 void init(void) { glClearColor(0.5f,0.5f,0.5f,1.0f); GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0}; // GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 }; GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess); // glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular); glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glFogi(GL_FOG_MODE, fogMode[fogfilter]); // 设置雾气的模式 glFogfv(GL_FOG_COLOR, fogColor); // 设置雾的颜色 glFogf(GL_FOG_DENSITY, 0.55f); // 设置雾的密度 glHint(GL_FOG_HINT, GL_NICEST); // 设置系统如何计算雾气 glFogf(GL_FOG_START, -10.0f); // 雾气的开始位置 glFogf(GL_FOG_END, 10.0f); // 雾气的结束位置 glEnable(GL_FOG); // 使用雾气 } void display(void) { GLfloat mat_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; GLfloat mat_diffuse[]= { 0.8, 0.8, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat mat_shininess[] = { 60.0 }; GLfloat light0_diffuse[]= { 0.0, 0.0, 1.0, 1.0}; // GLfloat light0_position[] = { 1.0, 1.0, 1.0, 0.0 }; GLfloat light1_ambient[]= { 0.2, 0.2, 0.2, 1.0 }; GLfloat light1_diffuse[]= { 1.0, 0.0, 0.0, 1.0 }; GLfloat light1_specular[] = { 1.0, 0.6, 0.6, 1.0 }; GLfloat light1_position[] = { -3.0, -3.0, 3.0, 1.0 }; GLfloat spot_direction[]={ 1.0,1.0,-1.0}; GLfloat emitioncolor[] = { 0.0, 1.0, 0.0, 1.0 }; glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); glMaterialfv(GL_FRONT, GL_SHININESS,mat_shininess); // glMaterialfv(GL_FRONT, GL_EMISSION, emitioncolor); glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse); glLightfv(GL_LIGHT0, GL_POSITION,light0_position); glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient); glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse); glLightfv(GL_LIGHT1, GL_SPECULAR,light1_specular); glLightfv(GL_LIGHT1, GL_POSITION,light1_position); glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0); glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION,spot_direction); glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,GL_FALSE); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_LIGHT1); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.5,0,0); glutSolidSphere(1.0,20,16); glPopMatrix(); glFlush(); } void reshape(int w,int h) { glViewport(0,0,(GLsizei)w,(GLsizei)h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<=h) glOrtho(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0); else glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void keyAction(unsigned char key,int x,int y) { switch(key) { case 'q' : light0_position[0]=light0_position[0]+1; break; case 'w' : light0_position[1]=light0_position[1]+1; break; case 'e' : light0_position[2]=light0_position[2]+1; break; case 'a' : light0_position[0]=light0_position[0]-1; break; case 's' : light0_position[1]=light0_position[1]-1; break; case 'd' : light0_position[2]=light0_position[2]-1; break; case 't': fogfilter++; fogfilter=fogfilter%3; glFogi (GL_FOG_MODE, fogMode[fogfilter]); break; } glutPostRedisplay(); } int main(int argc,char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(500,500); glutInitWindowPosition(100,100); glutCreateWindow("hello,GL"); init(); glutDisplayFunc(display); glutReshapeFunc(reshape); glutKeyboardFunc(keyAction); glutMainLoop(); return 0; }