WIP of modes
[opengl.git] / mode / animationMode.cpp
1 #include "animationMode.hpp"
2
3 AnimationMode::AnimationMode(std::string modelPath) {
4         const aiScene *scene = importer.ReadFile(
5                         modelPath, aiProcess_Triangulate | aiProcess_CalcTangentSpace |
6                         aiProcess_GenNormals | aiProcess_FlipUVs);
7         if (!scene) {
8                 std::cerr << importer.GetErrorString() << std::endl;
9                 exit(1);
10         }
11
12         if (scene->mNumCameras > 0) {
13                 aiCamera *cam = scene->mCameras[0];
14                 glm::mat4 camTrans;
15                 if (findNodeTrans(scene->mRootNode, cam->mName, &camTrans) != 0)
16                         abort(); // there must be a node with the same name as camera
17
18                 camPos = {camTrans[3][0], camTrans[3][1], camTrans[3][2]};
19
20                 glm::vec3 camLookAt =
21                         glm::vec3(cam->mLookAt.x, cam->mLookAt.y, cam->mLookAt.z);
22                 camFront = camLookAt - camPos;
23
24                 camUp = glm::vec3(cam->mUp.x, cam->mUp.y, cam->mUp.z);
25
26                 fov = cam->mHorizontalFOV;
27                 // TODO: aspectRatio = cam->mAspect;
28                 znear = cam->mClipPlaneNear;
29                 zfar = cam->mClipPlaneFar;
30         }
31
32         for (int i = 0; i < scene->mNumLights; i++) {
33                 aiLight *light = scene->mLights[i];
34                 glm::mat4 trans;
35                 findNodeTrans(scene->mRootNode, light->mName, &trans);
36                 glm::vec3 col = {light->mColorAmbient.r, light->mColorAmbient.g,
37                         light->mColorAmbient.b};
38                 Light l = {trans, col};
39                 lights.push_back(l);
40         }
41
42         sceneModel = new Model(scene, *pbrProg);
43 }
44
45 void AnimationMode::display(float d) override {
46         sceneModel->draw(skyboxes[activeSkybox], d * 1000);
47
48         for (Light &light: lights) drawLight(light);
49 }
50
51 void drawLight(Light &light) {
52         drawPlainProg(plainProg, lightVao, light.trans, light.color);
53 }
54
55 void AnimationMode::timer() override {};
56 void AnimationMode::motion(int x, int y, int dx, int dy) override {}
57 void AnimationMode::passiveMotion(int x, int y, int dx, int dy) override {}
58 void AnimationMode::mouse(int button, int state, int x, int y) override {}
59
60 std::vector<Light> AnimationMode::getLights(float d) {
61         std::vector<Light> ls = lights;
62         if (discoLights) {
63         if (discoLights) {
64                 for (int i = 0; i < 3; i++)
65                         auto m = glm::translate(glm::mat4(1.f), glm::vec3(-2.5, 0, 0));
66                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(1, 0, 0));
67                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 1, 0));
68                         m = glm::rotate(m, glm::radians(d * 100 + i * 30), glm::vec3(0, 0, 1));
69                         Light l = { glm::translate(glm::mat4(1), glm::vec3(m * glm::vec4(5, 0, 0, 1)))
70                                           , glm::vec3(0.2)
71                                                 };
72                         if (i % 3 == 0) l.color.x = sin(d);
73                         if (i % 3 == 1) l.color.y = cos(d * 3);
74                         if (i % 3 == 2) l.color.z = cos(d);
75                         ls.push_back(l);
76                 }
77         }
78         return ls;
79 }