Created
August 10, 2019 05:39
-
-
Save PirunSeng/91740a3f20c5404ebd58d9adc0a22cd1 to your computer and use it in GitHub Desktop.
OpenGL Lighting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<windows.h> | |
#include <GL/glut.h> | |
#include <GL/glu.h> | |
#include <GL/gl.h> | |
#include <stdio.h> | |
#include <math.h> | |
#include <stdlib.h> | |
void drawGoldSphere() { | |
GLfloat mat_specular[] = { 0.628281, 0.555802, 0.366065, 1.0 }; | |
GLfloat mat_shininess[] = { 51.2 }; | |
GLfloat mat_ambient[] = { 0.24725, 0.1995, 0.0745, 1.0 }; | |
GLfloat mat_diffuse[] = { 0.75164, 0.60648, 0.22648, 1.0 }; | |
//Material Properties | |
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); | |
} | |
void drawGraySphere() { | |
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; | |
GLfloat mat_shininess[] = { 50.0 }; | |
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); | |
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); | |
} | |
void init(void) | |
{ | |
// light from middle left | |
//GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 }; | |
// light from top right | |
GLfloat light_position_top_right[] = { 1.0, 1.0, 1.0, 0.0 }; | |
// light from bottom left | |
GLfloat light_position_bottom_left[] = { -1.0, -1.0, 1.0, 0.0 }; | |
// light from bottom left | |
//GLfloat light_position[] = { -0.1, -0.1, -0.1, 0.0 }; | |
// light from middle top | |
//GLfloat light_position[] = { 0.0, 1.0, 0.0, 0.0 }; | |
// light from middle right | |
//GLfloat light_position[] = { 1.0, 0.0, 0.0, 0.0 }; | |
// light from middle middle | |
//GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 }; | |
// light from middle bottom | |
//GLfloat light_position[] = { 0.0, -1.0, 1.0, 0.0 }; | |
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 }; | |
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 }; | |
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; | |
GLfloat high_shininess[] = { 100.0 }; | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glLightfv(GL_LIGHT0, GL_POSITION, light_position_top_right); | |
glLightfv(GL_LIGHT1, GL_POSITION, light_position_bottom_left); | |
glEnable(GL_LIGHTING); | |
glEnable(GL_LIGHT0); | |
glEnable(GL_LIGHT1); | |
glEnable(GL_DEPTH_TEST); | |
} | |
void display(void) | |
{ | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glPushMatrix(); | |
glTranslatef(1.0, 0.0, -1.0); | |
drawGraySphere(); | |
glutSolidSphere(0.5, 50, 50); | |
glPopMatrix(); | |
glPushMatrix(); | |
glTranslatef(-0.75, -0.8, 0.0); | |
drawGoldSphere(); | |
glutSolidSphere(0.5, 50, 50); | |
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(); | |
} | |
int main(int argc, char** argv) | |
{ | |
glutInit(&argc, argv); | |
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); | |
glutInitWindowSize(500, 500); | |
glutInitWindowPosition(100, 100); | |
glutCreateWindow(argv[0]); | |
init(); | |
glutDisplayFunc(display); | |
glutReshapeFunc(reshape); | |
glutMainLoop(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Expected light position