Add Phong lighting
[opengl.git] / main.cpp
index 503f469bba97524bf92b10a64164a8d4bdbed39d..ef18bf2f72de2475d2f8fda9270445bc1f146524 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -31,7 +31,8 @@ void display() {
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        glUseProgram(progId);
        glBindVertexArray(vaos[0]);
-       for (int i = 0; i < 100; i++) {
+
+       float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
 
        GLuint projId = glGetUniformLocation(progId, "projection");
        glm::mat4 proj = glm::perspective(glm::radians(45.f), 1.33f, 0.01f, 10000.f);
@@ -41,11 +42,20 @@ void display() {
        glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp);
        glUniformMatrix4fv(viewId, 1, GL_FALSE, glm::value_ptr(view));
 
+       GLuint lightPosLoc = glGetUniformLocation(progId, "lightPos");
+       glm::vec3 lightPos = glm::vec3(sin(d) * 10, 0, 0);//10 * cos(d));
+       glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
+       
+       GLuint viewPosLoc = glGetUniformLocation(progId, "viewPos");
+       glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
+
        GLuint modelId = glGetUniformLocation(progId, "model");
-               float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
+
+       for (int i = 0; i < 10; i++) {
+
                glm::mat4 model = glm::mat4(1.f);
 
-               model = glm::translate(model, glm::vec3(sin(i * 30) * 10, cos(i * 30) * 10, i * 2));
+               model = glm::translate(model, glm::vec3(sin(i * 30) * 10, 0, i * 2 - 10));
                
                if (doRotate) {
                        model = glm::rotate(model, d * glm::radians(30.f), glm::vec3(0.f, 1.f, 0.f));
@@ -114,7 +124,7 @@ GLuint compileShaders(char* vertexShader, char* fragmentShader) {
 
 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
 
-GLuint setupBuffers(GLfloat* vertices, GLuint progId) {
+GLuint setupBuffers(glm::vec3* vertices, glm::vec3* normals, GLuint progId) {
 
        GLfloat colors[] = {
                0, 1, 0, 1,
@@ -144,22 +154,29 @@ GLuint setupBuffers(GLfloat* vertices, GLuint progId) {
 
        GLuint posId = glGetAttribLocation(progId, "vPosition");
        GLuint colorId = glGetAttribLocation(progId, "vColor");
+       GLuint normalLoc = glGetAttribLocation(progId, "vNormal");
 
        GLuint vertsLen = numVerts * 3 * sizeof(GLfloat);
        GLuint colorsLen = numVerts * 4 * sizeof(GLfloat);
+       GLuint normalLen = numVerts * 3 * sizeof(GLfloat);
 
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
-       glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW);
+       glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen + normalLen, NULL, GL_STATIC_DRAW);
 
-       glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices);
+       glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, glm::value_ptr(vertices[0]));
        glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors);
+       glBufferSubData(GL_ARRAY_BUFFER, vertsLen + colorsLen, normalLen, glm::value_ptr(normals[0]));
        
        glBindVertexArray(vao);
+
        glEnableVertexAttribArray(posId);
        glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0);
 
        glEnableVertexAttribArray(colorId);
-       glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat)));
+       glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertsLen));
+
+       glEnableVertexAttribArray(normalLoc);
+       glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(vertsLen + colorsLen));
 
        return vao;
 }
@@ -178,33 +195,47 @@ void validateProgram(GLuint progId) {
 }
 
 void init() {
-       GLfloat vertices[9*4] = {
-               -1.0f, -1.0f, -1.0f,
-               1.0f, -1.0f, -1.0f,
-               0.0f, 1.0f, 0.0f,
-
-               -1.0f, -1.0f, 1.0f,
-               1.0f, -1.0f, 1.0f,
-               0.0f, 1.0f, 0.0f,
-
-               -1.0f, -1.0f, -1.0f,
-               -1.0f, -1.0f, 1.0f,
-               0.0f, 1.0f, 0.0f,
-
-               1.0f, -1.0f, -1.0f,
-               1.0f, -1.0f, 1.0f,
-               0.0f, 1.0f, 0.0f
-
+       glm::vec3 vertices[12] = {
+               glm::vec3(0.0f, 1.0f, 0.0f),
+               glm::vec3(1.0f, -1.0f, -1.0f),
+               glm::vec3(-1.0f, -1.0f, -1.0f),
+
+               glm::vec3(0.0f, 1.0f, 0.0f),
+               glm::vec3(-1.0f, -1.0f, 1.0f),
+               glm::vec3(1.0f, -1.0f, 1.0f),
+
+               glm::vec3(0.0f, 1.0f, 0.0f),
+               glm::vec3(-1.0f, -1.0f, -1.0f),
+               glm::vec3(-1.0f, -1.0f, 1.0f),
+
+               glm::vec3(0.0f, 1.0f, 0.0f),
+               glm::vec3(1.0f, -1.0f, 1.0f),
+               glm::vec3(1.0f, -1.0f, -1.0f)
        };
 
+       // work out the normals
+       glm::vec3 normals[12];
+       for (int i = 0; i < 4; i++) {
+               glm::vec3 a = vertices[i * 3];
+               glm::vec3 b = vertices[i * 3 + 1];
+               glm::vec3 c = vertices[i * 3 + 2];
+               glm::vec3 u = glm::normalize(a - c);
+               glm::vec3 v = glm::normalize(b - c);
+               glm::vec3 norm = glm::normalize(glm::cross(v, u));
+               for(int j = 0; j < 3; j++) {
+                       normals[i * 3 + j] = glm::vec3(norm);
+               }
+       }
+
        vaos = new GLuint[2];
 
        progId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl");
        glUseProgram(progId);
-       vaos[0] = setupBuffers(vertices, progId);
+       vaos[0] = setupBuffers(vertices, normals, progId);
        validateProgram(progId);
 
        glEnable(GL_DEPTH_TEST); 
+       glEnable(GL_CULL_FACE); 
 }
 
 bool* keyStates = new bool[256];
@@ -223,7 +254,7 @@ void keyboardUp(unsigned char key, int x, int y) {
        keyStates[key] = false;
 }
 
-void idle(int _) {
+void timer(int _) {
        float xSpeed = 0.f, ySpeed = 0.f, zSpeed = 0.f;
        if (keyStates['w'])
                zSpeed = 0.1f;
@@ -242,7 +273,7 @@ void idle(int _) {
        camPos.y += ySpeed;
        camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw);
        glutPostRedisplay();
-       glutTimerFunc(16, idle, 0);
+       glutTimerFunc(16, timer, 0);
 }
 
 int prevMouseX, prevMouseY;
@@ -288,7 +319,7 @@ int main(int argc, char** argv) {
 
        glutKeyboardFunc(keyboard);
        glutKeyboardUpFunc(keyboardUp);
-       glutTimerFunc(16, idle, 0);
+       glutTimerFunc(16, timer, 0);
        glutMotionFunc(motion);
        glutMouseFunc(mouse);