// For this tutorial, I am adding a new function called Lighting // and it looks like this: // void Lighting (void) { // GLfloat LightPosition[] = { 0.0, 0.0, 5.0, 1.0}; // GLfloat DiffuseLight[] = {1.0, 0.0, 0.0}; // GLfloat AmbientLight[] = {0.2, 0.2, 0.2}; // GLfloat SpecularLight[] = {1.0, 1.0, 1.0}; // glLightfv (GL_LIGHT0, GL_SPECULAR, SpecularLight); // glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight); // glLightfv (GL_LIGHT0, GL_AMBIENT, AmbientLight); // glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); // GLfloat mShininess[] = {8}; // GLfloat DiffuseMaterial[] = {1.0, 0.0, 0.0}; // GLfloat AmbientMaterial[] = {0.2, 0.2, 0.2}; // GLfloat SpecularMaterial[] = {1.0, 1.0, 1.0}; // glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, DiffuseMaterial); // glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, AmbientMaterial); // glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecularMaterial); // glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess); // } // If you have followed the lighting tutorials in the OpenGL // section, you would know what this all meant. So if you // have not yet checked it out, go to the OpenGL section // and scroll down to the Lighting Types and Material Lighting // tutorials. // This Lighting function, is being called from within our // display function, just after glLoadIdentity(); #include #include #include #include #include #pragma comment(lib,"glew32.lib") char *VertexShaderSource,*FragmentShaderSource; int VertexShader,FragmentShader; int ShaderProgram; GLfloat angle = 0.0; char *readShaderFile(char *FileName) { FILE *fp; char *DATA = NULL; int flength = 0; fp = fopen(FileName,"rt"); fseek(fp, 0, SEEK_END); flength = ftell(fp); rewind(fp); DATA = (char *)malloc(sizeof(char) * (flength+1)); flength = fread(DATA, sizeof(char), flength, fp); DATA[flength] = '\0'; fclose(fp); return DATA; } void Lighting (void) { GLfloat LightPosition[] = { 0.0, 0.0, 5.0, 1.0}; GLfloat DiffuseLight[] = {1.0, 0.0, 0.0}; GLfloat AmbientLight[] = {0.2, 0.2, 0.2}; GLfloat SpecularLight[] = {1.0, 1.0, 1.0}; glLightfv (GL_LIGHT0, GL_SPECULAR, SpecularLight); glLightfv (GL_LIGHT0, GL_DIFFUSE, DiffuseLight); glLightfv (GL_LIGHT0, GL_AMBIENT, AmbientLight); glLightfv(GL_LIGHT0, GL_POSITION, LightPosition); GLfloat mShininess[] = {8}; GLfloat DiffuseMaterial[] = {1.0, 0.0, 0.0}; GLfloat AmbientMaterial[] = {0.2, 0.2, 0.2}; GLfloat SpecularMaterial[] = {1.0, 1.0, 1.0}; glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, DiffuseMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, AmbientMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecularMaterial); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mShininess); } void display (void) { glClearColor (0.0,0.0,0.0,1.0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); Lighting(); glTranslatef(0,0,-5); glRotatef(angle,1,1,1); glRotatef(angle,0,1,1); glutSolidTeapot(2); glutSwapBuffers(); angle+=0.5; } void InitShader (void) { GLEW_ARB_vertex_shader; GLEW_ARB_fragment_shader; VertexShader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); VertexShaderSource = readShaderFile("light-advanced.vert"); FragmentShaderSource = readShaderFile("light-advanced.frag"); const char * VS = VertexShaderSource; const char * FS = FragmentShaderSource; glShaderSourceARB(VertexShader, 1, &VS,NULL); glShaderSourceARB(FragmentShader, 1, &FS,NULL); free(VertexShaderSource);free(FragmentShaderSource); glCompileShaderARB(VertexShader); glCompileShaderARB(FragmentShader); ShaderProgram = glCreateProgramObjectARB(); glAttachObjectARB(ShaderProgram,VertexShader); glAttachObjectARB(ShaderProgram,FragmentShader); glLinkProgramARB(ShaderProgram); glUseProgramObjectARB(ShaderProgram); } void DeInitShader (void) { glDetachObjectARB(ShaderProgram,VertexShader); glDetachObjectARB(ShaderProgram,FragmentShader); glDeleteObjectARB(ShaderProgram); } void Init (void) { glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable (GL_LIGHTING); glEnable (GL_LIGHT0); } void reshape (int w, int h) { glViewport (0, 0, (GLsizei)w, (GLsizei)h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0); glMatrixMode (GL_MODELVIEW); } int main (int argc, char **argv) { glutInit (&argc, argv); glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); glutInitWindowSize (500, 500); glutInitWindowPosition (100, 100); glutCreateWindow ("A basic OpenGL Window"); glewInit(); InitShader(); Init(); glutDisplayFunc (display); glutIdleFunc (display); glutReshapeFunc (reshape); glutMainLoop (); DeInitShader(); return 0; }