X-Git-Url: http://git.lukelau.me/?a=blobdiff_plain;f=main.cpp;h=09ef264dc4997e53257bba328da9ef7c42f51c20;hb=ad1184d3e6a82eae4ed0bb86b0e737329c580eff;hp=82e0fff76d3259bafd8625137418c5468629565a;hpb=9b76ae474d43ab495b68bb88a9cb517864496f82;p=opengl.git 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++) { */