diff --git a/src/cam.c b/src/cam.c index c167032..85dec46 100644 --- a/src/cam.c +++ b/src/cam.c @@ -1,18 +1,18 @@ #include "math/mat4.h" -#include "math/vec3.h" #include "math/vec4.h" #include Mat4f_t perspCam(float fov, float asp, float near, float far) { - const float t = 1 / tanf(fov/2); + const float t = 1.f / tanf(fov*0.5f); + const float fn = near - far; const float persp[16] = { t/asp, 0.f, 0.f, 0.f, 0.f, t, 0.f, 0.f, - 0.f, 0.f, (far + near) / (far - near), -1.f, - 0.f, 0.f, (2.f * far * near) / (far - near), 0.f + 0.f, 0.f, (far + near) / fn, -1.f, + 0.f, 0.f, (2.f * far * near) / fn, 0.f }; return mat4f_from_array(persp); @@ -27,4 +27,10 @@ Vec4f_t* normCoord_r(Vec4f_t* vec) return vec; } -// Vec3f_t \ No newline at end of file +// first off, apply to your vec normCoord fn +Vec4f_t* viewport_r(Vec4f_t* vec, float width, float height) +{ + vec->x = (vec->x + 1.f) * 0.5f * width; + vec->y = (1.f - vec->y) * 0.5f * height; + return vec; +} \ No newline at end of file diff --git a/src/cam.h b/src/cam.h index e69de29..f533b32 100644 --- a/src/cam.h +++ b/src/cam.h @@ -0,0 +1,6 @@ +#include "math/mat4.h" +#include "math/vec4.h" + +Mat4f_t perspCam(float fov, float asp, float near, float far); +Vec4f_t* normCoord_r(Vec4f_t* vec); +Vec4f_t* viewport_r(Vec4f_t* vec, float width, float height); \ No newline at end of file diff --git a/src/main.c b/src/main.c index b8cec70..90aa6ee 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,38 @@ #include -#include "math/mat4.h" +#include "cam.h" +#include "math/vec4.h" + +Vec4f_t mat4f_mul_vec4f(const Mat4f_t mat, Vec4f_t v) +{ + Vec4f_t out; + for (int i = 0; i < 4; i++) + { + out.data[i] = 0.f; + for (int j = 0; j < 4; j++) + out.data[i] += mat.m[i*4 + j] * v.data[j]; + } + return out; +} int main(int argc, const char** argv) { - printf("hello world"); + const float fov = 60; + const float width = 1000, height = 800; + const float near = 0.1, far = 1000; + + const float aspect = width / height; + + // xyz = [-1, 1] + Vec4f_t vec = vec4f(0.f, 0.f, -1.f, 1.f); + Mat4f_t proj = perspCam(fov, aspect, near, far); + + Vec4f_t v_clip = mat4f_mul_vec4f(proj, vec); + + normCoord_r(&v_clip); + viewport_r(&v_clip, width, height); + + printf("Screen coordinates: x = %f, y = %f, z = %f\n", + v_clip.x, v_clip.y, v_clip.z); + return 0; } \ No newline at end of file