summaryrefslogtreecommitdiff
path: root/src/core/device.c
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-12-05 09:21:26 +0800
committerchai <chaifix@163.com>2019-12-05 09:21:26 +0800
commit8e684dc0c76708e3174f005aebcaabc144b85500 (patch)
treed7803e49ee1fdb5575128114d19499787f870c00 /src/core/device.c
parent2e82e2ddd0852b8063a3d6645366f53ee844e273 (diff)
*背面剔除
Diffstat (limited to 'src/core/device.c')
-rw-r--r--src/core/device.c34
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]],