#include "windows.h" #include "GL/glut.h" #include "stdio.h" #include "math.h" #define G_PI 3.14159265358979323846f void prepare_lighting(); void display(); void keyboard(unsigned char key, int x, int y); float theta, phi; int main() { theta = G_PI/2; phi = -G_PI/2; glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB ); glutInitWindowSize( 640, 640 ); glutCreateWindow( "hihi" ); glutDisplayFunc(display); glutKeyboardFunc( keyboard ); prepare_lighting(); glutMainLoop(); return 0; } void keyboard(unsigned char key, int x, int y) { switch( key ) { case 'w': theta -= .05; prepare_lighting(); glutPostRedisplay(); break; case 's': theta += .05; prepare_lighting(); glutPostRedisplay(); break; case 'a': phi -= .05; prepare_lighting(); glutPostRedisplay(); break; case 'd': phi += .05; prepare_lighting(); glutPostRedisplay(); break; }; } void display() { glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective( 20, 1, 0.1, 10 ); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt( 0,0,1, 0,0,0, 0,1,0 ); glEnable( GL_LIGHTING ); glEnable( GL_DEPTH_TEST ); glutSolidTeapot( .1 ); glutSwapBuffers(); } void prepare_lighting() { const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat light_position[4] = { sinf(theta) * cosf(phi), cosf(theta), -sinf(theta) * sinf(phi), 0 }; const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f }; const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f }; const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat high_shininess[] = { 100.0f }; theta = fmodf( theta, 2*G_PI ); phi = fmodf( phi, 2*G_PI ); glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); // glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glLightfv(GL_LIGHT0, GL_POSITION, light_position); 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, high_shininess); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glEnable( GL_LIGHT0 ); }
2012年7月17日 星期二
GLUT Light
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言