X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=main.cpp;h=742c8983840eff62fbb68652ba0365fe2da0c72b;hp=503f469bba97524bf92b10a64164a8d4bdbed39d;hb=85f8f7278f24fe73fef1a19174376b155319072f;hpb=5fa27af6222eab1f43e6ca01f1c37cbfa1d86fcd diff --git a/main.cpp b/main.cpp index 503f469..742c898 100644 --- a/main.cpp +++ b/main.cpp @@ -14,38 +14,77 @@ #include #include #include +#include "model.hpp" +#include "teapot.h" #pragma clang diagnostic ignored "-Wdeprecated-declarations" using namespace std; -GLuint* vaos; -GLuint progId; -glm::vec3 camPos = glm::vec3(0.0f, 0.0f, -5.0f); +GLuint pyramidVao, lightVao, teapotVao; +GLuint gradientProgId, plainProgId, normalProgId, solidProgId, textureProgId; +glm::vec3 camPos = glm::vec3(0.0f, 0.0f, 0.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; +Model *monkeyHead, *chest; +glm::vec3 lightPos(0); -void display() { - glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); - glUseProgram(progId); - glBindVertexArray(vaos[0]); - for (int i = 0; i < 100; i++) { +const int WIDTH = 800, HEIGHT = 600; +const float ASPECT = (float)WIDTH / (float)HEIGHT; - 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)); +void setProjectionAndViewUniforms(GLuint progId) { + GLuint projLoc = glGetUniformLocation(progId, "projection"); + glm::mat4 proj = glm::perspective(glm::radians(45.f), ASPECT, 0.01f, 10000.f); + glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); - GLuint viewId = glGetUniformLocation(progId, "view"); + GLuint viewLoc = glGetUniformLocation(progId, "view"); glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp); - glUniformMatrix4fv(viewId, 1, GL_FALSE, glm::value_ptr(view)); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); +} + +void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) { + GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor"); + glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor)); + + GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos"); + glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos)); + + GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos"); + glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos)); +} + +void drawLight(float d, glm::vec3 lightPos, glm::vec4 lightColor) { + glUseProgram(plainProgId); + glBindVertexArray(lightVao); + setProjectionAndViewUniforms(plainProgId); + glm::mat4 model = glm::translate(glm::mat4(1.f), lightPos); + model = glm::scale(model, glm::vec3(0.2)); + GLuint modelLoc = glGetUniformLocation(plainProgId, "model"); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + + GLuint colorLoc = glGetUniformLocation(plainProgId, "color"); + glm::vec4 color(lightColor); + glUniform4fv(colorLoc, 1, glm::value_ptr(color)); + + glDrawArrays(GL_TRIANGLES, 0, 36); +}; + +void drawPyramids(float d, glm::vec3 lightPos, glm::vec4 lightColor) { + glUseProgram(gradientProgId); + glBindVertexArray(pyramidVao); + setProjectionAndViewUniforms(gradientProgId); + + setLightColorAndPos(gradientProgId, lightPos, lightColor); + + GLuint modelId = glGetUniformLocation(gradientProgId, "model"); + + for (int i = 0; i < 10; i++) { - 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) * 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)); @@ -61,8 +100,90 @@ void display() { glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model)); - glDrawArrays(GL_TRIANGLES, 0, 12); + glDrawArrays(GL_TRIANGLES, 0, 18); } + +}; + +enum TeapotProjection { teapotOrtho, teapotCamera, teapotPerspStatic, teapotPerspAnimated }; + +void drawTeapot(TeapotProjection proj, bool rotate, float d, glm::vec3 lightPos, glm::vec4 lightColor) { + glUseProgram(normalProgId); + + GLuint projLoc = glGetUniformLocation(normalProgId, "projection"); + GLuint viewLoc = glGetUniformLocation(normalProgId, "view"); + switch (proj) { + case teapotCamera: + setProjectionAndViewUniforms(normalProgId); + break; + case teapotOrtho: + { + glm::mat4 proj = glm::ortho(-5.f * ASPECT, 5.f * ASPECT, -5.f, 5.f, 0.01f, 1000.f); + glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); + + glm::vec3 camPos(-5, 5, -5), camFront = glm::vec3(0) - camPos; + glm::mat4 view = glm::lookAt(camPos, camPos + camFront, camUp); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + } + break; + case teapotPerspStatic: + case teapotPerspAnimated: + { + float fov = glm::radians(45.f); + if (proj == teapotPerspAnimated) + fov += glm::radians(sin(d) * 30.f); + glm::mat4 proj = glm::perspective(fov, ASPECT, 0.01f, 10000.f); + glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); + + glm::mat4 view = glm::lookAt(glm::vec3(0, 0, -10), glm::vec3(0), camUp); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); + } + break; + } + setLightColorAndPos(normalProgId, lightPos, lightColor); + glBindVertexArray(teapotVao); + GLuint modelId = glGetUniformLocation(normalProgId, "model"); + glm::mat4 model(1); + model = glm::scale(model, glm::vec3(0.3)); + if (rotate) model = glm::rotate(model, d, glm::vec3(0, 1, 0)); + glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model)); + + glDrawArrays(GL_TRIANGLES, 0, teapot_vertex_count); +} + +void display() { + glClearColor(0.5, 0.5, 0.5, 1); + 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.f) * 10, 0, cos(d / 10.f) * 10); */ + glm::vec4 lightColor(1, 1, 1, 1); + + drawLight(d, lightPos, lightColor); + + glUseProgram(textureProgId); + setProjectionAndViewUniforms(textureProgId); + setLightColorAndPos(textureProgId, lightPos, lightColor); + + Model::Node *top = chest->find("top"); + top->model = glm::translate(glm::mat4(1), glm::vec3(0, 1, -1)); + top->model = glm::rotate(top->model, sin(d / 2.5f) * 0.5f, glm::vec3(1, 0, 0)); + top->model = glm::translate(top->model, glm::vec3(0, -1, 1)); + + /* Model::Node *jewels = chest->find("jewels"); */ + /* jewels->model = glm::scale(glm::mat4(1), glm::vec3((sin(d) + 1.2f) / 2.f)); */ + + /* Model::Node *lock = chest->find("lock"); */ + /* lock->model = glm::translate(glm::mat4(1), glm::vec3(0.22610, 3.36478, -0.75649)); */ + /* lock->model = glm::rotate(lock->model, (d / 2.5f), glm::vec3(0, 1, 0.4)); */ + /* lock->model = glm::translate(lock->model, -glm::vec3(0.22610, 3.36478, -0.75649)); */ + + /* Model::Node *key = chest->find("key"); */ + /* key->model = glm::translate(glm::mat4(1), glm::vec3(0, 0, sin(d))); */ + + /* chest->getRoot()->model = glm::translate(glm::mat4(1), lightPos); */ + chest->draw(); + glutSwapBuffers(); } @@ -114,7 +235,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, @@ -129,12 +250,20 @@ GLuint setupBuffers(GLfloat* vertices, GLuint progId) { 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 = 12; + GLuint numVerts = 18; GLuint vbo; glGenBuffers(1, &vbo); @@ -144,26 +273,141 @@ 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; } +vector quadToTriangles(glm::vec3 *quads) { + vector 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 +void append(vector &a, vector &b) { + a.insert(a.end(), b.begin(), b.end()); +}; + +void setupLightBuffers(GLuint progId) { + vector 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 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 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 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 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 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 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 setupTeapotBuffers(GLuint progId) { + GLuint vbos[2]; + glGenBuffers(2, vbos); + glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); + glBufferData(GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof(float), teapot_vertex_points, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); + glBufferData(GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof(float), teapot_normals, GL_STATIC_DRAW); + + glGenVertexArrays(1, &teapotVao); + glBindVertexArray(teapotVao); + + GLuint posLoc = glGetAttribLocation(progId, "vPosition"); + GLuint normalLoc = glGetAttribLocation(progId, "vNormal"); + + glEnableVertexAttribArray(posLoc); + glBindBuffer(GL_ARRAY_BUFFER, vbos[0]); + glVertexAttribPointer(posLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); + + glEnableVertexAttribArray(normalLoc); + glBindBuffer(GL_ARRAY_BUFFER, vbos[1]); + glVertexAttribPointer(normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); +} + void validateProgram(GLuint progId) { glValidateProgram(progId); @@ -178,33 +422,73 @@ 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, + glm::vec3 vertices[18] = { + 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), + + glm::vec3(1, -1, 1), + glm::vec3(-1, -1, 1), + glm::vec3(-1, -1, -1), + glm::vec3(-1, -1, -1), + glm::vec3(1, -1, -1), + glm::vec3(1, -1, 1) + }; + + // work out the normals + glm::vec3 normals[18]; + for (int i = 0; i < 6; 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); + } + } - -1.0f, -1.0f, 1.0f, - 1.0f, -1.0f, 1.0f, - 0.0f, 1.0f, 0.0f, + gradientProgId = compileShaders((char*)"vertex.glsl", (char*)"gradientfrag.glsl"); + glUseProgram(gradientProgId); + pyramidVao = setupBuffers(vertices, normals, gradientProgId); + validateProgram(gradientProgId); - -1.0f, -1.0f, -1.0f, - -1.0f, -1.0f, 1.0f, - 0.0f, 1.0f, 0.0f, + plainProgId = compileShaders((char*)"plainvertex.glsl", (char*)"plainfrag.glsl"); + glUseProgram(plainProgId); + setupLightBuffers(plainProgId); + validateProgram(plainProgId); - 1.0f, -1.0f, -1.0f, - 1.0f, -1.0f, 1.0f, - 0.0f, 1.0f, 0.0f + normalProgId = compileShaders((char*)"vertex.glsl", (char*)"normalfrag.glsl"); + glUseProgram(normalProgId); + setupTeapotBuffers(normalProgId); + validateProgram(normalProgId); - }; + solidProgId = compileShaders((char*)"vertex.glsl", (char*)"solidfrag.glsl"); + glUseProgram(solidProgId); + validateProgram(solidProgId); - vaos = new GLuint[2]; + textureProgId = compileShaders((char*)"texturevertex.glsl", (char*)"texturefrag.glsl"); + glUseProgram(textureProgId); + validateProgram(textureProgId); - progId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl"); - glUseProgram(progId); - vaos[0] = setupBuffers(vertices, progId); - validateProgram(progId); + monkeyHead = new Model("monkeyhead_smooth.dae", solidProgId); + chest = new Model("models/chest.dae", textureProgId); glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); } bool* keyStates = new bool[256]; @@ -223,7 +507,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; @@ -238,11 +522,18 @@ void idle(int _) { if (keyStates['e']) ySpeed = -0.1f; + if (keyStates['j']) lightPos.z += 0.1f; + if (keyStates['k']) lightPos.z -= 0.1f; + if (keyStates['h']) lightPos.x -= 0.1f; + if (keyStates['l']) lightPos.x += 0.1f; + if (keyStates['m']) lightPos.y -= 0.1f; + if (keyStates['n']) lightPos.y += 0.1f; + camPos.x += xSpeed * sin(yaw) + zSpeed * cos(yaw); camPos.y += ySpeed; camPos.z += zSpeed * sin(yaw) - xSpeed * cos(yaw); glutPostRedisplay(); - glutTimerFunc(16, idle, 0); + glutTimerFunc(16, timer, 0); } int prevMouseX, prevMouseY; @@ -268,6 +559,12 @@ void motion(int x, int y) { front.y = sin(pitch); front.z = cos(pitch) * sin(yaw); camFront = glm::normalize(front); + + if (pitch < -1.57079632679 || pitch >= 1.57079632679) { + camUp = glm::vec3(0, -1, 0); + } else { + camUp = glm::vec3(0, 1, 0); + } } void mouse(int button, int state, int x, int y) { @@ -278,7 +575,7 @@ void mouse(int button, int state, int x, int y) { int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DEPTH|GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE); - glutInitWindowSize(800, 600); + glutInitWindowSize(WIDTH, HEIGHT); int win = glutCreateWindow("Hello Triangle"); glutDisplayFunc(display); @@ -288,7 +585,7 @@ int main(int argc, char** argv) { glutKeyboardFunc(keyboard); glutKeyboardUpFunc(keyboardUp); - glutTimerFunc(16, idle, 0); + glutTimerFunc(16, timer, 0); glutMotionFunc(motion); glutMouseFunc(mouse);