gameobject 3d transform

This commit is contained in:
John Alanbrook 2023-11-21 23:05:06 +00:00
parent 6193bceb74
commit 811d8bb708
4 changed files with 82 additions and 16 deletions

View file

@ -173,7 +173,9 @@ struct model *MakeModel(const char *path) {
.type = SG_BUFFERTYPE_INDEXBUFFER});
}
if (primitive.material->has_pbr_metallic_roughness) {// && primitive.material->pbr_metallic_roughness.base_color_texture.texture) {
struct cgltf_material *mat = primitive.material;
if (mat && primitive.material->has_pbr_metallic_roughness) {
cgltf_image *img = primitive.material->pbr_metallic_roughness.base_color_texture.texture->image;
if (img->buffer_view) {
cgltf_buffer_view *buf = img->buffer_view;
@ -281,13 +283,14 @@ struct model *MakeModel(const char *path) {
return model;
}
HMM_Vec3 eye = {50,10,5};
/* eye position */
HMM_Vec3 eye = {0,0,100};
void draw_model(struct model *model, HMM_Mat4 amodel) {
HMM_Mat4 proj = HMM_Perspective_RH_ZO(45, (float)mainwin.width / mainwin.height, 0.1, 10000);
HMM_Mat4 proj = projection;//HMM_Perspective_RH_ZO(45, (float)mainwin.width / mainwin.height, 0.1, 10000);
HMM_Vec3 center = {0.f, 0.f, 0.f};
HMM_Vec3 up = {0.f, 1.f, 0.f};
HMM_Mat4 view = HMM_LookAt_RH(eye, center, up);
HMM_Mat4 view = HMM_LookAt_RH(eye, center, vUP);
HMM_Mat4 vp = HMM_MulM4(proj, view);
HMM_Mat4 mvp = HMM_MulM4(vp, amodel);
@ -322,15 +325,8 @@ void draw_drawmodel(struct drawmodel *dm)
{
if (!dm->model) return;
struct gameobject *go = id2go(dm->go);
cpVect pos = cpBodyGetPosition(go->body);
HMM_Mat4 scale = HMM_Scale(id2go(dm->go)->scale);
HMM_Mat4 trans = HMM_M4D(1.f);
trans.Elements[3][2] = -pos.x;
trans.Elements[3][1] = pos.y;
HMM_Mat4 rot = HMM_Rotate_RH(cpBodyGetAngle(go->body), vUP);
/* model matrix = trans * rot * scale */
draw_model(dm->model, HMM_MulM4(trans, HMM_MulM4(rot, scale)));
HMM_Mat4 rst = m4_rst(go2t3(go));
draw_model(dm->model, rst);
}
void free_drawmodel(struct drawmodel *dm)

View file

@ -100,6 +100,60 @@ HMM_Mat3 transform2d2mat(transform2d t)
return HMM_MulM3(mt_t(t), HMM_MulM3(mt_r(t), mt_s(t)));
}
transform3d mat2transform3d(HMM_Mat4 mat)
{
}
transform3d go2t3(gameobject *go)
{
transform3d t;
HMM_Vec2 p = go2pos(go);
t.pos.Y = p.Y;
t.pos.X = p.X;
t.scale = go->scale;
t.scale.Z = go->scale.X;
t.rotation = HMM_QFromAxisAngle_RH(vFWD, go2angle(go));
t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vRIGHT, -t.pos.Y/70), t.rotation);
t.rotation = HMM_MulQ(HMM_QFromAxisAngle_RH(vUP, t.pos.X/70), t.rotation);
return t;
}
HMM_Mat4 m4_t(transform3d t)
{
return HMM_Translate(t.pos);
}
HMM_Mat4 m4_s(transform3d t)
{
return HMM_Scale(t.scale);
}
HMM_Mat4 m4_r(transform3d t)
{
return HMM_QToM4(t.rotation);
}
HMM_Mat4 m4_st(transform3d t)
{
return HMM_MulM4(m4_t(t), m4_s(t));
}
HMM_Mat4 m4_rt(transform3d t)
{
return HMM_MulM4(m4_t(t), m4_r(t));
}
HMM_Mat4 m4_rst(transform3d t)
{
return HMM_MulM4(m4_st(t), m4_r(t));
}
HMM_Mat4 transform3d2mat(transform3d t)
{
return m4_rst(t);
}
HMM_Mat3 mt_rst(transform2d t)
{
return transform2d2mat(t);

View file

@ -22,6 +22,21 @@ typedef struct transform2d {
transform2d mat2transform2d(HMM_Mat3 mat);
HMM_Mat3 transform2d2mat(transform2d t);
typedef struct transform3d {
HMM_Vec3 pos;
HMM_Vec3 scale;
HMM_Quat rotation;
} transform3d;
transform3d mat2transform3d(HMM_Mat4 mat);
HMM_Mat4 transform3d2mat(transform3d t);
HMM_Mat4 m4_t(transform3d t);
HMM_Mat4 m4_s(transform3d t);
HMM_Mat4 m4_r(transform3d t);
HMM_Mat4 m4_rt(transform3d t);
HMM_Mat4 m4_st(transform3d t);
HMM_Mat4 m4_rst(transform3d t);
typedef struct gameobject {
cpBodyType bodytype;
int next;
@ -61,6 +76,7 @@ void gameobject_set_sensor(int id, int sensor);
HMM_Vec2 go2pos(struct gameobject *go);
float go2angle(struct gameobject *go);
transform2d go2t(gameobject *go);
transform3d go2t3(gameobject *go);
HMM_Vec2 go2world(struct gameobject *go, HMM_Vec2 pos);
HMM_Vec2 world2go(struct gameobject *go, HMM_Vec2 pos);

View file

@ -497,7 +497,7 @@ void full_2d_pass(struct window *window)
pos.x - zoom * window->rwidth / 2,
pos.x + zoom * window->rwidth / 2,
pos.y - zoom * window->rheight / 2,
pos.y + zoom * window->rheight / 2, -1.f, 1.f);
pos.y + zoom * window->rheight / 2, -1000.f, 1000.f);
hudproj = HMM_Orthographic_LH_ZO(0, window->rwidth, 0, window->rheight, -1.f, 1.f);
@ -528,8 +528,8 @@ void full_3d_pass(struct window *window)
model = HMM_MulM4(model, HMM_Scale((HMM_Vec3){scale,scale,scale}));
// Shadow pass
sg_begin_pass(sg_shadow.pass, &sg_shadow.pass_action);
sg_apply_pipeline(sg_shadow.pipe);
// sg_begin_pass(sg_shadow.pass, &sg_shadow.pass_action);
// sg_apply_pipeline(sg_shadow.pipe);
HMM_Mat4 light_proj = HMM_Orthographic_RH_ZO(-100.f, 100.f, -100.f, 100.f, 1.f, 100.f);
HMM_Mat4 light_view = HMM_LookAt_RH(dirl_pos, (HMM_Vec3){0,0,0}, (HMM_Vec3){0,1,0});