From 599bde975aa2a33244b37370d81bfba0839330ca Mon Sep 17 00:00:00 2001 From: devZoGok Date: Wed, 24 Mar 2021 21:25:51 +0200 Subject: [PATCH] code cleanup --- CMakeLists.txt | 1 + animation.cpp | 6 ------ animation.h | 3 +-- animationController.cpp | 6 ------ animationController.h | 4 ++-- ikSolver.h | 3 --- keyframeChannel.cpp | 6 +----- keyframeChannel.h | 1 - light.cpp | 10 ++-------- material.cpp | 2 +- material.h | 28 ++++------------------------ mesh.cpp | 13 ++++++------- mesh.h | 1 + node.h | 5 ----- particleEmitter.cpp | 17 +++++++++++------ particleEmitter.h | 2 +- particleEmitterTest.cpp | 4 ++-- root.h | 3 ++- shader.h | 2 +- skeleton.cpp | 9 ++++++--- skeleton.h | 1 + 21 files changed, 43 insertions(+), 84 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1f0d9a7..5def389 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,6 +11,7 @@ include_directories(/usr/include/cppunit) link_directories(/usr/lib/) set(CMAKE_BUILD_TYPE Debug) + set(gui text.cpp) set(utils util.cpp) set(math quaternion.cpp vector.cpp matrix.cpp ray.cpp) diff --git a/animation.cpp b/animation.cpp index 4b4b58e..331ae02 100644 --- a/animation.cpp +++ b/animation.cpp @@ -7,12 +7,6 @@ namespace vb01{ this->name = name; } - Animation::~Animation(){ - } - - void Animation::update(){ - } - KeyframeChannel* Animation::getKeyframeChannel(Animatable *animatable, KeyframeChannelType type){ KeyframeChannel *k = nullptr; diff --git a/animation.h b/animation.h index 2b7a5e1..8bea814 100644 --- a/animation.h +++ b/animation.h @@ -12,8 +12,7 @@ namespace vb01{ class Animation{ public: Animation(std::string); - ~Animation(); - void update(); + ~Animation(){} KeyframeChannel* getKeyframeChannel(Animatable*, KeyframeChannel::Type); std::vector getKeyframeChannelsByAnimatable(Animatable*); inline void addKeyframeChannels(std::vector keyframeChannels){this->keyframeChannels.assign(keyframeChannels.begin(), keyframeChannels.end());} diff --git a/animationController.cpp b/animationController.cpp index 86042a0..3160539 100644 --- a/animationController.cpp +++ b/animationController.cpp @@ -5,12 +5,6 @@ using namespace std; namespace vb01{ - AnimationController::AnimationController(){ - } - - AnimationController::~AnimationController(){ - } - void AnimationController::update(){ for(AnimationChannel *channel : channels){ channel->update(); diff --git a/animationController.h b/animationController.h index 30bfdb4..b57c032 100644 --- a/animationController.h +++ b/animationController.h @@ -15,8 +15,8 @@ namespace vb01{ class AnimationController{ public: - AnimationController(); - ~AnimationController(); + AnimationController(){} + ~AnimationController(){} void update(); Animation* getAnimation(std::string); inline std::vector getAnimations(){return animations;} diff --git a/ikSolver.h b/ikSolver.h index ee978e0..d9c5aaf 100644 --- a/ikSolver.h +++ b/ikSolver.h @@ -8,10 +8,7 @@ namespace vb01{ class IkSolver{ public: - IkSolver(); - ~IkSolver(){} static void calculateFabrik(int, Bone**, Vector3[], Vector3); - private: }; } diff --git a/keyframeChannel.cpp b/keyframeChannel.cpp index cd1ce86..9d7f2a9 100644 --- a/keyframeChannel.cpp +++ b/keyframeChannel.cpp @@ -23,8 +23,7 @@ namespace vb01{ float KeyframeChannel::interpolateBezier(vector points, float ratio){ int numPoints = points.size(); vector newPoints; - - for(int i = 0; i < numPoints - 1; i++) +for(int i = 0; i < numPoints - 1; i++) newPoints.push_back(points[i] + (points[i + 1] - points[i]) * ratio); if(newPoints.size() >= 2) @@ -80,7 +79,4 @@ namespace vb01{ } return keyframeChannel.keyframes[pastKeyframeId + (last ? 0 : 1)]; } - - void KeyframeChannel::transform(){ - } } diff --git a/keyframeChannel.h b/keyframeChannel.h index 74db8af..1847aca 100644 --- a/keyframeChannel.h +++ b/keyframeChannel.h @@ -67,7 +67,6 @@ namespace vb01{ static KeyframeChannel::Type getKeyframeChannelType(std::string); static float interpolate(Keyframe, Keyframe, float); static Keyframe findKeyframe(float, KeyframeChannel, bool); - static void transform(); }; typedef KeyframeChannel::Type KeyframeChannelType; diff --git a/light.cpp b/light.cpp index a4c921a..3acb96c 100755 --- a/light.cpp +++ b/light.cpp @@ -76,14 +76,12 @@ namespace vb01{ Root *root = Root::getSingleton(); Node *rootNode = root->getRootNode(); - Camera *cam=root->getCamera(); - float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(); - vector descendants; rootNode->getDescendants(descendants); descendants.push_back(rootNode); vector lights; vector materials; + for(Node *d : descendants){ for(Light *l : d->getLights()) lights.push_back(l); @@ -147,17 +145,13 @@ namespace vb01{ depthMapShader->setFloat(farPlane, "farPlane"); depthMapShader->setVec3(position, "lightPos"); if(type == POINT){ + vec3 dirs[] = {vec3(1, 0, 0), vec3(-1, 0, 0), vec3(0, 1, 0), vec3(0, -1, 0), vec3(0, 0, 1), vec3(0, 0, -1)}; for(int i = 0; i < 6; i++){ vec3 upVec; if(1 < i && i < 4) upVec = vec3(0, 0, -1); else upVec = vec3(0, -1, 0); - - vec3 dirs[] = { - vec3(1, 0, 0), vec3(-1, 0, 0), vec3(0, 1, 0), vec3(0, -1, 0), vec3(0, 0, 1), vec3(0, 0, -1) - }; - depthMapShader->setMat4(proj * lookAt(lightPos, lightPos + dirs[i], upVec), "shadowMat[" + to_string(i) + "]"); } } diff --git a/material.cpp b/material.cpp index 086e928..738ed33 100755 --- a/material.cpp +++ b/material.cpp @@ -6,7 +6,7 @@ using namespace std; namespace vb01{ Material::Material(Type type){ - this->type=type; + this->type = type; initShader(); } diff --git a/material.h b/material.h index 422f105..d8a804e 100755 --- a/material.h +++ b/material.h @@ -16,31 +16,11 @@ namespace vb01{ Material(Type = MATERIAL_2D); ~Material(); void update(); - inline void addDiffuseMap(std::string diffuseMap, bool flip = false){ - std::string p[]{diffuseMap}; - diffuseMapTextures.push_back(new Texture(p, 1, Texture::DIFFUSE, flip)); - } - inline void addDiffuseMap(std::string diffuseMap[], int numFrames){diffuseMapTextures.push_back(new Texture(diffuseMap, numFrames));} - inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));} inline void addDiffuseMap(Texture *texture){diffuseMapTextures.push_back(texture);} - inline void setDiffuseMap(Texture *texture, int i){diffuseMapTextures[i]=texture;} - inline void addNormalMap(std::string normalMap){ - std::string p[]{normalMap}; - normalMapTextures.push_back(new Texture(p, 1)); - } - inline void addNormalMap(std::string normalMap[], int numFrames){normalMapTextures.push_back(new Texture(normalMap, numFrames));} - inline void setNormalMap(Texture *texture, int i){normalMapTextures[i] = texture;} - inline void addSpecularMap(std::string specularMap){ - std::string p[]{specularMap}; - specularMapTextures.push_back(new Texture(p, 1)); - } - inline void addSpecularMap(std::string specularMap[], int numFrames){specularMapTextures.push_back(new Texture(specularMap, numFrames));} - inline void setSpecularMap(Texture *texture, int i){specularMapTextures[i] = texture;} - inline void addParallaxMap(std::string parallaxMap){ - std::string p[]{parallaxMap}; - parallaxMapTextures.push_back(new Texture(p, 1)); - } - inline void addParallaxMap(std::string parallaxMap[], int numFrames){parallaxMapTextures.push_back(new Texture(parallaxMap, numFrames));} + inline void addDiffuseMap(std::string diffuseMap[6]){diffuseMapTextures.push_back(new Texture(diffuseMap));} + inline void addNormalMap(Texture *texture){normalMapTextures.push_back(texture);} + inline void addSpecularMap(Texture *texture){specularMapTextures.push_back(texture);} + inline void addParallaxMap(Texture *texture){parallaxMapTextures.push_back(texture);} inline void setDiffuseColor(Vector4 diffuse){this->diffuseColor = diffuse;} inline void setSpecularColor(Vector4 specular){this->specularColor = specular;} inline void setLightingEnabled(bool lighting){this->lightingEnabled = lighting;} diff --git a/mesh.cpp b/mesh.cpp index 1a4037d..793c2db 100755 --- a/mesh.cpp +++ b/mesh.cpp @@ -122,9 +122,7 @@ namespace vb01{ void Mesh::update(){ Root *root = Root::getSingleton(); Camera *cam = root->getCamera(); - float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(); - float nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane(); - Vector3 dir = cam->getDirection(), up = cam->getUp(), camPos = Vector3::VEC_ZERO, pos = Vector3::VEC_ZERO, scale = Vector3::VEC_IJK; + Vector3 camPos = Vector3::VEC_ZERO, pos = Vector3::VEC_ZERO, scale = Vector3::VEC_IJK; Quaternion orient = Quaternion::QUAT_W; if(node){ @@ -140,19 +138,20 @@ namespace vb01{ model = translate(model, vec3(pos.x, pos.y, pos.z)); model = rotate(model, orient.getAngle(), vec3(rotAxis.x, rotAxis.y, rotAxis.z)); model = glm::scale(model, vec3(scale.x, scale.y, scale.z)); + + Vector3 dir = cam->getDirection(), up = cam->getUp(); mat4 view = lookAt(vec3(camPos.x, camPos.y, camPos.z), vec3(camPos.x + dir.x, camPos.y + dir.y, camPos.z + dir.z), vec3(up.x, up.y, up.z)); + + float fov = cam->getFov(), width = root->getWidth(), height = root->getHeight(), nearPlane = cam->getNearPlane(), farPlane = cam->getFarPlane(); mat4 proj = perspective(radians(fov), width / height, nearPlane, farPlane); material->update(); Shader *shader = material->getShader(); - if(reflect){ + if(reflect) updateReflection(shader, pos, width, height); - } - if(skeleton) updateSkeleton(shader); - if(numShapeKeys > 0) updateShapeKeys(shader); diff --git a/mesh.h b/mesh.h index df366bb..bd8dbd6 100755 --- a/mesh.h +++ b/mesh.h @@ -22,6 +22,7 @@ namespace vb01{ void animate(float, KeyframeChannel); }; + struct Vertex{ Vector3 pos, norm, tan, biTan; Vector2 uv; diff --git a/node.h b/node.h index fea21bc..1ae1f7a 100755 --- a/node.h +++ b/node.h @@ -20,11 +20,6 @@ namespace vb01{ class Node : public Animatable{ public: - struct Transform{ - Vector3 position = Vector3::VEC_ZERO, scale = Vector3::VEC_IJK; - Quaternion orientation = Quaternion::QUAT_W; - }; - Node(Vector3 = Vector3::VEC_ZERO, Quaternion = Quaternion::QUAT_W, Vector3 = Vector3::VEC_IJK, std::string = "", AnimationController *c = nullptr); virtual ~Node(); void attachMesh(Mesh*); diff --git a/particleEmitter.cpp b/particleEmitter.cpp index fbd3284..e7517a2 100755 --- a/particleEmitter.cpp +++ b/particleEmitter.cpp @@ -105,17 +105,22 @@ namespace vb01{ while(!heap){ bool end = true; for(int i = 0; 2 * i + 1 + offset < numParticles; i++){ - if(2 * i + 2 + offset < numParticles && (particles[i + offset]->d < particles[2 * i + 1 + offset]->d || particles[i + offset]->d < particles[2 * i + 2 + offset]->d)){ - bool leftChild = particles[2 * i + 1 + offset]->d > particles[2 * i + 2 + offset]->d; + if(2 * i + 2 + offset < numParticles && (particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane || + particles[i + offset]->distToCamPlane < particles[2 * i + 2 + offset]->distToCamPlane)) + { + bool leftChild = particles[2 * i + 1 + offset]->distToCamPlane > particles[2 * i + 2 + offset]->distToCamPlane; swap(particles[i + offset], leftChild ? particles[2 * i + 1 + offset] : particles[2 * i + 2 + offset]); } - else if(2 * i + 2 + offset == numParticles && particles[i + offset]->d < particles[2 * i + 1 + offset]->d) + else if(2 * i + 2 + offset == numParticles && particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane) swap(particles[i + offset], particles[2 * i + 1 + offset]); } for(int i = 0; 2 * i + 1 < numParticles; i++){ - if(2 * i + 2 + offset < numParticles && (particles[i + offset]->d < particles[2 * i + 1 + offset]->d || particles[i + offset]->d < particles[2 * i + 2 + offset]->d)) + if(2 * i + 2 + offset < numParticles && (particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane || + particles[i + offset]->distToCamPlane < particles[2 * i + 2 + offset]->distToCamPlane)) + { end = false; - else if(2 * i + 2 + offset == numParticles && particles[i + offset]->d < particles[2 * i + 1 + offset]->d) + } + else if(2 * i + 2 + offset == numParticles && particles[i + offset]->distToCamPlane < particles[2 * i + 1 + offset]->distToCamPlane) end = false; } if(end) @@ -185,7 +190,7 @@ namespace vb01{ dir = dir.norm() * speed + gravity; particles[i]->trans = particles[i]->trans + dir; - particles[i]->d = cos(camDir.getAngleBetween((particles[i]->trans - camPos).norm())) * camPos.getDistanceFrom(particles[i]->trans); + particles[i]->distToCamPlane = cos(camDir.getAngleBetween((particles[i]->trans - camPos).norm())) * camPos.getDistanceFrom(particles[i]->trans); float lifePercentage = (float)(getTime() - particles[i]->time) / particles[i]->timeToLive; diff --git a/particleEmitter.h b/particleEmitter.h index 5d25d0a..59bcab4 100755 --- a/particleEmitter.h +++ b/particleEmitter.h @@ -39,7 +39,7 @@ namespace vb01{ int VAO; s64 time = 0, timeToLive = 0; Vector3 trans; - float d; + float distToCamPlane; }; void makeHeap(int); diff --git a/particleEmitterTest.cpp b/particleEmitterTest.cpp index 535bf92..a9b8f9f 100644 --- a/particleEmitterTest.cpp +++ b/particleEmitterTest.cpp @@ -12,7 +12,7 @@ namespace vb01{ particleEmitter->particles = new ParticleEmitter::Particle*[numParticles]; for(int i = 0; i < numParticles; i++){ ParticleEmitter::Particle *particle = new ParticleEmitter::Particle; - particle->d = rand() % 1000; + particle->distToCamPlane = rand() % 1000; particleEmitter->particles[i] = particle; } } @@ -25,7 +25,7 @@ namespace vb01{ for(int i = 1; i < numParticles; i++){ ParticleEmitter::Particle *prevPart = particleEmitter->particles[i - 1]; ParticleEmitter::Particle *currPart = particleEmitter->particles[i]; - CPPUNIT_ASSERT(prevPart->d >= currPart->d); + CPPUNIT_ASSERT(prevPart->distToCamPlane >= currPart->distToCamPlane); } } } diff --git a/root.h b/root.h index ae3a0ab..6acd1c1 100755 --- a/root.h +++ b/root.h @@ -2,6 +2,7 @@ #define ROOT_H #include "camera.h" +#include "util.h" #include #include @@ -47,7 +48,7 @@ namespace vb01{ Box *skybox = nullptr; Quad *guiPlane = nullptr; int width, height, blurLevel = 10; - unsigned int FBO, RBO, pingpongBuffers[2]; + u32 FBO, RBO, pingpongBuffers[2]; GLFWwindow *window; Node *rootNode, *guiNode; Camera *camera; diff --git a/shader.h b/shader.h index 14145a9..1224b2f 100755 --- a/shader.h +++ b/shader.h @@ -32,7 +32,7 @@ namespace vb01{ void loadShaders(); void pushShader(u32&, std::string&, int, ErrorType); void checkCompileErrors(u32, ErrorType); - unsigned int id; + u32 id; bool geometry = false; std::string vString, fString, gString; diff --git a/skeleton.cpp b/skeleton.cpp index a1f76c2..6ae99a7 100644 --- a/skeleton.cpp +++ b/skeleton.cpp @@ -17,6 +17,11 @@ namespace vb01{ } void Skeleton::update(){ + updateIk(); + controller->update(); + } + + void Skeleton::updateIk(){ for(Bone *b : bones){ if(b->getIkTarget()){ const int chainLength = b->getIkChainLength(); @@ -29,11 +34,9 @@ namespace vb01{ delete[] boneChain; } } - for(Bone *b : bones){ + for(Bone *b : bones) if(b->getIkTarget()) solveIk(b); - } - controller->update(); } void Skeleton::solveIk(Bone *ikBone){ diff --git a/skeleton.h b/skeleton.h index fc17ddd..ce4bdde 100644 --- a/skeleton.h +++ b/skeleton.h @@ -23,6 +23,7 @@ namespace vb01{ inline std::string getName(){return name;} inline int getNumBones(){return bones.size();} private: + void updateIk(); void solveIk(Bone*); void transformIkChain(int, Bone*[], Vector3[], Bone*); Bone** getIkBoneChain(Bone*);