WIP of modes anim
authorLuke Lau <luke_lau@icloud.com>
Tue, 7 Apr 2020 00:38:03 +0000 (01:38 +0100)
committerLuke Lau <luke_lau@icloud.com>
Tue, 7 Apr 2020 00:38:03 +0000 (01:38 +0100)
Makefile
main.cpp
mode.hpp [new file with mode: 0644]
mode/animationMode.cpp [new file with mode: 0644]
mode/animationMode.hpp [new file with mode: 0644]

index fe9ca798e5649bb34c02c641a02d2f91927b6067..746d160584088107116e62c65202aa0d154e8bcc 100644 (file)
--- 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 \
index 82e0fff76d3259bafd8625137418c5468629565a..09ef264dc4997e53257bba328da9ef7c42f51c20 100644 (file)
--- 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<Light> 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<Light> 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 (file)
index 0000000..43db9dd
--- /dev/null
+++ b/mode.hpp
@@ -0,0 +1,16 @@
+#include <glm/glm.hpp>
+#include <vector>
+
+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<Light> getLights(float d) { return {}; }
+};
diff --git a/mode/animationMode.cpp b/mode/animationMode.cpp
new file mode 100644 (file)
index 0000000..a733cc1
--- /dev/null
@@ -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<Light> AnimationMode::getLights(float d) {
+       std::vector<Light> 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 (file)
index 0000000..83ac5e5
--- /dev/null
@@ -0,0 +1,17 @@
+#include "../mode.hpp"
+#include "../model.hpp"
+#include <vector>
+struct AnimationMode : public Mode {
+       Model *sceneModel;
+
+       bool discoLights = false;
+       std::vector<Light> 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;
+};