add transform cache mat4
This commit is contained in:
parent
f905dbc571
commit
08725d474d
|
@ -1525,6 +1525,9 @@ Vector.random = function() {
|
||||||
return Vector.norm(vec);
|
return Vector.norm(vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector.midpoint = function(a,b) { return [(a.x+b.x)/2, (a.y+b.y)/2]; }
|
||||||
|
Vector.distance = function(a,b) { return Math.hypot(b.x-a.x, b.y-a.y); }
|
||||||
|
|
||||||
Vector.angle_between = function(a,b)
|
Vector.angle_between = function(a,b)
|
||||||
{
|
{
|
||||||
var dot = Vector.dot(a,b);
|
var dot = Vector.dot(a,b);
|
||||||
|
|
|
@ -541,11 +541,9 @@ render.flush_poly = function()
|
||||||
|
|
||||||
render.line = function(points, color = Color.white, thickness = 1) {
|
render.line = function(points, color = Color.white, thickness = 1) {
|
||||||
var transform = os.make_transform();
|
var transform = os.make_transform();
|
||||||
var vv = points[1].sub(points[0]);
|
var dist = Vector.distance(points[0],points[1]);
|
||||||
var dist = Vector.length(vv);
|
transform.move(Vector.midpoint(points[0],points[1]));
|
||||||
var center = vv.scale(0.5).add(points[0]);
|
transform.rotate([0,0,-1], Vector.angle([points[1].x-points[0].x, points[1].y-points[0].y]));
|
||||||
transform.move([center.x,center.y,0]);
|
|
||||||
transform.rotate([0,0,-1], Vector.angle(vv));
|
|
||||||
transform.scale = [dist, thickness, 1];
|
transform.scale = [dist, thickness, 1];
|
||||||
poly_cache.push({
|
poly_cache.push({
|
||||||
transform:transform,
|
transform:transform,
|
||||||
|
@ -598,17 +596,26 @@ render.boundingbox = function(bb, color = Color.white) {
|
||||||
}
|
}
|
||||||
|
|
||||||
render.rectangle = function(lowerleft, upperright, color) {
|
render.rectangle = function(lowerleft, upperright, color) {
|
||||||
var thickness = upperright.x-lowerleft.x;
|
var transform = os.make_transform();
|
||||||
var mid = thickness/2;
|
var wh = [upperright.x-lowerleft.x, upperright.y-lowerleft.y];
|
||||||
var from = [mid+lowerleft.x, lowerleft.y];
|
transform.move(Vector.midpoint(lowerleft,upperright));
|
||||||
var to = [mid+lowerleft.x, upperright.y];
|
transform.scale = [wh.x,wh.y,1];
|
||||||
render.line([from,to], color, thickness);
|
poly_cache.push({
|
||||||
|
transform:transform,
|
||||||
|
color:color
|
||||||
|
});
|
||||||
|
check_flush(render.flush_poly);
|
||||||
};
|
};
|
||||||
|
|
||||||
render.box = function(pos, wh, color = Color.white) {
|
render.box = function(pos, wh, color = Color.white) {
|
||||||
var lower = pos.sub(wh.scale(0.5));
|
var transform = os.make_transform();
|
||||||
var upper = pos.add(wh.scale(0.5));
|
transform.move(pos);
|
||||||
render.rectangle(lower,upper,color);
|
transform.scale = [wh.x,wh.y,1];
|
||||||
|
poly_cache.push({
|
||||||
|
transform:transform,
|
||||||
|
color:color
|
||||||
|
});
|
||||||
|
check_flush(render.flush_poly);
|
||||||
};
|
};
|
||||||
|
|
||||||
render.window = function(pos, wh, color) {
|
render.window = function(pos, wh, color) {
|
||||||
|
|
|
@ -1769,11 +1769,6 @@ HMM_Mat4 HMM_QToM4(HMM_Quat Left) {
|
||||||
|
|
||||||
HMM_Mat4 HMM_M4TRS(HMM_Vec3 t, HMM_Quat q, HMM_Vec3 s)
|
HMM_Mat4 HMM_M4TRS(HMM_Vec3 t, HMM_Quat q, HMM_Vec3 s)
|
||||||
{
|
{
|
||||||
HMM_Mat4 T = HMM_Translate(t);
|
|
||||||
HMM_Mat4 R = HMM_QToM4(q);
|
|
||||||
HMM_Mat4 S = HMM_Scale(s);
|
|
||||||
return HMM_MulM4(T, HMM_MulM4(R, S));
|
|
||||||
//return HMM_MulM4(T,S);
|
|
||||||
HMM_Mat4 l;
|
HMM_Mat4 l;
|
||||||
float *lm = (float*)&l;
|
float *lm = (float*)&l;
|
||||||
|
|
||||||
|
|
|
@ -920,15 +920,15 @@ JSC_CCALL(render_setunim4,
|
||||||
JSValue arr = argv[2];
|
JSValue arr = argv[2];
|
||||||
int n = js_arrlen(arr);
|
int n = js_arrlen(arr);
|
||||||
if (n == 1)
|
if (n == 1)
|
||||||
m = transform2mat(*js2transform(js_getpropidx(arr,0)));
|
m = transform2mat(js2transform(js_getpropidx(arr,0)));
|
||||||
else {
|
else {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
HMM_Mat4 p = transform2mat(*js2transform(js_getpropidx(arr, i)));
|
HMM_Mat4 p = transform2mat(js2transform(js_getpropidx(arr, i)));
|
||||||
m = HMM_MulM4(p,m);
|
m = HMM_MulM4(p,m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (!JS_IsUndefined(argv[2]))
|
} else if (!JS_IsUndefined(argv[2]))
|
||||||
m = transform2mat(*js2transform(argv[2]));
|
m = transform2mat(js2transform(argv[2]));
|
||||||
|
|
||||||
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(m.e));
|
sg_apply_uniforms(js2number(argv[0]), js2number(argv[1]), SG_RANGE_REF(m.e));
|
||||||
);
|
);
|
||||||
|
@ -963,7 +963,7 @@ JSC_CCALL(render_make_particle_ssbo,
|
||||||
|
|
||||||
for (int i = 0; i < js_arrlen(array); i++) {
|
for (int i = 0; i < js_arrlen(array); i++) {
|
||||||
JSValue sub = js_getpropidx(array,i);
|
JSValue sub = js_getpropidx(array,i);
|
||||||
ms[i].model = transform2mat(*js2transform(js_getpropstr(sub, "transform")));
|
ms[i].model = transform2mat(js2transform(js_getpropstr(sub, "transform")));
|
||||||
ms[i].color = js2vec4(js_getpropstr(sub,"color"));
|
ms[i].color = js2vec4(js_getpropstr(sub,"color"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -998,7 +998,7 @@ JSC_CCALL(render_make_sprite_ssbo,
|
||||||
|
|
||||||
for (int i = 0; i < js_arrlen(array); i++) {
|
for (int i = 0; i < js_arrlen(array); i++) {
|
||||||
JSValue sub = js_getpropidx(array,i);
|
JSValue sub = js_getpropidx(array,i);
|
||||||
ms[i].model = transform2mat(*js2transform(js_getpropstr(sub, "transform")));
|
ms[i].model = transform2mat(js2transform(js_getpropstr(sub, "transform")));
|
||||||
ms[i].rect = js2vec4(js_getpropstr(sub,"rect"));
|
ms[i].rect = js2vec4(js_getpropstr(sub,"rect"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1027,7 +1027,7 @@ JSC_CCALL(render_make_t_ssbo,
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < js_arrlen(array); i++)
|
for (int i = 0; i < js_arrlen(array); i++)
|
||||||
ms[i] = transform2mat(*js2transform(js_getpropidx(array, i)));
|
ms[i] = transform2mat(js2transform(js_getpropidx(array, i)));
|
||||||
|
|
||||||
sg_append_buffer(*b, (&(sg_range){
|
sg_append_buffer(*b, (&(sg_range){
|
||||||
.ptr = ms,
|
.ptr = ms,
|
||||||
|
@ -1533,9 +1533,9 @@ static const JSCFunctionListEntry js_physics_funcs[] = {
|
||||||
CGETSET_ADD(physics, collision_persistence),
|
CGETSET_ADD(physics, collision_persistence),
|
||||||
};
|
};
|
||||||
|
|
||||||
JSC_GETSET(transform, pos, vec3)
|
JSC_GETSET_APPLY(transform, pos, vec3)
|
||||||
JSC_GETSET(transform, scale, vec3)
|
JSC_GETSET_APPLY(transform, scale, vec3)
|
||||||
JSC_GETSET(transform, rotation, quat)
|
JSC_GETSET_APPLY(transform, rotation, quat)
|
||||||
JSC_CCALL(transform_move, transform_move(js2transform(self), js2vec3(argv[0])); )
|
JSC_CCALL(transform_move, transform_move(js2transform(self), js2vec3(argv[0])); )
|
||||||
|
|
||||||
JSC_CCALL(transform_lookat,
|
JSC_CCALL(transform_lookat,
|
||||||
|
@ -1543,6 +1543,7 @@ JSC_CCALL(transform_lookat,
|
||||||
transform *go = js2transform(self);
|
transform *go = js2transform(self);
|
||||||
HMM_Mat4 m = HMM_LookAt_LH(go->pos, point, vUP);
|
HMM_Mat4 m = HMM_LookAt_LH(go->pos, point, vUP);
|
||||||
go->rotation = HMM_M4ToQ_LH(m);
|
go->rotation = HMM_M4ToQ_LH(m);
|
||||||
|
go->dirty = true;
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(transform_rotate,
|
JSC_CCALL(transform_rotate,
|
||||||
|
@ -1550,6 +1551,7 @@ JSC_CCALL(transform_rotate,
|
||||||
transform *t = js2transform(self);
|
transform *t = js2transform(self);
|
||||||
HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2angle(argv[1]));
|
HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2angle(argv[1]));
|
||||||
t->rotation = HMM_MulQ(t->rotation,rot);
|
t->rotation = HMM_MulQ(t->rotation,rot);
|
||||||
|
t->dirty = true;
|
||||||
)
|
)
|
||||||
|
|
||||||
JSC_CCALL(transform_angle,
|
JSC_CCALL(transform_angle,
|
||||||
|
|
|
@ -508,7 +508,7 @@ sg_bindings primitive_bind(primitive *p)
|
||||||
|
|
||||||
void model_draw_go(model *model, transform *go)
|
void model_draw_go(model *model, transform *go)
|
||||||
{
|
{
|
||||||
HMM_Mat4 gom = transform2mat(*go);
|
HMM_Mat4 gom = transform2mat(go);
|
||||||
|
|
||||||
animation_run(&model->anim, apptime());
|
animation_run(&model->anim, apptime());
|
||||||
|
|
||||||
|
|
|
@ -5,16 +5,21 @@
|
||||||
transform *make_transform()
|
transform *make_transform()
|
||||||
{
|
{
|
||||||
transform *t = calloc(sizeof(transform),1);
|
transform *t = calloc(sizeof(transform),1);
|
||||||
|
|
||||||
t->scale = (HMM_Vec3){1,1,1};
|
t->scale = (HMM_Vec3){1,1,1};
|
||||||
t->rotation = (HMM_Quat){0,0,0,1};
|
t->rotation = (HMM_Quat){0,0,0,1};
|
||||||
|
t->dirty = 1;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
void transform_free(transform *t) { free(t); }
|
void transform_free(transform *t) { free(t); }
|
||||||
|
|
||||||
|
void transform_apply(transform *t) { t->dirty = 1; }
|
||||||
|
|
||||||
void transform_move(transform *t, HMM_Vec3 v)
|
void transform_move(transform *t, HMM_Vec3 v)
|
||||||
{
|
{
|
||||||
t->pos = HMM_AddV3(t->pos, v);
|
t->pos = HMM_AddV3(t->pos, v);
|
||||||
|
t->dirty = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
HMM_Vec3 transform_direction(transform *t, HMM_Vec3 dir)
|
HMM_Vec3 transform_direction(transform *t, HMM_Vec3 dir)
|
||||||
|
@ -42,7 +47,6 @@ HMM_Vec2 mat_right(HMM_Mat3 m) { return HMM_NormV2(m.Columns[0].XY); }
|
||||||
float vec_angle(HMM_Vec2 a, HMM_Vec2 b) { return acos(HMM_DotV2(a,b)/(HMM_LenV2(a)*HMM_LenV2(b))); }
|
float vec_angle(HMM_Vec2 a, HMM_Vec2 b) { return acos(HMM_DotV2(a,b)/(HMM_LenV2(a)*HMM_LenV2(b))); }
|
||||||
float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b) { return atan2(b.x, b.y) - atan2(a.x, a.y); }
|
float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b) { return atan2(b.x, b.y) - atan2(a.x, a.y); }
|
||||||
|
|
||||||
|
|
||||||
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) { return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ; }
|
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos) { return HMM_MulM4V4(m, (HMM_Vec4){pos.X, pos.Y, pos.Z, 1}).XYZ; }
|
||||||
|
|
||||||
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir)
|
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir)
|
||||||
|
@ -51,8 +55,13 @@ HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir)
|
||||||
return mat3_t_pos(m, dir);
|
return mat3_t_pos(m, dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
HMM_Mat4 transform2mat(transform t) {
|
HMM_Mat4 transform2mat(transform *t) {
|
||||||
return HMM_M4TRS(t.pos, t.rotation, t.scale);
|
if (t->dirty) {
|
||||||
|
t->cache = HMM_M4TRS(t->pos, t->rotation, t->scale);
|
||||||
|
t->dirty = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return t->cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
HMM_Quat angle2rotation(float angle)
|
HMM_Quat angle2rotation(float angle)
|
||||||
|
|
|
@ -12,6 +12,7 @@ typedef struct transform {
|
||||||
} transform;
|
} transform;
|
||||||
|
|
||||||
transform *make_transform();
|
transform *make_transform();
|
||||||
|
void transform_apply(transform *t);
|
||||||
void transform_free(transform *t);
|
void transform_free(transform *t);
|
||||||
|
|
||||||
#define VEC2_FMT "[%g,%g]"
|
#define VEC2_FMT "[%g,%g]"
|
||||||
|
@ -33,7 +34,7 @@ float vec_dirangle(HMM_Vec2 a, HMM_Vec2 b);
|
||||||
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos);
|
HMM_Vec3 mat3_t_pos(HMM_Mat4 m, HMM_Vec3 pos);
|
||||||
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir);
|
HMM_Vec3 mat3_t_dir(HMM_Mat4 m, HMM_Vec3 dir);
|
||||||
|
|
||||||
HMM_Mat4 transform2mat(transform t);
|
HMM_Mat4 transform2mat(transform *t);
|
||||||
transform mat2transform(HMM_Mat4 m);
|
transform mat2transform(HMM_Mat4 m);
|
||||||
|
|
||||||
HMM_Quat angle2rotation(float angle);
|
HMM_Quat angle2rotation(float angle);
|
||||||
|
|
Loading…
Reference in a new issue