diff --git a/source/engine/3d/model.c b/source/engine/3d/model.c index d84caeb..c0678c2 100644 --- a/source/engine/3d/model.c +++ b/source/engine/3d/model.c @@ -127,6 +127,7 @@ uint32_t pack_int10_n2(float *norm) sg_buffer normal_floats(float *f, int n) { + return float_buffer(f, n); uint32_t packed_norms[n/3]; for (int v = 0, i = 0; v < n/3; v++, i+= 3) packed_norms[v] = pack_int10_n2(f+i); @@ -180,13 +181,14 @@ HMM_Vec3 index_to_vert(uint32_t idx, float *f) void primitive_gen_indices(primitive *prim) { - if (prim->idx_count == 0) return; + if (prim->idx_count != 0) return; + uint16_t *idxs = malloc(sizeof(*idxs)*prim->idx_count); for (int z = 0; z < prim->idx_count; z++) idxs[z] = z; - prim->idx = sg_make_buffer(&(sg_buffer_desc){ + prim->index = sg_make_buffer(&(sg_buffer_desc){ .data.ptr = idxs, .data.size = sizeof(uint16_t) * prim->idx_count, .type = SG_BUFFERTYPE_INDEXBUFFER}); @@ -207,7 +209,7 @@ struct primitive mesh_add_primitive(cgltf_primitive *prim) for (int i = 0; i < n; i++) idxs[i] = fidx[i]; - retp.idx = sg_make_buffer(&(sg_buffer_desc){ + retp.index = sg_make_buffer(&(sg_buffer_desc){ .data.ptr = idxs, .data.size = sizeof(*idxs) * n, .type = SG_BUFFERTYPE_INDEXBUFFER, @@ -498,7 +500,7 @@ sg_bindings primitive_bind(primitive *p) b.vertex_buffers[MAT_BONE] = p->bone; b.vertex_buffers[MAT_WEIGHT] = p->weight; b.vertex_buffers[MAT_COLOR] = p->color; - b.index_buffer = p->idx; + b.index_buffer = p->index; b.fs.images[0] = p->mat->diffuse->id; b.fs.samplers[0] = tex_sampler; return b; @@ -606,7 +608,7 @@ sg_bindings primitive_bindings(primitive *p, JSValue v) b.vertex_buffers[slot] = buf; } - b.index_buffer = p->idx; + b.index_buffer = p->index; return b; } diff --git a/source/engine/3d/model.h b/source/engine/3d/model.h index 2ca814c..16e349c 100644 --- a/source/engine/3d/model.h +++ b/source/engine/3d/model.h @@ -43,7 +43,7 @@ typedef struct primitive { sg_buffer bone; sg_buffer weight; sg_buffer color; - sg_buffer idx; + sg_buffer index; material *mat; uint32_t idx_count; } primitive; diff --git a/source/engine/HandmadeMath.c b/source/engine/HandmadeMath.c index 9e87c3c..6f5ec09 100644 --- a/source/engine/HandmadeMath.c +++ b/source/engine/HandmadeMath.c @@ -1229,6 +1229,18 @@ HMM_Mat4 HMM_InvOrthographic(HMM_Mat4 OrthoMatrix) { return Result; } +HMM_Mat4 HMM_Perspective_Metal(float FOV, float AspectRatio, 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_Perspective_LH_NO(FOV, AspectRatio, Near, Far); + return HMM_MulM4(opengl,adjust); +} + HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float Far) { HMM_Mat4 Result = {0}; diff --git a/source/engine/HandmadeMath.h b/source/engine/HandmadeMath.h index 1194220..2e2d0d0 100644 --- a/source/engine/HandmadeMath.h +++ b/source/engine/HandmadeMath.h @@ -615,6 +615,8 @@ HMM_Mat4 HMM_Perspective_RH_NO(float FOV, float AspectRatio, float Near, float F HMM_Mat4 HMM_Perspective_RH_ZO(float FOV, float AspectRatio, float Near, float Far); HMM_Mat4 HMM_Perspective_LH_NO(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_InvPerspective_RH(HMM_Mat4 PerspectiveMatrix); HMM_Mat4 HMM_InvPerspective_LH(HMM_Mat4 PerspectiveMatrix); HMM_Mat4 HMM_Translate(HMM_Vec3 Translation); diff --git a/source/engine/jsffi.c b/source/engine/jsffi.c index 5a24e22..2c7023b 100644 --- a/source/engine/jsffi.c +++ b/source/engine/jsffi.c @@ -44,6 +44,8 @@ static JSValue globalThis; static JSClassID js_ptr_id; static JSClassDef js_ptr_class = { "POINTER" }; +static JSValue JSUNDEF; + JSValue str2js(const char *c, ...) { if (!c) return JS_UNDEFINED; char *result = NULL; @@ -81,7 +83,6 @@ QJSCLASS(material) QJSCLASS(model) QJSCLASS(window) QJSCLASS(constraint) -QJSCLASS(primitive) QJSCLASS(sg_buffer) QJSCLASS(datastream) @@ -666,7 +667,7 @@ JSC_CCALL(render_end_pass, .clear_value = (sg_color){0,0,0,1} } } - }); + }); sg_pipeline p = {0}; switch(mainwin.mode) { @@ -709,18 +710,16 @@ JSC_SCALL(render_text_size, ret = bb2js(text_bb(str, js2number(argv[1]), js2numb JSC_CCALL(render_set_camera, JSValue cam = argv[0]; int ortho = js2boolean(js_getpropstr(cam, "ortho")); - int near = js2number(js_getpropstr(cam, "near")); - int far = js2number(js_getpropstr(cam, "far")); - float fov = js2number(js_getpropstr(cam, "fov")); - HMM_Vec4 viewport = js2vec4(js_getpropstr(cam, "viewport")); + float near = js2number(js_getpropstr(cam, "near")); + float far = js2number(js_getpropstr(cam, "far")); + float fov = js2number(js_getpropstr(cam, "fov"))*HMM_DegToRad; transform *t = js2transform(js_getpropstr(cam, "transform")); globalview.v = transform2mat(*t); HMM_Vec2 size = mainwin.mode == MODE_FULL ? mainwin.size : mainwin.rendersize; - //sg_apply_viewportf(viewport.x*size.x, viewport.y*size.y, viewport.z*size.x, viewport.w*size.y,0); if (ortho) - globalview.p = HMM_Orthographic_RH_ZO( + globalview.p = HMM_Orthographic_LH_ZO( -size.x/2, size.x/2, -size.y/2, @@ -729,7 +728,7 @@ JSC_CCALL(render_set_camera, far ); else - globalview.p = HMM_Perspective_RH_NO(fov, size.x/size.x, near, far); + globalview.p = HMM_Perspective_Metal(fov, size.x/size.y, near, far); globalview.vp = HMM_MulM4(globalview.p, globalview.v); ) @@ -831,13 +830,17 @@ JSC_CCALL(render_pipeline, p.layout = js2layout(argv[0]); p.cull_mode = js2number(js_getpropstr(argv[0], "cull")); p.primitive_type = js2number(js_getpropstr(argv[0], "primitive")); - p.face_winding = SG_FACEWINDING_CCW; + //p.face_winding = js2number(js_getpropstr(argv[0], "face")); + p.face_winding = 1; p.index_type = SG_INDEXTYPE_UINT16; if (js2boolean(js_getpropstr(argv[0], "blend"))) p.colors[0].blend = blend_trans; - //p.depth.write_enabled = true; - //p.depth.compare = SG_COMPAREFUNC_LESS_EQUAL; + int depth = js2boolean(js_getpropstr(argv[0], "depth")); + if (depth) { + p.depth.write_enabled = true; + p.depth.compare = SG_COMPAREFUNC_LESS; + } sg_pipeline pipe = sg_make_pipeline(&p); @@ -924,7 +927,7 @@ JSC_CCALL(render_text_ssbo, static const JSCFunctionListEntry js_render_funcs[] = { MIST_FUNC_DEF(render, flushtext, 0), - MIST_FUNC_DEF(render, end_pass, 0), + MIST_FUNC_DEF(render, end_pass, 2), MIST_FUNC_DEF(render, text_size, 3), MIST_FUNC_DEF(render, text_ssbo, 0), MIST_FUNC_DEF(render, set_camera, 1), @@ -1399,15 +1402,15 @@ JSC_GETSET(transform, rotation, quat) JSC_CCALL(transform_lookat, HMM_Vec3 point = js2vec3(argv[0]); transform *go = js2transform(this); - HMM_Mat4 m = HMM_LookAt_RH(go->pos, point, vUP); - go->rotation = HMM_M4ToQ_RH(m); + HMM_Mat4 m = HMM_LookAt_LH(go->pos, point, vUP); + go->rotation = HMM_M4ToQ_LH(m); ) JSC_CCALL(transform_rotate, HMM_Vec3 axis = js2vec3(argv[0]); transform *t = js2transform(this); - HMM_Quat rot = HMM_QFromAxisAngle_RH(axis, js2number(argv[1])); - t->rotation = HMM_MulQ(t->rotation, rot); + HMM_Quat rot = HMM_QFromAxisAngle_LH(axis, js2number(argv[1])); + t->rotation = HMM_MulQ(rot, t->rotation); ) static const JSCFunctionListEntry js_transform_funcs[] = { @@ -1862,7 +1865,34 @@ JSC_CCALL(os_make_font, JSC_CCALL(os_make_transform, return transform2js(make_transform())) JSC_SCALL(os_system, return number2js(system(str)); ) -JSC_SCALL(os_make_model, ret = model2js(model_make(str))) + +#define PRIMCHECK(VAR, JS, VAL) \ +if (VAR->VAL.id) js_setpropstr(JS, #VAL, sg_buffer2js(&VAR->VAL)); \ + +JSValue primitive2js(primitive *p) +{ + JSValue v = JS_NewObject(js); + PRIMCHECK(p, v, pos) + PRIMCHECK(p,v,norm) + PRIMCHECK(p,v,uv) + PRIMCHECK(p,v,bone) + PRIMCHECK(p,v,weight) + PRIMCHECK(p,v,color) + PRIMCHECK(p,v,index) + js_setpropstr(v, "count", number2js(p->idx_count)); + + printf("new primitive with count %d\n", p->idx_count); + + return v; +} + +JSC_SCALL(os_make_model, + model *m = model_make(str); + if (arrlen(m->meshes) != 1) return JSUNDEF; + mesh *me = m->meshes+0; + + return primitive2js(me->primitives+0); +) JSC_CCALL(os_make_emitter, emitter *e = make_emitter(); ret = emitter2js(e); @@ -1965,7 +1995,7 @@ JSValue parmesh2js(par_shapes_mesh *m) if (m->normals) { sg_buffer *norm = malloc(sizeof(*norm)); - *norm = float_buffer(m->normals, 3*m->npoints); + *norm = normal_floats(m->normals, 3*m->npoints); js_setpropstr(obj, "norm", sg_buffer2js(norm)); } @@ -2065,6 +2095,7 @@ static const JSCFunctionListEntry js_os_funcs[] = { #include "steam.h" void ffi_load() { + JSUNDEF = JS_UNDEFINED; globalThis = JS_GetGlobalObject(js); QJSCLASSPREP(ptr); diff --git a/source/engine/particle.c b/source/engine/particle.c index 5672b57..51692b4 100644 --- a/source/engine/particle.c +++ b/source/engine/particle.c @@ -95,7 +95,6 @@ void emitter_draw(emitter *e) } void emitter_step(emitter *e, double dt, transform *t) { - HMM_Vec4 g_accel = HMM_MulV4F((HMM_Vec4){cpSpaceGetGravity(space).x, cpSpaceGetGravity(space).y, 0, 0}, dt); for (int i = 0; i < arrlen(e->particles); i++) { diff --git a/source/engine/render.c b/source/engine/render.c index ea59436..e581240 100644 --- a/source/engine/render.c +++ b/source/engine/render.c @@ -147,6 +147,9 @@ void render_init() { #endif font_init(); + + sg_features feat = sg_query_features(); + printf("top left? %d\n", feat.origin_top_left); sg_color c = (sg_color){0,0,0,1}; pass_action = (sg_pass_action){ @@ -155,7 +158,12 @@ void render_init() { sg_color oc = (sg_color){35.0/255,60.0/255,92.0/255,1}; off_action = (sg_pass_action){ - .colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = oc} + .colors[0] = {.load_action = SG_LOADACTION_CLEAR, .clear_value = oc}, + .depth = { + .load_action = SG_LOADACTION_CLEAR, + .clear_value = 1 + //.store_action = SG_STOREACTION_STORE + } }; screencolor = sg_make_image(&(sg_image_desc){ @@ -176,11 +184,10 @@ void render_init() { offscreen = (sg_pass){ .attachments = sg_make_attachments(&(sg_attachments_desc){ .colors[0].image = screencolor, - .depth_stencil.image = screendepth + .depth_stencil.image = screendepth, }), .action = off_action, }; - } HMM_Mat4 projection = {0.f}; @@ -219,6 +226,7 @@ void openglRender(struct window *window) { }; } lastuse = usesize; + sg_begin_pass(&offscreen); } diff --git a/source/engine/transform.c b/source/engine/transform.c index 698c5d0..de09a85 100644 --- a/source/engine/transform.c +++ b/source/engine/transform.c @@ -47,7 +47,7 @@ HMM_Mat4 transform2mat(transform t) { HMM_Quat angle2rotation(float angle) { - return HMM_QFromAxisAngle_RH(vUP, angle); + return HMM_QFromAxisAngle_LH(vUP, angle); } float transform2angle(HMM_Vec3 axis) @@ -63,6 +63,6 @@ transform mat2transform(HMM_Mat4 m) t.scale.Elements[i] = HMM_LenV3(m.Columns[i].xyz); // for (int i = 0; i < 2; i++) // m.Columns[i].xyz = HMM_MulV3(m.Columns[i].xyz, t.scale.Elements[i]); - t.rotation = HMM_M4ToQ_RH(m); + t.rotation = HMM_M4ToQ_LH(m); return t; } diff --git a/source/engine/transform.h b/source/engine/transform.h index 00e7718..8976ed4 100644 --- a/source/engine/transform.h +++ b/source/engine/transform.h @@ -4,9 +4,11 @@ #include "HandmadeMath.h" typedef struct transform { - HMM_Vec3 pos; - HMM_Vec3 scale; - HMM_Quat rotation; + HMM_Vec3 pos; + HMM_Vec3 scale; + HMM_Quat rotation; + HMM_Mat4 cache; + int dirty; } transform; transform *make_transform(); diff --git a/source/engine/yugine.c b/source/engine/yugine.c index d7727ec..b3ec4ef 100644 --- a/source/engine/yugine.c +++ b/source/engine/yugine.c @@ -227,10 +227,6 @@ sapp_desc sokol_main(int argc, char **argv) { signal(SIGFPE, seghandle); #endif - HMM_Quat norot = HMM_QFromAxisAngle_RH((HMM_Vec3){0,1,0}, 0.1); - norot = HMM_M4ToQ_RH(HMM_LookAt_RH((HMM_Vec3){0,0,-100}, v3zero, vUP)); - printf("rot is %g,%g,%g,%g\n", norot.x, norot.y, norot.z, norot.w); - resources_init(); stm_setup(); /* time */ script_startup();