Merge with base
This commit is contained in:
103
main.cpp
103
main.cpp
@@ -15,7 +15,9 @@ OK Depth buffer
|
|||||||
KINDA_OK Gamma correct blending
|
KINDA_OK Gamma correct blending
|
||||||
OK Alpha blending??
|
OK Alpha blending??
|
||||||
OK Premultiplied alpha???
|
OK Premultiplied alpha???
|
||||||
|
OK Merge with base
|
||||||
* Lightning
|
* Lightning
|
||||||
|
OK GLOBAL Ilumination
|
||||||
* LookAt Camera
|
* LookAt Camera
|
||||||
* FPS Camera
|
* FPS Camera
|
||||||
OK Reading OBJ files
|
OK Reading OBJ files
|
||||||
@@ -31,38 +33,25 @@ OK Reading OBJ files
|
|||||||
* Gamma correct and alpha blending
|
* Gamma correct and alpha blending
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* What a codebase needs:
|
|
||||||
* Macros for debug, release, slow, fast builds, where debug - tooling, slow - enable asserts
|
|
||||||
* Macros for OS, Compiler, Architecture
|
|
||||||
* Nice way of outputing visible error messages
|
|
||||||
* FatalError and Assert function for release builds with an error message, Debug Assert with error message for slow builds
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#define OS_WINDOWS 1
|
#define OS_WINDOWS 1
|
||||||
#define PERSPECTIVE_CORRECT_INTERPOLATION 1
|
#define PERSPECTIVE_CORRECT_INTERPOLATION 1
|
||||||
#define BILINEAR_BLEND 1
|
#define BILINEAR_BLEND 1
|
||||||
#define GAMMA_CORRECT_BLENDING 1
|
#define GAMMA_CORRECT_BLENDING 1
|
||||||
#define PREMULTIPLIED_ALPHA_BLENDING 1
|
#define PREMULTIPLIED_ALPHA_BLENDING 1
|
||||||
|
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define PLATFORM
|
||||||
#include "main.h"
|
#include "base.h"
|
||||||
#include "platform.h"
|
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
#include "objparser.h"
|
#include "objparser.h"
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
|
||||||
GLOBAL OS os = {};
|
//GLOBAL OS os = {};
|
||||||
GLOBAL bool draw_rects = 0;
|
GLOBAL bool draw_rects = 0;
|
||||||
GLOBAL bool draw_wireframe = 0;
|
GLOBAL bool draw_wireframe = 0;
|
||||||
|
|
||||||
struct Face {
|
|
||||||
int p[3];
|
|
||||||
Vec2 tex[3];
|
|
||||||
};
|
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
void draw_rect(Image* dst, float X, float Y, float w, float h, U32 color) {
|
void draw_rect(Bitmap* dst, float X, float Y, float w, float h, U32 color) {
|
||||||
int max_x = (int)(MIN(X + w, dst->x) + 0.5f);
|
int max_x = (int)(MIN(X + w, dst->x) + 0.5f);
|
||||||
int max_y = (int)(MIN(Y + h, dst->y) + 0.5f);
|
int max_y = (int)(MIN(Y + h, dst->y) + 0.5f);
|
||||||
int min_x = (int)(MAX(0, X) + 0.5f);
|
int min_x = (int)(MAX(0, X) + 0.5f);
|
||||||
@@ -89,12 +78,12 @@ Vec4 srgb_to_almost_linear(Vec4 a) {
|
|||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Vec4 almost_linear_to_srgb(Vec4 a) {
|
Vec4 almost_linear_to_srgb(Vec4 a) {
|
||||||
Vec4 result = { sqrt(a.r), sqrt(a.g), sqrt(a.b), a.a };
|
Vec4 result = { sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a };
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
void draw_bitmap(Image* dst, Image* src, Vec2 pos) {
|
void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos) {
|
||||||
I64 minx = (I64)(pos.x + 0.5);
|
I64 minx = (I64)(pos.x + 0.5);
|
||||||
I64 miny = (I64)(pos.y + 0.5);
|
I64 miny = (I64)(pos.y + 0.5);
|
||||||
I64 maxx = minx + src->x;
|
I64 maxx = minx + src->x;
|
||||||
@@ -130,14 +119,14 @@ FUNCTION
|
|||||||
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
||||||
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
||||||
result_color = almost_linear_to_srgb(result_color);
|
result_color = almost_linear_to_srgb(result_color);
|
||||||
U32 color32 = color_to_u32abgr(result_color);
|
U32 color32 = vec4_to_u32abgr(result_color);
|
||||||
*dst_pixel = color32;
|
*dst_pixel = color32;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
void draw_triangle(Bitmap* dst, float *depth_buffer, Bitmap *src, float light,
|
||||||
Vec4 p0, Vec4 p1, Vec4 p2,
|
Vec4 p0, Vec4 p1, Vec4 p2,
|
||||||
Vec2 tex0, Vec2 tex1, Vec2 tex2) {
|
Vec2 tex0, Vec2 tex1, Vec2 tex2) {
|
||||||
float min_x1 = (float)(MIN(p0.x, MIN(p1.x, p2.x)));
|
float min_x1 = (float)(MIN(p0.x, MIN(p1.x, p2.x)));
|
||||||
@@ -216,7 +205,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
|||||||
result_color = almost_linear_to_srgb(result_color);
|
result_color = almost_linear_to_srgb(result_color);
|
||||||
//ASSERT(result_color.r <= 1 && result_color.g <= 1 && result_color.b <= 1);
|
//ASSERT(result_color.r <= 1 && result_color.g <= 1 && result_color.b <= 1);
|
||||||
#endif // GAMMA_CORRECT_BLENDING
|
#endif // GAMMA_CORRECT_BLENDING
|
||||||
U32 color32 = color_to_u32abgr(result_color);
|
U32 color32 = vec4_to_u32abgr(result_color);
|
||||||
#else // BILINEAR_BLEND
|
#else // BILINEAR_BLEND
|
||||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||||
@@ -225,7 +214,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
|||||||
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
||||||
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
||||||
result_color = almost_linear_to_srgb(result_color);
|
result_color = almost_linear_to_srgb(result_color);
|
||||||
U32 color32 = color_to_u32abgr(result_color);
|
U32 color32 = vec4_to_u32abgr(result_color);
|
||||||
#endif // BILINEAR_BLEND
|
#endif // BILINEAR_BLEND
|
||||||
|
|
||||||
*dst_pixel = color32;
|
*dst_pixel = color32;
|
||||||
@@ -241,7 +230,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
|||||||
}
|
}
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
void draw_line(Image *dst, float x0, float y0, float x1, float y1) {
|
void draw_line(Bitmap *dst, float x0, float y0, float x1, float y1) {
|
||||||
float delta_x = (x1 - x0);
|
float delta_x = (x1 - x0);
|
||||||
float delta_y = (y1 - y0);
|
float delta_y = (y1 - y0);
|
||||||
float longest_side_length = (ABS(delta_x) >= ABS(delta_y)) ? ABS(delta_x) : ABS(delta_y);
|
float longest_side_length = (ABS(delta_x) >= ABS(delta_y)) ? ABS(delta_x) : ABS(delta_y);
|
||||||
@@ -264,20 +253,25 @@ struct FaceA {
|
|||||||
int normal[3];
|
int normal[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
FN void null_terminate(Arena *arena) {
|
||||||
|
PUSH_SIZE(arena, 1);
|
||||||
|
}
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Obj load_obj(const char* file) {
|
Obj load_obj(S8 file) {
|
||||||
char* data = os.read_file(file);
|
Scratch scratch;
|
||||||
|
S8 data = os_read_file(scratch, file).error_is_fatal();
|
||||||
|
null_terminate(scratch);
|
||||||
char* memory = (char*)malloc(100000);
|
char* memory = (char*)malloc(100000);
|
||||||
Obj result = obj::parse(memory, 100000, data);
|
Obj result = obj::parse(memory, 100000, (char *)data.str);
|
||||||
free(data);
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Image load_image(const char* path) {
|
Bitmap load_image(const char* path) {
|
||||||
int x, y, n;
|
int x, y, n;
|
||||||
unsigned char* data = stbi_load(path, &x, &y, &n, 4);
|
unsigned char* data = stbi_load(path, &x, &y, &n, 4);
|
||||||
Image result = { (U32*)data, x, y };
|
Bitmap result = { (U32*)data, x, y };
|
||||||
#if PREMULTIPLIED_ALPHA_BLENDING
|
#if PREMULTIPLIED_ALPHA_BLENDING
|
||||||
U32 *p = result.pixels;
|
U32 *p = result.pixels;
|
||||||
for (int Y = 0; Y < y; Y++) {
|
for (int Y = 0; Y < y; Y++) {
|
||||||
@@ -286,7 +280,7 @@ Image load_image(const char* path) {
|
|||||||
color.r *= color.a;
|
color.r *= color.a;
|
||||||
color.g *= color.a;
|
color.g *= color.a;
|
||||||
color.b *= color.a;
|
color.b *= color.a;
|
||||||
*p++ = color_to_u32abgr(color);
|
*p++ = vec4_to_u32abgr(color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@@ -295,25 +289,28 @@ Image load_image(const char* path) {
|
|||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
obj::test();
|
obj::test();
|
||||||
os.init({ 1280,720 });
|
os.window_size.x = 1280;
|
||||||
|
os.window_size.y = 720;
|
||||||
|
os_init();
|
||||||
|
os_init_software_render();
|
||||||
|
|
||||||
float rotation = 0;
|
float rotation = 0;
|
||||||
Vec3 camera_pos = {0,0,-5};
|
Vec3 camera_pos = {0,0,-5};
|
||||||
|
|
||||||
Obj obj = load_obj("assets/f22.obj");
|
Obj obj = load_obj(LIT("assets/f22.obj"));
|
||||||
Vec3* vertices = (Vec3 *)obj.vertices;
|
Vec3* vertices = (Vec3 *)obj.vertices;
|
||||||
Vec2* tex_coords = (Vec2*)obj.texture;
|
Vec2* tex_coords = (Vec2*)obj.texture;
|
||||||
|
Vec3 *normals = (Vec3 *)obj.normals;
|
||||||
FaceA* faces = (FaceA*)obj.indices;
|
FaceA* faces = (FaceA*)obj.indices;
|
||||||
I64 face_count = obj.indices_count;
|
I64 face_count = obj.indices_count;
|
||||||
|
|
||||||
|
Bitmap img = load_image("assets/bricksx64.png");
|
||||||
Image img = load_image("assets/bricksx64.png");
|
|
||||||
int screen_x = 320;
|
int screen_x = 320;
|
||||||
int screen_y = 180;
|
int screen_y = 180;
|
||||||
Image screen320 = {(U32 *)malloc(screen_x*screen_y*sizeof(U32)), screen_x, screen_y};
|
Bitmap screen320 = {(U32 *)malloc(screen_x*screen_y*sizeof(U32)), screen_x, screen_y};
|
||||||
float* depth320 = (float *)malloc(sizeof(float) * screen_x * screen_y);
|
float* depth320 = (float *)malloc(sizeof(float) * screen_x * screen_y);
|
||||||
while (os.game_loop()) {
|
while (os_game_loop()) {
|
||||||
Mat4 perspective = make_matrix_perspective(60.f, (float)os.screen.x, (float)os.screen.y, 0.1f, 100.f);
|
Mat4 perspective = make_matrix_perspective(60.f, (float)screen->x, (float)screen->y, 0.1f, 100.f);
|
||||||
U32* p = screen320.pixels;
|
U32* p = screen320.pixels;
|
||||||
for (int y = 0; y < screen320.y; y++) {
|
for (int y = 0; y < screen320.y; y++) {
|
||||||
for (int x = 0; x < screen320.x; x++) {
|
for (int x = 0; x < screen320.x; x++) {
|
||||||
@@ -329,10 +326,11 @@ int main() {
|
|||||||
|
|
||||||
Mat4 transform = make_matrix_rotation_z(rotation);
|
Mat4 transform = make_matrix_rotation_z(rotation);
|
||||||
transform = transform * make_matrix_rotation_x(rotation);
|
transform = transform * make_matrix_rotation_x(rotation);
|
||||||
if (os.keydown_a) rotation += 0.05f;
|
if (os.key[Key_Escape].pressed) os.quit = true;
|
||||||
if (os.keydown_b) rotation -= 0.05f;
|
if (os.key[Key_O].down) rotation += 0.05f;
|
||||||
if (os.keydown_f1) draw_rects = !draw_rects;
|
if (os.key[Key_P].down) rotation -= 0.05f;
|
||||||
if (os.keydown_f2) draw_wireframe = !draw_wireframe;
|
if (os.key[Key_F1].pressed) draw_rects = !draw_rects;
|
||||||
|
if (os.key[Key_F2].pressed) draw_wireframe = !draw_wireframe;
|
||||||
for (int i = 0; i < face_count; i++) {
|
for (int i = 0; i < face_count; i++) {
|
||||||
FaceA* face = faces + i;
|
FaceA* face = faces + i;
|
||||||
Vec4 pos[3] = {
|
Vec4 pos[3] = {
|
||||||
@@ -345,6 +343,11 @@ int main() {
|
|||||||
tex_coords[face->tex[1] - 1],
|
tex_coords[face->tex[1] - 1],
|
||||||
tex_coords[face->tex[2] - 1],
|
tex_coords[face->tex[2] - 1],
|
||||||
};
|
};
|
||||||
|
Vec3 norm[3] = {
|
||||||
|
normals[face->normal[0] - 1],
|
||||||
|
normals[face->normal[1] - 1],
|
||||||
|
normals[face->normal[2] - 1],
|
||||||
|
};
|
||||||
|
|
||||||
//@Note: Transform
|
//@Note: Transform
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
@@ -356,7 +359,7 @@ int main() {
|
|||||||
Vec3 p0_to_p2 = pos[2].xyz - pos[0].xyz;
|
Vec3 p0_to_p2 = pos[2].xyz - pos[0].xyz;
|
||||||
Vec3 normal = normalize(cross(p0_to_p1, p0_to_p2));
|
Vec3 normal = normalize(cross(p0_to_p1, p0_to_p2));
|
||||||
float light = -dot(normal, vec3(0,1,0));
|
float light = -dot(normal, vec3(0,1,0));
|
||||||
light = CLAMP(0.05, light, 1);
|
light = CLAMP(0.05f, light, 1.f);
|
||||||
if (dot(normal, p0_to_camera) > 0) {
|
if (dot(normal, p0_to_camera) > 0) {
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
//@Note: Camera
|
//@Note: Camera
|
||||||
@@ -388,13 +391,13 @@ int main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// @Note: Draw 320screen to OS screen
|
// @Note: Draw 320screen to OS screen
|
||||||
U32* ptr = os.screen.pixels;
|
U32* ptr = screen->pixels;
|
||||||
for (int y = 0; y < os.screen.y; y++) {
|
for (int y = 0; y < screen->y; y++) {
|
||||||
for (int x = 0; x < os.screen.x; x++) {
|
for (int x = 0; x < screen->x; x++) {
|
||||||
float u = (float)x / (float)os.screen.x;
|
float u = (float)x / (float)screen->x;
|
||||||
float v = (float)y / (float)os.screen.y;
|
float v = (float)y / (float)screen->y;
|
||||||
int tx = (int)(u * screen320.x + 0.5f);
|
int tx = (int)(u * screen320.x );
|
||||||
int ty = (int)(v * screen320.y + 0.5f);
|
int ty = (int)(v * screen320.y );
|
||||||
*ptr++ = screen320.pixels[tx + ty * (screen320.x)];
|
*ptr++ = screen320.pixels[tx + ty * (screen320.x)];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
main.h
13
main.h
@@ -1,13 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
#define ASSERT(x) if(!(x)) *(volatile int *)0=0;
|
|
||||||
#define ARRAY_CAP(x) sizeof((x))/sizeof(*(x))
|
|
||||||
#define FUNCTION static
|
|
||||||
#define GLOBAL static
|
|
||||||
#define MIN(x,y) ((x)>(y)?(y):(x))
|
|
||||||
#define MAX(x,y) ((x)>(y)?(x):(y))
|
|
||||||
#define CLAMP(min,x,max) MAX(min,MIN(max, x))
|
|
||||||
#define ABS(x) ((x)<0?-(x):(x))
|
|
||||||
#include <stdint.h>
|
|
||||||
typedef int64_t I64;
|
|
||||||
typedef uint64_t U64;
|
|
||||||
typedef uint32_t U32;
|
|
||||||
273
math.h
273
math.h
@@ -1,110 +1,8 @@
|
|||||||
#include "main.h"
|
|
||||||
#include <intrin.h>
|
|
||||||
|
|
||||||
constexpr float PI32 = 3.14159265359f;
|
|
||||||
|
|
||||||
struct Mat4 {
|
|
||||||
float p[4][4];
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Vec2 {
|
|
||||||
float x, y;
|
|
||||||
};
|
|
||||||
|
|
||||||
union Vec3 {
|
|
||||||
struct { float x, y, z; };
|
|
||||||
struct { float r, g, b; };
|
|
||||||
};
|
|
||||||
|
|
||||||
union Vec4 {
|
|
||||||
struct { float x, y, z, w; };
|
|
||||||
struct { float r, g, b, a; };
|
|
||||||
struct { Vec3 xyz; };
|
|
||||||
};
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec4 vec4(Vec3 a, float b) {
|
|
||||||
Vec4 result = { a.x,a.y,a.z,b };
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 vec3(float x, float y, float z) {
|
|
||||||
Vec3 result = { x,y,z };
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Mat4 make_matrix_identity() {
|
|
||||||
Mat4 result = {
|
|
||||||
1,0,0,0,
|
|
||||||
0,1,0,0,
|
|
||||||
0,0,1,0,
|
|
||||||
0,0,0,1,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float sin(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_sin_ps(result128);
|
|
||||||
float result = *(float *)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float cos(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_cos_ps(result128);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float tan(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_tan_ps(result128);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float floor(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_floor_ps(result128);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float ceil(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_ceil_ps(result128);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float round(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_round_ps(result128, _MM_FROUND_TO_NEAREST_INT| _MM_FROUND_NO_EXC);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float sqrt(float value) {
|
|
||||||
__m128 result128 = _mm_set_ps1(value);
|
|
||||||
result128 = _mm_sqrt_ps(result128);
|
|
||||||
float result = *(float*)&result128;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Mat4 make_matrix_rotation_x(float rotation) {
|
Mat4 make_matrix_rotation_x(float rotation) {
|
||||||
float s = sin(rotation);
|
float s = sinf(rotation);
|
||||||
float c = cos(rotation);
|
float c = cosf(rotation);
|
||||||
Mat4 result = {
|
Mat4 result = {
|
||||||
c, s, 0, 0,
|
c, s, 0, 0,
|
||||||
-s, c, 0, 0,
|
-s, c, 0, 0,
|
||||||
@@ -116,8 +14,8 @@ Mat4 make_matrix_rotation_x(float rotation) {
|
|||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Mat4 make_matrix_rotation_y(float rotation) {
|
Mat4 make_matrix_rotation_y(float rotation) {
|
||||||
float s = sin(rotation);
|
float s = sinf(rotation);
|
||||||
float c = cos(rotation);
|
float c = cosf(rotation);
|
||||||
Mat4 result = {
|
Mat4 result = {
|
||||||
c, 0, -s, 0,
|
c, 0, -s, 0,
|
||||||
0, 1, 0, 0,
|
0, 1, 0, 0,
|
||||||
@@ -129,8 +27,8 @@ Mat4 make_matrix_rotation_y(float rotation) {
|
|||||||
|
|
||||||
FUNCTION
|
FUNCTION
|
||||||
Mat4 make_matrix_rotation_z(float rotation) {
|
Mat4 make_matrix_rotation_z(float rotation) {
|
||||||
float s = sin(rotation);
|
float s = sinf(rotation);
|
||||||
float c = cos(rotation);
|
float c = cosf(rotation);
|
||||||
Mat4 result = {
|
Mat4 result = {
|
||||||
1, 0, 0, 0,
|
1, 0, 0, 0,
|
||||||
0, c, s, 0,
|
0, c, s, 0,
|
||||||
@@ -143,7 +41,7 @@ Mat4 make_matrix_rotation_z(float rotation) {
|
|||||||
FUNCTION
|
FUNCTION
|
||||||
Mat4 make_matrix_perspective(float fov, float window_x, float window_y, float znear, float zfar) {
|
Mat4 make_matrix_perspective(float fov, float window_x, float window_y, float znear, float zfar) {
|
||||||
float aspect_ratio = window_y / window_x;
|
float aspect_ratio = window_y / window_x;
|
||||||
float f = (1.f / tan((fov/2.f)*(180.f/PI32)));
|
float f = (1.f / tanf((fov/2.f)*(180.f/PI32)));
|
||||||
Mat4 result = {
|
Mat4 result = {
|
||||||
aspect_ratio*f, 0, 0, 0,
|
aspect_ratio*f, 0, 0, 0,
|
||||||
0, f, 0, 0,
|
0, f, 0, 0,
|
||||||
@@ -172,160 +70,3 @@ Mat4 translate(Mat4 a, Vec3 translation) {
|
|||||||
a.p[0][2] += translation.z;
|
a.p[0][2] += translation.z;
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec4 operator-(Vec4 a, Vec4 b) {
|
|
||||||
Vec4 result = {
|
|
||||||
a.x - b.x,
|
|
||||||
a.y - b.y,
|
|
||||||
a.z - b.z,
|
|
||||||
a.w - b.w
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec4 operator+(Vec4 a, Vec4 b) {
|
|
||||||
Vec4 result = {
|
|
||||||
a.x + b.x,
|
|
||||||
a.y + b.y,
|
|
||||||
a.z + b.z,
|
|
||||||
a.w + b.w
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 operator-(Vec3 a, Vec3 b) {
|
|
||||||
Vec3 result = {
|
|
||||||
a.x - b.x,
|
|
||||||
a.y - b.y,
|
|
||||||
a.z - b.z,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 operator+(Vec3 a, Vec3 b) {
|
|
||||||
Vec3 result = {
|
|
||||||
a.x + b.x,
|
|
||||||
a.y + b.y,
|
|
||||||
a.z + b.z,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec4 operator*(Mat4 a, Vec4 b) {
|
|
||||||
Vec4 result = {
|
|
||||||
a.p[0][0] * b.x + a.p[0][1] * b.y + a.p[0][2] * b.z + a.p[0][3] * b.w,
|
|
||||||
a.p[1][0] * b.x + a.p[1][1] * b.y + a.p[1][2] * b.z + a.p[1][3] * b.w,
|
|
||||||
a.p[2][0] * b.x + a.p[2][1] * b.y + a.p[2][2] * b.z + a.p[2][3] * b.w,
|
|
||||||
a.p[3][0] * b.x + a.p[3][1] * b.y + a.p[3][2] * b.z + a.p[3][3] * b.w,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 operator*(Mat4 a, Vec3 b) {
|
|
||||||
Vec4 result= {
|
|
||||||
a.p[0][0] * b.x + a.p[0][1] * b.y + a.p[0][2] * b.z + a.p[0][3] * 1,
|
|
||||||
a.p[1][0] * b.x + a.p[1][1] * b.y + a.p[1][2] * b.z + a.p[1][3] * 1,
|
|
||||||
a.p[2][0] * b.x + a.p[2][1] * b.y + a.p[2][2] * b.z + a.p[2][3] * 1,
|
|
||||||
a.p[3][0] * b.x + a.p[3][1] * b.y + a.p[3][2] * b.z + a.p[3][3] * 1,
|
|
||||||
};
|
|
||||||
ASSERT(result.w == 1);
|
|
||||||
return result.xyz;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Mat4 operator*(Mat4 a, Mat4 b) {
|
|
||||||
Mat4 result;
|
|
||||||
for (int y = 0; y < 4; y++) {
|
|
||||||
for (int x = 0; x < 4; x++) {
|
|
||||||
result.p[y][x] = a.p[y][0] * b.p[0][x] + a.p[y][1] * b.p[1][x] + a.p[y][2] * b.p[2][x] + a.p[y][3] * b.p[3][x];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float dot(Vec3 a, Vec3 b) {
|
|
||||||
float result = a.x * b.x + a.y * b.y + a.z * b.z;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 cross(Vec3 a, Vec3 b) {
|
|
||||||
Vec3 result = {
|
|
||||||
a.y * b.z - a.z * b.y,
|
|
||||||
a.z * b.x - a.x * b.z,
|
|
||||||
a.x * b.y - a.y * b.x,
|
|
||||||
};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
float length(Vec3 a) {
|
|
||||||
float result = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec3 normalize(Vec3 a) {
|
|
||||||
float len = length(a);
|
|
||||||
Vec3 result = {a.x/len, a.y/len, a.z/len};
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
U32 color_to_u32argb(Vec4 a) {
|
|
||||||
uint8_t r8 = (uint8_t)(a.r * 255.f);
|
|
||||||
uint8_t g8 = (uint8_t)(a.g * 255.f);
|
|
||||||
uint8_t b8 = (uint8_t)(a.b * 255.f);
|
|
||||||
uint8_t a8 = (uint8_t)(a.a * 255.f);
|
|
||||||
U32 result = a8 << 24 | r8 << 16 | g8 << 8 | b8;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
U32 color_to_u32abgr(Vec4 a) {
|
|
||||||
uint8_t r8 = (uint8_t)(a.r * 255.f);
|
|
||||||
uint8_t g8 = (uint8_t)(a.g * 255.f);
|
|
||||||
uint8_t b8 = (uint8_t)(a.b * 255.f);
|
|
||||||
uint8_t a8 = (uint8_t)(a.a * 255.f);
|
|
||||||
U32 result = a8 << 24 | b8 << 16 | g8 << 8 | r8;
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
FUNCTION
|
|
||||||
Vec4 v4argb(U32 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 vec4abgr(U32 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 = a + (b-a)*t; //(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;
|
|
||||||
}
|
|
||||||
@@ -134,17 +134,12 @@ namespace obj {
|
|||||||
Obj parse(char* memory, size_t memory_size, char* data) {
|
Obj parse(char* memory, size_t memory_size, char* data) {
|
||||||
Obj_Arena arena = { memory, memory_size };
|
Obj_Arena arena = { memory, memory_size };
|
||||||
Obj result = {};
|
Obj result = {};
|
||||||
int parsing_vertices = 0;
|
|
||||||
int parsing_normals = 0;
|
|
||||||
int parsing_textures = 0;
|
|
||||||
|
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
Token token = next_token(&data);
|
Token token = next_token(&data);
|
||||||
if (token.type == TokenType::end) break;
|
if (token.type == TokenType::end) break;
|
||||||
else if (token.type == TokenType::word) {
|
else if (token.type == TokenType::word) {
|
||||||
if (equals(token, "v")) {
|
if (equals(token, "v")) {
|
||||||
assert(parsing_vertices != 2);
|
|
||||||
parsing_vertices = 1;
|
|
||||||
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
||||||
ptr[0] = (float)expect_number(&data);
|
ptr[0] = (float)expect_number(&data);
|
||||||
ptr[1] = (float)expect_number(&data);
|
ptr[1] = (float)expect_number(&data);
|
||||||
@@ -154,10 +149,6 @@ namespace obj {
|
|||||||
debug_expect_raw(&data, TokenType::whitespace);
|
debug_expect_raw(&data, TokenType::whitespace);
|
||||||
}
|
}
|
||||||
else if (equals(token, "vt")) {
|
else if (equals(token, "vt")) {
|
||||||
assert(parsing_textures != 2);
|
|
||||||
parsing_textures = 1;
|
|
||||||
parsing_vertices = 2;
|
|
||||||
|
|
||||||
float* ptr = (float*)push(&arena, sizeof(float) * 2);
|
float* ptr = (float*)push(&arena, sizeof(float) * 2);
|
||||||
ptr[0] = (float)expect_number(&data);
|
ptr[0] = (float)expect_number(&data);
|
||||||
ptr[1] = (float)expect_number(&data);
|
ptr[1] = (float)expect_number(&data);
|
||||||
@@ -165,10 +156,6 @@ namespace obj {
|
|||||||
debug_expect_raw(&data, TokenType::whitespace);
|
debug_expect_raw(&data, TokenType::whitespace);
|
||||||
}
|
}
|
||||||
else if (equals(token, "vn")) {
|
else if (equals(token, "vn")) {
|
||||||
assert((parsing_textures == 1 || parsing_textures == 2) && parsing_vertices == 2);
|
|
||||||
parsing_textures = 2;
|
|
||||||
parsing_normals = 1;
|
|
||||||
|
|
||||||
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
||||||
ptr[0] = (float)expect_number(&data);
|
ptr[0] = (float)expect_number(&data);
|
||||||
ptr[1] = (float)expect_number(&data);
|
ptr[1] = (float)expect_number(&data);
|
||||||
@@ -177,7 +164,6 @@ namespace obj {
|
|||||||
debug_expect_raw(&data, TokenType::whitespace);
|
debug_expect_raw(&data, TokenType::whitespace);
|
||||||
}
|
}
|
||||||
else if (equals(token, "f")) {
|
else if (equals(token, "f")) {
|
||||||
assert(parsing_normals == 1 && parsing_textures == 2 && parsing_vertices == 2);
|
|
||||||
int* ptr = (int*)push(&arena, sizeof(int) * 9);
|
int* ptr = (int*)push(&arena, sizeof(int) * 9);
|
||||||
ptr[0] = (int)expect_number(&data);
|
ptr[0] = (int)expect_number(&data);
|
||||||
expect_token(&data, '/');
|
expect_token(&data, '/');
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
/*
|
/*
|
||||||
* The OS layer should expect ABGR writes to the screen, the bitmap is bottom up(origin 0,0 is in left bottom corner)
|
* The OS layer should expect ABGR writes to the screen, the bitmap is bottom up(origin 0,0 is in left bottom corner)
|
||||||
*/
|
*/
|
||||||
|
#if 0
|
||||||
|
|
||||||
#define _CRT_SECURE_NO_WARNINGS
|
#define _CRT_SECURE_NO_WARNINGS
|
||||||
#include "platform.h"
|
#include "platform.h"
|
||||||
@@ -207,3 +208,5 @@ char* OS::read_file(const char* path) {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
#include "main.h"
|
// #include "main.h"
|
||||||
|
|
||||||
struct Image {
|
struct Image {
|
||||||
U32* pixels;
|
U32* pixels;
|
||||||
I64 x;
|
I64 x;
|
||||||
I64 y;
|
I64 y;
|
||||||
};
|
};
|
||||||
|
#if 0
|
||||||
|
|
||||||
struct OSInitArgs {
|
struct OSInitArgs {
|
||||||
int window_x;
|
int window_x;
|
||||||
@@ -30,3 +30,4 @@ struct OS {
|
|||||||
OS(OSInitArgs args) { init(args); }
|
OS(OSInitArgs args) { init(args); }
|
||||||
char os_internal_data[1024];
|
char os_internal_data[1024];
|
||||||
};
|
};
|
||||||
|
#endif
|
||||||
12372
stb_image.h
12372
stb_image.h
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user