Calculating the max bounds better

This commit is contained in:
Krzosa Karol
2022-02-17 10:48:32 +01:00
parent 8d4a747e1f
commit 512dba64cb
3 changed files with 66 additions and 28 deletions

View File

@@ -26,14 +26,6 @@ OK Basic linear transformations - rotation, translation, scaling
#include "math.h" #include "math.h"
#include "stb_image.h" #include "stb_image.h"
// TODO:
// Perspective correct interpolation
// Camera
// Perspective projection
// Texture mapping
// Reading OBJ
struct Face { struct Face {
int p[3]; int p[3];
Vec2 tex[3]; Vec2 tex[3];
@@ -88,20 +80,22 @@ float EdgeFunction(Vec4 vecp0, Vec4 vecp1, Vec4 p) {
FUNCTION FUNCTION
void DrawTriangle(Image* dst, Image *src, Vec4 p0, Vec4 p1, Vec4 p2, void DrawTriangle(Image* dst, Image *src, Vec4 p0, Vec4 p1, Vec4 p2,
Vec2 tex0, Vec2 tex1, Vec2 tex2) { Vec2 tex0, Vec2 tex1, Vec2 tex2) {
float min_x = (float)(MIN(p0.x, MIN(p1.x, p2.x))); float min_x1 = (float)(MIN(p0.x, MIN(p1.x, p2.x)));
float min_y = (float)(MIN(p0.y, MIN(p1.y, p2.y))); float min_y1 = (float)(MIN(p0.y, MIN(p1.y, p2.y)));
float max_x = (float)(MAX(p0.x, MAX(p1.x, p2.x))); float max_x1 = (float)(MAX(p0.x, MAX(p1.x, p2.x)));
float max_y = (float)(MAX(p0.y, MAX(p1.y, p2.y))); float max_y1 = (float)(MAX(p0.y, MAX(p1.y, p2.y)));
min_x = MAX(0, min_x) + 0.5f; int min_x = (int)MAX(0, floorf(min_x1));
min_y = MAX(0, min_y) + 0.5f; int min_y = (int)MAX(0, floorf(min_y1));
max_x = MIN(dst->x-1, max_x) + 0.5f; int max_x = (int)MIN(dst->x, ceilf(max_x1));
max_y = MIN(dst->y-1, max_y) + 0.5f; int max_y = (int)MIN(dst->y, ceilf(max_y1));
//@Todo: Fix the proper rounding //@Todo: Fix the proper rounding
//@Todo: Determine whether we need subprecision etc //@Todo: Determine whether we need subprecision etc
float area = EdgeFunction(p0, p1, p2); float area = EdgeFunction(p0, p1, p2);
for (int y = min_y; y < max_y; y++) { for (int y = min_y; y < max_y; y++) {
for (int x = min_x; x < max_x; x++) { for (int x = min_x; x < max_x; x++) {
int xi = (int)(x + 0.5f);
int yi = (int)(y + 0.5f);
float edge1 = EdgeFunction(p0, p1, { (float)x,(float)y }); float edge1 = EdgeFunction(p0, p1, { (float)x,(float)y });
float edge2 = EdgeFunction(p1, p2, { (float)x,(float)y }); float edge2 = EdgeFunction(p1, p2, { (float)x,(float)y });
float edge3 = EdgeFunction(p2, p0, { (float)x,(float)y }); float edge3 = EdgeFunction(p2, p0, { (float)x,(float)y });
@@ -110,6 +104,8 @@ void DrawTriangle(Image* dst, Image *src, Vec4 p0, Vec4 p1, Vec4 p2,
float w1 = edge2 / area; float w1 = edge2 / area;
float w2 = edge3 / area; float w2 = edge3 / area;
float w3 = edge1 / area; float w3 = edge1 / area;
float x_diff = xi - x;
float y_diff = yi - y;
#if 1 #if 1
float u = tex0.x * (w1 / p0.w) + tex1.x * (w2 / p1.w) + tex2.x * (w3 / p2.w); 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 v = tex0.y * (w1 / p0.w) + tex1.y * (w2 / p1.w) + tex2.y * (w3 / p2.w);
@@ -122,10 +118,20 @@ void DrawTriangle(Image* dst, Image *src, Vec4 p0, Vec4 p1, Vec4 p2,
float v = tex0.y * w1 + tex1.y * w2 + tex2.y * w3; float v = tex0.y * w1 + tex1.y * w2 + tex2.y * w3;
#endif #endif
int ui = (int)(u * (src->x - 1) + 0.5f); int ui = (int)(u * (src->x - 2) + 0.5f);
int vi = (int)(v * (src->y - 1) + 0.5f); int vi = (int)(v * (src->y - 2) + 0.5f);
uint32_t pixel = src->pixels[ui + (src->y - 1 - vi) * src->x]; uint32_t *pixel = src->pixels + (ui + (src->y - 1 - vi) * src->x);
dst->pixels[x + (dst->y - 1 - y) * dst->x] = pixel; Vec4 pixelx1y1 = V4ABGR(*pixel);
Vec4 pixelx2y1 = V4ABGR(*(pixel + 1));
Vec4 pixelx1y2 = V4ABGR(*(pixel - src->x));
Vec4 pixelx2y2 = V4ABGR(*(pixel + 1 - src->x));
Vec4 blendx1 = Lerp(pixelx1y1, pixelx2y1, x_diff);
Vec4 blendx2 = Lerp(pixelx1y2, pixelx2y2, x_diff);
Vec4 result_color = Lerp(blendx1, blendx2, y_diff);
uint32_t color32 = ColorToU32ARGB(pixelx1y1);
dst->pixels[xi + (dst->y - 1 - yi) * dst->x] = color32;
} }
} }
} }
@@ -153,7 +159,7 @@ void DrawLine(Image *dst, float x0, float y0, float x1, float y1) {
} }
int main() { int main() {
OS_Init({.window_x=1280, .window_y=720}); OS_Init({ 1280,720 });
float rotation = 0; float rotation = 0;
Vec3 camera_pos = {0,0,-5}; Vec3 camera_pos = {0,0,-5};
int x,y,n; int x,y,n;

40
math.h
View File

@@ -195,10 +195,42 @@ Vec3 Cross(Vec3 a, Vec3 b) {
FUNCTION FUNCTION
uint32_t ColorToU32ARGB(Vec4 a) { uint32_t ColorToU32ARGB(Vec4 a) {
uint8_t r8 = (uint8_t)(a.r * 255.f + 0.5f); uint8_t r8 = (uint8_t)(a.r * 255.f);
uint8_t g8 = (uint8_t)(a.g * 255.f + 0.5f); uint8_t g8 = (uint8_t)(a.g * 255.f);
uint8_t b8 = (uint8_t)(a.b * 255.f + 0.5f); uint8_t b8 = (uint8_t)(a.b * 255.f);
uint8_t a8 = (uint8_t)(a.a * 255.f + 0.5f); uint8_t a8 = (uint8_t)(a.a * 255.f);
uint32_t result = a8 << 24 | r8 << 16 | g8 << 8 | b8; uint32_t result = a8 << 24 | r8 << 16 | g8 << 8 | b8;
return result; return result;
} }
FUNCTION
Vec4 V4ARGB(uint32_t c) {
float a = ((c & 0xff000000) >> 24) / 255.f;
float r = ((c & 0x00ff0000) >> 16) / 255.f;
float g = ((c & 0x0000ff00) >> 8) / 255.f;
float b = ((c & 0x000000ff) >> 0) / 255.f;
Vec4 result = { r,g,b,a };
return result;
}
FUNCTION
Vec4 V4ABGR(uint32_t c) {
float a = ((c & 0xff000000) >> 24) / 255.f;
float b = ((c & 0x00ff0000) >> 16) / 255.f;
float g = ((c & 0x0000ff00) >> 8) / 255.f;
float r = ((c & 0x000000ff) >> 0) / 255.f;
Vec4 result = { r,g,b,a };
return result;
}
FUNCTION
float Lerp(float a, float b, float t) {
float result = (1.0f - t) * a + t * b;
return result;
}
FUNCTION
Vec4 Lerp(Vec4 a, Vec4 b, float t) {
Vec4 result = {Lerp(a.x,b.x,t), Lerp(a.y,b.y,t), Lerp(a.z,b.z,t), Lerp(a.w,b.w,t) };
return result;
}

View File

@@ -60,7 +60,7 @@ void Win32_ScreenInit(int window_x, int window_y) {
g_screen_dib = CreateDIBSection(g_window_dc, &bminfo, DIB_RGB_COLORS, (void**)&mem, 0, 0); g_screen_dib = CreateDIBSection(g_window_dc, &bminfo, DIB_RGB_COLORS, (void**)&mem, 0, 0);
g_screen_dc = CreateCompatibleDC(g_window_dc); g_screen_dc = CreateCompatibleDC(g_window_dc);
screen.pixels = (uint32_t*)mem; screen.pixels = (uint32_t*)mem;
depth_buffer = (float*)malloc(window_x * window_y * sizeof(float)); depth_buffer = (float*)malloc((size_t)window_x * (size_t)window_y * sizeof(float));
screen.x = window_x; screen.x = window_x;
screen.y = window_y; screen.y = window_y;
} }
@@ -80,7 +80,7 @@ void OS_Init(OSInitArgs i) {
wc.lpfnWndProc = WindowProc; wc.lpfnWndProc = WindowProc;
wc.hInstance = g_hinstance; wc.hInstance = g_hinstance;
wc.lpszClassName = CLASS_NAME; wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc); ASSERT(RegisterClass(&wc));
g_hwnd = CreateWindowEx( g_hwnd = CreateWindowEx(
0, // Optional window styles. 0, // Optional window styles.
@@ -95,7 +95,7 @@ void OS_Init(OSInitArgs i) {
); );
ASSERT(g_hwnd != 0); ASSERT(g_hwnd != 0);
ShowWindow(g_hwnd, g_cmdshow); ShowWindow(g_hwnd, SW_SHOW);
RECT rect; RECT rect;
GetWindowRect(g_hwnd, &rect); GetWindowRect(g_hwnd, &rect);
g_window_dc = GetWindowDC(g_hwnd); g_window_dc = GetWindowDC(g_hwnd);