Assignment 2
authorLuke Lau <luke_lau@icloud.com>
Mon, 8 Oct 2018 12:18:30 +0000 (13:18 +0100)
committerLuke Lau <luke_lau@icloud.com>
Mon, 8 Oct 2018 12:18:30 +0000 (13:18 +0100)
CMakeLists.txt
assignment1.cpp
vertex.glsl

index 10ab5450c33396706b5bc3292a03e435455f0faa..d228fa0e5a534376fda5e5937254ffd4209e733f 100644 (file)
@@ -22,3 +22,8 @@ endif(NOT GLEW_FOUND)
 include_directories(${GLEW_INCLUDE_DIR})
 target_link_libraries(assignment-1 ${GLEW_LIBRARIES})
 
 include_directories(${GLEW_INCLUDE_DIR})
 target_link_libraries(assignment-1 ${GLEW_LIBRARIES})
 
+find_package(GLM REQUIRED)
+if(NOT GLM_FOUND)
+       message(ERROR "GLM not found")
+endif(NOT GLM_FOUND)
+include_directories(${GLM_INCLUDE_DIR})
index 887ffb0d9a2444a8dacce433606dca42b2aeed75..4676882fe80ec1c061ead0589e7e547714bf3052 100644 (file)
@@ -1,7 +1,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <iostream>
 #include <fstream>
 #include <sstream>
 #include <fstream>
 #include <sstream>
+#include <array>
 #include <vector>
 #ifdef __APPLE__
 #include <GL/glew.h>
 #include <vector>
 #ifdef __APPLE__
 #include <GL/glew.h>
 #include <OpenGL/glew.h>
 #endif
 #include <GLUT/glut.h>
 #include <OpenGL/glew.h>
 #endif
 #include <GLUT/glut.h>
+#include <glm/glm.hpp>
+#include <glm/ext.hpp>
+#include <glm/gtc/type_ptr.hpp>
 
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
 
 using namespace std;
 
 GLuint* vaos;
 
 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
 
 using namespace std;
 
 GLuint* vaos;
-GLuint* progIds;
-
-bool hasDrawn = false;
+GLuint progId;
+glm::vec3 camPos   = glm::vec3(0.0f, 0.0f,  -5.0f);
+glm::vec3 camFront = glm::vec3(0.0f, 0.0f, 1.0f);
+glm::vec3 camUp    = glm::vec3(0.0f, 1.0f,  0.0f);
+float yaw = 1.57, pitch = 0;
+bool doScale, doRotate, doTranslate;
 
 void display() {
 
 void display() {
-       glClear(GL_COLOR_BUFFER_BIT);
-       for (int i = 0; i < 2; i++) {
-               glUseProgram(progIds[i]);
-               glBindVertexArray(vaos[i]);
-               glDrawArrays(GL_TRIANGLES, 0, 3);
+       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       glUseProgram(progId);
+       glBindVertexArray(vaos[0]);
+       for (int i = 0; i < 100; i++) {
+
+               GLuint projId = glGetUniformLocation(progId, "projection");
+               glm::mat4 proj = glm::perspective(glm::radians(45.f), 1.33f, 0.01f, 100.f);
+               glUniformMatrix4fv(projId, 1, GL_FALSE, glm::value_ptr(proj));
+
+               GLuint viewId = glGetUniformLocation(progId, "view");
+               glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp);
+               glUniformMatrix4fv(viewId, 1, GL_FALSE, glm::value_ptr(view));
+
+               GLuint modelId = glGetUniformLocation(progId, "model");
+               float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
+               glm::mat4 model = glm::mat4(1.f);
+
+               model = glm::translate(model, glm::vec3(sin(i * 30) * 2, cos(i * 30) * 2, i * 2));
+               
+               if (doRotate) {
+                       model = glm::rotate(model, d * glm::radians(30.f), glm::vec3(0.f, 1.f, 0.f));
+                       model = glm::rotate(model, d * glm::radians(20.f), glm::vec3(1.f, 0.f, 0.f));
                }
                }
-       glutSwapBuffers();
-       if (!hasDrawn) {
-               glutPostRedisplay();
-               hasDrawn = true;
+               
+               if (doScale)
+                       model = glm::scale(model, glm::vec3(1.f, 0.3f + 0.7f * (1 + sin(d * (i + 3))), 1.f));
+
+               if (doTranslate)
+                       model = glm::translate(model, glm::vec3(sin(d), cos(d), sin(d)));
+       
+
+               glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model));
+               
+               glDrawArrays(GL_TRIANGLES, 0, 12);
        }
        }
+       glutSwapBuffers();
 }
 
 void attachShader(GLuint progId, const char* filePath, GLenum type) {
 }
 
 void attachShader(GLuint progId, const char* filePath, GLenum type) {
@@ -84,12 +117,24 @@ GLuint compileShaders(char* vertexShader, char* fragmentShader) {
 GLuint setupBuffers(GLfloat* vertices, GLuint progId) {
 
        GLfloat colors[] = {
 GLuint setupBuffers(GLfloat* vertices, GLuint progId) {
 
        GLfloat colors[] = {
+               0, 1, 0, 1,
+               1, 0, 0, 1,
+               0, 0, 1, 1,
+
+               0, 1, 0, 1,
+               1, 0, 0, 1,
+               0, 0, 1, 1,
+
+               0, 1, 0, 1,
+               1, 0, 0, 1,
+               0, 0, 1, 1,
+
                0, 1, 0, 1,
                1, 0, 0, 1,
                0, 0, 1, 1
        };
 
                0, 1, 0, 1,
                1, 0, 0, 1,
                0, 0, 1, 1
        };
 
-       GLuint numVerts = 3;
+       GLuint numVerts = 12;
 
        GLuint vbo;
        glGenBuffers(1, &vbo);
 
        GLuint vbo;
        glGenBuffers(1, &vbo);
@@ -110,11 +155,10 @@ GLuint setupBuffers(GLfloat* vertices, GLuint progId) {
        glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors);
        
        glBindVertexArray(vao);
        glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors);
        
        glBindVertexArray(vao);
-
        glEnableVertexAttribArray(posId);
        glEnableVertexAttribArray(posId);
-       glEnableVertexAttribArray(colorId);
-       
        glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0);
        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)));
 
        return vao;
        glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat)));
 
        return vao;
@@ -134,41 +178,95 @@ void validateProgram(GLuint progId) {
 }
 
 void init() {
 }
 
 void init() {
-       GLfloat vertices[2][9] = {
-               {
-                       0.0f, -1.0f, 0.0f,
-                       1.0f, -1.0f, 0.0f,
-                       0.5f, 1.0f, 0.0f
-               },
-
-               {
-                       -1.0f, -1.0f, 0.0f,
-                       0.0f, -1.0f, 0.0f,
-                       -0.5f, 1.0f, 0.0f
-               }
+       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
+
        };
 
        vaos = new GLuint[2];
        };
 
        vaos = new GLuint[2];
-       progIds = new GLuint[2];
 
 
-       GLuint progId1 = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl");
-       vaos[0] = setupBuffers(vertices[0], progId1);
-       progIds[0] = progId1;
-       validateProgram(progId1);
+       progId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl");
+       glUseProgram(progId);
+       vaos[0] = setupBuffers(vertices, progId);
+       validateProgram(progId);
 
 
-       GLuint progId2 = compileShaders((char*)"vertex.glsl", (char*)"yellow.glsl");
-       vaos[1] = setupBuffers(vertices[1], progId2);
-       progIds[1] = progId2;
-       validateProgram(progId2);
+       glEnable(GL_DEPTH_TEST); 
 }
 
 void keyboard(unsigned char key, int x, int y) {
 }
 
 void keyboard(unsigned char key, int x, int y) {
+       if (key == 'w')
+               camPos.z += 0.1f;
+       if (key == 's')
+               camPos.z -= 0.1f;
+       if (key == 'a')
+               camPos.x += 0.1f;
+       if (key == 'd')
+               camPos.x -= 0.1f;
+       if (key == 'q')
+               camPos.y += 0.1f;
+       if (key == 'e')
+               camPos.y -= 0.1f;
+       if (key == 'z')
+               doScale = !doScale;
+       if (key == 'x')
+               doRotate = !doRotate;
+       if (key == 'c')
+               doTranslate = !doTranslate;
+       glutPostRedisplay();
+}
+
+void idle(int _) {
        glutPostRedisplay();
        glutPostRedisplay();
+       glutTimerFunc(16, idle, 0);
+}
+
+int prevMouseX, prevMouseY;
+bool firstMouse = true;
+
+void motion(int x, int y) {
+       if (firstMouse) {
+               prevMouseX = x;
+               prevMouseY = y;
+               firstMouse = false;
+       }
+       int dx = x - prevMouseX, dy = y - prevMouseY;
+
+       prevMouseX = x;
+       prevMouseY = y;
+
+       const float sensitivity = 0.005f;
+       yaw += dx * sensitivity;
+       pitch -= dy * sensitivity;
+
+       glm::vec3 front;
+       front.x = cos(pitch) * cos(yaw);
+       front.y = sin(pitch);
+       front.z = cos(pitch) * sin(yaw);
+       camFront = glm::normalize(front);
+}
+
+void mouse(int button, int state, int x, int y) {
+       if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
+               firstMouse = true;
 }
 
 int main(int argc, char** argv) {
        glutInit(&argc, argv);
 }
 
 int main(int argc, char** argv) {
        glutInit(&argc, argv);
-       glutInitDisplayMode(GLUT_STENCIL|GLUT_SINGLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
+       glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
        glutInitWindowSize(800, 600);
        int win = glutCreateWindow("Hello Triangle");
        glutDisplayFunc(display);
        glutInitWindowSize(800, 600);
        int win = glutCreateWindow("Hello Triangle");
        glutDisplayFunc(display);
@@ -178,6 +276,9 @@ int main(int argc, char** argv) {
        init();
 
        glutKeyboardFunc(keyboard);
        init();
 
        glutKeyboardFunc(keyboard);
+       glutTimerFunc(16, idle, 0);
+       glutMotionFunc(motion);
+       glutMouseFunc(mouse);
 
        glutMainLoop();
 
 
        glutMainLoop();
 
index 6333f5488b26232a0aec08ba8219fd88723e3422..3bf0a4836bbe79ba552fb749ef5bbf12ba322266 100644 (file)
@@ -1,9 +1,12 @@
 #version 330
 in vec3 vPosition;
 in vec4 vColor;
 #version 330
 in vec3 vPosition;
 in vec4 vColor;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
 out vec4 color;
 
 void main() {
 out vec4 color;
 
 void main() {
-       gl_Position = vec4(vPosition, 1.0);
+       gl_Position = projection * view * model * vec4(vPosition, 1.f);
        color = vColor;
 }
        color = vColor;
 }