Add solid shader and light cube thing
authorLuke Lau <luke_lau@icloud.com>
Fri, 12 Oct 2018 21:48:58 +0000 (22:48 +0100)
committerLuke Lau <luke_lau@icloud.com>
Fri, 12 Oct 2018 21:48:58 +0000 (22:48 +0100)
.gitignore
main.cpp
solidfrag.glsl [new file with mode: 0644]
solidvertex.glsl [new file with mode: 0644]

index 409f1a5..2e68889 100644 (file)
@@ -14,3 +14,4 @@ bin
 *.aux
 *.log
 _minted-report
+.vim
index ef18bf2..1df11ec 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 
 using namespace std;
 
-GLuint* vaos;
-GLuint progId;
+GLuint pyramidVao, lightVao;
+GLuint gradientProgId, solidProgId;
 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() {
-       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
-       glUseProgram(progId);
-       glBindVertexArray(vaos[0]);
-
-       float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
-
+void setProjectionAndViewUniforms(GLuint progId) {
        GLuint projId = glGetUniformLocation(progId, "projection");
        glm::mat4 proj = glm::perspective(glm::radians(45.f), 1.33f, 0.01f, 10000.f);
        glUniformMatrix4fv(projId, 1, GL_FALSE, glm::value_ptr(proj));
@@ -41,15 +35,36 @@ void display() {
        GLuint viewId = glGetUniformLocation(progId, "view");
        glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp);
        glUniformMatrix4fv(viewId, 1, GL_FALSE, glm::value_ptr(view));
+}
+
+void drawLight(float d, glm::vec3 lightPos) {
+       glUseProgram(solidProgId);
+       glBindVertexArray(lightVao);
+       setProjectionAndViewUniforms(solidProgId);
+       glm::mat4 model = glm::translate(glm::mat4(1.f), lightPos);
+       model = glm::scale(model, glm::vec3(0.2));
+       GLuint modelId = glGetUniformLocation(solidProgId, "model");
+       glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model));
 
-       GLuint lightPosLoc = glGetUniformLocation(progId, "lightPos");
-       glm::vec3 lightPos = glm::vec3(sin(d) * 10, 0, 0);//10 * cos(d));
+       GLuint colorLoc = glGetUniformLocation(solidProgId, "color");
+       glm::vec3 color = glm::vec3(1, 1, 1);
+       glUniform3fv(colorLoc, 1, glm::value_ptr(color));
+               
+       glDrawArrays(GL_TRIANGLES, 0, 36);
+};
+
+void drawPyramids(float d, glm::vec3 lightPos) {
+       glUseProgram(gradientProgId);
+       glBindVertexArray(pyramidVao);
+       setProjectionAndViewUniforms(gradientProgId);
+
+       GLuint lightPosLoc = glGetUniformLocation(gradientProgId, "lightPos");
        glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos));
        
-       GLuint viewPosLoc = glGetUniformLocation(progId, "viewPos");
+       GLuint viewPosLoc = glGetUniformLocation(gradientProgId, "viewPos");
        glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos));
 
-       GLuint modelId = glGetUniformLocation(progId, "model");
+       GLuint modelId = glGetUniformLocation(gradientProgId, "model");
 
        for (int i = 0; i < 10; i++) {
 
@@ -73,6 +88,18 @@ void display() {
                
                glDrawArrays(GL_TRIANGLES, 0, 12);
        }
+
+};
+
+void display() {
+       glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
+       float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f;
+
+       glm::vec3 lightPos = glm::vec3(sin(d) * 10, 0, cos(d) * 10);
+
+       drawPyramids(d, lightPos);
+       drawLight(d, lightPos);
+
        glutSwapBuffers();
 }
 
@@ -181,6 +208,91 @@ GLuint setupBuffers(glm::vec3* vertices, glm::vec3* normals, GLuint progId) {
        return vao;
 }
 
+vector<glm::vec3> quadToTriangles(glm::vec3 *quads) {
+       vector<glm::vec3> triangles(6);
+       triangles[0] = quads[0];
+       triangles[1] = quads[1];
+       triangles[2] = quads[2];
+       triangles[3] = quads[2];
+       triangles[4] = quads[3];
+       triangles[5] = quads[0];
+       return triangles;
+}
+
+template <typename T>
+void append(vector<T> &a, vector<T> &b) {
+       a.insert(a.end(), b.begin(), b.end());
+};
+
+void setupLightBuffers(GLuint progId) {
+       vector<glm::vec3> vertices;
+       glm::vec3 front[] = {
+               glm::vec3(1, -1, -1),
+               glm::vec3(-1, -1, -1),
+               glm::vec3(-1, 1, -1),
+               glm::vec3(1, 1, -1)
+       };
+       vector<glm::vec3> frontTriangles = quadToTriangles(front);
+       append(vertices, frontTriangles);
+
+       glm::vec3 back[] = {
+               glm::vec3(1, 1, 1),
+               glm::vec3(-1, 1, 1),
+               glm::vec3(-1, -1, 1),
+               glm::vec3(1, -1, 1)
+       };
+       vector<glm::vec3> backQuads = quadToTriangles(back);
+       append(vertices, backQuads);
+
+       glm::vec3 top[] = {
+               glm::vec3(1, 1, -1),
+               glm::vec3(-1, 1, -1),
+               glm::vec3(-1, 1, 1),
+               glm::vec3(1, 1, 1)
+       };
+       vector<glm::vec3> topTriangles = quadToTriangles(top);
+       append(vertices, topTriangles);
+
+       glm::vec3 bottom[] = {
+               glm::vec3(1, -1, 1),
+               glm::vec3(-1, -1, 1),
+               glm::vec3(-1, -1, -1),
+               glm::vec3(1, -1, -1)
+       };
+       vector<glm::vec3> bottomTriangles = quadToTriangles(bottom);
+       append(vertices, bottomTriangles);
+
+       glm::vec3 left[] = {
+               glm::vec3(-1, 1, 1),
+               glm::vec3(-1, 1, -1),
+               glm::vec3(-1, -1, -1),
+               glm::vec3(-1, -1, 1)
+       };
+       vector<glm::vec3> leftTriangles = quadToTriangles(left);
+       append(vertices, leftTriangles);
+
+       glm::vec3 right[] = {
+               glm::vec3(1, 1, -1),
+               glm::vec3(1, 1, 1),
+               glm::vec3(1, -1, 1),
+               glm::vec3(1, -1, -1)
+       };
+       vector<glm::vec3> rightTriangles = quadToTriangles(right);
+       append(vertices, rightTriangles);
+       GLuint verticesSize = 36 * 3 * sizeof(GLfloat);
+
+       glGenVertexArrays(1, &lightVao);
+       GLuint vbo;
+       glBindVertexArray(lightVao);
+       glGenBuffers(1, &vbo);
+       glBindBuffer(GL_ARRAY_BUFFER, vbo);
+       glBufferData(GL_ARRAY_BUFFER, verticesSize, NULL, GL_STATIC_DRAW);
+       glBufferSubData(GL_ARRAY_BUFFER, 0, verticesSize, glm::value_ptr(vertices[0]));
+       GLuint posLoc = glGetAttribLocation(progId, "vPosition");
+       glEnableVertexAttribArray(posLoc);
+       glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
+}
+
 void validateProgram(GLuint progId) {
        glValidateProgram(progId);
        
@@ -227,12 +339,15 @@ void init() {
                }
        }
 
-       vaos = new GLuint[2];
+       gradientProgId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl");
+       glUseProgram(gradientProgId);
+       pyramidVao = setupBuffers(vertices, normals, gradientProgId);
+       validateProgram(gradientProgId);
 
-       progId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl");
-       glUseProgram(progId);
-       vaos[0] = setupBuffers(vertices, normals, progId);
-       validateProgram(progId);
+       solidProgId = compileShaders((char*)"solidvertex.glsl", (char*)"solidfrag.glsl");
+       glUseProgram(solidProgId);
+       setupLightBuffers(solidProgId);
+       validateProgram(solidProgId);
 
        glEnable(GL_DEPTH_TEST); 
        glEnable(GL_CULL_FACE); 
diff --git a/solidfrag.glsl b/solidfrag.glsl
new file mode 100644 (file)
index 0000000..b729865
--- /dev/null
@@ -0,0 +1,6 @@
+#version 330
+uniform vec3 color;
+out vec4 FragColor;
+void main() {
+  FragColor = vec4(color, 1);
+}
diff --git a/solidvertex.glsl b/solidvertex.glsl
new file mode 100644 (file)
index 0000000..c3938de
--- /dev/null
@@ -0,0 +1,8 @@
+#version 330
+in vec3 vPosition;
+uniform mat4 model;
+uniform mat4 view;
+uniform mat4 projection;
+void main() {
+  gl_Position = projection * view * model * vec4(vPosition, 1);
+}