From: Luke Lau Date: Tue, 7 Apr 2020 00:38:03 +0000 (+0100) Subject: WIP of modes X-Git-Url: https://git.lukelau.me/?p=opengl.git;a=commitdiff_plain;h=refs%2Fheads%2Fanim WIP of modes --- diff --git a/Makefile b/Makefile index fe9ca79..746d160 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ all: bin/main CXX_FLAGS := -g --std=c++17 -march=native -Wall -bin/main: model.o material.o image.o skybox.o program.o ik.o main.o util.o ui.o blendshapes.o +bin/main: model.o material.o image.o skybox.o program.o ik.o main.o util.o ui.o blendshapes.o mode/animationMode.o clang++ $(CXX_FLAGS) $^ \ -I/usr/local/include -L/usr/local/lib \ -lassimp \ diff --git a/main.cpp b/main.cpp index 82e0fff..09ef264 100644 --- a/main.cpp +++ b/main.cpp @@ -25,6 +25,7 @@ #include "ik.hpp" #include "blendshapes.hpp" #include "ui.hpp" +#include "mode/animationMode.hpp" #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -56,15 +57,6 @@ VertIdx curManipulator = {-1,-1}; BlendshapeModel bsModel; bool playBlendshapeAnim = false; -struct Light { - glm::mat4 trans; - glm::vec3 color; -}; - -std::vector lights; - -bool discoLights = false; - int windowWidth = 800, windowHeight = 600; float aspect() { return (float)windowWidth / (float)windowHeight; @@ -116,10 +108,6 @@ void drawPlainProg(Program *p, GLuint vao, glm::mat4 trans, glm::vec3 color) { glDrawArrays(GL_TRIANGLES, 0, 36); } -void drawLight(Light &light) { - drawPlainProg(plainProg, lightVao, light.trans, light.color); -} - int findNodeTrans(const struct aiNode *n, const struct aiString name, glm::mat4 *dest) { if (strcmp(n->mName.data, name.data) == 0) { *dest = aiMatrixToMat4(n->mTransformation); @@ -154,8 +142,8 @@ void keyboard(unsigned char key, int x, int y) { keyStates[key] = true; if (key == 'z') activeSkybox = (activeSkybox + 1) % skyboxes.size(); - if (key == 'c') - discoLights = !discoLights; + /* if (key == 'c') */ + /* discoLights = !discoLights; */ } void keyboardUp(unsigned char key, int x, int y) { @@ -164,70 +152,6 @@ void keyboardUp(unsigned char key, int x, int y) { int mouseX, mouseY; -struct Mode { - virtual void display(float d) = 0; - virtual void timer() = 0; - virtual void motion(int x, int y, int dx, int dy) = 0; - virtual void passiveMotion(int x, int y, int dx, int dy) = 0; - virtual void mouse(int button, int state, int x, int y) = 0; -}; - -struct AnimationMode : public Mode { - Model *sceneModel; - - AnimationMode(std::string modelPath) { - const aiScene *scene = importer.ReadFile( - modelPath, aiProcess_Triangulate | aiProcess_CalcTangentSpace | - aiProcess_GenNormals | aiProcess_FlipUVs); - if (!scene) { - std::cerr << importer.GetErrorString() << std::endl; - exit(1); - } - - if (scene->mNumCameras > 0) { - aiCamera *cam = scene->mCameras[0]; - glm::mat4 camTrans; - if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0) - abort(); // there must be a node with the same name as camera - - camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]}; - - glm::vec3 camLookAt = - glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z); - camFront = camLookAt - camPos; - - camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z); - - fov = cam->mHorizontalFOV; - // TODO: aspectRatio = cam->mAspect; - znear = cam->mClipPlaneNear; - zfar = cam->mClipPlaneFar; - } - - for (int i = 0; i < scene->mNumLights; i++) { - aiLight *light = scene->mLights[i]; - glm::mat4 trans; - findNodeTrans(scene->mRootNode, light->mName, &trans); - glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g, - light->mColorAmbient.b}; - Light l = {trans, col}; - lights.push_back(l); - } - - sceneModel = new Model(scene, *pbrProg); - } - - void display(float d) override { - sceneModel->draw(skyboxes[activeSkybox], d * 1000); - } - - void timer() override {}; - void motion(int x, int y, int dx, int dy) override {} - void passiveMotion(int x, int y, int dx, int dy) override {} - void mouse(int button, int state, int x, int y) override {} -}; - - // TODO: move these inside bool needToCalculateClosestVertex = false; bool needToInterpolateBlendshapes = false; @@ -411,26 +335,13 @@ void display() { glUseProgram(pbrProg->progId); setProjectionAndViewUniforms(pbrProg->progId); - size_t numLights = lights.size() + (discoLights ? 3 : 0); + std::vector lights = curMode->getLights(d); glm::vec3 lightPositions[numLights], lightColors[numLights]; for (int i = 0; i < lights.size(); i++) { lightPositions[i] = glm::vec3(lights[i].trans[3]); lightColors[i] = lights[i].color; } - if (discoLights) { - for (int i = numLights - 3; i < numLights; i++) { - auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0)); - m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0)); - m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0)); - m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1)); - lightPositions[i] = glm::vec3(m * glm::vec4(5, 0, 0, 1)); - lightColors[i] = glm::vec3(0.2); - if (i % 3 == 0) lightColors[i].x = sin(d); - if (i % 3 == 1) lightColors[i].y = cos(d * 3); - if (i % 3 == 2) lightColors[i].z = cos(d); - } - } glUniform1ui(glGetUniformLocation(pbrProg->progId, "numLights"), numLights); glUniform3fv(glGetUniformLocation(pbrProg->progId, "lightPositions"), numLights, glm::value_ptr(lightPositions[0])); @@ -452,8 +363,6 @@ void display() { curMode->display(d); - for (Light &light: lights) drawLight(light); - // TODO: restore /* if (discoLights) { */ /* for (int i = numLights - 3; i < numLights; i++) { */ diff --git a/mode.hpp b/mode.hpp new file mode 100644 index 0000000..43db9dd --- /dev/null +++ b/mode.hpp @@ -0,0 +1,16 @@ +#include +#include + +struct Light { + glm::mat4 trans; + glm::vec3 color; +}; + +struct Mode { + virtual void display(float d) = 0; + virtual void timer() = 0; + virtual void motion(int x, int y, int dx, int dy) = 0; + virtual void passiveMotion(int x, int y, int dx, int dy) = 0; + virtual void mouse(int button, int state, int x, int y) = 0; + std::vector getLights(float d) { return {}; } +}; diff --git a/mode/animationMode.cpp b/mode/animationMode.cpp new file mode 100644 index 0000000..a733cc1 --- /dev/null +++ b/mode/animationMode.cpp @@ -0,0 +1,79 @@ +#include "animationMode.hpp" + +AnimationMode::AnimationMode(std::string modelPath) { + const aiScene *scene = importer.ReadFile( + modelPath, aiProcess_Triangulate | aiProcess_CalcTangentSpace | + aiProcess_GenNormals | aiProcess_FlipUVs); + if (!scene) { + std::cerr << importer.GetErrorString() << std::endl; + exit(1); + } + + if (scene->mNumCameras > 0) { + aiCamera *cam = scene->mCameras[0]; + glm::mat4 camTrans; + if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0) + abort(); // there must be a node with the same name as camera + + camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]}; + + glm::vec3 camLookAt = + glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z); + camFront = camLookAt - camPos; + + camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z); + + fov = cam->mHorizontalFOV; + // TODO: aspectRatio = cam->mAspect; + znear = cam->mClipPlaneNear; + zfar = cam->mClipPlaneFar; + } + + for (int i = 0; i < scene->mNumLights; i++) { + aiLight *light = scene->mLights[i]; + glm::mat4 trans; + findNodeTrans(scene->mRootNode, light->mName, &trans); + glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g, + light->mColorAmbient.b}; + Light l = {trans, col}; + lights.push_back(l); + } + + sceneModel = new Model(scene, *pbrProg); +} + +void AnimationMode::display(float d) override { + sceneModel->draw(skyboxes[activeSkybox], d * 1000); + + for (Light &light: lights) drawLight(light); +} + +void drawLight(Light &light) { + drawPlainProg(plainProg, lightVao, light.trans, light.color); +} + +void AnimationMode::timer() override {}; +void AnimationMode::motion(int x, int y, int dx, int dy) override {} +void AnimationMode::passiveMotion(int x, int y, int dx, int dy) override {} +void AnimationMode::mouse(int button, int state, int x, int y) override {} + +std::vector AnimationMode::getLights(float d) { + std::vector ls = lights; + if (discoLights) { + if (discoLights) { + for (int i = 0; i < 3; i++) + auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0)); + m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0)); + m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0)); + m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1)); + Light l = { glm::translate(glm::mat4(1), glm::vec3(m * glm::vec4(5, 0, 0, 1))) + , glm::vec3(0.2) + }; + if (i % 3 == 0) l.color.x = sin(d); + if (i % 3 == 1) l.color.y = cos(d * 3); + if (i % 3 == 2) l.color.z = cos(d); + ls.push_back(l); + } + } + return ls; +} diff --git a/mode/animationMode.hpp b/mode/animationMode.hpp new file mode 100644 index 0000000..83ac5e5 --- /dev/null +++ b/mode/animationMode.hpp @@ -0,0 +1,17 @@ +#include "../mode.hpp" +#include "../model.hpp" +#include +struct AnimationMode : public Mode { + Model *sceneModel; + + bool discoLights = false; + std::vector lights; + + AnimationMode(std::string modelPath); + + void display(float d) override; + void timer() override; + void motion(int x, int y, int dx, int dy) override; + void passiveMotion(int x, int y, int dx, int dy) override; + void mouse(int button, int state, int x, int y) override; +};