/* Matrices: - Column-major math convention. - Column-major memory storage. Coordinate systems: - Right-handed. - NDC in [-1, +1]. - Viewport goes up and to the right. - Window goes down and to the right. - (x,y) is the center of a pixel. - Top-left: (x - 1/2, y - 1/2) - Bottom-right: (x + 1/2, y + 1/2) */ #include #include #include // sqrt #include #include static constexpr sgVec3 Up3 = (sgVec3){0,1,0}; typedef struct sgViewport_t { int x0, y0, width, height; } sgViewport_t; typedef struct sgAABB2 { sgVec2 pmin, pmax; } sgAABB2; // Column-major math, column-major storage. typedef struct sgMat4 { R val[4][4]; // (col, row) } sgMat4; typedef struct swgfx { sgVec2i dims; // Colour buffer dimensions. sgPixel* colour; // Colour buffer. R* depth; // Depth buffer. sgViewport_t viewport; sgMat4 model; // Model matrix. sgMat4 view; // View matrix. sgMat4 proj; // Projection matrix. // Pre-multiplied matrices. // The model matrix changes once per object, more frequently than view or // projection. View and projection are expected to change infrequently, maybe // once per frame. // Make it so that changing the model matrix only requires one matrix // multiplication (mvp = model * viewProj) and not two (mvp = model * view * projection) // before rendering the model's triangles. sgMat4 viewProj; // View-projection matrix. sgMat4 mvp; // Model-view-projection matrix. const sgTexture_t* texture;// User-specified texture. sgTexture_t defaultTexture; // A default for when no texture is provided. sgPixel defaultPixel; // The single-pixel of the default texture. } swgfx; static inline R rmin(R a, R b) { return (a <= b) ? a : b; } static inline R rmax(R a, R b) { return (a >= b) ? a : b; } static inline int imin(int a, int b) { return (a <= b) ? a : b; } static inline int imax(int a, int b) { return (a >= b) ? a : b; } static inline sgVec2 min2(sgVec2 a, sgVec2 b) { return (sgVec2){.x = rmin(a.x, b.x), .y = rmin(a.y, b.y) }; } static inline sgVec2 max2(sgVec2 a, sgVec2 b) { return (sgVec2){.x = rmax(a.x, b.x), .y = rmax(a.y, b.y) }; } static inline sgVec2i min2i(sgVec2i a, sgVec2i b) { return (sgVec2i){.x = imin(a.x, b.x), .y = imin(a.y, b.y) }; } static inline sgVec2i max2i(sgVec2i a, sgVec2i b) { return (sgVec2i){.x = imax(a.x, b.x), .y = imax(a.y, b.y) }; } static inline sgVec2 add2(sgVec2 a, sgVec2 b) { return (sgVec2){a.x + b.x, a.y + b.y}; } static inline sgVec2 scale2(sgVec2 v, R s) { return (sgVec2){v.x * s, v.y * s}; } static inline sgVec3 neg3(sgVec3 v) { return (sgVec3){-v.x, -v.y, -v.z}; } static inline sgVec3 sub3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x - b.x, a.y - b.y, a.z - b.z}; } static inline sgVec3 div3(sgVec3 a, sgVec3 b) { return (sgVec3){a.x / b.x, a.y / b.y, a.z / b.z}; } static inline R dot3(sgVec3 a, sgVec3 b) { return a.x * b.x + a.y * b.y + a.z * b.z; } static inline R normsq3(sgVec3 v) { return v.x * v.x + v.y * v.y + v.z * v.z; } static inline R norm3 (sgVec3 v) { return (R)sqrt(normsq3(v)); } static inline sgVec3 cross3(sgVec3 a, sgVec3 b) { return (sgVec3) { a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x}; } static inline sgVec3 normalize3(sgVec3 v) { const R n = norm3(v); return (n > 0) ? (sgVec3){v.x / n, v.y / n, v.z / n} : (sgVec3){0, 0, 0}; } static inline sgVec4 Vec4FromVec3(sgVec3 v, R w) { return (sgVec4){v.x, v.y, v.z, w}; } static inline sgMat4 Mat4( R m00, R m01, R m02, R m03, // v0.x v1.x v2.x v3.x R m10, R m11, R m12, R m13, // v0.y v1.y v2.y v3.y R m20, R m21, R m22, R m23, // v0.z v1.z v2.z v3.z R m30, R m31, R m32, R m33) { // v0.w v1.w v2.w v3.w return (sgMat4) { .val = {{m00, m10, m20, m30}, // col 0 {m01, m11, m21, m31}, // col 1 {m02, m12, m22, m32}, // col 2 {m03, m13, m23, m33}}}; // col 3 } static inline sgMat4 Mat4FromVec3(sgVec3 right, sgVec3 up, sgVec3 forward, sgVec3 position) { return Mat4( right.x, up.x, forward.x, position.x, right.y, up.y, forward.y, position.y, right.z, up.z, forward.z, position.z, 0, 0, 0, 1); } static inline R Mat4At(sgMat4 m, int row, int col) { return m.val[col][row]; } static inline sgVec3 Mat4v0(sgMat4 m) { return *((sgVec3*)m.val[0]); } static inline sgVec3 Mat4v1(sgMat4 m) { return *((sgVec3*)m.val[1]); } static inline sgVec3 Mat4v2(sgMat4 m) { return *((sgVec3*)m.val[2]); } static inline sgVec3 Mat4v3(sgMat4 m) { return *((sgVec3*)m.val[3]); } static inline sgMat4 Mat4Mul(sgMat4 A, sgMat4 B) { R m00 = Mat4At(A, 0, 0) * Mat4At(B, 0, 0) + Mat4At(A, 0, 1) * Mat4At(B, 1, 0) + Mat4At(A, 0, 2) * Mat4At(B, 2, 0) + Mat4At(A, 0, 3) * Mat4At(B, 3, 0); R m01 = Mat4At(A, 0, 0) * Mat4At(B, 0, 1) + Mat4At(A, 0, 1) * Mat4At(B, 1, 1) + Mat4At(A, 0, 2) * Mat4At(B, 2, 1) + Mat4At(A, 0, 3) * Mat4At(B, 3, 1); R m02 = Mat4At(A, 0, 0) * Mat4At(B, 0, 2) + Mat4At(A, 0, 1) * Mat4At(B, 1, 2) + Mat4At(A, 0, 2) * Mat4At(B, 2, 2) + Mat4At(A, 0, 3) * Mat4At(B, 3, 2); R m03 = Mat4At(A, 0, 0) * Mat4At(B, 0, 3) + Mat4At(A, 0, 1) * Mat4At(B, 1, 3) + Mat4At(A, 0, 2) * Mat4At(B, 2, 3) + Mat4At(A, 0, 3) * Mat4At(B, 3, 3); R m10 = Mat4At(A, 1, 0) * Mat4At(B, 0, 0) + Mat4At(A, 1, 1) * Mat4At(B, 1, 0) + Mat4At(A, 1, 2) * Mat4At(B, 2, 0) + Mat4At(A, 1, 3) * Mat4At(B, 3, 0); R m11 = Mat4At(A, 1, 0) * Mat4At(B, 0, 1) + Mat4At(A, 1, 1) * Mat4At(B, 1, 1) + Mat4At(A, 1, 2) * Mat4At(B, 2, 1) + Mat4At(A, 1, 3) * Mat4At(B, 3, 1); R m12 = Mat4At(A, 1, 0) * Mat4At(B, 0, 2) + Mat4At(A, 1, 1) * Mat4At(B, 1, 2) + Mat4At(A, 1, 2) * Mat4At(B, 2, 2) + Mat4At(A, 1, 3) * Mat4At(B, 3, 2); R m13 = Mat4At(A, 1, 0) * Mat4At(B, 0, 3) + Mat4At(A, 1, 1) * Mat4At(B, 1, 3) + Mat4At(A, 1, 2) * Mat4At(B, 2, 3) + Mat4At(A, 1, 3) * Mat4At(B, 3, 3); R m20 = Mat4At(A, 2, 0) * Mat4At(B, 0, 0) + Mat4At(A, 2, 1) * Mat4At(B, 1, 0) + Mat4At(A, 2, 2) * Mat4At(B, 2, 0) + Mat4At(A, 2, 3) * Mat4At(B, 3, 0); R m21 = Mat4At(A, 2, 0) * Mat4At(B, 0, 1) + Mat4At(A, 2, 1) * Mat4At(B, 1, 1) + Mat4At(A, 2, 2) * Mat4At(B, 2, 1) + Mat4At(A, 2, 3) * Mat4At(B, 3, 1); R m22 = Mat4At(A, 2, 0) * Mat4At(B, 0, 2) + Mat4At(A, 2, 1) * Mat4At(B, 1, 2) + Mat4At(A, 2, 2) * Mat4At(B, 2, 2) + Mat4At(A, 2, 3) * Mat4At(B, 3, 2); R m23 = Mat4At(A, 2, 0) * Mat4At(B, 0, 3) + Mat4At(A, 2, 1) * Mat4At(B, 1, 3) + Mat4At(A, 2, 2) * Mat4At(B, 2, 3) + Mat4At(A, 2, 3) * Mat4At(B, 3, 3); R m30 = Mat4At(A, 3, 0) * Mat4At(B, 0, 0) + Mat4At(A, 3, 1) * Mat4At(B, 1, 0) + Mat4At(A, 3, 2) * Mat4At(B, 2, 0) + Mat4At(A, 3, 3) * Mat4At(B, 3, 0); R m31 = Mat4At(A, 3, 0) * Mat4At(B, 0, 1) + Mat4At(A, 3, 1) * Mat4At(B, 1, 1) + Mat4At(A, 3, 2) * Mat4At(B, 2, 1) + Mat4At(A, 3, 3) * Mat4At(B, 3, 1); R m32 = Mat4At(A, 3, 0) * Mat4At(B, 0, 2) + Mat4At(A, 3, 1) * Mat4At(B, 1, 2) + Mat4At(A, 3, 2) * Mat4At(B, 2, 2) + Mat4At(A, 3, 3) * Mat4At(B, 3, 2); R m33 = Mat4At(A, 3, 0) * Mat4At(B, 0, 3) + Mat4At(A, 3, 1) * Mat4At(B, 1, 3) + Mat4At(A, 3, 2) * Mat4At(B, 2, 3) + Mat4At(A, 3, 3) * Mat4At(B, 3, 3); return Mat4( m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33); } static inline sgVec3 Mat4MulVec3(sgMat4 m, sgVec3 v, R w) { return (sgVec3) { .x = Mat4At(m, 0, 0) * v.x + Mat4At(m, 0, 1) * v.y + Mat4At(m, 0, 2) * v.z + Mat4At(m, 0, 3) * w, .y = Mat4At(m, 1, 0) * v.x + Mat4At(m, 1, 1) * v.y + Mat4At(m, 1, 2) * v.z + Mat4At(m, 1, 3) * w, .z = Mat4At(m, 2, 0) * v.x + Mat4At(m, 2, 1) * v.y + Mat4At(m, 2, 2) * v.z + Mat4At(m, 2, 3) * w}; } static inline sgVec4 Mat4MulVec4(sgMat4 m, sgVec4 v) { sgVec4 u; u.x = Mat4At(m, 0, 0) * v.x + Mat4At(m, 0, 1) * v.y + Mat4At(m, 0, 2) * v.z + Mat4At(m, 0, 3) * v.w; u.y = Mat4At(m, 1, 0) * v.x + Mat4At(m, 1, 1) * v.y + Mat4At(m, 1, 2) * v.z + Mat4At(m, 1, 3) * v.w; u.z = Mat4At(m, 2, 0) * v.x + Mat4At(m, 2, 1) * v.y + Mat4At(m, 2, 2) * v.z + Mat4At(m, 2, 3) * v.w; u.w = Mat4At(m, 3, 0) * v.x + Mat4At(m, 3, 1) * v.y + Mat4At(m, 3, 2) * v.z + Mat4At(m, 3, 3) * v.w; return u; } static inline sgMat4 Mat4InverseTransform(sgMat4 m) { const sgVec3 r = Mat4v0(m); const sgVec3 u = Mat4v1(m); const sgVec3 f = Mat4v2(m); const sgVec3 t = Mat4v3(m); return Mat4( r.x, r.y, r.z, -dot3(r, t), u.x, u.y, u.z, -dot3(u, t), f.x, f.y, f.z, -dot3(f, t), 0.f, 0.f, 0.f, 1.f); } static inline sgMat4 Mat4Look(sgVec3 position, sgVec3 forward, sgVec3 up) { const sgVec3 right = normalize3(cross3(forward, up)); up = normalize3(cross3(right, forward)); return Mat4FromVec3(right, up, neg3(forward), position); } static inline sgMat4 Mat4Perspective(R fovy, R aspect, R near, R far) { R f = (R)tan(fovy / 2.0); assert(f > 0.0); f = 1.f / f; const R a = near - far; return Mat4( f / aspect, 0, 0, 0, 0, f, 0, 0, 0, 0, (far + near) / a, (2 * far * near / a), 0, 0, -1, 0); } #ifndef _NDEBUG static bool InBounds(int width, int height, int x, int y) { return (0 <= x) && (x < width) && (0 <= y) && (y < height); } #endif // _NDEBUG static inline sgPixel* Pixel(swgfx* gfx, int x, int y) { assert(gfx); assert(gfx->colour); assert(InBounds(gfx->dims.x, gfx->dims.y, x, y)); return gfx->colour + (y * gfx->dims.x) + x; } static inline R* Depth(swgfx* gfx, int x, int y) { assert(gfx); assert(gfx->depth); assert(InBounds(gfx->dims.x, gfx->dims.y, x, y)); return gfx->depth + (y * gfx->dims.x) + x; } void SetPixel(swgfx* gfx, const sgVec2i p, sgPixel colour) { assert(gfx); *Pixel(gfx, p.x, p.y) = colour; } void SetDepth(swgfx* gfx, const sgVec2i p, R depth) { assert(gfx); *Depth(gfx, p.x, p.y) = depth; } // TODO: Mipmapping. sgPixel Sample(const sgTexture_t* texture, sgVec2 uv) { assert(texture); assert(texture->pixels); // TODO: (1/2, 1/2) is the center of the pixel. Do we need to do something // about it here? #define INDEX(X,Y) texture->pixels[(Y) * texture->width + (X)] // Doing a nearest sample for now. TODO: Other sampling strategies. const int x = (int)(uv.x * (R)texture->width); const int y = (int)(uv.y * (R)texture->height); // Repeat for now. TODO: Clamping and other strategies. const int xx = x % texture->width; const int yy = y % texture->height; return INDEX(xx,yy); } static inline sgAABB2 TriangleAabb2(sgVec2 p0, sgVec2 p1, sgVec2 p2) { return (sgAABB2){.pmin = min2(min2(p0, p1), p2), .pmax = max2(max2(p0, p1), p2)}; } static inline sgVec2i Clip(const swgfx* gfx, const sgVec2i p) { assert(gfx); constexpr sgVec2i lower = (sgVec2i){0,0}; const sgVec2i upper = (sgVec2i){gfx->viewport.width - 1, gfx->viewport.height - 1}; return max2i(lower, min2i(upper, p)); } static inline R BarycentricInterp1(sgVec3 bar, R a, R b, R c) { return bar.x*a + bar.y*b + bar.z*c; } static inline sgVec2 BarycentricInterp2(sgVec3 bar, sgVec2 a, sgVec2 b, sgVec2 c) { return add2(add2(scale2(a, bar.x), scale2(b, bar.y)), scale2(c, bar.z)); } static inline sgVec2 PerspectiveInterp2(sgVec3 bar, sgVec3 depths, R z, sgVec2 a, sgVec2 b, sgVec2 c) { return scale2(BarycentricInterp2(div3(bar, depths), a, b, c), z); } static inline R f(sgVec2 a, sgVec2 b, sgVec2 p) { return (a.y - b.y)*p.x + (b.x - a.x)*p.y + a.x*b.y - b.x*a.y; } static inline sgVec3 Barycentric(sgVec2 p0, sgVec2 p1, sgVec2 p2, sgVec2 p) { // There is no need to compute the third coordinate explicitly: a + b + c = 1. // But this results in a worse rasterization of the triangle along one of the edges. // It seems we can patch it with a small epsilon, though. // --- // Division by zero is only possible if the triangle has zero area. /*return (sgVec3){ f(p1, p2, p) / f(p1, p2, p0), f(p2, p0, p) / f(p2, p0, p1), f(p0, p1, p) / f(p0, p1, p2)};*/ const R b = f(p0, p2, p) / f(p0, p2, p1); const R c = f(p0, p1, p) / f(p0, p1, p2); const R a = /*f(p1, p2, p) / f(p1, p2, p0);*/1.f - b - c - (R)1e-7; return (sgVec3){a,b,c}; } // Input triangle in screen space. Retains Z for depth testing and vertex // attribute interpolation. static void DrawTriangle2(swgfx* gfx, const sgTri3* const tri) { assert(gfx); assert(tri); const sgVec2 p0 = (sgVec2){tri->p0.pos.x, tri->p0.pos.y}; const sgVec2 p1 = (sgVec2){tri->p1.pos.x, tri->p1.pos.y}; const sgVec2 p2 = (sgVec2){tri->p2.pos.x, tri->p2.pos.y}; const sgAABB2 bbox = TriangleAabb2(p0, p1, p2); // We consider (x,y) to be the pixel center. // Draw all pixels touched by the bounding box. TODO: Multi-sampling. sgVec2i pmin = (sgVec2i){(int)bbox.pmin.x, (int)bbox.pmin.y}; sgVec2i pmax = (sgVec2i){(int)(bbox.pmax.x + 0.5f), (int)(bbox.pmax.y + 0.5f)}; // Clip to screen space. pmin = Clip(gfx, pmin); pmax = Clip(gfx, pmax); // Draw. for (int y = pmin.y; y <= pmax.y; ++y) { for (int x = pmin.x; x <= pmax.x; ++x) { const sgVec2 p = (sgVec2){(R)x, (R)y}; // TODO: there is an incremental optimization to computing barycentric coordinates; // read more about it. const sgVec3 bar = Barycentric(p0, p1, p2, p); // We need to check the third coordinate. // a + b + c = 1 // So, e.g., if a >= 0 and b >= 0, then we have c <= 1, but we could also have c <= 0. // In the case c <= 0, then point is outside the triangle. if ((bar.x >= 0) && (bar.y >= 0) && (bar.z >= 0)) { const R z = BarycentricInterp1(bar, tri->p0.pos.z, tri->p1.pos.z, tri->p2.pos.z); R* depth = Depth(gfx, x, y); if ((0.f <= z) && (z <= 1.f) && (z <= *depth)) { *depth = z; const sgVec3 depths = (sgVec3){tri->p0.pos.z, tri->p1.pos.z, tri->p2.pos.z}; const sgVec2 uv = PerspectiveInterp2(bar, depths, z, tri->p0.uv, tri->p1.uv, tri->p2.uv); const sgPixel colour = Sample(gfx->texture, uv); //const sgPixel colour = (sgPixel){255, 0, 255, 255}; // TODO: When doing lighting, need to tone-map here. /*const int r = (int)(uv.x * 255.f); const int g = (int)(uv.y * 255.f); const sgPixel colour = (sgPixel){r, g, 255, 255};*/ const sgVec2i pix = (sgVec2i){x,y}; SetPixel(gfx, pix, colour); } } } } } static inline sgVec3 PerspDivide(sgVec4 v) { return (sgVec3){v.x / v.w, v.y / v.w, v.z / v.w}; } // TODO: Compute a viewport matrix in sgViewport() instead. static inline sgVec3 ViewportTransform(sgViewport_t vp, sgVec3 ndc) { return (sgVec3){ .x = (ndc.x+1.f) * ((R)vp.width/2.f) + (R)vp.x0, .y = (ndc.y+1.f) * ((R)vp.height/2.f) + (R)vp.y0, .z = ndc.z}; } static inline sgVec3 ViewportToWindow(sgViewport_t vp, sgVec3 p) { return (sgVec3){p.x, (R)vp.height - p.y, p.z}; } static inline sgVec3 TransformPosition(const swgfx* gfx, sgVec3 p) { assert(gfx); // Model to clip space. const sgVec4 p_clip = Mat4MulVec4(gfx->mvp, Vec4FromVec3(p, 1)); // TODO: Backface culling. // Perspective divide. const sgVec3 p_ndc = PerspDivide(p_clip); // TODO: Clip. const sgVec3 p_vp = ViewportTransform(gfx->viewport, p_ndc); return ViewportToWindow(gfx->viewport, p_vp); } static void DrawTriangle3(swgfx* gfx, const sgTri3* const tri) { assert(gfx); assert(tri); const sgVec3 p0 = TransformPosition(gfx, tri->p0.pos); const sgVec3 p1 = TransformPosition(gfx, tri->p1.pos); const sgVec3 p2 = TransformPosition(gfx, tri->p2.pos); const sgVec2 uv0 = tri->p0.uv; const sgVec2 uv1 = tri->p1.uv; const sgVec2 uv2 = tri->p2.uv; const sgTri3 tri_screen = (sgTri3){ (sgVert3){p0, uv0}, (sgVert3){p1, uv1}, (sgVert3){p2, uv2}}; DrawTriangle2(gfx, &tri_screen); } #define is_pow2_or_0(X) ((X & (X - 1)) == 0) #define SG_ALIGN 64 #define SG_ALLOC(PP_MEM, COUNT, TYPE) (TYPE*)Alloc(PP_MEM, COUNT, sizeof(TYPE)) static void* AlignPtr(void* address) { assert(is_pow2_or_0(SG_ALIGN)); constexpr size_t mask = SG_ALIGN - 1; return (void*)(((uintptr_t)address + mask) & ~mask); } static size_t Align(size_t size) { static_assert(is_pow2_or_0(SG_ALIGN)); constexpr size_t mask = SG_ALIGN - 1; return (size + mask) & (~mask); } static void* Alloc(void** ppMem, size_t count, size_t size) { assert(ppMem); assert(*ppMem); assert(*ppMem == AlignPtr(*ppMem)); // Should already be aligned. const size_t total = Align(count * size); void* ptr = *ppMem; *ppMem = ptr + total; memset(ptr, 0, total); return ptr; } size_t sgMem(int width, int height) { return Align(sizeof(swgfx)) + Align(width * height * sizeof(sgPixel)) + Align(width * height * sizeof(R)) + (SG_ALIGN - 1); // To make room to align allocations within the buffer. } swgfx* sgNew(int width, int height, void* mem) { void* aligned = AlignPtr(mem); // Uses the extra room we made in sgMem(). swgfx* gfx = SG_ALLOC(&aligned, 1, swgfx); gfx->dims = (sgVec2i){width, height}; gfx->colour = SG_ALLOC(&aligned, width * height, sgPixel); gfx->depth = SG_ALLOC(&aligned, width * height, R); gfx->defaultPixel = (sgPixel){255, 255, 255, 255}; gfx->defaultTexture = (sgTexture_t){ .width = 1, .height = 1, .pixels = &gfx->defaultPixel, }; return gfx; } void sgDel(swgfx** ppSwgfx) { assert(ppSwgfx); if (*ppSwgfx) { *ppSwgfx = nullptr; } } void sgPresent(swgfx* gfx, sgVec2i dimensions, sgScreenPixel* screen) { assert(gfx); assert(screen); // Integer scaling only. assert((dimensions.x % gfx->dims.x) == 0); assert((dimensions.y % gfx->dims.y) == 0); const int sx = dimensions.x / gfx->dims.x; const int sy = dimensions.y / gfx->dims.y; const sgPixel* src = gfx->colour; for (int y = 0; y < gfx->dims.y; ++y, src += gfx->dims.x) { // Replicate each row 'sy' times. for (int yy = 0; yy < sy; ++yy) { const sgPixel* src_col = src; for (int x = 0; x < gfx->dims.x; ++x, ++src_col) { // Replicate each column 'sx' times. for (int xx = 0; xx < sx; ++xx, ++screen) { screen->r = src_col->r; screen->g = src_col->g; screen->b = src_col->b; screen->a = src_col->a; } } } } } static void sgUpdateViewProjection(swgfx* gfx) { assert(gfx); gfx->viewProj = Mat4Mul(gfx->proj, gfx->view); } static void sgUpdateMvp(swgfx* gfx) { assert(gfx); gfx->mvp = Mat4Mul(gfx->viewProj, gfx->model); } void sgModelId(swgfx* gfx) { assert(gfx); sgModel(gfx, (sgVec3){0,0,0}, (sgVec3){1, 0, 0}, (sgVec3){0, 1, 0}, (sgVec3){0, 0, 1}); } void sgModel(swgfx* gfx, sgVec3 position, sgVec3 right, sgVec3 up, sgVec3 forward) { assert(gfx); gfx->model = Mat4FromVec3(right, up, forward, position); sgUpdateMvp(gfx); } void sgView(swgfx* gfx, sgVec3 position, sgVec3 forward) { assert(gfx); const sgMat4 camera = Mat4Look(position, forward, Up3); gfx->view = Mat4InverseTransform(camera); sgUpdateViewProjection(gfx); sgUpdateMvp(gfx); } void sgPerspective(swgfx* gfx, R fovy, R aspect, R near, R far) { assert(gfx); gfx->proj = Mat4Perspective(fovy, aspect, near, far); sgUpdateViewProjection(gfx); sgUpdateMvp(gfx); } void sgViewport(swgfx* gfx, int x0, int y0, int width, int height) { assert(gfx); gfx->viewport = (sgViewport_t){x0, y0, width, height}; } void sgTexture(swgfx* gfx, const sgTexture_t* texture) { assert(gfx); assert(texture); gfx->texture = texture; } void sgClear(swgfx* gfx) { assert(gfx); const int N = gfx->dims.x * gfx->dims.y; memset(gfx->colour, 0, N * sizeof(*gfx->colour)); for (int i = 0; i < N; ++i) { gfx->depth[i] = 1.0f; } } void sgPixels(swgfx* gfx, size_t count, const sgVec2i* positions, sgPixel colour) { assert(gfx); for (size_t i = 0; i < count; ++i) { SetPixel(gfx, positions[i], colour); } } // TODO: DrawTriangle3 with clipping. Leave DrawTriangle2 to not clip for // performance; assume that 2D triangles are within bounds. // TODO: If the triangle is out of bounds, skip entirely. // TODO: Otherwise, rasterize the triangle the simple way and check whether each // individual pixel is within bounds; do not explicitly clip the triangle. // TODO: Actually, I think we can just clip the triangle's AABB and then walk // over those pixels instead of checking every individual pixel in the // non-clipped AABB. Edit: I think this doesn't work; draw it and you'll // see. Some pixels that should be rasterized will fall out of the clipped // AABB. void sgTriangles2(swgfx* gfx, size_t count, const sgTri2* tris) { assert(gfx); for (size_t i = 0; i < count; ++i) { const sgTri3 tri3 = (sgTri3) { .p0 = (sgVert3){.pos = (sgVec3){tris[i].p0.pos.x, tris[i].p0.pos.y, 0}, .uv = tris[i].p0.uv}, .p1 = (sgVert3){.pos = (sgVec3){tris[i].p1.pos.x, tris[i].p1.pos.y, 0}, .uv = tris[i].p1.uv}, .p2 = (sgVert3){.pos = (sgVec3){tris[i].p2.pos.x, tris[i].p2.pos.y, 0}, .uv = tris[i].p2.uv}, }; DrawTriangle2(gfx, &tri3); } } void sgTriangles(swgfx* gfx, size_t count, const sgTri3* tris, const sgNormal*) { assert(gfx); assert(tris); for (size_t i = 0; i < count; ++i) { const sgTri3* tri = &tris[i]; DrawTriangle3(gfx, tri); } } void sgTrianglesIndexed(swgfx* gfx, size_t numIndices, const sgIdx* indices, const sgVec3* positions, const sgVec2* texcoords) { assert(gfx); assert(indices); assert(positions); assert(texcoords); for (size_t i = 0; i < numIndices; i+=3) { const sgIdx i0 = indices[i]; const sgIdx i1 = indices[i+1]; const sgIdx i2 = indices[i+2]; const sgVec3 p0 = positions[i0]; const sgVec3 p1 = positions[i1]; const sgVec3 p2 = positions[i2]; const sgVec2 uv0 = texcoords[i0]; const sgVec2 uv1 = texcoords[i1]; const sgVec2 uv2 = texcoords[i2]; const sgTri3 tri = (sgTri3){ (sgVert3){p0, uv0}, (sgVert3){p1, uv1}, (sgVert3){p2, uv2}}; DrawTriangle3(gfx, &tri); } } void sgTrianglesIndexedNonUniform(swgfx* gfx, size_t numTris, const sgTriIdx* tris, const sgVec3* positions, const sgVec2* texcoords) { assert(gfx); assert(tris); assert(positions); assert(texcoords); for (size_t t = 0; t < numTris; ++t) { const sgTriIdx* triIdx = &tris[t]; const sgTri3 tri = (sgTri3){ (sgVert3){positions[triIdx->v0.pos], texcoords[triIdx->v0.uv]}, (sgVert3){positions[triIdx->v1.pos], texcoords[triIdx->v1.uv]}, (sgVert3){positions[triIdx->v2.pos], texcoords[triIdx->v2.uv]}}; DrawTriangle3(gfx, &tri); } } static bool ViewportWithinBuffer(swgfx* gfx) { assert(gfx); const sgViewport_t vp = gfx->viewport; return ((vp.x0 + vp.width) <= gfx->dims.x) && ((vp.y0 + vp.height) <= gfx->dims.y); } void sgCheck(swgfx* gfx) { assert(gfx); assert(ViewportWithinBuffer(gfx)); }