diff options
Diffstat (limited to 'src/core/device.c')
-rw-r--r-- | src/core/device.c | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/src/core/device.c b/src/core/device.c index f5ed4eb..ad640c6 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -172,12 +172,19 @@ bool ssr_isenable(uint mask) { void ssr_viewport(float l, float r, float b, float t) { } +void ssr_ortho(float l, float r, float b, float t, float n, float f) { + Mat4 m; + mat4_setortho(l, r, b, t, n, f, &m); + mat4_multiply(&MATRIX, &m, &MATRIX); +} + void ssr_frustum(float l, float r, float b, float t, float n, float f) { Mat4 m; mat4_setfrustum(l, r, b, t, n, f, &m); mat4_multiply(&MATRIX, &m, &MATRIX); } +/*注意这里nf都要是正数,代表距离*/ void ssr_perspective(float fov, float aspect, float n, float f) { Mat4 m; mat4_setperspective(fov, aspect, n, f, &m); @@ -232,9 +239,11 @@ bool ssr_testdepthf(uint x, uint y, float depth) { void ssrU_viewport(Vec2* p, Vec2* out) { ssr_assert(p && out); - int halfw = config.width >> 1, halfh = config.height >> 1; - out->x = p->x * halfw + halfw + 0.5f; - out->y = halfh - p->y * halfh + 0.5f; + float halfw = config.width / 2.f, halfh = config.height / 2.f; + out->x = (int)(p->x * halfw + halfw); + out->y = (int)(halfh - p->y * halfh); + //out->x = (int)((p->x + 1) * (halfw - 0.5f)); + //out->y = (int)((1 - p->y) * (halfh - 0.5f)); } void ssr_bindvertices(Vert** verts, int nverts, uint* indices, int nprims) { @@ -254,8 +263,8 @@ void ssr_unuseprogram() { state.program = NULL; } -static Vec4* homos = 0; -static uint tri[3]; /*三角形三个顶点的索引*/ +static Vec4* homos = NULL; +static uint tri[3]; static Vec3 ab, ac; static Mat4 mvp, mv; void ssr_draw(ssr_PrimitiveType primitive) { @@ -285,14 +294,19 @@ void ssr_draw(ssr_PrimitiveType primitive) { tri[2] = state.indices[i * 3 + 2]; /* back face culling */ - if (ssr_isenable(ENABLEMASK_BACKFACECULL)) { - vec3_minus(&homos[tri[1]], &homos[tri[0]], &ab); - vec3_minus(&homos[tri[2]], &homos[tri[0]], &ac); - if (ab.x * ac.y - ab.y * ac.x <= 0) { - continue; + if (ssr_isenable(ENABLEMASK_BACKFACECULL)) { + float w0 = 1 / homos[tri[0]].w, w1 = 1 / homos[tri[1]].w, w2 = 1 / homos[tri[2]].w; + Vec2* h0 = &homos[tri[0]], *h1 = &homos[tri[1]], *h2 = &homos[tri[2]]; + ab.x = h1->x * w1 - h0->x * w0; /*在屏幕空间做背面剔除*/ + ab.y = h1->y * w1 - h0->y * w0; + ac.x = h2->x * w2 - h0->x * w0; + ac.y = h2->y * w2 - h0->y * w0; + if (ab.x * ac.y - ab.y * ac.x <= 0) { + continue; } } + /* render triangle */ ssrR_triangle( &homos[tri[0]], &homos[tri[1]], |