Fix transform issues

This commit is contained in:
John Alanbrook 2024-05-15 07:54:19 -05:00
parent f8db84538f
commit f1813a046a
10 changed files with 41 additions and 275 deletions

View file

@ -1,201 +0,0 @@
#include "light.h"
#include <stdbool.h>
/*
void Light::serialize(FILE * file)
{
GameObject::serialize(file);
SerializeFloat(file, &strength);
SerializeVec3(file, (float *) &color);
SerializeBool(file, &dynamic);
}
void Light::deserialize(FILE * file)
{
GameObject::deserialize(file);
DeserializeFloat(file, &strength);
DeserializeVec3(file, (float *) &color);
DeserializeBool(file, &dynamic);
}
static const mfloat_t dlight_init_rot[3] = { 80.f, 120.f, 165.f };
struct mDirectionalLight *dLight = NULL;
struct mDirectionalLight *MakeDLight()
{
if (dLight != NULL) {
dLight =
(struct mDirectionalLight *)
malloc(sizeof(struct mDirectionalLight));
quat_from_euler(dLight->light.obj.transform.rotation,
dlight_init_rot);
return dLight;
}
return dLight;
}
void dlight_prepshader(struct mDirectionalLight *light,
struct shader *shader)
{
mfloat_t fwd[3] = { 0.f };
trans_forward(fwd, &light->light.obj.transform);
shader_setvec3(shader, "dirLight.direction", fwd);
shader_setvec3(shader, "dirLight.color", light->light.color);
shader_setfloat(shader, "dirLight.strength", light->light.strength);
}
static struct mPointLight *pointLights[4];
static int numLights = 0;
struct mPointLight *MakePointlight()
{
if (numLights < 4) {
struct mPointLight *light =
(struct mPointLight *) malloc(sizeof(struct mPointLight));
pointLights[numLights++] = light;
light->light.strength = 0.2f;
light->constant = 1.f;
light->linear = 0.9f;
light->quadratic = 0.032f;
return light;
}
return NULL;
}
static void prepstring(char *buffer, char *prepend, const char *append)
{
snprintf(buffer, 100, "%s%s", prepend, append);
}
void pointlights_prepshader(struct shader *shader)
{
for (int i = 0; i < numLights; i++)
pointlight_prepshader(pointLights[i], shader, i);
}
void pointlight_prepshader(struct mPointLight *light,
struct shader *shader, int num)
{
shader_use(shader);
char prepend[100] = { '\0' };
snprintf(prepend, 100, "%s%d%s", "pointLights[", num, "].");
char str[100] = { '\0' };
prepstring(str, prepend, "position");
shader_setvec3(shader, str, light->light.obj.transform.position);
prepstring(str, prepend, "constant");
shader_setfloat(shader, str, light->constant);
prepstring(str, prepend, "linear");
shader_setfloat(shader, str, light->linear);
prepstring(str, prepend, "quadratic");
shader_setfloat(shader, str, light->quadratic);
prepstring(str, prepend, "strength");
shader_setfloat(shader, str, light->light.strength);
prepstring(str, prepend, "color");
shader_setvec3(shader, str, light->light.color);
}
static struct mSpotLight *spotLights[4];
static int numSpots = 0;
struct mSpotLight *MakeSpotlight()
{
if (numSpots < 4) {
struct mSpotLight *light =
(struct mSpotLight *) malloc(sizeof(struct mSpotLight));
spotLights[numSpots++] = light;
return light;
}
return NULL;
}
void spotlights_prepshader(struct shader *shader)
{
for (int i = 0; i < numSpots; i++)
spotlight_prepshader(spotLights[i], shader, i);
}
void spotlight_prepshader(struct mSpotLight *light, struct shader *shader,
int num)
{
mfloat_t fwd[3] = { 0.f };
trans_forward(fwd, &light->light.obj.transform);
shader_use(shader);
shader_setvec3(shader, "spotLight.position",
light->light.obj.transform.position);
shader_setvec3(shader, "spotLight.direction", fwd);
shader_setvec3(shader, "spotLight.color", light->light.color);
shader_setfloat(shader, "spotLight.strength", light->light.strength);
shader_setfloat(shader, "spotLight.cutoff", light->cutoff);
shader_setfloat(shader, "spotLight.distance", light->distance);
shader_setfloat(shader, "spotLight.outerCutoff", light->outerCutoff);
shader_setfloat(shader, "spotLight.linear", light->linear);
shader_setfloat(shader, "spotLight.quadratic", light->quadratic);
shader_setfloat(shader, "spotLight.constant", light->constant);
}
*/
/*
void light_gui(struct mLight *light)
{
object_gui(&light->obj);
if (nk_tree_push(ctx, NK_TREE_NODE, "Light", NK_MINIMIZED)) {
nk_property_float(ctx, "Strength", 0.f, &light->strength, 1.f, 0.01f, 0.001f);
// ImGui::ColorEdit3("Color", &light->color[0]);
nk_checkbox_label(ctx, "Dynamic", (bool *) &light->dynamic);
nk_tree_pop(ctx);
}
}
void pointlight_gui(struct mPointLight *light)
{
light_gui(&light->light);
if (nk_tree_push(ctx, NK_TREE_NODE, "Point Light", NK_MINIMIZED)) {
nk_property_float(ctx, "Constant", 0.f, &light->constant, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Linear", 0.f, &light->linear, 0.3f, 0.01f, 0.001f);
nk_property_float(ctx, "Quadratic", 0.f, &light->quadratic, 0.3f, 0.01f, 0.001f);
nk_tree_pop(ctx);
}
}
void spotlight_gui(struct mSpotLight *spot)
{
light_gui(&spot->light);
if (nk_tree_push(ctx, NK_TREE_NODE, "Spotlight", NK_MINIMIZED)) {
nk_property_float(ctx, "Linear", 0.f, &spot->linear, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Quadratic", 0.f, &spot->quadratic, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Distance", 0.f, &spot->distance, 200.f, 1.f, 0.1f, 200.f);
nk_property_float(ctx, "Cutoff Degrees", 0.f, &spot->cutoff, 0.7f, 0.01f, 0.001f);
nk_property_float(ctx, "Outer Cutoff Degrees", 0.f, &spot->outerCutoff, 0.7f, 0.01f, 0.001f);
nk_tree_pop(ctx);
}
}
*/

View file

@ -1,63 +0,0 @@
#ifndef LIGHT_H
#define LIGHT_H
#include <stdint.h>
/*
struct mLight {
struct gameobject *go;
uint8_t color[3];
float strength;
int dynamic;
int on;
};
struct mPointLight {
struct mLight light;
float constant;
float linear;
float quadratic;
};
struct mPointLight *MakePointlight();
void pointlight_prepshader(struct mPointLight *light,
struct shader *shader, int num);
void pointlights_prepshader(struct shader *shader);
struct mSpotLight {
struct mLight light;
float constant;
float linear;
float quadratic;
float distance;
float cutoff;
float outerCutoff;
};
struct mSpotLight *MakeSpotlight();
void spotlight_gui(struct mSpotLight *light);
void spotlight_prepshader(struct mSpotLight *light, struct shader *shader,
int num);
void spotlights_prepshader(struct shader *shader);
struct mDirectionalLight {
struct mLight light;
};
void dlight_prepshader(struct mDirectionalLight *light,
struct shader *shader);
struct mDirectionalLight *MakeDLight();
extern struct mDirectionalLight *dLight;
void light_gui(struct mLight *light);
void pointlight_gui(struct mPointLight *light);
void spotlight_gui(struct mSpotLight *spot);
*/
#endif

View file

@ -1229,6 +1229,18 @@ HMM_Mat4 HMM_InvOrthographic(HMM_Mat4 OrthoMatrix) {
return Result; return Result;
} }
HMM_Mat4 HMM_Orthographic_Metal(float l, float r, float b, float t, float near, float far)
{
HMM_Mat4 adjust = {0};
adjust.e[0][0] = 1;
adjust.e[1][1] = 1;
adjust.e[2][2] = 0.5;
adjust.e[3][2] = 0.5;
adjust.e[3][3] = 1;
HMM_Mat4 opengl = HMM_Orthographic_LH_ZO(l, r, b, t, near, far);
return HMM_MulM4(opengl, adjust);
}
HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRatio, float Near, float Far) HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRatio, float Near, float Far)
{ {
HMM_Mat4 adjust = {0}; HMM_Mat4 adjust = {0};
@ -1239,6 +1251,7 @@ HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRatio, float Near, float F
adjust.e[3][3] = 1; adjust.e[3][3] = 1;
HMM_Mat4 opengl = HMM_Perspective_LH_NO(FOV, AspectRatio, Near, Far); HMM_Mat4 opengl = HMM_Perspective_LH_NO(FOV, AspectRatio, Near, Far);
return HMM_MulM4(opengl,adjust); return HMM_MulM4(opengl,adjust);
} }
HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float Far) { HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float Far) {

View file

@ -617,6 +617,7 @@ HMM_Mat4 HMM_Perspective_LH_NO(float FOV, float AspectRatio, float Near, float F
HMM_Mat4 HMM_Perspective_LH_ZO(float FOV, float AspectRatio, float Near, float Far); HMM_Mat4 HMM_Perspective_LH_ZO(float FOV, float AspectRatio, float Near, float Far);
HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRation, float Near, float Far); HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRation, float Near, float Far);
HMM_Mat4 HMM_Orthographic_Metal(float l, float r, float b, float t, float near, float far);
HMM_Mat4 HMM_InvPerspective_RH(HMM_Mat4 PerspectiveMatrix); HMM_Mat4 HMM_InvPerspective_RH(HMM_Mat4 PerspectiveMatrix);
HMM_Mat4 HMM_InvPerspective_LH(HMM_Mat4 PerspectiveMatrix); HMM_Mat4 HMM_InvPerspective_LH(HMM_Mat4 PerspectiveMatrix);
HMM_Mat4 HMM_Translate(HMM_Vec3 Translation); HMM_Mat4 HMM_Translate(HMM_Vec3 Translation);

View file

@ -715,11 +715,13 @@ JSC_CCALL(render_set_camera,
float fov = js2number(js_getpropstr(cam, "fov"))*HMM_DegToRad; float fov = js2number(js_getpropstr(cam, "fov"))*HMM_DegToRad;
transform *t = js2transform(js_getpropstr(cam, "transform")); transform *t = js2transform(js_getpropstr(cam, "transform"));
globalview.v = transform2mat(*t); //globalview.v = transform2mat(*t);
HMM_Vec3 look = HMM_AddV3(t->pos, transform_direction(t, vFWD));
globalview.v = HMM_LookAt_LH(t->pos, look, vUP);
HMM_Vec2 size = mainwin.mode == MODE_FULL ? mainwin.size : mainwin.rendersize; HMM_Vec2 size = mainwin.mode == MODE_FULL ? mainwin.size : mainwin.rendersize;
if (ortho) if (ortho)
globalview.p = HMM_Orthographic_LH_ZO( globalview.p = HMM_Orthographic_Metal(
-size.x/2, -size.x/2,
size.x/2, size.x/2,
-size.y/2, -size.y/2,
@ -1399,6 +1401,7 @@ static const JSCFunctionListEntry js_emitter_funcs[] = {
JSC_GETSET(transform, pos, vec3) JSC_GETSET(transform, pos, vec3)
JSC_GETSET(transform, scale, vec3) JSC_GETSET(transform, scale, vec3)
JSC_GETSET(transform, rotation, quat) JSC_GETSET(transform, rotation, quat)
JSC_CCALL(transform_move, transform_move(js2transform(this), js2vec3(argv[0])); )
JSC_CCALL(transform_lookat, JSC_CCALL(transform_lookat,
HMM_Vec3 point = js2vec3(argv[0]); HMM_Vec3 point = js2vec3(argv[0]);
transform *go = js2transform(this); transform *go = js2transform(this);
@ -1410,15 +1413,22 @@ JSC_CCALL(transform_rotate,
HMM_Vec3 axis = js2vec3(argv[0]); HMM_Vec3 axis = js2vec3(argv[0]);
transform *t = js2transform(this); transform *t = js2transform(this);
HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2number(argv[1])); HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2number(argv[1]));
t->rotation = HMM_MulQ(rot, t->rotation); t->rotation = HMM_MulQ(t->rotation,rot);
)
JSC_CCALL(transform_direction,
transform *t = js2transform(this);
return vec32js(HMM_QVRot(js2vec3(argv[0]), t->rotation));
) )
static const JSCFunctionListEntry js_transform_funcs[] = { static const JSCFunctionListEntry js_transform_funcs[] = {
CGETSET_ADD(transform, pos), CGETSET_ADD(transform, pos),
CGETSET_ADD(transform, scale), CGETSET_ADD(transform, scale),
CGETSET_ADD(transform, rotation), CGETSET_ADD(transform, rotation),
MIST_FUNC_DEF(transform, move, 1),
MIST_FUNC_DEF(transform, rotate, 2), MIST_FUNC_DEF(transform, rotate, 2),
MIST_FUNC_DEF(transform, lookat, 1) MIST_FUNC_DEF(transform, lookat, 1),
MIST_FUNC_DEF(transform, direction, 1)
}; };
JSC_GETSET(dsp_node, pass, boolean) JSC_GETSET(dsp_node, pass, boolean)

View file

@ -45,7 +45,7 @@ int emitter_spawn(emitter *e, transform *t)
particle p = {0}; particle p = {0};
p.life = e->life; p.life = e->life;
p.pos = (HMM_Vec4){t->pos.x,t->pos.y,t->pos.z,0}; p.pos = (HMM_Vec4){t->pos.x,t->pos.y,t->pos.z,0};
HMM_Vec3 up = trans_forward(t); HMM_Vec3 up = transform_direction(t, vFWD);
float newan = (frand(e->divergence)-(e->divergence/2))*HMM_TurnToRad; float newan = (frand(e->divergence)-(e->divergence/2))*HMM_TurnToRad;
HMM_Vec2 v2n = HMM_V2Rotate((HMM_Vec2){0,1}, newan); HMM_Vec2 v2n = HMM_V2Rotate((HMM_Vec2){0,1}, newan);
HMM_Vec3 norm = (HMM_Vec3){v2n.x, v2n.y,0}; HMM_Vec3 norm = (HMM_Vec3){v2n.x, v2n.y,0};

View file

@ -12,6 +12,16 @@ transform *make_transform()
void transform_free(transform *t) { free(t); } void transform_free(transform *t) { free(t); }
void transform_move(transform *t, HMM_Vec3 v)
{
t->pos = HMM_AddV3(t->pos, v);
}
HMM_Vec3 transform_direction(transform *t, HMM_Vec3 dir)
{
return HMM_QVRot(dir, t->rotation);
}
HMM_Vec3 trans_forward(const transform *const trans) { return HMM_QVRot(vFWD, trans->rotation); } HMM_Vec3 trans_forward(const transform *const trans) { return HMM_QVRot(vFWD, trans->rotation); }
HMM_Vec3 trans_back(const transform *trans) { return HMM_QVRot(vBKWD, trans->rotation); } HMM_Vec3 trans_back(const transform *trans) { return HMM_QVRot(vBKWD, trans->rotation); }
HMM_Vec3 trans_up(const transform *trans) { return HMM_QVRot(vUP, trans->rotation); } HMM_Vec3 trans_up(const transform *trans) { return HMM_QVRot(vUP, trans->rotation); }

View file

@ -17,12 +17,8 @@ void transform_free(transform *t);
#define VEC2_FMT "[%g,%g]" #define VEC2_FMT "[%g,%g]"
#define VEC2_MEMS(s) (s).x, (s).y #define VEC2_MEMS(s) (s).x, (s).y
HMM_Vec3 trans_forward(const transform *const trans); void transform_move(transform *t, HMM_Vec3 v);
HMM_Vec3 trans_back(const transform *trans); HMM_Vec3 transform_direction(transform *t, HMM_Vec3 dir);
HMM_Vec3 trans_up(const transform *trans);
HMM_Vec3 trans_down(const transform *trans);
HMM_Vec3 trans_right(const transform *trans);
HMM_Vec3 trans_left(const transform *trans);
/* Transform a position via the matrix */ /* Transform a position via the matrix */
HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos); HMM_Vec2 mat_t_pos(HMM_Mat3 m, HMM_Vec2 pos);