// simpler.cpp : Defines the entry point for the console application. // // We can reuse the tester. If you do not want to use your implementation of // the transformations, do the following: //# define GLXFORM //Get matrices from GL and use gluProject to obtain screen space vertex positions #include #include #define NEAR 1 #define FAR 20 #define gllEnable glEnable /* #include "gllReplace.h" */ void displayCube() { // gllSetMatrix(); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); /* glDisable(GL_LIGHTING); */ glBegin(GL_QUADS); glNormal3f(-1, -1, -1); glVertex3f(0, 0, 0); glNormal3f(0, 1, 0); glVertex3f(0, 1, 0); glNormal3f(1, 1, 0); glVertex3f(1, 1, 0); glNormal3f(1, 0, 0); glVertex3f(1, 0, 0); glNormal3f(0, 0, 1); glVertex3f(0, 0, 1); glNormal3f(0, 1, 1); glVertex3f(0, 1, 1); glNormal3f(1, 1, 1); glVertex3f(1, 1, 1); glNormal3f(1, 0, 1); glVertex3f(1, 0, 1); glNormal3f(-1, -1, -1); glVertex3f(0, 0, 0); glNormal3f(0, 0, 1); glVertex3f(0, 0, 1); glNormal3f(0, 1, 1); glVertex3f(0, 1, 1); glNormal3f(0, 1, 0); glVertex3f(0, 1, 0); glNormal3f(1, 0, 0); glVertex3f(1, 0, 0); glNormal3f(1, 0, 1); glVertex3f(1, 0, 1); glNormal3f(1, 1, 1); glVertex3f(1, 1, 1); glNormal3f(1, 1, 0); glVertex3f(1, 1, 0); glNormal3f(-1, -1, -1); glVertex3f(0, 0, 0); glNormal3f(0, 0, 1); glVertex3f(0, 0, 1); glNormal3f(1, 0, 1); glVertex3f(1, 0, 1); glNormal3f(1, 0, 0); glVertex3f(1, 0, 0); glNormal3f(0, 1, 0); glVertex3f(0, 1, 0); glNormal3f(0, 1, 1); glVertex3f(0, 1, 1); glNormal3f(1, 1, 1); glVertex3f(1, 1, 1); glNormal3f(1, 1, 0); glVertex3f(1, 1, 0); glEnd(); /* glEnable(GL_LIGHTING); */ glFinish(); // Put in the back buffer glutSwapBuffers(); } void keybd(unsigned char ch, int x, int y) { if(ch == 'q' || ch == 'Q' || ch == '\033') exit(0); else if(ch == 'w' || ch == 'W') glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else if(ch == 's' || ch == 'S') glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glutPostRedisplay(); } void splkeybd(int ch, int x, int y) { float t = 0.1; printf("Ch is #%c#%d#\n", ch, (int)ch); if(ch == GLUT_KEY_UP) glTranslatef(0, t, 0); else if(ch == GLUT_KEY_DOWN) glTranslatef(0, -t, 0); else if(ch == GLUT_KEY_RIGHT) glTranslatef(t, 0, 0); else if(ch == GLUT_KEY_LEFT) glTranslatef(-t, 0, 0); glutPostRedisplay(); } void mouse(int button, int action, int x, int y) { float r = 10.0; float t = 0.5; float axis[] = {.5, .2, .2}; float l; int mode; mode = (glutGetModifiers() == GLUT_ACTIVE_SHIFT)? 1:2; l = (float) 1.0/sqrt(axis[0]*axis[0] + axis[1]*axis[1] + axis[2]*axis[2]); axis[0] *= l; axis[1] *= l; axis[2] *= l; printf("Mode = %d. Button = %d\n", mode, button); if(mode == 1) { if(button == GLUT_LEFT_BUTTON && action == GLUT_UP) glRotatef(r, axis[0], axis[1], axis[2]); else if(button == GLUT_MIDDLE_BUTTON && action == GLUT_UP) glScalef(1.2, 1.2, 1.2); else if(button == GLUT_RIGHT_BUTTON && action == GLUT_UP) glRotatef(-r, axis[0], axis[1], axis[2]); } else { if(button == GLUT_LEFT_BUTTON && action == GLUT_UP) glTranslatef(t, .1, .1); else if(button == GLUT_MIDDLE_BUTTON && action == GLUT_UP) { printf("Scale\n"); glScalef(1/1.2, 1/1.2, 1/1.2); } else if(button == GLUT_RIGHT_BUTTON && action == GLUT_UP) glTranslatef(-t, -.1, -.1); } if(action == GLUT_UP) glutPostRedisplay(); } void initLight() { GLfloat light_specular[] = {0.8, 0.8, 0.8, 1.0}; GLfloat light_diffuse[] = {0.8, 0.5, 0.5, 1.0}; GLfloat light_ambient[] = {0.2, 0.2, 0.2, 1.0}; GLfloat light_position[] = {.578, .578, .578, 0.0}; // Directional glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); glLightfv(GL_LIGHT0, GL_POSITION, light_position); glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); glDisable(GL_LIGHTING); gllEnable(GL_LIGHT0); gllEnable(GL_LIGHTING); glShadeModel(GL_SMOOTH); } void initMaterial() { GLfloat Ambient[] = { 0.2, 0.2, 0.2, 1.0 }; // (0.2,0.2,0.2,1) DEFAULT GLfloat Diffuse[] = { 0.8, 0.5, 0.6, 1.0 }; // (0.8,0.8,0.8,1) GLfloat Specular[] = { 0.4, 0.4, 0.4, 1 }; // (0,0,0,1) GLfloat SpecularExp[] = { 10 }; // (0) GLfloat Emission[] = { 0, 0, .2, 1 }; // (0,0,0,1) glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, SpecularExp); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission); glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE); glEnable(GL_COLOR_MATERIAL); } void initLighting() { initLight(); initMaterial(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); glutCreateWindow("Simple Glut"); glutKeyboardFunc(keybd); glutSpecialFunc(splkeybd); glutMouseFunc(mouse); glutDisplayFunc(displayCube); glMatrixMode(GL_PROJECTION); gluPerspective(45,1,NEAR,FAR); glMatrixMode(GL_MODELVIEW); gluLookAt(0,0,5,0,0,0,0,1,0); glEnable(GL_DEPTH_TEST); initLighting(); glutMainLoop(); return 0; }