X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=blobdiff_plain;f=main.cpp;h=4dd4e605963d7be0cc169e9fbeb92028baa2e30a;hp=99312d384ed00ea36ff83640f32d5621201704fd;hb=8abaf8f77191e1c660def0832d8036a8b4639ba8;hpb=150ba29f1ce70777762cb7fc44e00e52339bb66e diff --git a/main.cpp b/main.cpp index 99312d3..4dd4e60 100644 --- a/main.cpp +++ b/main.cpp @@ -1,8 +1,6 @@ #include #include #include -#include -#include #include #include #ifdef __APPLE__ @@ -11,200 +9,157 @@ #include #endif #include +#include "shapes.hpp" #include #include #include +#include "model.hpp" +#include "program.hpp" +#include "skybox.hpp" #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 lightVao; + +Program *textureProg, *plainProg, *reflectProg; +Skybox *skybox; + +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 *chest, *mirrorCube; +glm::vec3 lightPos(3); -void display() { - 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, 10000.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)); +const int WIDTH = 800, HEIGHT = 600; +const float ASPECT = (float)WIDTH / (float)HEIGHT; - GLuint modelId = glGetUniformLocation(progId, "model"); - glm::mat4 model = glm::mat4(1.f); - - float d = (float)glutGet(GLUT_ELAPSED_TIME) * 0.001f; - model = glm::translate(model, glm::vec3(sin(i * 30) * 10, cos(i * 30) * 10, 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)); +glm::mat4 projMat() { + return glm::perspective(glm::radians(45.f), ASPECT, 0.01f, 10000.f); } - if (doScale) - model = glm::scale(model, glm::vec3(1.f, 0.7f + 0.7f * (1 + sin(d + (i + 3))), 1.f)); - - if (doTranslate) - model = glm::translate(model, glm::vec3(sin(d + (i + 1)), cos(d + (i + -3)), sin(d + (i + 4)))); - - - glUniformMatrix4fv(modelId, 1, GL_FALSE, glm::value_ptr(model)); - - glDrawArrays(GL_TRIANGLES, 0, 12); +glm::mat4 viewMat() { + return glm::lookAt(camPos, camPos + camFront, camUp); } - glutSwapBuffers(); -} - -void attachShader(GLuint progId, const char* filePath, GLenum type) { - GLuint shader = glCreateShader(type); - if (!shader) { - fprintf(stderr, "error creating shader\n"); - exit(1); - } +void setProjectionAndViewUniforms(GLuint progId) { + GLuint projLoc = glGetUniformLocation(progId, "projection"); + glm::mat4 proj = projMat(); + glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(proj)); - ifstream file(filePath); - stringstream buffer; - buffer << file.rdbuf(); - string str = buffer.str(); - const char* contents = str.c_str(); - - glShaderSource(shader, 1, (const GLchar**)&contents, NULL); - glCompileShader(shader); - GLint success; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) { - GLchar log[1024]; - glGetShaderInfoLog(shader, 1024, NULL, log); - fprintf(stderr, "error: %s\n", log); - exit(1); - } - glAttachShader(progId, shader); + GLuint viewLoc = glGetUniformLocation(progId, "view"); + glm::mat4 view = viewMat(); + glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); } -GLuint compileShaders(char* vertexShader, char* fragmentShader) { - GLuint progId = glCreateProgram(); +void setLightColorAndPos(GLuint progId, glm::vec3 lightPos, glm::vec4 lightColor) { + GLuint lightColorLoc = glGetUniformLocation(progId, "lightColor"); + glUniform4fv(lightColorLoc, 1, glm::value_ptr(lightColor)); - attachShader(progId, vertexShader, GL_VERTEX_SHADER); - attachShader(progId, fragmentShader, GL_FRAGMENT_SHADER); + GLuint lightPosLoc = glGetUniformLocation(progId, "vLightPos"); + glUniform3fv(lightPosLoc, 1, glm::value_ptr(lightPos)); - glLinkProgram(progId); - GLint success = 0; - glGetProgramiv(progId, GL_LINK_STATUS, &success); - if (!success) { - GLchar log[1024]; - glGetProgramInfoLog(progId, sizeof(log), NULL, log); - fprintf(stderr, "error linking: %s\n", log); - exit(1); + GLuint viewPosLoc = glGetUniformLocation(progId, "vViewPos"); + glUniform3fv(viewPosLoc, 1, glm::value_ptr(camPos)); } - return progId; -} +void drawLight(float d, glm::vec3 lightPos, glm::vec4 lightColor) { + glUseProgram(plainProg->progId); + glBindVertexArray(lightVao); + setProjectionAndViewUniforms(plainProg->progId); + glm::mat4 model = glm::translate(glm::mat4(1.f), lightPos); + model = glm::scale(model, glm::vec3(0.2)); + GLuint modelLoc = glGetUniformLocation(plainProg->progId, "model"); + glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); -#define BUFFER_OFFSET(i) ((char *)NULL + (i)) + GLuint colorLoc = glGetUniformLocation(plainProg->progId, "color"); + glm::vec4 color(lightColor); + glUniform4fv(colorLoc, 1, glm::value_ptr(color)); -GLuint setupBuffers(GLfloat* vertices, GLuint progId) { - - GLfloat colors[] = { - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, + glDrawArrays(GL_TRIANGLES, 0, 36); +} - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1, +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; - 0, 1, 0, 1, - 1, 0, 0, 1, - 0, 0, 1, 1 - }; + glm::vec4 lightColor(1, 1, 1, 1); - GLuint numVerts = 12; + drawLight(d, lightPos, lightColor); - GLuint vbo; - glGenBuffers(1, &vbo); + glUseProgram(textureProg->progId); + setProjectionAndViewUniforms(textureProg->progId); + setLightColorAndPos(textureProg->progId, lightPos, lightColor); - GLuint vao; - glGenVertexArrays(1, &vao); + /* 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)); */ - GLuint posId = glGetAttribLocation(progId, "vPosition"); - GLuint colorId = glGetAttribLocation(progId, "vColor"); + /* Model::Node *jewels = chest->find("jewels"); */ + /* jewels->model = glm::scale(glm::mat4(1), glm::vec3((sin(d) + 1.2f) / 2.f)); */ - GLuint vertsLen = numVerts * 3 * sizeof(GLfloat); - GLuint colorsLen = numVerts * 4 * sizeof(GLfloat); + /* 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)); */ - glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, vertsLen + colorsLen, NULL, GL_STATIC_DRAW); + /* Model::Node *key = chest->find("key"); */ + /* key->model = glm::translate(glm::mat4(1), glm::vec3(0, 0, sin(d))); */ - glBufferSubData(GL_ARRAY_BUFFER, 0, vertsLen, vertices); - glBufferSubData(GL_ARRAY_BUFFER, vertsLen, colorsLen, colors); + /* chest->draw(); */ - glBindVertexArray(vao); - glEnableVertexAttribArray(posId); - glVertexAttribPointer(posId, 3, GL_FLOAT, GL_FALSE, 0, 0); + mirrorCube->draw(); - glEnableVertexAttribArray(colorId); - glVertexAttribPointer(colorId, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(numVerts * 3 * sizeof(GLfloat))); + skybox->draw(projMat(), viewMat()); - return vao; + glutSwapBuffers(); } -void validateProgram(GLuint progId) { - glValidateProgram(progId); +void setupLightBuffers(GLuint progId) { + auto vertices = cube(); + GLuint verticesSize = 36 * 3 * sizeof(GLfloat); - GLint success; - glGetProgramiv(progId, GL_VALIDATE_STATUS, &success); - if (!success) { - GLchar log[1024]; - glGetProgramInfoLog(progId, sizeof(log), NULL, log); - fprintf(stderr, "error: %s\n", log); - exit(1); - } + 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 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 - + plainProg = new Program("plainvertex.glsl", "plainfrag.glsl"); + glUseProgram(plainProg->progId); + setupLightBuffers(plainProg->progId); + plainProg->validate(); + + std::vector faces = { + "models/skybox/right.jpg", + "models/skybox/left.jpg", + "models/skybox/top.jpg", + "models/skybox/bottom.jpg", + "models/skybox/front.jpg", + "models/skybox/back.jpg" }; + skybox = new Skybox(faces); - vaos = new GLuint[2]; + textureProg = new Program("texturevertex.glsl", "texturefrag.glsl"); + /* chest = new Model("models/chest.dae", *textureProg, *skybox); */ - progId = compileShaders((char*)"vertex.glsl", (char*)"fragment.glsl"); - glUseProgram(progId); - vaos[0] = setupBuffers(vertices, progId); - validateProgram(progId); + mirrorCube = new Model("models/mirrorCube.dae", *textureProg, *skybox); glEnable(GL_DEPTH_TEST); + glEnable(GL_CULL_FACE); } bool* keyStates = new bool[256]; @@ -238,11 +193,18 @@ void timer(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 +230,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 +246,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);