Skip to content

Commit

Permalink
Bugfix and dirtyflag
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfrev0 committed Sep 7, 2023
1 parent a2fecd5 commit 69c1c71
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 51 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ A simple game engine codebase from scratch
Memory Leak https://github.com/glfw/glfw/issues/2093

## TODO
* Model animation
- GPU Skinning
* Migrate to glm
* DirtyFlag propagation when adopt/abandon
* Use shader instead fixed function pipeline (https://learnopengl.com/Lighting/Basic-Lighting)
* GPU skinning(https://community.khronos.org/t/skinning-on-the-gpu-vs-the-cpu/73169/8)
* Camera z axis rotation issue (adjust quaternion roll axis to zero)
* Dirty flag
* ECS https://en.wikipedia.org/wiki/Entity_component_system
* Textbox, show FPS
* Textbox, show FPS
* Improve performance
2 changes: 1 addition & 1 deletion src/game/component/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct BoneInfluence{
float weight=0;
Bone* bone=nullptr;
};
const int MAX_BONE_INFLUENCE=8;
const int MAX_BONE_INFLUENCE=4;

class Mesh:public Component{
public:
Expand Down
49 changes: 30 additions & 19 deletions src/game/entity/Entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ Entity* Entity::loadFromFile(const std::string& model_path){
int influence_idx=0;
while(influence_idx<MAX_BONE_INFLUENCE and mesh->bone_influences[target_vertex][influence_idx].bone)
influence_idx++;
assert(influence_idx<MAX_BONE_INFLUENCE);
assert(influence_idx<=MAX_BONE_INFLUENCE);

mesh->bone_influences[target_vertex][influence_idx].bone=&bone;
mesh->bone_influences[target_vertex][influence_idx].weight=weight;
Expand Down Expand Up @@ -211,25 +211,36 @@ void Entity::draw(const Mat44& world2camera, Mat44 parent2world, std::vector<Ren
}

for(auto mesh:meshes){
vector<Vec3> vertices_cur(mesh->vertices.size()), normals_cur(mesh->vertices.size());
for(int i=0;i<mesh->vertices.size();i++){
Vec3 vtx_skinned;
for(int j=0;j<MAX_BONE_INFLUENCE;j++){
if(mesh->bone_influences[i][j].weight!=0)
vtx_skinned += mesh->bone_influences[i][j].bone->node->local2world()*mesh->bone_influences[i][j].bone->offset * mesh->vertices[i] * mesh->bone_influences[i][j].weight;
if(mesh->bones.size()){
vector<Vec3> vertices_cur(mesh->vertices.size()), normals_cur(mesh->vertices.size());
for(int i=0;i<mesh->vertices.size();i++){
Vec3 vtx_skinned;
for(int j=0;j<MAX_BONE_INFLUENCE;j++){
if(mesh->bone_influences[i][j].weight!=0)
vtx_skinned += mesh->bone_influences[i][j].bone->node->local2world()*mesh->bone_influences[i][j].bone->offset * mesh->vertices[i] * mesh->bone_influences[i][j].weight;
}
vertices_cur[i]=vtx_skinned;
//TODO: normal
normals_cur[i]=mesh->normals[i];
}
vertices_cur[i]=vtx_skinned;
//TODO: normal
normals_cur[i]=mesh->normals[i];
render_q.emplace_back(
world2camera,
mesh->primitive_type,
vertices_cur,
normals_cur,
mesh->texcoord,
mesh->faces,
mesh->material->getTextureID());
}else{
render_q.emplace_back(
world2camera*parent2world*local2parent(),
mesh->primitive_type,
mesh->vertices,
mesh->normals,
mesh->texcoord,
mesh->faces,
mesh->material->getTextureID());
}
render_q.emplace_back(
world2camera*local2parent(),
mesh->primitive_type,
vertices_cur,
normals_cur,
mesh->texcoord,
mesh->faces,
mesh->material->getTextureID());
}
// //bone tree
// render_q.emplace_back(
Expand All @@ -244,7 +255,7 @@ void Entity::draw(const Mat44& world2camera, Mat44 parent2world, std::vector<Ren

void Entity::update(float delta_time){
static float elapsed=0.0f;
elapsed+=delta_time;
elapsed+=delta_time*10;
if(anims.size()){

if(auto it = upper_bound(anims.back().positions.begin(),anims.back().positions.end(),elapsed,[](float time, const auto& keyframe){
Expand Down
37 changes: 18 additions & 19 deletions src/game/entity/Entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ struct Entity{
Vec3 position()const{ return _position; }
Quaternion rotate()const{ return _rotate; }
Vec3 scale()const{ return _scale; }
void position(const Vec3& v){ _position=v; }
void rotate(const Quaternion& q){ _rotate=q; }
void scale(const Vec3& v){ _scale=v; }
void position_acc(const Vec3& v){ _position+=v; }
void rotate_acc(Quaternion q){ _rotate=q*_rotate; }
void scale_acc(const Vec3& v){ _scale*=v; }
void position(const Vec3& v){ _position=v; local2world_dirty=world2local_dirty=true; }
void rotate(const Quaternion& q){ _rotate=q; local2world_dirty=world2local_dirty=true; }
void scale(const Vec3& v){ _scale=v; local2world_dirty=world2local_dirty=true; }
void position_acc(const Vec3& v){ _position+=v; local2world_dirty=world2local_dirty=true; }
void rotate_acc(Quaternion q){ _rotate=q*_rotate; local2world_dirty=world2local_dirty=true; }
void scale_acc(const Vec3& v){ _scale*=v; local2world_dirty=world2local_dirty=true; }

Entity* parent()const{return _parent;}
void adopt(Entity* child){
Expand Down Expand Up @@ -96,24 +96,23 @@ struct Entity{
};
return mscale*mrotate*mposition;
}

mutable bool local2world_dirty=true;
mutable Mat44 local2world_memo;
Mat44 local2world()const{
auto ret=Mat44::identity();
auto node=this;
while(node){
ret=node->local2parent()*ret;
node=node->parent();
if(local2world_dirty){
local2world_memo=_parent?_parent->local2world()*local2parent():local2parent();
local2world_dirty=false;
}
return ret;
return local2world_memo;
}
mutable bool world2local_dirty=true;
mutable Mat44 world2local_memo;
Mat44 world2local()const{
auto ret=Mat44::identity();
auto node=this;
while(node){
ret=ret*node->parent2local();
node=node->parent();
if(world2local_dirty){
world2local_memo=_parent?parent2local()*_parent->world2local():parent2local();
world2local_dirty=false;
}
return ret;
return world2local_memo;
}

void draw(const Mat44& world2camera, Mat44 parent2world, std::vector<RenderRequest>& render_q)const;
Expand Down
14 changes: 6 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,12 @@ void init(){
// };
// entity_root->adopt(koume);

// auto cube = Entity::loadFromFile("models/cube/cube.obj");
// cube->onUpdate=[](Entity*self, float delta_time){
// static float t=0;
// t+=delta_time/3;
// self->rotate(Quaternion().slerp(Quaternion({0,1,0},PI/3),fmod(t,1)).normalized());
// };
// cube->position({0,0,5});
// entity_root->adopt(cube);
auto cube = Entity::loadFromFile("models/cube/cube.obj");
cube->onUpdate=[](Entity*self, float delta_time){
self->rotate_acc(Quaternion({0,1,0},delta_time));
};
cube->position({3,0,0});
entity_root->adopt(cube);

// auto backpack = Entity::loadFromFile("models/backpack/backpack.obj");
// backpack->position({5,0,0});
Expand Down

0 comments on commit 69c1c71

Please sign in to comment.