diff options
author | chai <chaifix@163.com> | 2019-12-05 09:21:26 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-12-05 09:21:26 +0800 |
commit | 8e684dc0c76708e3174f005aebcaabc144b85500 (patch) | |
tree | d7803e49ee1fdb5575128114d19499787f870c00 | |
parent | 2e82e2ddd0852b8063a3d6645366f53ee844e273 (diff) |
*背面剔除
-rw-r--r-- | src/core/device.c | 34 | ||||
-rw-r--r-- | src/core/device.h | 2 | ||||
-rw-r--r-- | src/core/rasterizer.c | 159 | ||||
-rw-r--r-- | src/core/shader.h | 32 | ||||
-rw-r--r-- | src/example/example_texture.c | 43 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/math/math.c | 3 | ||||
-rw-r--r-- | src/math/math.h | 1 | ||||
-rw-r--r-- | src/math/matrix.c | 12 | ||||
-rw-r--r-- | src/math/vec3.c | 4 | ||||
-rw-r--r-- | src/util/type.h | 2 |
11 files changed, 181 insertions, 113 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]], diff --git a/src/core/device.h b/src/core/device.h index aa6e73f..1350105 100644 --- a/src/core/device.h +++ b/src/core/device.h @@ -52,6 +52,7 @@ void ssr_multmatrix(Mat4* m);/*ҳ˾*/ void ssr_loadmatrix(Mat4* m); void ssr_frustum(float l, float r, float b, float t, float n, float f); void ssr_perspective(float fov, float aspect, float n, float f); +void ssr_ortho(float l, float r, float b, float t, float n, float f); void ssr_viewport(float l, float r, float b, float t); @@ -78,7 +79,6 @@ void ssr_draw(ssr_PrimitiveType primitive); void ssr_clearcolor(Color color); void ssr_cleardepth(); -/*ֱӻƵ*/ void ssr_putpoint(uint screenx, uint screeny, Color color); /*ֱijλõɫ*/ void ssr_present(); diff --git a/src/core/rasterizer.c b/src/core/rasterizer.c index 0c8c6eb..0e5b785 100644 --- a/src/core/rasterizer.c +++ b/src/core/rasterizer.c @@ -73,51 +73,88 @@ void ssrR_center(Vec2* A, Vec2* B, Vec2* C, Vec2* out) { out->y = k * (A->y + B->y + C->y); } -/* -** clipc ŷŵβü -** program shader -*/ +static void puttriangle(Vec2* A, Vec2* B, Vec2* C, Color c) { + ssr_assert(A && B && C); + ssrR_putline(A->x, A->y, B->x, B->y, c); + ssrR_putline(A->x, A->y, C->x, C->y, c); + ssrR_putline(C->x, C->y, B->x, B->y, c); +} + +#define discardif(condition) if(condition) continue + static Vec3 SA, SB, SC; // screen coords static Vec3 bcp; // -void _ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, Vert* A, Vert* B, Vert* C, Program* program, UniformCollection* uniforms) { +void ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, Vert* A, Vert* B, Vert* C, Program* program, UniformCollection* uniforms) { ssr_assert(CA && CB && CC && program); + vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA); vec4_dividew(CB, &SB); ssrU_viewport(&SB, &SB); vec4_dividew(CC, &SC); ssrU_viewport(&SC, &SC); - /*Ļռyֵ, AyСBУC*/ + + //puttriangle(&SA, &SB, &SC, 0xffff0000); + //return; + Vec3 *sa = &SA, *sb = &SB, *sc = &SC, *tmp; Vec4* v4tmp; Vert* vtemp; - if (sb->y < sa->y) { sa = &SB; sb = &SA; v4tmp = CA; CA = CB; CB = v4tmp; vtemp = A; A = B; B = vtemp; } - if (sc->y < sb->y) { tmp = sc; sc = sb; sb = tmp; v4tmp = CC; CC = CB; CB = v4tmp; vtemp = B; B = C; C = vtemp; } - if (sc->y < sa->y) { tmp = sc; sc = sa; sa = tmp; v4tmp = CC; CC = CA; CA = v4tmp; vtemp = C; C = A; A = vtemp; } +#define swap(t, a, b) {t = a; a = b; b = t;} /*sort in y axis*/ + if (sb->y < sa->y) { swap(tmp, sa, sb); swap(v4tmp, CA, CB); swap(vtemp, A, B); } + if (sc->y < sb->y) { swap(tmp, sb, sc); swap(v4tmp, CB, CC); swap(vtemp, B, C); } + if (sb->y < sa->y) { swap(tmp, sa, sb); swap(v4tmp, CA, CB); swap(vtemp, A, B); } +#undef swap - float invkAC = (SC.x - SA.x) / (SC.y - SA.y + EPSILON); - float invkAB = (SB.x - SA.x) / (SB.y - SA.y + EPSILON); - float invkBC = (SC.x - SB.x) / (SC.y - SB.y + EPSILON); + Vec2 AB = {sb->x - sa->x, sb->y - sa->y}, AC = { sc->x - sa->x, sc->y - sa->y }; + int order = (AB.x * AC.y - AC.x * AB.y) > 0 ? 1 : -1; /*1BACϷ*/ - int order = 1; if (((sb->y - sa->y) / (sb->x - sa->x) - invkAC) * invkAC > 0) { order = -1; } + float invkAC = (sc->x - sa->x) / (sc->y - sa->y + EPSILON); + float invkAB = (sb->x - sa->x) / (sb->y - sa->y + EPSILON); + float invkBC = (sc->x - sb->x) / (sc->y - sb->y + EPSILON); FragmentShaderIn in = {A, B, C, 0}; Color color; - Vec2 l, r, p; - float depth; Vec3 cdepth = {SA.z, SB.z, SC.z}; + float from, to; /*scan line*/ + Vec2 p; + float depth; Vec3 cdepth = {sa->z, sb->z, sc->z}; float CAw = 1.f / CA->w, CBw = 1.f / CB->w, CCw = 1.f / CC->w; - for (p.y = sa->y; p.y <= sc->y; ++p.y) { - l.y = p.y; - r.y = p.y; - if (compare(SC.y, SA.y)) r.x = sc->x; - else r.x = sa->x + invkAC * (p.y - sa->y); /*AC*/ - if (p.y <= sb->y) { - if (compare(SB.y, SA.y)) l.x = sb->x; - else l.x = sa->x + invkAB * (p.y - sa->y); - } else { - if (compare(SC.y, SB.y)) l.x = sc->x; - else l.x = sb->x + invkBC * (p.y - sb->y); + for (p.y = sa->y; p.y < sb->y; ++p.y) { + from = (int)(invkAC * (p.y - sa->y) + sa->x); + to = (int)(invkAB * (p.y - sa->y) + sa->x); + for (p.x = from; order * (p.x - to) <= 0; p.x += order) { + + if (!ssrR_barycentric(sa, sb, sc, &p, &bcp)) { + continue; /*discard fragment*/ + } + + discardif(bcp.x < 0 || bcp.y < 0 || bcp.z < 0); + + bcp.x *= CAw; bcp.y *= CBw; bcp.z *= CCw; + vec3_scale(&bcp, 1.f / (bcp.x + bcp.y + bcp.z), &bcp); + /*depth test*/ + depth = vec3_dot(&bcp, &cdepth) * 0.5f + 0.5f; + if (!ssr_testdepthf(p.x, p.y, depth)) { + continue; /*discard fragment*/ + } + /*enter fragment shader*/ + in.bc = &bcp; + program->fragmentshader(uniforms, &in, &color); + ssr_putpoint(p.x, p.y, color); + } + } + for (p.y = sb->y; p.y <= sc->y; ++p.y) { + if (p.y == sb->y && sb->y == sc->y) { + from = sc->x; + to = sb->x; } - for (p.x = l.x; order * (r.x - p.x) >= 0; p.x += order) { + else { + from = (int)(invkAC * (p.y - sa->y) + sa->x); + to = (int)(invkBC * (p.y - sb->y) + sb->x); + } + for (p.x = from; order * (p.x - to) <= 0; p.x += order) { /*Եģβüռ䣩꣬*/ if (!ssrR_barycentric(sa, sb, sc, &p, &bcp)) { continue; /*discard fragment*/ } + + discardif(bcp.x < 0 || bcp.y < 0 || bcp.z < 0); + bcp.x *= CAw; bcp.y *= CBw; bcp.z *= CCw; vec3_scale(&bcp, 1.f / (bcp.x + bcp.y + bcp.z), &bcp); /*depth test*/ @@ -134,47 +171,67 @@ void _ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, Vert* A, Vert* B, Vert* C, Pro } static Vec2 bboxmin, bboxmax; -void ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, Vert* A, Vert* B, Vert* C, Program* program, UniformCollection* uniforms) { - ssr_assert(CA && CB && CC && program); +void _ssrR_triangle(Vec4* CA, Vec4* CB, Vec4* CC, Vert* A, Vert* B, Vert* C, Program* program, UniformCollection* uniforms) { + ssr_assert(CA && CB && CC && program && A && B && C && uniforms); + vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA); vec4_dividew(CB, &SB); ssrU_viewport(&SB, &SB); vec4_dividew(CC, &SC); ssrU_viewport(&SC, &SC); - bboxmin.x = min(SA.x, min(SB.x, SC.x)); - bboxmax.x = max(SA.x, max(SB.x, SC.x)); - bboxmin.y = min(SA.y, min(SB.y, SC.y)); - bboxmax.y = max(SA.y, max(SB.y, SC.y)); + + bboxmin.x = (int)min(SA.x, min(SB.x, SC.x)); /*һҪintȷûз϶*/ + bboxmax.x = (int)max(SA.x, max(SB.x, SC.x)); + bboxmin.y = (int)min(SA.y, min(SB.y, SC.y)); + bboxmax.y = (int)max(SA.y, max(SB.y, SC.y)); + float CAw = 1.f / CA->w, CBw = 1.f / CB->w, CCw = 1.f / CC->w; float depth; Vec3 cdepth = { SA.z, SB.z, SC.z }; FragmentShaderIn in = { A, B, C, 0 }; - Vec2 p; Color color; + Vec2 p; + Color color; + Vec3 s[2], u; + s[0].x = SC.x - SA.x; s[0].y = SB.x - SA.x; + s[1].x = SC.y - SA.y; s[1].y = SB.y - SA.y; + bool dontdiscad; for (p.y = bboxmin.y; p.y <= bboxmax.y; ++p.y) { + s[1].z = SA.y - p.y; for (p.x = bboxmin.x; p.x <= bboxmax.x; ++p.x) { - /*Եģβüռ䣩꣬*/ - if (!ssrR_barycentric(&SA, &SB, &SC, &p, &bcp)) { - continue; /*discard fragment*/ - } - if (bcp.x < 0 || bcp.y < 0 || bcp.z < 0) { - continue; /*discard fragment*/ - } + /*Եģβüռ䣩*/ + s[0].z = SA.x - p.x; + vec3_cross(&s[0], &s[1], &u); + + discardif(compare(u.z, 0)); + + u.z = 1.f / u.z; + bcp.x = 1.f - (u.x + u.y) * u.z; + bcp.y = u.y * u.z; + bcp.z = u.x * u.z; + + discardif(bcp.x < 0 || bcp.y < 0 || bcp.z < 0); + bcp.x *= CAw; bcp.y *= CBw; bcp.z *= CCw; vec3_scale(&bcp, 1.f / (bcp.x + bcp.y + bcp.z), &bcp); - /*Ȳ*/ + /*depth test*/ if (ssr_isenable(ENABLEMASK_DEPTHTEST)) { depth = vec3_dot(&bcp, &cdepth) * 0.5f + 0.5f; - if (!ssr_testdepthf(p.x, p.y, depth)) { - continue; /*discard fragment*/ - } + discardif(!ssr_testdepthf(p.x, p.y, depth)); } /*enter fragment shader*/ in.bc = &bcp; - bool discad = program->fragmentshader(uniforms, &in, &color); - if (!discad) + dontdiscad = program->fragmentshader(uniforms, &in, &color); + if (dontdiscad) ssr_putpoint(p.x, p.y, color); } } - //ssrR_putline(SA.x, SA.y, SB.x, SB.y, 0xffff0000); - //ssrR_putline(SA.x, SA.y, SC.x, SC.y, 0xffff0000); - //ssrR_putline(SC.x, SC.y, SB.x, SB.y, 0xffff0000); -}
\ No newline at end of file +} + +void ssrR_line(Vec4* CA, Vec4* CB, Vert* A, Vert* B, Program* program, UniformCollection* uniforms) { + ssr_assert(CA && CB && program && A && B && uniforms); + +} + +void ssrR_point(Vec4* CA, Vert* A, Program* program, UniformCollection* uniforms) { + ssr_assert(CA && program && A && uniforms); + +} diff --git a/src/core/shader.h b/src/core/shader.h index 8da8ae3..15dfc26 100644 --- a/src/core/shader.h +++ b/src/core/shader.h @@ -18,10 +18,10 @@ typedef struct UniformCollection { Vec2 var_vec2[8]; } UniformCollection; -#define ssrum4(i) (uniforms->var_mat4[i]) -#define ssruv2(i) (uniforms->var_vec2[i]) -#define ssruv3(i) (uniforms->var_vec3[i]) -#define ssruv4(i) (uniforms->var_vec4[i]) +#define UM4(i) (&uniforms->var_mat4[i]) +#define UV2(i) (&uniforms->var_vec2[i]) +#define UV3(i) (&uniforms->var_vec3[i]) +#define UV4(i) (&uniforms->var_vec4[i]) typedef struct VertexShaderIn { Vert* vertex; @@ -46,6 +46,11 @@ void ssrS_bcpvec2(Vec3* bc, Vec2* A, Vec2* B, Vec2* C, Vec2* out); void ssrS_bcpvec3(Vec3* bc, Vec3* A, Vec3* B, Vec3* C, Vec3* out); void ssrS_bcpvec4(Vec3* bc, Vec4* A, Vec4* B, Vec4* C, Vec4* out); +void ssrS_lerpcolor(float t, Color A, Color B, Color* out); +void ssrS_lerpvec2(float t, Vec2* A, Vec2* B, Vec2* out); +void ssrS_lerpvec3(float t, Vec3* A, Vec3* B, Vec3* out); +void ssrS_lerpvec4(float t, Vec4* A, Vec4* B, Vec4* out); + /* ** ⣬ṩļĴ */ @@ -65,19 +70,12 @@ typedef struct Register { }; } Register; -void ssrS_setregvec4_01i(uint idx, Vec4* value); -void ssrS_setregvec4_02i(uint idx, Vec4* value); -void ssrS_setregvec2_01i(uint idx, Vec2* value); -void ssrS_setregvec2_02i(uint idx, Vec2* value); -void ssrS_setregvec3_01i(uint idx, Vec3* value); -void ssrS_setregvec3_02i(uint idx, Vec3* value); - -Vec4* ssrS_getregvec4_01i(uint idx); -Vec4* ssrS_getregvec4_02i(uint idx); -Vec2* ssrS_getregvec2_01i(uint idx); -Vec2* ssrS_getregvec2_02i(uint idx); -Vec3* ssrS_getregvec3_01i(uint idx); -Vec3* ssrS_getregvec3_02i(uint idx); +void ssrS_setregvec4_01(Vert* vert, Vec4* value); +void ssrS_setregvec4_02(Vert* vert, Vec4* value); +void ssrS_setregvec2_01(Vert* vert, Vec2* value); +void ssrS_setregvec2_02(Vert* vert, Vec2* value); +void ssrS_setregvec3_01(Vert* vert, Vec3* value); +void ssrS_setregvec3_02(Vert* vert, Vec3* value); Vec4* ssrS_getregvec4_01(Vert* vert); Vec4* ssrS_getregvec4_02(Vert* vert); diff --git a/src/example/example_texture.c b/src/example/example_texture.c index 5411a12..db2868b 100644 --- a/src/example/example_texture.c +++ b/src/example/example_texture.c @@ -16,43 +16,31 @@ int face[] = { 0, 1, 2, 0, 2, 3, 1, 5, 2, 2, 5, 6, 4, 6, 5, 4, 7, 6, - 0, 7, 4, 0, 3, 7, + 0, 3, 7, 0, 7, 4, 0, 4, 1, 1, 4, 5, 2, 6, 3, 3, 6, 7 }; -static Mat4 m; - void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* homocoord) { static Vec4 p; p.xyz = *in->vertex->position; p.w = 1; mat4_applytovec4(uniforms->mvp, &p, homocoord); } bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color* color) { ssrS_bcpcolor(in->bc, in->A->color, in->B->color, in->C->color, color); - //ssrum4(1); - //Vec2 uv; - //ssrS_bcpvec2(in->bc, in->A->uv, in->B->uv, in->C->uv, &uv); - //int x = uv.x * 10; - //int y = uv.y * 10; - //if (x % 2 && y % 2) { - // *color = 0xffffffff; - //} else { - // *color = 0xff111111; - //} - return 0; + return 1; } Program program = { vert, frag }; void onloadtexture(void* data) { ssr_matrixmode(MATRIX_PROJECTION); ssr_loadidentity(); - ssr_perspective(100, 1.25f, 0.1f, 10); + ssr_perspective(90, 1.25f, 0.1f, 10); + //ssr_ortho(-5, 5, -4, 4, 0.1, 10); ssr_matrixmode(MATRIX_VIEW); ssr_loadidentity(); - //Vec3 pos = { 0,0,0 }, target = { 0,0,-1 }, up = { 0,1,0 }; - //ssr_lookat(&pos, &target, &up); + Vec3 p = { 0, 0, 0 }, target = { 0, 0, -1 }; + ssr_lookat(&p, &target, &vec3up); - /*ö*/ quad = ssrM_newvector(Vert*, 8); quad[0] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR); quad[1] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR); @@ -62,14 +50,14 @@ void onloadtexture(void* data) { quad[5] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR); quad[6] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR); quad[7] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR); - *quad[0]->position = verts[0]; quad[0]->color = colors[0]; quad[0]->index = 0; - *quad[1]->position = verts[1]; quad[1]->color = colors[1]; quad[1]->index = 1; - *quad[2]->position = verts[2]; quad[2]->color = colors[2]; quad[2]->index = 2; - *quad[3]->position = verts[3]; quad[3]->color = colors[3]; quad[3]->index = 3; - *quad[4]->position = verts[4]; quad[4]->color = colors[4]; quad[4]->index = 4; - *quad[5]->position = verts[5]; quad[5]->color = colors[5]; quad[5]->index = 5; - *quad[6]->position = verts[6]; quad[6]->color = colors[6]; quad[6]->index = 6; - *quad[7]->position = verts[7]; quad[7]->color = colors[7]; quad[7]->index = 7; + quad[0]->index = 0; *quad[0]->position = verts[0]; quad[0]->color = colors[0]; + quad[1]->index = 1; *quad[1]->position = verts[1]; quad[1]->color = colors[1]; + quad[2]->index = 2; *quad[2]->position = verts[2]; quad[2]->color = colors[2]; + quad[3]->index = 3; *quad[3]->position = verts[3]; quad[3]->color = colors[3]; + quad[4]->index = 4; *quad[4]->position = verts[4]; quad[4]->color = colors[4]; + quad[5]->index = 5; *quad[5]->position = verts[5]; quad[5]->color = colors[5]; + quad[6]->index = 6; *quad[6]->position = verts[6]; quad[6]->color = colors[6]; + quad[7]->index = 7; *quad[7]->position = verts[7]; quad[7]->color = colors[7]; ssr_bindvertices(quad, 8, face, 12); @@ -89,8 +77,7 @@ void onupdatetexture(void*data) { ssr_matrixmode(MATRIX_MODEL); ssr_loadidentity(); ssr_translate(0, 0, -3); -// ssr_rotate(-30, 1, 1, 0); - ssr_rotate(360 * sin(_t += dt/5000.f), 1, 1, 0); + ssr_rotate(_t -= dt / 50.f, 1, 1, 1); } void ondrawtexture(void*data) { @@ -30,7 +30,7 @@ SETEXAMPLEF(onupdate, i) int main(int argc, char* argv[]) { - if (SDL_Init(SDL_INIT_VIDEO) < 0) + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) return 1; SDL_Window* wnd = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); SDL_Event e; diff --git a/src/math/math.c b/src/math/math.c index 4cca29e..7d731f8 100644 --- a/src/math/math.c +++ b/src/math/math.c @@ -5,14 +5,13 @@ char printbuffer[2048] = { 0 }; float rsqrt(float number) { long i; float x2, y; - const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = *(long *)&y; i = 0x5f3759df - (i >> 1); y = *(float *)&i; - y = y * (threehalfs - (x2 * y * y)); + y = y * (1.5F - (x2 * y * y)); return y; } diff --git a/src/math/math.h b/src/math/math.h index 4498920..90c7521 100644 --- a/src/math/math.h +++ b/src/math/math.h @@ -211,6 +211,7 @@ void mat4_zero(Mat4* out); void mat4_setidentity(Mat4* out); void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out); void mat4_setperspective(float fov, float aspect, float near, float far, Mat4* out); +void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out); void mat4_setscale(float kx, float ky, float kz, Mat4* out); void mat4_setposition(float x, float y, float z, Mat4* out); void mat4_setrotatez(float angle, Mat4* out); diff --git a/src/math/matrix.c b/src/math/matrix.c index 458f432..15a194d 100644 --- a/src/math/matrix.c +++ b/src/math/matrix.c @@ -61,6 +61,18 @@ void mat4_setidentity(Mat4* out) { out->e33 = 1; } +void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out) { + ssr_assert(out); + mat4_zero(out); + out->e00 = 2 / (r - l); + out->e03 = -(r + l) / (r - l); + out->e11 = 2 / (t - b); + out->e13 = -(t + b) / (t - b); + out->e22 = -2 / (f - n); + out->e23 = -(f + n) / (f - n); + out->e33 = 1; +} + void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out) { ssr_assert(out); mat4_zero(out); diff --git a/src/math/vec3.c b/src/math/vec3.c index 4d77b96..4ca8519 100644 --- a/src/math/vec3.c +++ b/src/math/vec3.c @@ -96,8 +96,8 @@ void vec3_multiply(Vec3* v1, Vec3* v2, Quat* out) { void vec3_normalize(Vec3* v, Vec3* out) { ssr_assert(v && out); - //float mag = rsqrt(v->x * v->x + v->y * v->y + v->z * v->z); - float mag = 1.f / vec3_magnitude(v); + float mag = rsqrt(v->x * v->x + v->y * v->y + v->z * v->z); + //float mag = 1.f / vec3_magnitude(v); out->x = v->x * mag; out->y = v->y * mag; out->z = v->z * mag; diff --git a/src/util/type.h b/src/util/type.h index 709cb32..f62bbfa 100644 --- a/src/util/type.h +++ b/src/util/type.h @@ -2,6 +2,6 @@ #define _SOFTSHADEROOM_TYPE_H_ typedef unsigned int uint; -typedef int bool; +typedef unsigned char bool; #endif
\ No newline at end of file |