WIP of modes
[opengl.git] / mode / animationMode.cpp
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;
+}