Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/torch anim #211

Merged
merged 9 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 21 additions & 22 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
{

"game": {
"maze": {
"directory": "maps",
"procedural": false,
"procedural": true,
"maze_file": "test/orb_drop.maze"
},
"disable_enemies": true
},
"network": {
"server_ip": "localhost",
"server_port": 2355
},
"server": {
"lobby_name": "Hope you're doing well!",
"lobby_broadcast": true,
"max_players": 1,
"disable_dm": true,
"skip_intro": true
},
"client": {
"default_name": "Conan O'Brien",
"lobby_discovery": true,
"fullscreen": false,
"draw_bboxes": false,
"fps_counter": true,
"presentation": false,
"render": 100
}
"network": {
"server_ip": "localhost",
"server_port": 2355
},
"server": {
"lobby_name": "Hope you're doing well!",
"lobby_broadcast": true,
"max_players": 1,
"disable_dm": false,
"skip_intro": true
},
"client": {
"default_name": "Conan O'Brien",
"lobby_discovery": true,
"fullscreen": true,
"draw_bboxes": false,
"fps_counter": true,
"presentation": false,
"render": 75
}
}
5 changes: 5 additions & 0 deletions include/client/animation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Animation {

Animation(const std::string& animationPath, Model* model);

Animation(const std::string& animationDirPath, const std::string& animName, int frames);

~Animation() {}

Bone* findBone(const std::string& name);
Expand All @@ -30,6 +32,8 @@ class Animation {
inline float getDuration() { return m_duration;}
inline const AssimpNodeData& getRootNode() { return m_rootNode; }
inline const std::map<std::string,BoneInfo>& getBoneIDMap() { return m_boneInfoMap; }
inline Model* getFrame(int frame) { return model_keyframes[(frame % model_keyframes.size())]; }
inline int getKeyframeSize() { return model_keyframes.size(); }

private:
void readMissingBones(const aiAnimation* animation, Model& model);
Expand All @@ -41,4 +45,5 @@ class Animation {
std::vector<Bone> m_bones;
AssimpNodeData m_rootNode;
std::map<std::string, BoneInfo> m_boneInfoMap;
std::vector<Model*> model_keyframes;
};
9 changes: 8 additions & 1 deletion include/client/animationmanager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <vector>
#include <unordered_map>
#include <utility>
#include <random>

#include <assimp/scene.h>
#include <assimp/Importer.hpp>
Expand All @@ -19,6 +20,8 @@ class AnimationManager

void updateAnimation(float dt);

Model* updateFrameAnimation(float time);

void playAnimation(Animation* pAnimation);

void calculateBoneTransform(const AssimpNodeData* node, glm::mat4 parentTransform);
Expand All @@ -27,15 +30,19 @@ class AnimationManager

void setAnimation(EntityID id, ModelType modelType, AnimState animState);

void setFrameAnimation(EntityID id, ModelType modelType, AnimState animState);

std::vector<glm::mat4> getFinalBoneMatrices() { return m_finalBoneMatrices; }

private:
std::vector<glm::mat4> m_finalBoneMatrices;
std::unordered_map<EntityID, std::pair<int, Animation*>> entityAnimFrameMap;
std::unordered_map<EntityID, std::pair<float, Animation*>> entityAnimMap;
std::unordered_map<ModelType, std::unordered_map<AnimState, Animation*>> objAnimMap;
Animation* m_currentAnimation;
EntityID currEntity;
float m_currentTime;
float m_deltaTime;

double lastFrameTime;
int currFrame;
};
1 change: 1 addition & 0 deletions include/client/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ class Client {

std::unique_ptr<Model> bear_model;
std::unique_ptr<Model> torchlight_model;
std::unique_ptr<Model> torchpost_model;
std::unique_ptr<Model> wall_model;
std::unique_ptr<Model> pillar_model;
std::unique_ptr<Model> sungod_model;
Expand Down
11 changes: 11 additions & 0 deletions include/client/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ class Model : public Renderable {
glm::vec3 camPos,
bool fill) override;

/**
* Draws all the meshes of a given model
*
* @param Shader to use while drawing all the
* meshes of the model
*/
void draw(Shader* shader,
glm::vec3 camPos,
bool fill,
glm::vec3 color);

/**
* Sets the position of the Model to the given x,y,z
* values
Expand Down
3 changes: 2 additions & 1 deletion include/shared/game/sharedmodel.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ enum class ModelType {
FireballTrapDown,
SunGod,
Mirror,
LightCut
LightCut,
TorchPost
};
18 changes: 9 additions & 9 deletions maps/test/anim_test.maze
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#########
#.......#
#.......#
#.......#
#.......#
#.......#
#...@...#
#.......#
#########
###[###
#.....#
{..3..}
#.....#
#.....#
#.....#
{.@...}
#.....#
###]###
8 changes: 8 additions & 0 deletions src/client/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Animation::Animation(const std::string& animationPath, Model* model) {
readMissingBones(animation, *model);
}

Animation::Animation(const std::string& animationDirPath, const std::string& animName, int frames) {
for (int i = 1; i <= frames; i++) {
auto frame_model_path = animationDirPath + "/" + animName + std::to_string(i) + ".obj";
auto frame_model = new Model(frame_model_path, true);
model_keyframes.push_back(frame_model);
}
}

Bone* Animation::findBone(const std::string& name) {
auto iter = std::find_if(m_bones.begin(), m_bones.end(),
[&](const Bone& Bone)
Expand Down
33 changes: 33 additions & 0 deletions src/client/animationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ AnimationManager::AnimationManager() {

for (int i = 0; i < 100; i++)
m_finalBoneMatrices.push_back(glm::mat4(1.0f));

currFrame = 0;
lastFrameTime = 0.0;
}

void AnimationManager::updateAnimation(float dt) {
Expand All @@ -25,6 +28,26 @@ void AnimationManager::updateAnimation(float dt) {
}
}

Model* AnimationManager::updateFrameAnimation(float time) {
if (entityAnimFrameMap[currEntity].second) {
m_currentAnimation = entityAnimFrameMap[currEntity].second;
int currFrame = entityAnimFrameMap[currEntity].first + 1;

// /* Change this to a constant */
// if (time - lastFrameTime >= 0.01667) {
// std::cout << "time: " << time << ", lastTime: " << lastFrameTime << ", diff: " << (time - lastFrameTime) << ", currFrame: " << currFrame << std::endl;
// currFrame += 1;
// std::cout << currFrame << std::endl;
// lastFrameTime = time;
// }
entityAnimFrameMap[currEntity].first = currFrame;
return m_currentAnimation->getFrame(currFrame);
} else {
return nullptr;
}
}


void AnimationManager::playAnimation(Animation* pAnimation) {
m_currentAnimation = pAnimation;
m_currentTime = 0.0f;
Expand Down Expand Up @@ -69,6 +92,16 @@ void AnimationManager::setAnimation(EntityID id, ModelType modelType, AnimState
currEntity = id;
}

void AnimationManager::setFrameAnimation(EntityID id, ModelType modelType, AnimState animState) {
if (entityAnimFrameMap.find(id) == entityAnimFrameMap.end() || entityAnimFrameMap[id].second != objAnimMap[modelType][animState]) {
static std::random_device dev;
static std::mt19937 rng(dev());
static std::uniform_int_distribution<std::mt19937::result_type> dist(0,51);
entityAnimFrameMap[id] = std::make_pair(dist(rng), objAnimMap[modelType][animState]);
}
currEntity = id;
}

void AnimationManager::addAnimation(Animation* anim, ModelType modelType, AnimState animState) {
if (objAnimMap.find(modelType) == objAnimMap.end()) {
std::unordered_map<AnimState, Animation*> animMap;
Expand Down
42 changes: 36 additions & 6 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ bool Client::init() {
auto torchlight_model_path = env_models_dir / "exit.obj";
this->torchlight_model = std::make_unique<Model>(torchlight_model_path.string(), true);

auto torchpost_model_path = env_models_dir / "Torch" / "Torch.obj";
this->torchpost_model = std::make_unique<Model>(torchpost_model_path.string(), false);

auto slime_model_path = entity_models_dir / "slime.obj";
this->slime_model = std::make_unique<Model>(slime_model_path.string(), true);

Expand Down Expand Up @@ -259,6 +262,8 @@ bool Client::init() {
auto player_run_path = graphics_assets_dir / "animations/run.fbx";
auto player_atk_path = graphics_assets_dir / "animations/slash.fbx";
auto player_use_potion_path = graphics_assets_dir / "animations/drink.fbx";
auto torchlight_anim_path = graphics_assets_dir / "animations/fire-fix";


auto fire_player_model_path = player_models_dir / "char_1_rename/char1.fbx";
auto lightning_player_model_path = player_models_dir / "char_2_rename/char2.fbx";
Expand All @@ -278,6 +283,8 @@ bool Client::init() {
};

animManager = new AnimationManager();
Animation* torchlight_animation = new Animation(torchlight_anim_path.string(), "fire-fix", 45);
animManager->addAnimation(torchlight_animation, ModelType::Torchlight, AnimState::IdleAnim);

for (int i = 0; i < NUM_PLAYER_MODELS; i++) {
Animation* player_walk = new Animation(player_walk_path.string(), player_models.at(i).second);
Expand Down Expand Up @@ -830,10 +837,12 @@ void Client::geometryPass() {
sharedObject->type == ObjectType::Lava ||
sharedObject->type == ObjectType::ArrowTrap ||
sharedObject->type == ObjectType::SpikeTrap ||
sharedObject->type == ObjectType::Projectile ) && dist > this->config.client.render) {
sharedObject->type == ObjectType::Projectile ||
sharedObject->type == ObjectType::Torchlight) // dont render post from far away
&& dist > this->config.client.render) {
continue;
}

switch (sharedObject->type) {
case ObjectType::Player: {
// search all player inventories to see who has the orb and update it's position (while it's held by a player)
Expand All @@ -853,6 +862,7 @@ void Client::geometryPass() {
glm::vec3 pos = sharedObject->physics.getCenterPosition();
pos.y -= sharedObject->physics.dimensions.y / 2.0f;
pos.y += PLAYER_EYE_LEVEL;
// pos.z += 3.0f;
cam->updatePos(pos);

// update listener position & facing
Expand Down Expand Up @@ -1238,6 +1248,17 @@ void Client::geometryPass() {
}
break;
}
case ObjectType::Torchlight: {
// render the post in the geometry pass, and the light in the lighting pass
// i fucked with the y value to make it the right height but the x and z are from the
// model coords
this->torchpost_model->setDimensions(glm::vec3(0.860699, 5.2f, 0.827778));
this->torchpost_model->translateAbsolute(sharedObject->physics.getCenterPosition());
this->torchpost_model->translateRelative(glm::vec3(0, -0.2f, 0));
this->torchpost_model->draw(this->deferred_geometry_shader.get(),
this->cam->getPos(),
true);
}
default:
break;
}
Expand Down Expand Up @@ -1363,7 +1384,6 @@ void Client::lightingPass() {
glBlitFramebuffer(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT, GL_DEPTH_BUFFER_BIT, GL_NEAREST);
glBindFramebuffer(GL_FRAMEBUFFER, 0);


// render torch lights on top of scene
this->deferred_light_box_shader->use();
glm::mat4 viewProj = this->cam->getViewProj();
Expand All @@ -1390,11 +1410,21 @@ void Client::lightingPass() {

glm::vec3 v(1.0f);


animManager->setFrameAnimation(sharedObject->globalID, sharedObject->modelType, sharedObject->animState);

/* Update model animation */
Model* torch_frame_model = animManager->updateFrameAnimation(glfwGetTime());
glm::vec3 dims = torch_frame_model->getDimensions();
this->deferred_light_box_shader->use();
this->deferred_light_box_shader->setVec3("lightColor", properties.diffuse_color);
this->torchlight_model->setDimensions(2.0f * sharedObject->physics.dimensions);
this->torchlight_model->translateAbsolute(sharedObject->physics.getCenterPosition());
this->torchlight_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true);
// this->torchlight_model->setDimensions(2.0f * sharedObject->physics.dimensions);
// this->torchlight_model->translateAbsolute(sharedObject->physics.getCenterPosition());
// this->torchlight_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true);
torch_frame_model->setDimensions(2.0f * sharedObject->physics.dimensions);
torch_frame_model->translateAbsolute(sharedObject->physics.getCenterPosition());
// torch_frame_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true);
torch_frame_model->draw(this->deferred_light_box_shader.get(), this->cam->getPos(), true, properties.diffuse_color);
}

}
Expand Down
13 changes: 13 additions & 0 deletions src/client/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ void Model::draw(Shader* shader,
}
}

void Model::draw(Shader* shader,
glm::vec3 camPos,
bool fill,
glm::vec3 color) {
float scale = 1.1f;
for(Mesh& mesh : this->meshes) {
glm::vec3 scaledColor = scale * color;
shader->setVec3("lightColor", scaledColor);
mesh.draw(shader, camPos, fill);
scale += 0.15f;
}
}

void Model::translateAbsolute(const glm::vec3& new_pos) {
Renderable::translateAbsolute(new_pos);
for(Mesh& mesh : this->meshes) {
Expand Down
Loading