From 8d4a747e1f0ad35c00c52a88bfdcc70034da6591 Mon Sep 17 00:00:00 2001 From: Krzosa Karol Date: Wed, 16 Feb 2022 23:24:19 +0100 Subject: [PATCH] Fixed triangle drawing, used to draw gaps in between triangles --- main.cpp | 105 ++++++++++++++++++++++++++++++++++++------------------- math.h | 21 +++++++++++ 2 files changed, 91 insertions(+), 35 deletions(-) diff --git a/main.cpp b/main.cpp index 3e084c8..7cef7fc 100644 --- a/main.cpp +++ b/main.cpp @@ -1,3 +1,26 @@ +/* Things to do: +OK Drawing triangles +OK Drawing cubes and lines for testing +OK Y up coordinate system, left handed +OK Drawing a cube with perspective +OK Culling triangles facing away from camera +OK Texture mapping +? Basic math operations on Vec4 Mat4 - Muls, Dot, Cross etc. +OK Basic linear transformations - rotation, translation, scaling +* Bilinear filtering of textures / subpixel precison +* Perspective matrix vs simple perspective +* Perspective correct interpolation +* Depth buffer +* FPS Camera +* Reading OBJ files +* Rendering multiple objects, queue renderer +* Clipping +* Optimizations +* SIMD +* Multithreading +* +*/ + #include "main.h" #include "platform.h" #include "math.h" @@ -6,8 +29,9 @@ // TODO: // Perspective correct interpolation +// Camera +// Perspective projection // Texture mapping -// Counter clockwise triangle culling // Reading OBJ struct Face { @@ -56,42 +80,52 @@ void DrawRect(Image* dst, float X, float Y, float w, float h, uint32_t color) { } FUNCTION -float EdgeFunction(Vec3 vecp0, Vec3 vecp1, Vec3 p) { +float EdgeFunction(Vec4 vecp0, Vec4 vecp1, Vec4 p) { float result = (vecp1.y - vecp0.y) * (p.x - vecp0.x) - (vecp1.x - vecp0.x) * (p.y - vecp0.y); return result; } FUNCTION -void DrawTriangle(Image* dst, Image *src, Vec3 p0, Vec3 p1, Vec3 p2, +void DrawTriangle(Image* dst, Image *src, Vec4 p0, Vec4 p1, Vec4 p2, Vec2 tex0, Vec2 tex1, Vec2 tex2) { - float min_x = (float)(MIN(p0.x, MIN(p1.x, p2.x)) + 0.5f); - float min_y = (float)(MIN(p0.y, MIN(p1.y, p2.y)) + 0.5f); - float max_x = (float)(MAX(p0.x, MAX(p1.x, p2.x)) + 0.5f); - float max_y = (float)(MAX(p0.y, MAX(p1.y, p2.y)) + 0.5f); - min_x = MAX(0, min_x); - min_y = MAX(0, min_y); - max_x = MIN(dst->x-1, max_x); - max_y = MIN(dst->y-1, max_y); + float min_x = (float)(MIN(p0.x, MIN(p1.x, p2.x))); + float min_y = (float)(MIN(p0.y, MIN(p1.y, p2.y))); + float max_x = (float)(MAX(p0.x, MAX(p1.x, p2.x))); + float max_y = (float)(MAX(p0.y, MAX(p1.y, p2.y))); + min_x = MAX(0, min_x) + 0.5f; + min_y = MAX(0, min_y) + 0.5f; + max_x = MIN(dst->x-1, max_x) + 0.5f; + max_y = MIN(dst->y-1, max_y) + 0.5f; + //@Todo: Fix the proper rounding + //@Todo: Determine whether we need subprecision etc float area = EdgeFunction(p0, p1, p2); - for (float y = min_y; y < max_y; y++) { - for (float x = min_x; x < max_x; x++) { - float edge1 = EdgeFunction(p0, p1, { x,y }); - float edge2 = EdgeFunction(p1, p2, { x,y }); - float edge3 = EdgeFunction(p2, p0, { x,y }); + for (int y = min_y; y < max_y; y++) { + for (int x = min_x; x < max_x; x++) { + float edge1 = EdgeFunction(p0, p1, { (float)x,(float)y }); + float edge2 = EdgeFunction(p1, p2, { (float)x,(float)y }); + float edge3 = EdgeFunction(p2, p0, { (float)x,(float)y }); if (edge1 >= 0 && edge2 >= 0 && edge3 >= 0) { - int xi = (int)(x + 0.5f); - int yi = (int)(y + 0.5f); float w1 = edge2 / area; float w2 = edge3 / area; float w3 = edge1 / area; +#if 1 + float u = tex0.x * (w1 / p0.w) + tex1.x * (w2 / p1.w) + tex2.x * (w3 / p2.w); + float v = tex0.y * (w1 / p0.w) + tex1.y * (w2 / p1.w) + tex2.y * (w3 / p2.w); + + float interpolated_z = (1.f / p0.w) * w1 + (1.f / p1.w) * w2 + (1.f / p2.w) * w3; + u /= interpolated_z; + v /= interpolated_z; +#else float u = tex0.x * w1 + tex1.x * w2 + tex2.x * w3; float v = tex0.y * w1 + tex1.y * w2 + tex2.y * w3; +#endif + int ui = (int)(u * (src->x - 1) + 0.5f); int vi = (int)(v * (src->y - 1) + 0.5f); uint32_t pixel = src->pixels[ui + (src->y - 1 - vi) * src->x]; - dst->pixels[xi + (dst->y - 1 - yi) * dst->x] = pixel; + dst->pixels[x + (dst->y - 1 - y) * dst->x] = pixel; } } } @@ -124,9 +158,8 @@ int main() { Vec3 camera_pos = {0,0,-5}; int x,y,n; unsigned char *data = stbi_load("assets/cat.png", &x, &y, &n, 4); - Image img = { - (uint32_t *)data, x, y - }; + Image img = {(uint32_t *)data, x, y}; + Mat4 perspective = Mat4Perspective(60.f, screen.x, screen.y, 0.1f, 100.f); while (OS_GameLoop()) { for (int y = 0; y < screen.y; y++) { for (int x = 0; x < screen.x; x++) { @@ -143,10 +176,10 @@ int main() { if (keydown_b) rotation -= 0.05f; for (int i = 0; i < ARRAY_CAP(cube_faces); i++) { Face* face = cube_faces + i; - Vec3 pos[3] = { - cube_vertices[face->p[0] - 1], - cube_vertices[face->p[1] - 1], - cube_vertices[face->p[2] - 1], + Vec4 pos[3] = { + vec4(cube_vertices[face->p[0] - 1], 1), + vec4(cube_vertices[face->p[1] - 1], 1), + vec4(cube_vertices[face->p[2] - 1], 1), }; //@Note: Transform @@ -154,17 +187,19 @@ int main() { pos[j] = transform * pos[j]; } //@Note: Cull - Vec3 p0_to_camera = camera_pos - pos[0]; - Vec3 p0_to_p1 = pos[1] - pos[0]; - Vec3 p0_to_p2 = pos[2] - pos[0]; + Vec3 p0_to_camera = camera_pos - pos[0].xyz; + Vec3 p0_to_p1 = pos[1].xyz - pos[0].xyz; + Vec3 p0_to_p2 = pos[2].xyz - pos[0].xyz; Vec3 normal = Cross(p0_to_p1, p0_to_p2); if (Dot(normal, p0_to_camera) > 0) { for (int j = 0; j < 3; j++) { //@Note: Camera - pos[j] = pos[j] - camera_pos; + pos[j].xyz = pos[j].xyz - camera_pos; //@Note: Perspective - pos[j].x /= pos[j].z; - pos[j].y /= pos[j].z; + pos[j] = perspective * pos[j]; + pos[j].x = pos[j].x / pos[j].w; + pos[j].y = pos[j].y / pos[j].w; + pos[j].z = pos[j].z / pos[j].w; //@Note: To pixel space pos[j].x *= screen.x / 2; pos[j].y *= screen.y / 2; @@ -172,9 +207,9 @@ int main() { pos[j].y += screen.y / 2; } DrawTriangle(&screen, &img, pos[0], pos[1], pos[2], face->tex[0], face->tex[1], face->tex[2]); - DrawLine(&screen, pos[0].x, pos[0].y, pos[1].x, pos[1].y); - DrawLine(&screen, pos[1].x, pos[1].y, pos[2].x, pos[2].y); - DrawLine(&screen, pos[2].x, pos[2].y, pos[0].x, pos[0].y); + //DrawLine(&screen, pos[0].x, pos[0].y, pos[1].x, pos[1].y); + //DrawLine(&screen, pos[1].x, pos[1].y, pos[2].x, pos[2].y); + //DrawLine(&screen, pos[2].x, pos[2].y, pos[0].x, pos[0].y); } } #endif diff --git a/math.h b/math.h index 7ab6a45..9edd06f 100644 --- a/math.h +++ b/math.h @@ -21,6 +21,14 @@ union Vec4 { struct { Vec3 xyz; }; }; +constexpr float PI32 = 3.14159265359f; + +FUNCTION +Vec4 vec4(Vec3 a, float b) { + Vec4 result = { a.x,a.y,a.z,b }; + return result; +} + FUNCTION Mat4 Mat4Identity() { Mat4 result = { @@ -71,6 +79,19 @@ Mat4 Mat4RotationX(float rotation) { return result; } +FUNCTION +Mat4 Mat4Perspective(float fov, float window_x, float window_y, float znear, float zfar) { + float aspect_ratio = window_y / window_x; + float f = (1.f / tanf((fov/2.f)*(180.f/PI32))); + Mat4 result = { + aspect_ratio*f, 0, 0, 0, + 0, f, 0, 0, + 0, 0, (zfar)-(zfar-znear),(-zfar*znear)-(zfar - znear), + 0,0,1,0 + }; + return result; +} + FUNCTION Mat4 Mat4Translate(Mat4 a, Vec3 translation) { a.p[0][0] += translation.x;