diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/core/device.c | 40 | ||||
-rw-r--r-- | src/core/rasterizer.c | 18 | ||||
-rw-r--r-- | src/core/texture.c | 6 | ||||
-rw-r--r-- | src/example/02_cube/02_cube.c | 4 | ||||
-rw-r--r-- | src/example/04_bloom/postprocess.c | 10 | ||||
-rw-r--r-- | src/example/04_bloom/preprocess.c | 2 | ||||
-rw-r--r-- | src/extend/camera.c | 18 | ||||
-rw-r--r-- | src/extend/mesh.c | 10 | ||||
-rw-r--r-- | src/extend/transform.c | 18 | ||||
-rw-r--r-- | src/gizmo/gizmo.c | 8 | ||||
-rw-r--r-- | src/math/mat.c | 216 | ||||
-rw-r--r-- | src/math/math.h | 204 | ||||
-rw-r--r-- | src/math/quat.c | 120 | ||||
-rw-r--r-- | src/math/vec2.c | 14 | ||||
-rw-r--r-- | src/math/vec3.c | 66 | ||||
-rw-r--r-- | src/math/vec4.c | 16 | ||||
-rw-r--r-- | src/shaders/common.h | 6 | ||||
-rw-r--r-- | src/shaders/default.c | 14 | ||||
-rw-r--r-- | src/shaders/pbr.c | 16 | ||||
-rw-r--r-- | src/shaders/unlit.c | 10 | ||||
-rw-r--r-- | src/test/test_mat4.c | 132 | ||||
-rw-r--r-- | src/test/test_quat.c | 60 |
22 files changed, 504 insertions, 504 deletions
diff --git a/src/core/device.c b/src/core/device.c index 6b2bb7c..ac055c9 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -151,7 +151,7 @@ void ssr_matrixmode(ssr_MatrixMode mode) { } void ssr_loadidentity() { - mat4_setidentity(&MATRIX); + internal_mat4_setidentity(&MATRIX); } void ssr_pushmatrix() { @@ -168,34 +168,34 @@ void ssr_popmatrix() { void ssr_lookat(Vec3* pos, Vec3* target, Vec3* up) { ssr_assert(pos && target && up); Vec3 z; - vec3_minus(pos, target, &z); vec3_normalize(&z, &z); /*去除缩放影响*/ + internal_vec3_minus(pos, target, &z); internal_vec3_normalize(&z, &z); /*去除缩放影响*/ Vec3 x; - vec3_cross(up, &z, &x); vec3_normalize(&x, &x); + internal_vec3_cross(up, &z, &x); internal_vec3_normalize(&x, &x); Vec3 y; - vec3_cross(&z, &x, &y); vec3_normalize(&y, &y); + internal_vec3_cross(&z, &x, &y); internal_vec3_normalize(&y, &y); Mat4 m = { /*注意这里是列主序*/ x.x, y.x, z.x, 0, x.y, y.y, z.y, 0, x.z, y.z, z.z, 0, - -vec3_dot(&x, pos), -vec3_dot(&y, pos), -vec3_dot(&z, pos), 1 + -internal_vec3_dot(&x, pos), -internal_vec3_dot(&y, pos), -internal_vec3_dot(&z, pos), 1 }; - mat4_multiply(&MATRIX, &m, &MATRIX); + internal_mat4_multiply(&MATRIX, &m, &MATRIX); } void ssr_scale(float sx, float sy, float sz) { Vec3 scale = { sx, sy, sz }; - mat4_scale(&MATRIX, &scale, &MATRIX); + internal_mat4_scale(&MATRIX, &scale, &MATRIX); } void ssr_rotate(float angle, float x, float y, float z) { Vec3 axis = { x, y, z }; - vec3_normalize(&axis, &axis); - mat4_rotate(&MATRIX, angle, &axis, &MATRIX); + internal_vec3_normalize(&axis, &axis); + internal_mat4_rotate(&MATRIX, angle, &axis, &MATRIX); } void ssr_translate(float x, float y, float z) { Vec3 trans = { x, y, z }; - mat4_translate(&MATRIX, &trans, &MATRIX); + internal_mat4_translate(&MATRIX, &trans, &MATRIX); } void ssr_loadmatrix(Mat4* m) { @@ -205,13 +205,13 @@ void ssr_loadmatrix(Mat4* m) { void ssr_multmatrix(Mat4* m) { ssr_assert(m); - mat4_multiply(&MATRIX, m, &MATRIX); + internal_mat4_multiply(&MATRIX, m, &MATRIX); } void ssr_getmvp(Mat4* out) { ssr_assert(out); - mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out); - mat4_multiply(&GETMATRIX(MATRIX_PROJECTION), out, out); + internal_mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out); + internal_mat4_multiply(&GETMATRIX(MATRIX_PROJECTION), out, out); } void ssr_getm(Mat4* out) { @@ -221,7 +221,7 @@ void ssr_getm(Mat4* out) { void ssr_getmv(Mat4* out) { ssr_assert(out); - mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out); + internal_mat4_multiply(&GETMATRIX(MATRIX_VIEW), &GETMATRIX(MATRIX_MODEL), out); } void ssr_enable(uint mask) { @@ -245,22 +245,22 @@ 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); + internal_mat4_setortho(l, r, b, t, n, f, &m); + internal_mat4_multiply(&MATRIX, &m, &MATRIX); } void ssr_frustum(float l, float r, float b, float t, float n, float f) { ssr_assert(n > 0 && f > 0 && f > n); Mat4 m; - mat4_setfrustum(l, r, b, t, n, f, &m); - mat4_multiply(&MATRIX, &m, &MATRIX); + internal_mat4_setfrustum(l, r, b, t, n, f, &m); + internal_mat4_multiply(&MATRIX, &m, &MATRIX); } void ssr_perspective(float fov, float aspect, float n, float f) { ssr_assert(n > 0 && f > 0 && f > n); Mat4 m; - mat4_setperspective(fov, aspect, n, f, &m); - mat4_multiply(&MATRIX, &m, &MATRIX); + internal_mat4_setperspective(fov, aspect, n, f, &m); + internal_mat4_multiply(&MATRIX, &m, &MATRIX); } void ssr_present() { diff --git a/src/core/rasterizer.c b/src/core/rasterizer.c index d926cb0..0c78893 100644 --- a/src/core/rasterizer.c +++ b/src/core/rasterizer.c @@ -50,7 +50,7 @@ bool ssrR_barycentric(Vec2* A, Vec2* B, Vec2* C, Vec2* p, Vec3* out) { Vec3 s[2], u; s[0].x = C->x - A->x; s[0].y = B->x - A->x; s[0].z = A->x - p->x; s[1].x = C->y - A->y; s[1].y = B->y - A->y; s[1].z = A->y - p->y; - vec3_cross(&s[0], &s[1], &u); + internal_vec3_cross(&s[0], &s[1], &u); if (compare(u.z, 0)) { return 0; } else { @@ -93,9 +93,9 @@ void ssrR_triangle( ssr_assert(CA && CB && CC && program); Vec4 SA, SB, SC; - vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA); - vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB); - vec4_dividewnoz(CC, &SC); ssrU_viewport(&SC, &SC); + internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA); + internal_vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB); + internal_vec4_dividewnoz(CC, &SC); ssrU_viewport(&SC, &SC); /* puttriangle(&SA, &SB, &SC, 0xffff0000); @@ -151,7 +151,7 @@ void ssrR_triangle( for (p.x = from; order * (p.x - to) <= 0; p.x += order) { \ /*calculate barycentric coordinate*/ \ s[0].z = sa->x - p.x; \ - vec3_cross(&s[0], &s[1], &u); \ + internal_vec3_cross(&s[0], &s[1], &u); \ discardif(compare(u.z, 0)); \ u.z = 1.f / u.z; \ bc.x = 1.f - (u.x + u.y) * u.z; \ @@ -160,7 +160,7 @@ void ssrR_triangle( discardif(bc.x < 0 || bc.y < 0 || bc.z < 0); \ /*perspective correction*/ \ bc.x *= CAw; bc.y *= CBw; bc.z *= CCw; \ - vec3_scale(&bc, 1.f / (bc.x + bc.y + bc.z), &bc); \ + internal_vec3_scale(&bc, 1.f / (bc.x + bc.y + bc.z), &bc); \ /*early depth testing*/ \ if(depth_test){ \ depth = bc.x*sa->z + bc.y*sb->z + bc.z*sc->z; \ @@ -238,8 +238,8 @@ void ssrR_line( ssr_assert(CA && CB && program && uniforms); Vec4 SA, SB; - vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA); - vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB); + internal_vec4_dividewnoz(CA, &SA); ssrU_viewport(&SA, &SA); + internal_vec4_dividewnoz(CB, &SB); ssrU_viewport(&SB, &SB); FragmentShader frag_shader = program->fragmentshader; @@ -329,7 +329,7 @@ void ssrR_point(Vec4* CA, uint IA, Program* program, UniformCollection* uniforms FragmentShader frag_shader = program->fragmentshader; Vec3 SA; - vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA); + internal_vec4_dividew(CA, &SA); ssrU_viewport(&SA, &SA); bool depth_test = ssr_isenable(ENABLE_DEPTHTEST); bool blend = ssr_isenable(ENABLE_BLEND); diff --git a/src/core/texture.c b/src/core/texture.c index bb33bae..ecde0f4 100644 --- a/src/core/texture.c +++ b/src/core/texture.c @@ -107,9 +107,9 @@ Color32 texture_sampling(Texture* tex, float x01, float y01) { bl = sampling(tex, wrap_mode, x_min, y_max); br = sampling(tex, wrap_mode, x_max, y_max); - vec4_lerp(&tl, &tr, x - x_min, &t); - vec4_lerp(&bl, &br, x - x_min, &b); - vec4_lerp(&t, &b, y - y_min, &out); + internal_vec4_lerp(&tl, &tr, x - x_min, &t); + internal_vec4_lerp(&bl, &br, x - x_min, &b); + internal_vec4_lerp(&t, &b, y - y_min, &out); return out; } diff --git a/src/example/02_cube/02_cube.c b/src/example/02_cube/02_cube.c index 7e0840e..3479619 100644 --- a/src/example/02_cube/02_cube.c +++ b/src/example/02_cube/02_cube.c @@ -52,11 +52,11 @@ void ondrawcube(void*data) { for (int i = 0; i < 8; ++i) { Vec4 v = { verts[i].x, verts[i].y ,verts[i].z ,1 }, temp; - mat4_mulvec4(&m, &v, &temp); + internal_mat4_mulvec4(&m, &v, &temp); temp.x /= temp.w; temp.y /= temp.w; temp.z /= temp.w; - //vec4_print(&temp); + //internal_vec4_print(&temp); proj[i].x = temp.x; proj[i].y = temp.y; } diff --git a/src/example/04_bloom/postprocess.c b/src/example/04_bloom/postprocess.c index 1ec10e4..06cb105 100644 --- a/src/example/04_bloom/postprocess.c +++ b/src/example/04_bloom/postprocess.c @@ -19,18 +19,18 @@ static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* col float off = 1 / 600.f; //blur *color = tex2d(_frag_tex, _texcoord); - vec4_scale(color, weight[0], color); + internal_vec4_scale(color, weight[0], color); Vec2 p = {0, _texcoord->y}; Color32 c; for (int i = 1; i < 5; ++i) { p.x = _texcoord->x + off * i; c = tex2d(_frag_tex, &p); - vec4_scale(&c, weight[i], &c); - vec4_add(color, &c, color); + internal_vec4_scale(&c, weight[i], &c); + internal_vec4_add(color, &c, color); p.x = _texcoord->x - off * i; c = tex2d(_frag_tex, &p); - vec4_scale(&c, weight[i], &c); - vec4_add(color, &c, color); + internal_vec4_scale(&c, weight[i], &c); + internal_vec4_add(color, &c, color); } return 1; } diff --git a/src/example/04_bloom/preprocess.c b/src/example/04_bloom/preprocess.c index def9fd8..d7d4b21 100644 --- a/src/example/04_bloom/preprocess.c +++ b/src/example/04_bloom/preprocess.c @@ -7,7 +7,7 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) { static Vec4 p; p.xyz = in->vertex->position; p.w = 1; - mat4_mulvec4(uniforms->mvp, &p, clipcoord); + internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); color_tocolor32(in->vertex->color, _color); } diff --git a/src/extend/camera.c b/src/extend/camera.c index dd77172..2e66afc 100644 --- a/src/extend/camera.c +++ b/src/extend/camera.c @@ -40,8 +40,8 @@ Camera* camera_create(wog_Window* wnd, CameraConfig* config) { // camera local = world cam->transform.parent = NULL ; cam->transform.localposition = config->position; - cam->transform.localscale = vec3_make(1, 1, 1); - quat_fromeuler(&config->euler, &cam->transform.localrotation); + cam->transform.localscale = internal_vec3_make(1, 1, 1); + internal_quat_fromeuler(&config->euler, &cam->transform.localrotation); cam->near = config->near; cam->far = config->far; @@ -86,7 +86,7 @@ void camera_getprojmatrix(Camera* cam, Mat4* out) { if (out)*out = cam->cached_proj_matrix; return; } - mat4_setperspective(cam->fov, cam->aspect, cam->near, cam->far, &cam->cached_proj_matrix); + internal_mat4_setperspective(cam->fov, cam->aspect, cam->near, cam->far, &cam->cached_proj_matrix); if(out) *out = cam->cached_proj_matrix; cam->is_projdirty = FALSE; } @@ -94,9 +94,9 @@ void camera_getprojmatrix(Camera* cam, Mat4* out) { static void _onwheelscroll(Camera* cam, int wheel, float dt) { Quat rot; transform_getrotation(&cam->transform, &rot); Vec3 forward = {0,0,-1}; - quat_applytovec3(&rot, &forward, &forward); - vec3_scale(&forward, cam->zoom_speed * wheel * dt * (cam->speedup ? cam->speedupv : 1), &forward); - vec3_plus(&forward, &cam->transform.localposition, &cam->transform.localposition); + internal_quat_applytovec3(&rot, &forward, &forward); + internal_vec3_scale(&forward, cam->zoom_speed * wheel * dt * (cam->speedup ? cam->speedupv : 1), &forward); + internal_vec3_plus(&forward, &cam->transform.localposition, &cam->transform.localposition); cam->is_viewdirty = TRUE; } @@ -112,7 +112,7 @@ static void _onlookaround(Camera* cam,float dt) { cam->euler.pitch -= angle.x; cam->euler.yaw += angle.y; //printf("%f %f\n", cam->euler.pitch, cam->euler.yaw); - quat_fromeuler(&cam->euler, &cam->transform.localrotation); + internal_quat_fromeuler(&cam->euler, &cam->transform.localrotation); cam->mouse_prev.x = x; cam->mouse_prev.y = y; cam->is_viewdirty = TRUE; } @@ -124,9 +124,9 @@ static void _onmovearound(Camera* cam, float dt) { Vec3 dd = { cam->mouse_prev.x - x, y - cam->mouse_prev.y , 0}; dd.x *= cam->move_sensitivity.x * dt * (cam->speedup ? cam->speedupv : 1); dd.y *= cam->move_sensitivity.y * dt * (cam->speedup ? cam->speedupv : 1); - quat_applytovec3(&cam->transform.localrotation, &dd, &dd); + internal_quat_applytovec3(&cam->transform.localrotation, &dd, &dd); //printf("%f %f %f\n", dd.x, dd.y, dd.z); - vec3_plus(&cam->transform.localposition, &dd, &cam->transform.localposition); + internal_vec3_plus(&cam->transform.localposition, &dd, &cam->transform.localposition); cam->mouse_prev.x = x; cam->mouse_prev.y = y; cam->is_viewdirty = TRUE; } diff --git a/src/extend/mesh.c b/src/extend/mesh.c index c8cdf2a..20153d9 100644 --- a/src/extend/mesh.c +++ b/src/extend/mesh.c @@ -52,7 +52,7 @@ void *darray_hold(void *darray, int count, int itemsize) { } } -Vec3 vec3_min(Vec3 a, Vec3 b) { +Vec3 internal_vec3_min(Vec3 a, Vec3 b) { float x = min(a.x, b.x); float y = min(a.y, b.y); float z = min(a.z, b.z); @@ -60,7 +60,7 @@ Vec3 vec3_min(Vec3 a, Vec3 b) { return r; } -Vec3 vec3_max(Vec3 a, Vec3 b) { +Vec3 internal_vec3_max(Vec3 a, Vec3 b) { float x = max(a.x, b.x); float y = max(a.y, b.y); float z = max(a.z, b.z); @@ -110,14 +110,14 @@ static Mesh *build_mesh( mesh->triangles[i] = position_index; - //bbox_min = vec3_min(bbox_min, vertices[i].position); - //bbox_max = vec3_max(bbox_max, vertices[i].position); + //bbox_min = internal_vec3_min(bbox_min, vertices[i].position); + //bbox_max = internal_vec3_max(bbox_max, vertices[i].position); } mesh->tris_count = num_faces; mesh->vert_count = nvert; mesh->vertices = vertices; - //mesh->center = vec3_div(vec3_add(bbox_min, bbox_max), 2); + //mesh->center = internal_vec3_div(internal_vec3_add(bbox_min, bbox_max), 2); return mesh; } diff --git a/src/extend/transform.c b/src/extend/transform.c index 6979e40..62776f7 100644 --- a/src/extend/transform.c +++ b/src/extend/transform.c @@ -6,11 +6,11 @@ void transform_getpositionandrotation(Transform* trans, Vec3* pos, Quat* rot) { Transform* cur = trans->parent; while (cur) { /*按照srt的顺序计算pos*/ - vec3_scale3(pos, &cur->localscale, pos); - quat_applytovec3(&cur->localrotation, pos, pos); - vec3_plus(pos, &cur->localposition, pos); + internal_vec3_scale3(pos, &cur->localscale, pos); + internal_quat_applytovec3(&cur->localrotation, pos, pos); + internal_vec3_plus(pos, &cur->localposition, pos); /*计算旋转*/ - quat_multiply(&cur->localrotation, rot, rot); + internal_quat_multiply(&cur->localrotation, rot, rot); cur = cur->parent; } @@ -19,18 +19,18 @@ void transform_getpositionandrotation(Transform* trans, Vec3* pos, Quat* rot) { void transform_getinvmatrixnoscale(Transform* trans, Mat4* worldToLocal) { Vec3 pos; Quat rot; transform_getpositionandrotation(trans, &pos, &rot); - quat_invert(&rot, &rot); + internal_quat_invert(&rot, &rot); /*(TR)^-1 = R^-1T^-1*/ - quat_tomat4(&rot, worldToLocal); - vec3_scale(&pos, -1, &pos); - mat4_translate(worldToLocal, &pos, worldToLocal); + internal_quat_tomat4(&rot, worldToLocal); + internal_vec3_scale(&pos, -1, &pos); + internal_mat4_translate(worldToLocal, &pos, worldToLocal); } void transform_getrotation(Transform* trans, Quat* rot) { *rot = trans->localrotation; Transform* cur = trans->parent; while (cur) { - quat_multiply(&cur->localrotation, rot, rot); + internal_quat_multiply(&cur->localrotation, rot, rot); } } diff --git a/src/gizmo/gizmo.c b/src/gizmo/gizmo.c index 61acdcb..ddd7045 100644 --- a/src/gizmo/gizmo.c +++ b/src/gizmo/gizmo.c @@ -76,14 +76,14 @@ void gizmo_grid() { continue; } verts[i].index = i; - verts[i].position = vec3_make(-field, 0, y); + verts[i].position = internal_vec3_make(-field, 0, y); verts[i].normal = vec3zero; verts[i].tangent = vec4zero; verts[i].texcoord = vec2zero; verts[i].color = color; grid[i] = i++; verts[i].index = i; - verts[i].position = vec3_make(field, 0, y); + verts[i].position = internal_vec3_make(field, 0, y); verts[i].normal = vec3zero; verts[i].tangent = vec4zero; verts[i].texcoord = vec2zero; @@ -97,14 +97,14 @@ void gizmo_grid() { continue; } verts[i].index = i; - verts[i].position = vec3_make(x, 0, -field); + verts[i].position = internal_vec3_make(x, 0, -field); verts[i].normal = vec3zero; verts[i].tangent = vec4zero; verts[i].texcoord = vec2zero; verts[i].color = color; grid[i] = i++; verts[i].index = i; - verts[i].position = vec3_make(x, 0, field); + verts[i].position = internal_vec3_make(x, 0, field); verts[i].normal = vec3zero; verts[i].tangent = vec4zero; verts[i].texcoord = vec2zero; diff --git a/src/math/mat.c b/src/math/mat.c index c254c02..e3c6ce3 100644 --- a/src/math/mat.c +++ b/src/math/mat.c @@ -31,7 +31,7 @@ sharedMat2 = *p;\ p = &sharedMat2;\ }while(0) -void mat4_tostring(Mat4* m, char str[]) { +void internal_mat4_tostring(Mat4* m, char str[]) { ssrM_zero(str, sizeof(str)); for (int r = 0; r < 4; ++r) { for (int c = 0; c < 4; ++c) { @@ -43,28 +43,28 @@ void mat4_tostring(Mat4* m, char str[]) { } } -void mat4_print(Mat4* m) { - mat4_tostring(m, printbuffer); +void internal_mat4_print(Mat4* m) { + internal_mat4_tostring(m, printbuffer); printf("\n%s\n", printbuffer); } -void mat4_zero(Mat4* out) { +void internal_mat4_zero(Mat4* out) { ssr_assert(out); ssrM_zero(out, sizeof(Mat4)); } -void mat4_setidentity(Mat4* out) { +void internal_mat4_setidentity(Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = 1; out->e11 = 1; out->e22 = 1; out->e33 = 1; } -void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out) { +void internal_mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = 2 / (r - l); out->e03 = -(r + l) / (r - l); out->e11 = 2 / (t - b); @@ -74,9 +74,9 @@ void mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* o out->e33 = 1; } -void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out) { +void internal_mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = (2.f * n) / (r - l); out->e02 = (r + l) / (r - l); out->e11 = 2.f * n / (t - b); @@ -86,12 +86,12 @@ void mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out->e32 = -1; } -void mat4_setperspective(float _fov, float aspect, float near, float far, Mat4* out) { +void internal_mat4_setperspective(float _fov, float aspect, float near, float far, Mat4* out) { float fov = _fov * PI / 180.f; float tanf = tan(fov * 0.5); ssr_assert(fov > 0 && aspect > 0); ssr_assert(near > 0 && far > 0 && near != far); - mat4_setfrustum( + internal_mat4_setfrustum( -near*tanf*aspect, near*tanf*aspect, -near*tanf, @@ -108,10 +108,10 @@ static float _mul(float* r, float* c) { #define mul(r, c) _mul(&MAT(m1,r,0), &MAT(m2,0,c)) -void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { +void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { ssr_assert(m1 && m2 && out); - if (mat4_isidentity(m1)) { if(m2 != out) *out = *m2; return; } - if (mat4_isidentity(m2)) { if(m1 != out) *out = *m1; return; } + if (internal_mat4_isidentity(m1)) { if(m2 != out) *out = *m2; return; } + if (internal_mat4_isidentity(m2)) { if(m1 != out) *out = *m1; return; } if (m1 == out) shrmat(m1); if (m2 == out) shrmat2(m2); @@ -121,44 +121,44 @@ void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out) { out->e30 = mul(3, 0); out->e31 = mul(3, 1); out->e32 = mul(3, 2); out->e33 = mul(3, 3); } -void mat4_setscale(float kx, float ky, float kz, Mat4* out) { +void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out) { ssr_assert(out); - mat4_zero(out); + internal_mat4_zero(out); out->e00 = kx; out->e11 = ky; out->e22 = kz; out->e33 = 1; } -void mat4_setposition(float x, float y, float z, Mat4* out) { +void internal_mat4_setposition(float x, float y, float z, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); out->e03 = x; out->e13 = y; out->e23 = z; } -void mat4_setrotatez(float angle, Mat4* out) { +void internal_mat4_setrotatez(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e00 = c; out->e01 = -s; out->e10 = s; out->e11 = c; } -void mat4_setrotatex(float angle, Mat4* out) { +void internal_mat4_setrotatex(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e11 = c; out->e12 = -s; out->e21 = s; out->e22 = c; } -void mat4_setrotatey(float angle, Mat4* out) { +void internal_mat4_setrotatey(float angle, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angle = radians(angle); float s = sin(angle), c = cos(angle); out->e00 = c; out->e02 = s; @@ -166,9 +166,9 @@ void mat4_setrotatey(float angle, Mat4* out) { } /*https://www.geometrictools.com/Documentation/EulerAngles.pdf*/ -void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { +void internal_mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { ssr_assert(out); - mat4_setidentity(out); + internal_mat4_setidentity(out); angleX = radians(angleX); angleY = radians(angleY); angleZ = radians(angleZ); float sx = sin(angleX), cx = cos(angleX); float sy = sin(angleY), cy = cos(angleY); @@ -178,7 +178,7 @@ void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out) { out->e20 = -cz * sy + cy * sx * sz; out->e21 = cy * cz*sx + sy * sz; out->e22 = cx * cy; } -void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { +void internal_mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { ssr_assert(ax && out); float a = radians(angle); @@ -187,8 +187,8 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { Vec3 axis = *ax; Vec3 temp; - vec3_normalize(&axis, &axis); - vec3_scale(&axis, 1 - c, &temp); + internal_vec3_normalize(&axis, &axis); + internal_vec3_scale(&axis, 1 - c, &temp); /* rotation matrix 推导过程 https://zhuanlan.zhihu.com/p/56587491 @@ -198,7 +198,7 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { 0, 0, 0, 1 */ - mat4_setidentity(out); + internal_mat4_setidentity(out); out->m[0][0] = c + temp.x * axis.x; out->m[0][1] = 0 + temp.x * axis.y + s * axis.z; out->m[0][2] = 0 + temp.x * axis.z - s * axis.y; @@ -213,9 +213,9 @@ void mat4_setaxisangle(Vec3* ax, float angle, Mat4* out) { } -void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { +void internal_mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { ssr_assert(x && y && z); - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 asix = { x->x, x->y, x->z, 0 }; Vec4 asiy = { y->x, y->y, y->z, 0 }; Vec4 asiz = { z->x, z->y, z->z, 0 }; @@ -224,7 +224,7 @@ void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out) { out->colums[2] = asiz; } -bool mat4_isidentity(Mat4* m) { +bool internal_mat4_isidentity(Mat4* m) { ssr_assert(m); //return memcmp(m, &mat4identity, sizeof(Mat4)) == 0; return @@ -234,95 +234,95 @@ bool mat4_isidentity(Mat4* m) { compare(m->pos.x, 0 ) && compare(m->pos.y, 0 ) && compare(m->pos.z, 0 ) &&compare( m->pos.w, 1); } -bool mat4_isorthogonal(Mat4* m) { +bool internal_mat4_isorthogonal(Mat4* m) { ssr_assert(m); Mat4 trans = {0}, res = { 0 }; - mat4_transpose(m, &trans); - mat4_multiply(m, &trans, &res); - return mat4_isidentity(&res); + internal_mat4_transpose(m, &trans); + internal_mat4_multiply(m, &trans, &res); + return internal_mat4_isidentity(&res); } /* ** 以z轴为准进行正交化,分为施密特正交化和叉乘正交化,施密特过程更加普遍,叉乘适用于三维空间,两种方法实际上等价 ** 如果用叉乘的方法,只需要关注yz,x通过叉乘得到 */ -void mat4_orthogonalize(Mat4* in, Mat4* out) { +void internal_mat4_orthogonalize(Mat4* in, Mat4* out) { ssr_assert(in && out); if (in == out) { shrmat(in); } - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 z = in->basis.z; - vec3_normalize(&z, &z); + internal_vec3_normalize(&z, &z); Vec4 y = in->basis.y; Vec4 x = {0}; - vec3_cross(&y, &z, &x); - vec3_normalize(&x, &x); - vec3_cross(&z, &x, &y); + internal_vec3_cross(&y, &z, &x); + internal_vec3_normalize(&x, &x); + internal_vec3_cross(&z, &x, &y); out->basis.x = x; out->basis.y = y; out->basis.z = z; /* - mat4_setidentity(out); + internal_mat4_setidentity(out); Vec4 x = in->basis.x; Vec4 y = in->basis.y; Vec4 z = in->basis.z; Vec3 temp, temp2; - vec3_normalize(&z, &z); + internal_vec3_normalize(&z, &z); out->basis.z = z; - float dot = vec3_dot(&y, &z); - vec3_scale(&z, dot, &temp); - vec3_minus(&y, &temp, &y); - vec3_normalize(&y, &y); + float dot = internal_vec3_dot(&y, &z); + internal_vec3_scale(&z, dot, &temp); + internal_vec3_minus(&y, &temp, &y); + internal_vec3_normalize(&y, &y); out->basis.y = y; - vec3_cross(&y, &z, &out->basis.x); + internal_vec3_cross(&y, &z, &out->basis.x); */ /*针对右手系调整basis.x的方向*/ /*https://math.stackexchange.com/questions/1847465/why-to-use-gram-schmidt-process-to-orthonormalise-a-basis-instead-of-cross-produ*/ /*由于需要针对右手系,这里不这样计算,因为可能要对结果进行翻转 - dot = vec3_dot(&x, &z); - vec3_scale(&z, dot, &temp); - vec3_minus(&x, &temp, &temp2); - dot = vec3_dot(&x, &y); - vec3_scale(&y, dot, &temp); - vec3_minus(&temp2, &temp, &x); - vec3_normalize(&x, &x); + dot = internal_vec3_dot(&x, &z); + internal_vec3_scale(&z, dot, &temp); + internal_vec3_minus(&x, &temp, &temp2); + dot = internal_vec3_dot(&x, &y); + internal_vec3_scale(&y, dot, &temp); + internal_vec3_minus(&temp2, &temp, &x); + internal_vec3_normalize(&x, &x); out->basis.x = x; */ } -bool mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out) { +bool internal_mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out) { ssr_assert(view && up && out); /*正交化*/ - float mag = vec3_magnitude(view); + float mag = internal_vec3_magnitude(view); if (mag < EPSILON) return 0; Vec3 z; - vec3_scale(view, 1.f / mag, &z); + internal_vec3_scale(view, 1.f / mag, &z); Vec3 x; - vec3_cross(up, &z, &x); - mag = vec3_magnitude(&x); + internal_vec3_cross(up, &z, &x); + mag = internal_vec3_magnitude(&x); if (mag < EPSILON) return 0; - vec3_scale(&x, 1.f / mag, &x); + internal_vec3_scale(&x, 1.f / mag, &x); Vec3 y; - vec3_cross(&z, &x, &y); - mag = vec3_magnitude(&y); + internal_vec3_cross(&z, &x, &y); + mag = internal_vec3_magnitude(&y); if (!compare(mag, 1)) return 0; - mat4_setorthonormalbias(&x, &y, &z, out); /*xyz正交*/ + internal_mat4_setorthonormalbias(&x, &y, &z, out); /*xyz正交*/ return 1; } -void mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { +void internal_mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { ssr_assert(mat && v && out); if (v == out) { sharedVec4 = *v; @@ -337,7 +337,7 @@ void mat4_mulvec4(Mat4* mat, Vec4* v, Vec4* out) { /* ** mat3 apply to vec3 */ -void mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { +void internal_mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { ssr_assert(mat && v && out); if (v == out) { sharedVec3 = *v; @@ -350,7 +350,7 @@ void mat4_mulvec3(Mat4* mat, Vec3* v, Vec3* out) { #define trans(r, c) out->e##r##c = m->e##c##r -void mat4_transpose(Mat4* m, Mat4* out) { +void internal_mat4_transpose(Mat4* m, Mat4* out) { ssr_assert(m && out); if (m == out) shrmat(m); @@ -362,10 +362,10 @@ void mat4_transpose(Mat4* m, Mat4* out) { /* ** 使用高斯消元法计算任意矩阵的逆矩阵。针对不含投影的3D变换矩阵,应该使用 -** mat4_invertgeneral3d() +** internal_mat4_invertgeneral3d() ** 更快一些 */ -bool mat4_invertfull(Mat4* m, Mat4* out) { +bool internal_mat4_invertfull(Mat4* m, Mat4* out) { ssr_assert(m && out); #define _m(r, c) MAT(m, r, c) @@ -473,11 +473,11 @@ bool mat4_invertfull(Mat4* m, Mat4* out) { ** 乘上平移矩阵的逆矩阵,即 ** M^-1 = (T(RS))^-1 = (RS)^-1 * T^-1 */ -bool mat4_invertgeneral3d(Mat4* in, Mat4* out) { +bool internal_mat4_invertgeneral3d(Mat4* in, Mat4* out) { ssr_assert(in && out); if (in == out) shrmat(in); - mat4_setidentity(out); + internal_mat4_setidentity(out); /*计算左上角3x3矩阵的行列式*/ float pos = 0, neg = 0, t; @@ -527,32 +527,32 @@ bool mat4_invertgeneral3d(Mat4* in, Mat4* out) { return 1; } -void mat4_invertpos(Mat4* in, Mat4* out) { +void internal_mat4_invertpos(Mat4* in, Mat4* out) { } -void mat4_invertscale(Mat4* in, Mat4* out) { +void internal_mat4_invertscale(Mat4* in, Mat4* out) { } -void mat4_invertrot(Mat4* in, Mat4* out) { +void internal_mat4_invertrot(Mat4* in, Mat4* out) { ssr_assert(in && out); - mat4_transpose(in, out); + internal_mat4_transpose(in, out); } -void mat4_settr(Vec3* pos, Quat* rot, Mat4* out) { +void internal_mat4_settr(Vec3* pos, Quat* rot, Mat4* out) { ssr_assert(pos && rot && out); - mat4_zero(out); - quat_tomat4(rot, out); + internal_mat4_zero(out); + internal_quat_tomat4(rot, out); out->e03 = pos->x; out->e13 = pos->y; out->e23 = pos->z; } -void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { +void internal_mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { ssr_assert(pos && rot && scale && out); - mat4_zero(out); - quat_tomat4(rot, out); /*pos*rot*scale的顺序*/ + internal_mat4_zero(out); + internal_quat_tomat4(rot, out); /*pos*rot*scale的顺序*/ out->e00 *= scale->x; out->e01 *= scale->y; out->e02 *= scale->z; out->e10 *= scale->x; out->e11 *= scale->y; out->e12 *= scale->z; out->e20 *= scale->x; out->e21 *= scale->y; out->e22 *= scale->z; @@ -561,16 +561,16 @@ void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out) { out->e23 = pos->z; } -void mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out) { +void internal_mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out) { ssr_assert(pos && rot && out); - mat4_zero(out); - quat_invert(rot, rot); - quat_tomat4(rot, out); + internal_mat4_zero(out); + internal_quat_invert(rot, rot); + internal_quat_tomat4(rot, out); Vec3 reverse = { -pos->x, -pos->y, -pos->z}; - mat4_translate(out, &reverse, out); /* (TR)^-1 = R^-1*T^-1所以这里是右乘*/ + internal_mat4_translate(out, &reverse, out); /* (TR)^-1 = R^-1*T^-1所以这里是右乘*/ } -void mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { +void internal_mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { ssr_assert(m && scale && out); if (out != m) { *out = *m; @@ -598,7 +598,7 @@ void mat4_scale(Mat4* m, Vec3* scale, Mat4* out) { out->e32 *= scale->z; } -void mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { +void internal_mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { ssr_assert(m && pos && out); if (out != m) { *out = *m; @@ -616,14 +616,14 @@ void mat4_translate(Mat4* m, Vec3* pos, Mat4* out) { out->e33 = out->e30 * pos->x + out->e31 * pos->y + out->e32 * pos->z + out->e33; } -void mat4_rotate(Mat4* m, float angle, Vec3* ax, Mat4* out) { +void internal_mat4_rotate(Mat4* m, float angle, Vec3* ax, Mat4* out) { ssr_assert(m && ax && out); Mat4 rot; - mat4_setaxisangle(ax, angle, &rot); - mat4_multiply(m, &rot, out); + internal_mat4_setaxisangle(ax, angle, &rot); + internal_mat4_multiply(m, &rot, out); } -void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { +void internal_mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { ssr_assert(src && pos && quat && scale); Vec3* x = &src->colums[0]; @@ -633,11 +633,11 @@ void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale) { *pos = *w; - quat_setlookrotation(z, y, quat); + internal_quat_setlookrotation(z, y, quat); - scale->x = vec3_magnitude(x); - scale->y = vec3_magnitude(y); - scale->z = vec3_magnitude(z); + scale->x = internal_vec3_magnitude(x); + scale->y = internal_vec3_magnitude(y); + scale->z = internal_vec3_magnitude(z); } static void MakePositive(Euler* euler) {/*弧度制欧拉角*/ @@ -665,7 +665,7 @@ static void SanitizeEuler(Euler* e) {/*弧度制欧拉角*/ } /*from unity src*/ -bool mat4_toeuler(Mat4* in, Euler* out) { +bool internal_mat4_toeuler(Mat4* in, Euler* out) { ssr_assert(in && out); // from http://www.geometrictools.com/Documentation/EulerAngles.pdf // YXZ order @@ -676,9 +676,9 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = asin(-MAT(in, 1, 2)); out->y = atan2(MAT(in, 0, 2), MAT(in, 2, 2)); out->z = atan2(MAT(in, 1, 0), MAT(in, 1, 1)); - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 1; } else @@ -687,9 +687,9 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = PI * 0.5F; out->y = atan2(MAT(in, 0, 1), MAT(in, 0, 0)); out->z = 0.0F; - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 0; } } @@ -699,16 +699,16 @@ bool mat4_toeuler(Mat4* in, Euler* out) { out->x = -PI * 0.5F; out->y = atan2(-MAT(in, 0, 1), MAT(in, 0, 0)); out->z = 0.0F; - //euler_rad2deg(out, out); + //internal_euler_rad2deg(out, out); SanitizeEuler(out); - euler_rad2deg(out, out); + internal_euler_rad2deg(out, out); return 0; } } /*from unity source*/ /*https://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/*/ -void mat4_toquat(Mat4* in, Quat* out) { +void internal_mat4_toquat(Mat4* in, Quat* out) { // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes // article "Quaternionf Calculus and Fast Animation". float fTrace = MAT(in, 0, 0) + MAT(in, 1, 1) + MAT(in, 2, 2); @@ -745,7 +745,7 @@ void mat4_toquat(Mat4* in, Quat* out) { *apkQuat[j] = (MAT(in, j, i) + MAT(in, i, j))*fRoot; *apkQuat[k] = (MAT(in, k, i) + MAT(in, i, k))*fRoot; } - quat_normalize(out, out); + internal_quat_normalize(out, out); } void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out) { diff --git a/src/math/math.h b/src/math/math.h index a69dabe..8f59137 100644 --- a/src/math/math.h +++ b/src/math/math.h @@ -133,17 +133,17 @@ typedef union { /* Vec */ /************************************************************************/ -void vec2_scale(Vec2* v, float k, Vec2* out); -void vec2_plus(Vec2* v1, Vec2* v2, Vec2* out); -void vec2_offset(Vec2* v, float offset, Vec2* out); -void vec2_rotate(Vec2* v, float angle, Vec2* out); +void internal_vec2_scale(Vec2* v, float k, Vec2* out); +void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out); +void internal_vec2_offset(Vec2* v, float offset, Vec2* out); +void internal_vec2_rotate(Vec2* v, float angle, Vec2* out); -float vec2_dot(Vec2* v1, Vec2* v2); +float internal_vec2_dot(Vec2* v1, Vec2* v2); -void vec2_tostring(Vec2* v, char buf[]); -void vec2_print(Vec2* v); +void internal_vec2_tostring(Vec2* v, char buf[]); +void internal_vec2_print(Vec2* v); -#define vec3_xy(v) (v->xy) +#define internal_vec3_xy(v) (v->xy) extern Vec3 vec3forward; /*(0,0,1)*/ extern Vec3 vec3up; /*(0,1,0)*/ @@ -156,41 +156,41 @@ extern Vec4 vec4zero; /*(0,0,0)*/ #define zerovec3 {0, 0, 0} #define zerovec4 {0, 0, 0, 0} -void vec3_tostring(Vec3* v, char buf[]); -void vec3_print(Vec3* v); +void internal_vec3_tostring(Vec3* v, char buf[]); +void internal_vec3_print(Vec3* v); -Vec3 vec3_make(float x, float y, float z); -float vec3_intersection(Vec3* v1, Vec3* v2); /*夹角*/ -void vec3_projection(Vec3* v1, Vec3* v2, Vec3* out);/*v1在v2上的投影*/ -void vec3_scale(Vec3* v, float k, Vec3* out); -void vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out); -void vec3_plus(Vec3* v1, Vec3* v2, Vec3* out); -void vec3_offset(Vec3* v, float offset, Vec3* out); -void vec3_normalize(Vec3* v, Vec3* out); -void vec3_vec4(float w, Vec4* out); +Vec3 internal_vec3_make(float x, float y, float z); +float internal_vec3_intersection(Vec3* v1, Vec3* v2); /*夹角*/ +void internal_vec3_projection(Vec3* v1, Vec3* v2, Vec3* out);/*v1在v2上的投影*/ +void internal_vec3_scale(Vec3* v, float k, Vec3* out); +void internal_vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out); +void internal_vec3_plus(Vec3* v1, Vec3* v2, Vec3* out); +void internal_vec3_offset(Vec3* v, float offset, Vec3* out); +void internal_vec3_normalize(Vec3* v, Vec3* out); +void internal_vec3_vec4(float w, Vec4* out); -void vec3_minus(Vec3* v1, Vec3* v2, Vec3* out); -float vec3_dot(Vec3* v1, Vec3* v2); -void vec3_cross(Vec3* v1, Vec3* v2, Vec3* out); -void vec3_multiply(Vec3* v1, Vec3* v2, Quat* out);// 向量的乘法,st=sxt-s*t,结果是一个四元数 +void internal_vec3_minus(Vec3* v1, Vec3* v2, Vec3* out); +float internal_vec3_dot(Vec3* v1, Vec3* v2); +void internal_vec3_cross(Vec3* v1, Vec3* v2, Vec3* out); +void internal_vec3_multiply(Vec3* v1, Vec3* v2, Quat* out);// 向量的乘法,st=sxt-s*t,结果是一个四元数 -float vec3_magnitude(Vec3* v1); -float vec3_magnitude2(Vec3* v1); +float internal_vec3_magnitude(Vec3* v1); +float internal_vec3_magnitude2(Vec3* v1); -void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out); -void vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out); +void internal_vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out); +void internal_vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out); -void vec4_dividew(Vec4* v, Vec3* out); -void vec4_dividewnoz(Vec4* v, Vec4* out); +void internal_vec4_dividew(Vec4* v, Vec3* out); +void internal_vec4_dividewnoz(Vec4* v, Vec4* out); -void vec4_tostring(Vec4* v, char buf[]); -void vec4_print(Vec4* v); +void internal_vec4_tostring(Vec4* v, char buf[]); +void internal_vec4_print(Vec4* v); -void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out); +void internal_vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out); -void vec4_scale(Vec4* v, float t, Vec4* out); +void internal_vec4_scale(Vec4* v, float t, Vec4* out); -void vec4_add(Vec4* v1, Vec4* v2, Vec4* out); +void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out); /************************************************************************/ @@ -199,57 +199,57 @@ void vec4_add(Vec4* v1, Vec4* v2, Vec4* out); extern Mat4 mat4identity; -void mat4_tostring(Mat4* m, char str[]); -void mat4_print(Mat4* m); +void internal_mat4_tostring(Mat4* m, char str[]); +void internal_mat4_print(Mat4* m); -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); -void mat4_setrotatex(float angle, Mat4* out); -void mat4_setrotatey(float angle, Mat4* out); -void mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out);/*RyRxRz*/ -void mat4_setaxisangle(Vec3* axis, float angle, Mat4* out); +void internal_mat4_zero(Mat4* out); +void internal_mat4_setidentity(Mat4* out); +void internal_mat4_setfrustum(float l, float r, float b, float t, float n, float f, Mat4* out); +void internal_mat4_setperspective(float fov, float aspect, float near, float far, Mat4* out); +void internal_mat4_setortho(float l, float r, float b, float t, float n, float f, Mat4* out); +void internal_mat4_setscale(float kx, float ky, float kz, Mat4* out); +void internal_mat4_setposition(float x, float y, float z, Mat4* out); +void internal_mat4_setrotatez(float angle, Mat4* out); +void internal_mat4_setrotatex(float angle, Mat4* out); +void internal_mat4_setrotatey(float angle, Mat4* out); +void internal_mat4_setrotate(float angleX, float angleY, float angleZ, Mat4* out);/*RyRxRz*/ +void internal_mat4_setaxisangle(Vec3* axis, float angle, Mat4* out); -bool mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out); -void mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out); /*正交的三个轴*/ +bool internal_mat4_setlookrotation(Vec3* view, Vec3* up, Mat4* out); +void internal_mat4_setorthonormalbias(Vec3* x, Vec3* y, Vec3* z, Mat4* out); /*正交的三个轴*/ -void mat4_orthogonalize(Mat4* in, Mat4* out); /*解决矩阵蠕变,对左上角3x3矩阵进行正交化,结果是右手系的正交矩阵*/ -bool mat4_isorthogonal(Mat4* m); /*判断是不是正交矩阵*/ -bool mat4_isidentity(Mat4* m); +void internal_mat4_orthogonalize(Mat4* in, Mat4* out); /*解决矩阵蠕变,对左上角3x3矩阵进行正交化,结果是右手系的正交矩阵*/ +bool internal_mat4_isorthogonal(Mat4* m); /*判断是不是正交矩阵*/ +bool internal_mat4_isidentity(Mat4* m); -void mat4_settr(Vec3* pos, Quat* rot, Mat4* out); /*用旋转和平移初始化mat4*/ -void mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out); -void mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out); +void internal_mat4_settr(Vec3* pos, Quat* rot, Mat4* out); /*用旋转和平移初始化mat4*/ +void internal_mat4_settrs(Vec3* pos, Quat* rot, Vec3* scale, Mat4* out); +void internal_mat4_settrinverse(Vec3* pos, Quat* rot, Mat4* out); -void mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out); /* m1的行乘m2的列,意义是用m1变换m2 */ +void internal_mat4_multiply(Mat4* m1, Mat4* m2, Mat4* out); /* m1的行乘m2的列,意义是用m1变换m2 */ -void mat4_transpose(Mat4* m, Mat4* out); +void internal_mat4_transpose(Mat4* m, Mat4* out); -void mat4_scale(Mat4* m, Vec3* scale, Mat4* out);/* 后乘post-multiply scale */ -void mat4_translate(Mat4* m, Vec3* pos, Mat4* out); /* 后乘post-multiply translate */ -void mat4_rotate(Mat4*m, float angle, Vec3* rot, Mat4* out);/*后乘绕任意轴向量旋转矩阵*/ +void internal_mat4_scale(Mat4* m, Vec3* scale, Mat4* out);/* 后乘post-multiply scale */ +void internal_mat4_translate(Mat4* m, Vec3* pos, Mat4* out); /* 后乘post-multiply translate */ +void internal_mat4_rotate(Mat4*m, float angle, Vec3* rot, Mat4* out);/*后乘绕任意轴向量旋转矩阵*/ -bool mat4_invertfull(Mat4* in, Mat4* out); /* 并不是所有矩阵都能求逆 */ -bool mat4_invertgeneral3d(Mat4* in, Mat4* out); /* 对scale rotate translate求逆 */ -void mat4_invertscale(Mat4* scale, Mat4* out); /* 对缩放矩阵求逆 */ -void mat4_invertrot(Mat4* rot, Mat4* out); /* 对旋转矩阵求逆 */ -void mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */ +bool internal_mat4_invertfull(Mat4* in, Mat4* out); /* 并不是所有矩阵都能求逆 */ +bool internal_mat4_invertgeneral3d(Mat4* in, Mat4* out); /* 对scale rotate translate求逆 */ +void internal_mat4_invertscale(Mat4* scale, Mat4* out); /* 对缩放矩阵求逆 */ +void internal_mat4_invertrot(Mat4* rot, Mat4* out); /* 对旋转矩阵求逆 */ +void internal_mat4_invertpos(Mat4* pos, Mat4* out); /* 对平移矩阵求逆 */ -void mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/ +void internal_mat4_decomposetrs(Mat4* src, Vec3* pos, Quat* quat, Vec3* scale); /*分解trs矩阵*/ -void mat4_mulvec4(Mat4* m, Vec4* v, Vec4* out); -void mat4_mulvec3(Mat4* m, Vec3* v, Vec3* out); +void internal_mat4_mulvec4(Mat4* m, Vec4* v, Vec4* out); +void internal_mat4_mulvec3(Mat4* m, Vec3* v, Vec3* out); -bool mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */ -void mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/ +bool internal_mat4_toeuler(Mat4* in, Euler* out); /* 计算YXZ旋转矩阵的欧拉角 */ +void internal_mat4_toquat(Mat4* in, Quat* out); /*in是正交矩阵*/ #define ROWMAT(A, ...)\ -Mat4 A={__VA_ARGS__};mat4_transpose(&A, &A); +Mat4 A={__VA_ARGS__};internal_mat4_transpose(&A, &A); void mat3_multvec3(Mat3* m, Vec3* v, Vec3* out); void mat23_applytovec3(Mat23* m, Vec3* v, Vec2* out); @@ -259,47 +259,47 @@ void mat43_applytovec3(Mat43* m, Vec3* v, Vec4* out); /* Quat */ /************************************************************************/ -void quat_tostring(Quat* q, char str[]); -void quat_print(Quat* q); +void internal_quat_tostring(Quat* q, char str[]); +void internal_quat_print(Quat* q); -Quat quat_make(float rx, float ry, float rz); +Quat internal_quat_make(float rx, float ry, float rz); -void euler_toquat(Euler* e, Quat* out); -void euler_deg2rad(Euler* in, Euler* out); -void euler_rad2deg(Euler* in, Euler* out); +void internal_euler_toquat(Euler* e, Quat* out); +void internal_euler_deg2rad(Euler* in, Euler* out); +void internal_euler_rad2deg(Euler* in, Euler* out); -void euler_tostring(Euler* v, char buf[]); -void euler_print(Euler* v); +void internal_euler_tostring(Euler* v, char buf[]); +void internal_euler_print(Euler* v); -void quat_fromaxisangle(Vec3* axis, float angle, Quat* out); /*轴角转四元数*/ -void quat_fromeuler(Euler* euler, Quat* out); /*按照zxy顺序*/ +void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out); /*轴角转四元数*/ +void internal_quat_fromeuler(Euler* euler, Quat* out); /*按照zxy顺序*/ -void quat_tomat4(Quat* q, Mat4* out); -void quat_toeuler(Quat*q, Euler* out); +void internal_quat_tomat4(Quat* q, Mat4* out); +void internal_quat_toeuler(Quat*q, Euler* out); -void quat_normalize(Quat* q, Quat* out); /*解决蠕变,保持四元数合法*/ +void internal_quat_normalize(Quat* q, Quat* out); /*解决蠕变,保持四元数合法*/ -void quat_scale(Quat* q, float scale, Quat* out); -void quat_rotate(); +void internal_quat_scale(Quat* q, float scale, Quat* out); +void internal_quat_rotate(); -void quat_minus(Quat* q1, Quat* q2, Quat* out); -void quat_slerp(Quat* start, Quat* end, float t, Quat* out); -void quat_lerp(Quat* start, Quat* end, float t, Quat* out); -void quat_translate(Quat* q, Vec4* v, Vec4* out); -void quat_invert(Quat* q, Quat* out); -float quat_dot(Quat* q1, Quat* q2); -void quat_multiply(Quat* q1, Quat* q2, Quat* out); -void quat_devide(Quat* q, float k, Quat* out); -void quat_negtive(Quat* in, Quat* out); -bool quat_isidentity(Quat* q); +void internal_quat_minus(Quat* q1, Quat* q2, Quat* out); +void internal_quat_slerp(Quat* start, Quat* end, float t, Quat* out); +void internal_quat_lerp(Quat* start, Quat* end, float t, Quat* out); +void internal_quat_translate(Quat* q, Vec4* v, Vec4* out); +void internal_quat_invert(Quat* q, Quat* out); +float internal_quat_dot(Quat* q1, Quat* q2); +void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out); +void internal_quat_devide(Quat* q, float k, Quat* out); +void internal_quat_negtive(Quat* in, Quat* out); +bool internal_quat_isidentity(Quat* q); -void quat_applytovec3(Quat* q, Vec3* v, Vec3* out); /*用四元数直接旋转向量*/ +void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out); /*用四元数直接旋转向量*/ -void quat_conjugate(Quat* in, Quat* out); +void internal_quat_conjugate(Quat* in, Quat* out); -bool quat_setlookrotation(Vec3* view, Vec3* up, Quat* out); +bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out); -float quat_magnitude(Quat* q); -float quat_magnitude2(Quat* q); +float internal_quat_magnitude(Quat* q); +float internal_quat_magnitude2(Quat* q); #endif
\ No newline at end of file diff --git a/src/math/quat.c b/src/math/quat.c index 8f7274d..07215d2 100644 --- a/src/math/quat.c +++ b/src/math/quat.c @@ -35,59 +35,59 @@ sharedEuler = *v;\ v = &sharedEuler;\ }while(0) -void quat_tostring(Quat* v, char buf[]) { +void internal_quat_tostring(Quat* v, char buf[]) { ssr_assert(v); sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void quat_print(Quat* q) { +void internal_quat_print(Quat* q) { ssr_assert(q); - quat_tostring(q, printbuffer); + internal_quat_tostring(q, printbuffer); printf("\n%s\n", printbuffer); } -void euler_tostring(Euler* v, char buf[]) { +void internal_euler_tostring(Euler* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void euler_print(Euler* v) { - euler_tostring(v, printbuffer); +void internal_euler_print(Euler* v) { + internal_euler_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void euler_deg2rad(Euler* in, Euler* out) { +void internal_euler_deg2rad(Euler* in, Euler* out) { ssr_assert(in && out); out->x = radians(in->x); out->y = radians(in->y); out->z = radians(in->z); } -void euler_rad2deg(Euler* in, Euler* out) { +void internal_euler_rad2deg(Euler* in, Euler* out) { ssr_assert(in && out); out->x = degree(in->x); out->y = degree(in->y); out->z = degree(in->z); } -Quat quat_make(float rx, float ry, float rz) { +Quat internal_quat_make(float rx, float ry, float rz) { Quat rot; Euler euler = {rx, ry, rz}; - quat_fromeuler(&euler, &rot); + internal_quat_fromeuler(&euler, &rot); return rot; } -void euler_toquat(Euler* euler, Quat* out) { +void internal_euler_toquat(Euler* euler, Quat* out) { ssr_assert(euler && out); - quat_fromeuler(euler, out); + internal_quat_fromeuler(euler, out); } /*这里精度不对*/ -bool quat_isidentity(Quat* q) { - return compare(quat_magnitude(q), 1.f); +bool internal_quat_isidentity(Quat* q) { + return compare(internal_quat_magnitude(q), 1.f); } -void quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { - ssr_assert(compare(vec3_magnitude2(axis), 1.f)); +void internal_quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { + ssr_assert(compare(internal_vec3_magnitude2(axis), 1.f)); angle = radians(angle); float half = angle * 0.5; float s = sin(half); @@ -97,10 +97,10 @@ void quat_fromaxisangle(Vec3* axis, float angle, Quat* out) { out->z = s * axis->z; } -void quat_fromeuler(Euler* el, Quat* out) { +void internal_quat_fromeuler(Euler* el, Quat* out) { ssr_assert(el && out); Euler euler; - euler_deg2rad(el, &euler); + internal_euler_deg2rad(el, &euler); float cx = cos(euler.x * 0.5f); float sx = sin(euler.x * 0.5f); float cy = cos(euler.y * 0.5f); @@ -111,15 +111,15 @@ void quat_fromeuler(Euler* el, Quat* out) { Quat qy = {0, sy, 0,cy}; Quat qz = {0, 0,sz,cz}; /*按ZXY顺序*/ - quat_multiply(&qx, &qz, &qx); - quat_multiply(&qy, &qx, out); - ssr_assert(quat_isidentity(out)); + internal_quat_multiply(&qx, &qz, &qx); + internal_quat_multiply(&qy, &qx, out); + ssr_assert(internal_quat_isidentity(out)); } /* ** 四元数直接对向量进行旋转和先转成矩阵形式再旋转计算量一样 */ -void quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { +void internal_quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { ssr_assert(q && v && out); if (v == out) { shrvec3(v); @@ -145,13 +145,13 @@ void quat_applytovec3(Quat* q, Vec3* v, Vec3* out) { out->z = (xz - wy) * v->x + (yz + wx) * v->y + (1.0f - (xx + yy)) * v->z; } -void quat_tomat4(Quat* q, Mat4* out) { +void internal_quat_tomat4(Quat* q, Mat4* out) { ssr_assert(q && out); - ssr_assert(quat_isidentity(q)); + ssr_assert(internal_quat_isidentity(q)); - mat4_setidentity(out); + internal_mat4_setidentity(out); - float x = q->x * 2.0F; /*从quat_applytovec3能得到矩阵形式*/ + float x = q->x * 2.0F; /*从internal_quat_applytovec3能得到矩阵形式*/ float y = q->y * 2.0F; float z = q->z * 2.0F; float xx = q->x * x; @@ -164,7 +164,7 @@ void quat_tomat4(Quat* q, Mat4* out) { float wy = q->w * y; float wz = q->w * z; - /*和mat4_setaxisangle实际上结果一样*/ + /*和internal_mat4_setaxisangle实际上结果一样*/ out->l[0] = 1.0f - (yy + zz); out->l[1] = xy + wz; out->l[2] = xz - wy; @@ -178,20 +178,20 @@ void quat_tomat4(Quat* q, Mat4* out) { out->l[10] = 1.0f - (xx + yy); } -void quat_toeuler(Quat* q, Euler* out) { +void internal_quat_toeuler(Quat* q, Euler* out) { ssr_assert(q && out); Mat4 mat; - quat_tomat4(q, &mat); /*计算变换矩阵*/ - mat4_toeuler(&mat, out); /*mat是按照RyRxRz顺序(z->y->x)的旋转矩阵*/ + internal_quat_tomat4(q, &mat); /*计算变换矩阵*/ + internal_mat4_toeuler(&mat, out); /*mat是按照RyRxRz顺序(z->y->x)的旋转矩阵*/ } -void quat_normalize(Quat* q, Quat* out) { +void internal_quat_normalize(Quat* q, Quat* out) { ssr_assert(q && out); - float mag = quat_magnitude(q); - quat_scale(q, 1.f/mag, out); + float mag = internal_quat_magnitude(q); + internal_quat_scale(q, 1.f/mag, out); } -void quat_scale(Quat* q, float scale, Quat* out) { +void internal_quat_scale(Quat* q, float scale, Quat* out) { ssr_assert(q && out); out->x = q->x * scale; out->y = q->y * scale; @@ -199,7 +199,7 @@ void quat_scale(Quat* q, float scale, Quat* out) { out->w = q->w * scale; } -void quat_negtive(Quat* in, Quat* out) { +void internal_quat_negtive(Quat* in, Quat* out) { ssr_assert(in && out); out->w = -in->w; out->x = -in->x; @@ -207,7 +207,7 @@ void quat_negtive(Quat* in, Quat* out) { out->z = -in->z; } -void quat_devide(Quat* q, float k, Quat* out) { +void internal_quat_devide(Quat* q, float k, Quat* out) { ssr_assert(q && out); k = 1 / k; out->w = q->w * k; @@ -216,31 +216,31 @@ void quat_devide(Quat* q, float k, Quat* out) { out->z = q->z * k; } -void quat_invert(Quat* q, Quat* out) { +void internal_quat_invert(Quat* q, Quat* out) { ssr_assert(q && out); - float mag2 = quat_magnitude2(q); - quat_conjugate(q, out); - quat_devide(out, mag2, out); + float mag2 = internal_quat_magnitude2(q); + internal_quat_conjugate(q, out); + internal_quat_devide(out, mag2, out); /*实际上如果是单位四元数,共轭就是逆*/ } -bool quat_setlookrotation(Vec3* view, Vec3* up, Quat* out) { +bool internal_quat_setlookrotation(Vec3* view, Vec3* up, Quat* out) { ssr_assert(view && up && out); Mat4 m; - mat4_setlookrotation(view, up, &m); /*先以view为准构建正交矩阵*/ - mat4_toquat(&m, out); + internal_mat4_setlookrotation(view, up, &m); /*先以view为准构建正交矩阵*/ + internal_mat4_toquat(&m, out); return 1; } /*q1 * q2^-1*/ -void quat_minus(Quat* q1, Quat* q2, Quat* out) { +void internal_quat_minus(Quat* q1, Quat* q2, Quat* out) { ssr_assert(q1 && q2 && out); - Quat q2i; quat_invert(q2, &q2i); - quat_multiply(q1, &q2i, out); + Quat q2i; internal_quat_invert(q2, &q2i); + internal_quat_multiply(q1, &q2i, out); } /*q1*q2*/ -void quat_multiply(Quat* q1, Quat* q2, Quat* out) { +void internal_quat_multiply(Quat* q1, Quat* q2, Quat* out) { ssr_assert(q1 && q2 && out); float w1 = q1->w, x1 = q1->x, y1 = q1->y, z1 = q1->z, @@ -254,7 +254,7 @@ void quat_multiply(Quat* q1, Quat* q2, Quat* out) { /* ** 共轭的几何意义是颠倒旋转轴方向,代表了和q相反的角位移 */ -void quat_conjugate(Quat* in, Quat* out) { +void internal_quat_conjugate(Quat* in, Quat* out) { ssr_assert(in && out); out->w = in->w; out->x = -in->x; @@ -262,25 +262,25 @@ void quat_conjugate(Quat* in, Quat* out) { out->z = -in->z; } -float quat_dot(Quat* q1, Quat* q2) { +float internal_quat_dot(Quat* q1, Quat* q2) { ssr_assert(q1 && q2); return (q1->x*q2->x + q1->y*q2->y + q1->z*q2->z + q1->w*q2->w); } -float quat_magnitude(Quat* q) { +float internal_quat_magnitude(Quat* q) { ssr_assert(q); - return sqrt(quat_dot(q, q)); + return sqrt(internal_quat_dot(q, q)); } -float quat_magnitude2(Quat* q) { +float internal_quat_magnitude2(Quat* q) { ssr_assert(q); - return quat_dot(q, q); + return internal_quat_dot(q, q); } -void quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { +void internal_quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { ssr_assert(q1 && q2 && out); - ssr_assert(quat_isidentity(q1) && quat_isidentity(q2)); /*适用于单位四元数*/ - float dot = quat_dot(q1, q2); + ssr_assert(internal_quat_isidentity(q1) && internal_quat_isidentity(q2)); /*适用于单位四元数*/ + float dot = internal_quat_dot(q1, q2); Quat temp; if (dot < 0.0f) { dot = -dot; @@ -302,14 +302,14 @@ void quat_slerp(Quat* q1, Quat* q2, float t, Quat* out) { out->z = (q1->z*sinaomt + temp.z*sinat)*sinadiv; out->w = (q1->w*sinaomt + temp.w*sinat)*sinadiv; } else { /*小范围时使用lerp*/ - quat_lerp(q1, &temp, t, out); + internal_quat_lerp(q1, &temp, t, out); } } -void quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { +void internal_quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { ssr_assert(q1 && q2 && out); float t2 = 1 - t; - if (quat_dot(q1, q2) < 0.f) { /*点乘不能是负的,翻转一个四元数的符号,使得落回360°*/ + if (internal_quat_dot(q1, q2) < 0.f) { /*点乘不能是负的,翻转一个四元数的符号,使得落回360°*/ out->x = q1->x * t2 - t * q2->x; out->y = q1->y * t2 - t * q2->y; out->z = q1->z * t2 - t * q2->z; @@ -320,5 +320,5 @@ void quat_lerp(Quat* q1, Quat* q2, float t, Quat* out) { out->z = q1->z * t2 + t * q2->z; out->w = q1->w * t2 + t * q2->w; } - quat_normalize(out, out); + internal_quat_normalize(out, out); } diff --git a/src/math/vec2.c b/src/math/vec2.c index 152f965..dc964e7 100644 --- a/src/math/vec2.c +++ b/src/math/vec2.c @@ -4,35 +4,35 @@ Vec2 vec2zero = { 0, 0}; -void vec2_scale(Vec2* v, float k, Vec2* out) { +void internal_vec2_scale(Vec2* v, float k, Vec2* out) { ssr_assert(v && out); out->x = v->x * k; out->y = v->y * k; } -void vec2_plus(Vec2* v1, Vec2* v2, Vec2* out) { +void internal_vec2_plus(Vec2* v1, Vec2* v2, Vec2* out) { ssr_assert(v1 && v2 && out); out->x = v1->x + v2->x; out->y = v1->y + v2->y; } -void vec2_offset(Vec2* v, float offset, Vec2* out) { +void internal_vec2_offset(Vec2* v, float offset, Vec2* out) { ssr_assert(v && out); out->x = v->x + offset; out->y = v->y + offset; } -float vec2_dot(Vec2* v1, Vec2* v2) { +float internal_vec2_dot(Vec2* v1, Vec2* v2) { ssr_assert(v1 && v2); float d = v1->x * v2->x + v1->y * v2->y; return d; } -void vec2_tostring(Vec2* v, char buf[]) { +void internal_vec2_tostring(Vec2* v, char buf[]) { sprintf(buf, "%8.3f %8.3f", v->x, v->y); } -void vec2_print(Vec2* v) { - vec2_tostring(v, printbuffer); +void internal_vec2_print(Vec2* v) { + internal_vec2_tostring(v, printbuffer); printf("%s", printbuffer); }
\ No newline at end of file diff --git a/src/math/vec3.c b/src/math/vec3.c index df56f3f..e9c12a4 100644 --- a/src/math/vec3.c +++ b/src/math/vec3.c @@ -9,58 +9,58 @@ Vec3 vec3up = {0, 1, 0}; Vec3 vec3left = {1, 0, 0}; Vec3 vec3zero = {0, 0, 0}; -void vec3_cross(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_cross(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->y*v2->z - v1->z*v2->y; out->y = v1->z*v2->x - v1->x*v2->z; out->z = v1->x*v2->y - v1->y*v2->x; } -Vec3 vec3_make(float x, float y, float z) { +Vec3 internal_vec3_make(float x, float y, float z) { Vec3 v = {x, y, z}; return v; } -void vec3_scale(Vec3* v, float k, Vec3* out) { +void internal_vec3_scale(Vec3* v, float k, Vec3* out) { ssr_assert(v && out); out->x = v->x * k; out->y = v->y * k; out->z = v->z * k; } -void vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out) { +void internal_vec3_scale3(Vec3* v, Vec3* scalar, Vec3* out) { ssr_assert(v && scalar && out); out->x = v->x * scalar->x; out->y = v->y * scalar->y; out->z = v->z * scalar->z; } -void vec3_plus(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_plus(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->x + v2->x; out->y = v1->y + v2->y; out->z = v1->z + v2->z; } -void vec3_offset(Vec3* v, float offset, Vec3* out) { +void internal_vec3_offset(Vec3* v, float offset, Vec3* out) { ssr_assert(v && out); out->x = v->x + offset; out->y = v->y + offset; out->z = v->z + offset; } -float vec3_magnitude(Vec3* v) { +float internal_vec3_magnitude(Vec3* v) { ssr_assert(v); - float l2 = vec3_magnitude2(v); + float l2 = internal_vec3_magnitude2(v); return sqrt(l2); } -float vec3_magnitude2(Vec3* v) { +float internal_vec3_magnitude2(Vec3* v) { ssr_assert(v); return v->x*v->x + v->y*v->y + v->z*v->z; } -void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { +void internal_vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { ssr_assert(v1 && v2 && out); float t2 = 1 - t; out->x = v1->x * t2 + v2->x * t; @@ -68,49 +68,49 @@ void vec3_lerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { out->z = v1->z * t2 + v2->z * t; } -void vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { +void internal_vec3_slerp(Vec3* v1, Vec3* v2, float t, Vec3* out) { ssr_assert(v1 && v2 && out); - float mag1 = vec3_magnitude(v1); - float mag2 = vec3_magnitude(v2); + float mag1 = internal_vec3_magnitude(v1); + float mag2 = internal_vec3_magnitude(v2); if (mag1 < EPSILON || mag2 < EPSILON) { - vec3_lerp(v1, v2, t, out); + internal_vec3_lerp(v1, v2, t, out); return; } float lerplen = lerp(mag1, mag2, t); /*分为长度上的lerp和角度上的slerp*/ - float dot = vec3_dot(v1, v2) / (mag1 * mag2); + float dot = internal_vec3_dot(v1, v2) / (mag1 * mag2); if (dot > 1.f - EPSILON) { /*几乎是同向*/ - vec3_lerp(v1, v2, t, out); + internal_vec3_lerp(v1, v2, t, out); return; } else if (dot < -1 + EPSILON) { /*几乎是反向*/ } else { - Vec3 axis; vec3_cross(v1, v2, &axis); vec3_normalize(&axis, &axis); - Vec3 v1n; vec3_normalize(v1, &v1n); + Vec3 axis; internal_vec3_cross(v1, v2, &axis); internal_vec3_normalize(&axis, &axis); + Vec3 v1n; internal_vec3_normalize(v1, &v1n); float angle = acos(dot) * t; - //Mat4 m; mat4_setaxisangle(&axis, angle, &m); - //mat4_mulvec4(&m, &v1n, &v1n); - Quat q; quat_fromaxisangle(&axis, angle, &q); - quat_applytovec3(&q, &v1n, &v1n); - vec3_scale(&v1n, lerplen, out); + //Mat4 m; internal_mat4_setaxisangle(&axis, angle, &m); + //internal_mat4_mulvec4(&m, &v1n, &v1n); + Quat q; internal_quat_fromaxisangle(&axis, angle, &q); + internal_quat_applytovec3(&q, &v1n, &v1n); + internal_vec3_scale(&v1n, lerplen, out); } } -float vec3_dot(Vec3* v1, Vec3* v2) { +float internal_vec3_dot(Vec3* v1, Vec3* v2) { ssr_assert(v1 && v2); return v1->x * v2->x + v1->y * v2->y + v1->z * v2->z; } -void vec3_multiply(Vec3* v1, Vec3* v2, Quat* out) { +void internal_vec3_multiply(Vec3* v1, Vec3* v2, Quat* out) { ssr_assert(v1 && v2 && out); - vec3_cross(v1, v2, out); - out->w = -vec3_dot(v1, v2); + internal_vec3_cross(v1, v2, out); + out->w = -internal_vec3_dot(v1, v2); } -void vec3_normalize(Vec3* v, Vec3* out) { +void internal_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 = vec3_magnitude(v); + float mag = internal_vec3_magnitude(v); if (compare(mag, 0)) { return; } @@ -120,16 +120,16 @@ void vec3_normalize(Vec3* v, Vec3* out) { out->z = v->z * mag; } -void vec3_tostring(Vec3* v, char buf[]) { +void internal_vec3_tostring(Vec3* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f", v->x, v->y, v->z); } -void vec3_print(Vec3* v) { - vec3_tostring(v, printbuffer); +void internal_vec3_print(Vec3* v) { + internal_vec3_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void vec3_minus(Vec3* v1, Vec3* v2, Vec3* out) { +void internal_vec3_minus(Vec3* v1, Vec3* v2, Vec3* out) { ssr_assert(v1 && v2 && out); out->x = v1->x - v2->x; out->y = v1->y - v2->y; diff --git a/src/math/vec4.c b/src/math/vec4.c index 3d6df46..24df08f 100644 --- a/src/math/vec4.c +++ b/src/math/vec4.c @@ -8,7 +8,7 @@ Vec4 vec4zero = { 0, 0, 0,0 }; static Vec4 sharedmat4; -void vec4_dividew(Vec4* v, Vec3* out) { +void internal_vec4_dividew(Vec4* v, Vec3* out) { ssr_assert(out && v); float w = 1.f / v->w; out->x = v->x * w ; @@ -16,7 +16,7 @@ void vec4_dividew(Vec4* v, Vec3* out) { out->z = v->z * w; } -void vec4_dividewnoz(Vec4* v, Vec4* out) { +void internal_vec4_dividewnoz(Vec4* v, Vec4* out) { ssr_assert(out && v); float w = 1.f / v->w; out->x = v->x * w; @@ -25,30 +25,30 @@ void vec4_dividewnoz(Vec4* v, Vec4* out) { out->w = v->w; } -void vec4_tostring(Vec4* v, char buf[]) { +void internal_vec4_tostring(Vec4* v, char buf[]) { sprintf(buf, "%8.3f %8.3f %8.3f %8.3f", v->x, v->y, v->z, v->w); } -void vec4_print(Vec4* v) { - vec4_tostring(v, printbuffer); +void internal_vec4_print(Vec4* v) { + internal_vec4_tostring(v, printbuffer); printf("\n%s\n", printbuffer); } -void vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) { +void internal_vec4_lerp(Vec4* a, Vec4* b, float t, Vec4* out) { out->x = lerp(a->x, b->x, t); out->y = lerp(a->y, b->y, t); out->z = lerp(a->z, b->z, t); out->w = lerp(a->w, b->w, t); } -void vec4_scale(Vec4* v, float t, Vec4* out) { +void internal_vec4_scale(Vec4* v, float t, Vec4* out) { out->x = v->x * t; out->y = v->y * t; out->z = v->z * t; out->w = v->w * t; } -void vec4_add(Vec4* v1, Vec4* v2, Vec4* out) { +void internal_vec4_add(Vec4* v1, Vec4* v2, Vec4* out) { out->x = v1->x + v2->x; out->y = v1->y + v2->y; out->z = v1->z + v2->z; diff --git a/src/shaders/common.h b/src/shaders/common.h index 61066e5..8d0ac83 100644 --- a/src/shaders/common.h +++ b/src/shaders/common.h @@ -63,14 +63,14 @@ if(cond) return 0; \ #define MVP_PROCESS \ do{ \ static Vec4 p; p.xyz = in->vertex->position; p.w = 1; \ -mat4_mulvec4(uniforms->mvp, &p, clipcoord); \ +internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); \ }while(0) -#define object2clip(pos, out) mat4_mulvec4(_mvp_matrix, pos, out); +#define object2clip(pos, out) internal_mat4_mulvec4(_mvp_matrix, pos, out); /*need defined _it_model_matrix of model matrix, i-nverse, t-ranspose*/ #define object2world_normal(normal, out) \ -mat4_mulvec4(_it_model_matrix, normal, out) +internal_mat4_mulvec4(_it_model_matrix, normal, out) /*take sample from normal map and translate to normal*/ #define unpack_normal(color, out_v3) \ diff --git a/src/shaders/default.c b/src/shaders/default.c index f184e27..6d12c8e 100644 --- a/src/shaders/default.c +++ b/src/shaders/default.c @@ -13,29 +13,29 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) { static Vec4 p; p.xyz = in->vertex->position; p.w = 1; - mat4_mulvec4(uniforms->mvp, &p, clipcoord); + internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); Vec4 normal = { in->vertex->normal.x, in->vertex->normal.y, in->vertex->normal.z, 1 }; - Vec4 worldnormal; mat4_mulvec4(object2world, &normal, &worldnormal); - vec3_normalize(light, light); - *rough = 1 - vec3_dot(&worldnormal, light); + Vec4 worldnormal; internal_mat4_mulvec4(object2world, &normal, &worldnormal); + internal_vec3_normalize(light, light); + *rough = 1 - internal_vec3_dot(&worldnormal, light); } static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { - vec3_normalize(light, light); + internal_vec3_normalize(light, light); float roughness = *rough; //(*color).r = 1; //(*color).g = 1; //(*color).b = 1; //(*color).a = 1; //return 1; - //float rough = 1- vec3_dot(&in->normal, light); + //float rough = 1- internal_vec3_dot(&in->normal, light); //Color32 nc = tex2d(noramltex, in->texcoord); - //vec3_scale(&c, roughness, &c); + //internal_vec3_scale(&c, roughness, &c); return 1; } diff --git a/src/shaders/pbr.c b/src/shaders/pbr.c index b6238f1..89556af 100644 --- a/src/shaders/pbr.c +++ b/src/shaders/pbr.c @@ -28,9 +28,9 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor in->vertex->normal.z, 1 }; - Vec4 worldnormal; mat4_mulvec4(_object2world, &normal, &worldnormal); - vec3_normalize(_light, _light); - //*rough = 1 - vec3_dot(&worldnormal, light); + Vec4 worldnormal; internal_mat4_mulvec4(_object2world, &normal, &worldnormal); + internal_vec3_normalize(_light, _light); + //*rough = 1 - internal_vec3_dot(&worldnormal, light); //*vnormal = in->vertex->normal; *_texcoord = in->vertex->texcoord; _clip_pos->x = clipcoord->z; @@ -38,24 +38,24 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoor } static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { - //vec3_normalize(light, light); - //vec3_normalize(vnormal, vnormal); + //internal_vec3_normalize(light, light); + //internal_vec3_normalize(vnormal, vnormal); //float roughness = *rough; //(*color).r = 1; //(*color).g = 1; //(*color).b = 1; //(*color).a = 1; //return 1; - //float rough = 1- vec3_dot(&in->normal, light); + //float rough = 1- internal_vec3_dot(&in->normal, light); float depth = _clip_pos->x / _clip_pos->y; depth = (depth + 1) / 2; depth = linear01depth(depth); Color32 c = tex2d(_albedo_tex, _texcoord); //Color32 nc = tex2d(noramltex, in->texcoord); - //vec3_scale(&c, roughness, &c); + //internal_vec3_scale(&c, roughness, &c); color32_saturate(&c); *color = c; - //vec3_scale(color, 1 - depth, color); + //internal_vec3_scale(color, 1 - depth, color); return 1; } diff --git a/src/shaders/unlit.c b/src/shaders/unlit.c index 9a807af..e887d07 100644 --- a/src/shaders/unlit.c +++ b/src/shaders/unlit.c @@ -10,20 +10,20 @@ static void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* clipcoord) { static Vec4 p; p.xyz = in->vertex->position; p.w = 1; - mat4_mulvec4(uniforms->mvp, &p, clipcoord); + internal_mat4_mulvec4(uniforms->mvp, &p, clipcoord); Vec4 normal = { in->vertex->normal.x, in->vertex->normal.y, in->vertex->normal.z, 1 }; - Vec4 worldnormal; mat4_mulvec4(object2world, &normal, &worldnormal); - vec3_normalize(light, light); - *reg_num_00 = 1 - vec3_dot(&worldnormal, light); + Vec4 worldnormal; internal_mat4_mulvec4(object2world, &normal, &worldnormal); + internal_vec3_normalize(light, light); + *reg_num_00 = 1 - internal_vec3_dot(&worldnormal, light); } static bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color32* color) { - vec3_normalize(light, light); + internal_vec3_normalize(light, light); return 1; } diff --git a/src/test/test_mat4.c b/src/test/test_mat4.c index e7a65fc..3ad56a4 100644 --- a/src/test/test_mat4.c +++ b/src/test/test_mat4.c @@ -19,8 +19,8 @@ Mat4 m2 = { static void test_multiply() { print("test_multiply\n"); Mat4 res; - mat4_multiply(&m1, &m2, &res); - mat4_print(&res); + internal_mat4_multiply(&m1, &m2, &res); + internal_mat4_print(&res); } static void test_invert() { @@ -32,11 +32,11 @@ static void test_invert() { 6, 0, 2, 1, }; Mat4 res, res2; - mat4_invertfull(&m, &res); - mat4_print(&res); - mat4_multiply(&m1, &m, &res2); - mat4_multiply(&res2, &res, &res2); - mat4_print(&res2); + internal_mat4_invertfull(&m, &res); + internal_mat4_print(&res); + internal_mat4_multiply(&m1, &m, &res2); + internal_mat4_multiply(&res2, &res, &res2); + internal_mat4_print(&res2); /* -0.250 0.333 1.667 -1.833 0.083 0.000 -0.667 0.833 @@ -48,68 +48,68 @@ static void test_invert() { static void test_transpose() { print("test_transpose\n"); Mat4 res; - mat4_transpose(&m1, &res); - mat4_print(&res); + internal_mat4_transpose(&m1, &res); + internal_mat4_print(&res); } static void test_scale() { print("test_scale\n"); Mat4 scale; - mat4_setscale(1, 2, 3, &scale); + internal_mat4_setscale(1, 2, 3, &scale); Mat4 res; - mat4_multiply(&scale, &m1, &res); - mat4_print(&scale); + internal_mat4_multiply(&scale, &m1, &res); + internal_mat4_print(&scale); } static void test_position() { print("test_position\n"); Mat4 pos; - mat4_setposition(1, 2, 3, &pos); - mat4_print(&pos); + internal_mat4_setposition(1, 2, 3, &pos); + internal_mat4_print(&pos); } static void test_rotation() { print("test_rotation\n"); Mat4 i; - mat4_setidentity(&i); + internal_mat4_setidentity(&i); Vec3 axis = { 0, 0, 1 }; - mat4_rotate(&i, 90, &axis, &i); - mat4_print(&i); + internal_mat4_rotate(&i, 90, &axis, &i); + internal_mat4_print(&i); } static void test_invertgeneral3d() { print("test_invertgeneral3d\n"); Mat4 trans; - mat4_setidentity(&trans); - print("original matrix"); mat4_print(&m1); + internal_mat4_setidentity(&trans); + print("original matrix"); internal_mat4_print(&m1); Vec3 v; - v.x = 1; v.y = 3.3; v.z = 0; mat4_translate(&trans, &v, &trans); - v.x = 1; v.y = 0; v.z = 0; mat4_rotate(&trans, 23, &v, &trans); - v.x = 1; v.y = 1.2; v.z = 1; mat4_scale(&trans, &v, &trans); - v.x = 1; v.y = 3; v.z = 0; mat4_rotate(&trans, 23, &v, &trans); - v.x = 2; v.y = 3.3; v.z = 0; mat4_translate(&trans, &v, &trans); - print("transform matrix"); mat4_print(&trans); + v.x = 1; v.y = 3.3; v.z = 0; internal_mat4_translate(&trans, &v, &trans); + v.x = 1; v.y = 0; v.z = 0; internal_mat4_rotate(&trans, 23, &v, &trans); + v.x = 1; v.y = 1.2; v.z = 1; internal_mat4_scale(&trans, &v, &trans); + v.x = 1; v.y = 3; v.z = 0; internal_mat4_rotate(&trans, 23, &v, &trans); + v.x = 2; v.y = 3.3; v.z = 0; internal_mat4_translate(&trans, &v, &trans); + print("transform matrix"); internal_mat4_print(&trans); Mat4 res; - mat4_multiply(&trans, &m1, &res); - mat4_invertgeneral3d(&trans, &trans); - print("inverse transform matrix"); mat4_print(&trans); - mat4_multiply(&trans, &res, &res); - print("inverted result"); mat4_print(&res); + internal_mat4_multiply(&trans, &m1, &res); + internal_mat4_invertgeneral3d(&trans, &trans); + print("inverse transform matrix"); internal_mat4_print(&trans); + internal_mat4_multiply(&trans, &res, &res); + print("inverted result"); internal_mat4_print(&res); } void test_invertrot() { print("test_invertrot\n"); - print("original matrix"); mat4_print(&m1); - Mat4 rot; mat4_setidentity(&rot); - Vec3 axis = { 1,2,3 }; mat4_rotate(&rot, 90, &axis, &rot); - print("rotation matrix"); mat4_print(&rot); + print("original matrix"); internal_mat4_print(&m1); + Mat4 rot; internal_mat4_setidentity(&rot); + Vec3 axis = { 1,2,3 }; internal_mat4_rotate(&rot, 90, &axis, &rot); + print("rotation matrix"); internal_mat4_print(&rot); Mat4 res; - mat4_multiply(&rot, &m1, &res); - print("result matrix"); mat4_print(&res); - mat4_invertgeneral3d(&rot, &rot); - print("inverse rotation matrix"); mat4_print(&rot); - mat4_multiply(&rot, &res, &res); - print("result matrix"); mat4_print(&res); + internal_mat4_multiply(&rot, &m1, &res); + print("result matrix"); internal_mat4_print(&res); + internal_mat4_invertgeneral3d(&rot, &rot); + print("inverse rotation matrix"); internal_mat4_print(&rot); + internal_mat4_multiply(&rot, &res, &res); + print("result matrix"); internal_mat4_print(&res); } TEST(test_orthogonalize) @@ -121,55 +121,55 @@ TEST(test_orthogonalize) ); Mat4 A; - TIME_STAMP("mat4_orthogonalize time used") + TIME_STAMP("internal_mat4_orthogonalize time used") for (int i = 0; i < 1000000; ++i) { - mat4_orthogonalize(&A0, &A); + internal_mat4_orthogonalize(&A0, &A); } TIME_STAMP_END - TIME_STAMP("mat4_isorthogonal time used") + TIME_STAMP("internal_mat4_isorthogonal time used") for (int i = 0; i < 1000000; ++i) { - mat4_isorthogonal(&A0); + internal_mat4_isorthogonal(&A0); } TIME_STAMP_END - mat4_print(&A); - float res = vec3_dot(&A.basis.y, &A.basis.x); + internal_mat4_print(&A); + float res = internal_vec3_dot(&A.basis.y, &A.basis.x); printf("%f\n", res); - printf("%d\n", mat4_isorthogonal(&A)); + printf("%d\n", internal_mat4_isorthogonal(&A)); Mat4 rev; - mat4_transpose(&A, &rev); - mat4_multiply(&A, &rev, &rev); - mat4_print(&rev); + internal_mat4_transpose(&A, &rev); + internal_mat4_multiply(&A, &rev, &rev); + internal_mat4_print(&rev); END TEST(test_toeuler) Euler euler = { 90, 0, 33 }; Quat rot; - quat_fromeuler(&euler, &rot); + internal_quat_fromeuler(&euler, &rot); Mat4 m; - quat_tomat4(&rot, &m); + internal_quat_tomat4(&rot, &m); Euler res; - quat_toeuler(&rot, &res); - euler_print(&res); + internal_quat_toeuler(&rot, &res); + internal_euler_print(&res); END TEST(test_rotatematrix) Euler euler = { 10, 20, 30 }; Quat rot; - quat_fromeuler(&euler, &rot); + internal_quat_fromeuler(&euler, &rot); Mat4 m; - quat_tomat4(&rot, &m); - mat4_print(&m); + internal_quat_tomat4(&rot, &m); + internal_mat4_print(&m); Mat4 mz, mx, my; - mat4_setrotatez(30, &mz); - mat4_setrotatex(10, &mx); - mat4_setrotatey(20, &my); - mat4_multiply(&mx, &mz, &mx); - mat4_multiply(&my, &mx, &m); - mat4_print(&m); - mat4_setrotate(10, 20, 30, &m); - mat4_print(&m); + internal_mat4_setrotatez(30, &mz); + internal_mat4_setrotatex(10, &mx); + internal_mat4_setrotatey(20, &my); + internal_mat4_multiply(&mx, &mz, &mx); + internal_mat4_multiply(&my, &mx, &m); + internal_mat4_print(&m); + internal_mat4_setrotate(10, 20, 30, &m); + internal_mat4_print(&m); END TEST(test_isidentity) @@ -181,7 +181,7 @@ TEST(test_isidentity) ) bool res; TIME_STAMP_1000000("test is isidentity") - res = mat4_isidentity(&mat); + res = internal_mat4_isidentity(&mat); TIME_STAMP_END printf("%d\n", res); END diff --git a/src/test/test_quat.c b/src/test/test_quat.c index 5d86842..1b85361 100644 --- a/src/test/test_quat.c +++ b/src/test/test_quat.c @@ -6,25 +6,25 @@ TEST(test_euler) Euler e = {90, 90, 0}; Vec3 p = {2,3,4}, res; Quat qut; -quat_fromeuler(&e, &qut); -quat_applytovec3(&qut, &p, &res); -vec3_print(&res); +internal_quat_fromeuler(&e, &qut); +internal_quat_applytovec3(&qut, &p, &res); +internal_vec3_print(&res); /*用轴角定义*/ Vec3 axis = { 1, 0,0 }; Quat qut2; -quat_fromaxisangle(&axis, 90, &qut2); -quat_applytovec3(&qut, &p, &res); -vec3_print(&res); +internal_quat_fromaxisangle(&axis, 90, &qut2); +internal_quat_applytovec3(&qut, &p, &res); +internal_vec3_print(&res); Mat4 mat; -quat_tomat4(&qut, &mat); -mat4_print(&mat); +internal_quat_tomat4(&qut, &mat); +internal_mat4_print(&mat); Vec4 p2 = { 2,3,4,1 }; Vec4 res2; -mat4_mulvec4(&mat, &p2, &res2); -vec4_print(&res2); +internal_mat4_mulvec4(&mat, &p2, &res2); +internal_vec4_print(&res2); END @@ -32,46 +32,46 @@ TEST(test_quat2euler) Euler e = { 10, 20, 30}; Quat q; -euler_toquat(&e, &q); -euler_deg2rad(&e, &e); -quat_print(&q); -euler_print(&e); +internal_euler_toquat(&e, &q); +internal_euler_deg2rad(&e, &e); +internal_quat_print(&q); +internal_euler_print(&e); Euler res; -quat_toeuler(&q, &res); +internal_quat_toeuler(&q, &res); -euler_print(&res); +internal_euler_print(&res); -quat_fromeuler(&res, &q); -quat_print(&q); +internal_quat_fromeuler(&res, &q); +internal_quat_print(&q); END TEST(test_conjugate) Quat rot; Vec3 axis = {2,3,4}; -vec3_normalize(&axis, &axis); -quat_fromaxisangle(&axis, 46, &rot); +internal_vec3_normalize(&axis, &axis); +internal_quat_fromaxisangle(&axis, 46, &rot); Vec3 p = {12, 3, 55}; -vec3_print(&p); +internal_vec3_print(&p); Vec3 res; -quat_applytovec3(&rot, &p, &res); -vec3_print(&res); +internal_quat_applytovec3(&rot, &p, &res); +internal_vec3_print(&res); Quat rev; -quat_invert(&rot, &rev); -quat_applytovec3(&rev, &res, &res); -vec3_print(&res); +internal_quat_invert(&rot, &rev); +internal_quat_applytovec3(&rev, &res, &res); +internal_vec3_print(&res); END TEST(test_lookrotation) Vec3 view = {1, 0, 0}; Vec3 up = {0, 1, 0}; Quat rot; -quat_setlookrotation(&view, &up, &rot); -quat_print(&rot); +internal_quat_setlookrotation(&view, &up, &rot); +internal_quat_print(&rot); Euler euler; -quat_toeuler(&rot, &euler); -euler_print(&euler); +internal_quat_toeuler(&rot, &euler); +internal_euler_print(&euler); END void test_quat() { |