Updating renderer to work with new base version

This commit is contained in:
Krzosa Karol
2022-06-29 09:42:47 +02:00
parent c1904a33ef
commit 59dbbc2a57
7 changed files with 262 additions and 284 deletions

View File

@@ -25,6 +25,8 @@
- [x] FPS Camera - [x] FPS Camera
- [ ] Quarternions for rotations - [ ] Quarternions for rotations
- [x] Reading OBJ models - [x] Reading OBJ models
- [x] Dumping raw obj files
- [x] Loading raw obj files, big startup speedup!
- [ ] Reading more OBJ formats - [ ] Reading more OBJ formats
- [x] Reading OBJ .mtl files - [x] Reading OBJ .mtl files
- [x] Loading materials - [x] Loading materials
@@ -41,7 +43,7 @@
- [ ] Outlines - [ ] Outlines
- [ ] Lightning - [ ] Lightning
- [ ] Proper normal interpolation - [ ] Proper normal interpolation
* https://hero.handmade.network/episode/code/day101/#105 * `https://hero.handmade.network/episode/code/day101/#105
- [ ] Phong - [ ] Phong
- [x] diffuse - [x] diffuse
- [x] ambient - [x] ambient
@@ -76,6 +78,12 @@
- [x] Simple scatter plot - [x] Simple scatter plot
### Urgent:
- [ ] Simplify the code, especially for the 2d routines
- [x] Asset processor as second program
## Clipping ## Clipping
There are 3 clipping stages, 2 clipping stages in 3D space against zfar and znear and 1 clipping There are 3 clipping stages, 2 clipping stages in 3D space against zfar and znear and 1 clipping

View File

@@ -336,6 +336,7 @@ dump_bitmap_image(String_Builder *sb, Bitmap *bm){
sb->append_data(bm->pixels, sizeof(U32)*bm->x*bm->y); sb->append_data(bm->pixels, sizeof(U32)*bm->x*bm->y);
} }
function B32 function B32
_os_write_file(String file, String data, B32 append = false) { _os_write_file(String file, String data, B32 append = false) {
B32 result = false; B32 result = false;
@@ -368,7 +369,7 @@ _os_write_file(String file, String data, B32 append = false) {
} }
function B32 function B32
os_write_file(String file, String data) { os_write_file2(String file, String data) {
return _os_write_file(file, data, false); return _os_write_file(file, data, false);
} }
function B32 function B32
@@ -393,9 +394,9 @@ dump_obj_to_file(Obj *obj, String out_name){
obj->materials.allocator = 0; obj->materials.allocator = 0;
obj->materials.cap = obj->materials.len; obj->materials.cap = obj->materials.len;
Iter(obj->mesh){ For(obj->mesh){
it->indices.allocator = 0; it.indices.allocator = 0;
it->indices.cap = it->indices.len; it.indices.cap = it.indices.len;
} }
Scratch arena; Scratch arena;
@@ -408,20 +409,20 @@ dump_obj_to_file(Obj *obj, String out_name){
sb.append_data(obj->mesh.data, obj->mesh.len*sizeof(Obj_Mesh)); sb.append_data(obj->mesh.data, obj->mesh.len*sizeof(Obj_Mesh));
sb.append_data(obj->materials.data, obj->materials.len*sizeof(Obj_Material)); sb.append_data(obj->materials.data, obj->materials.len*sizeof(Obj_Material));
Iter(obj->mesh){ For(obj->mesh){
sb.append_data(it->indices.data, sizeof(Obj_Index)*it->indices.len); sb.append_data(it.indices.data, sizeof(Obj_Index)*it.indices.len);
} }
Iter(obj->materials){ For(obj->materials){
sb.append_data(it, sizeof(Obj_Material)); sb.append_data(&it, sizeof(Obj_Material));
dump_bitmap_image(&sb, &it->texture_ambient); dump_bitmap_image(&sb, &it.texture_ambient);
dump_bitmap_image(&sb, &it->texture_diffuse); dump_bitmap_image(&sb, &it.texture_diffuse);
dump_bitmap_image(&sb, &it->texture_dissolve); dump_bitmap_image(&sb, &it.texture_dissolve);
dump_bitmap_image(&sb, &it->texture_displacement); dump_bitmap_image(&sb, &it.texture_displacement);
} }
String result = string_flatten(arena, &sb); String result = string_flatten(arena, &sb);
os_write_file(out_name, result); os_write_file2(out_name, result);
} }
global FILE *output_file; global FILE *output_file;
@@ -451,8 +452,8 @@ main(int argc, char **argv){
material.texture_ambient = load_image("assets/bricksx64.png"_s); material.texture_ambient = load_image("assets/bricksx64.png"_s);
plane_obj.materials.add(material); plane_obj.materials.add(material);
For(plane_obj.mesh){ For(plane_obj.mesh){
IFor(it->indices, jt, j){ For_Named(it.indices, jt){
jt->material_id = 0; jt.material_id = 0;
} }
} }

View File

@@ -1,8 +1,7 @@
@echo off @echo off
rem clang assets.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o assets.exe -Wl,user32.lib clang assets.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o assets.exe -Wl,user32.lib
assets.exe
rem assets.exe
clang main.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o main.exe -Wl,user32.lib clang main.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o main.exe -Wl,user32.lib

View File

@@ -164,12 +164,10 @@ void draw_rect(Bitmap* dst, F32 X, F32 Y, F32 w, F32 h, Vec4 color) {
} }
} }
function function void
void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos, Vec2 size=vec2(F32MAX, F32MAX)) { draw_bitmap(Bitmap *dst, Bitmap *src, Vec2 pos){
S64 minx = (S64)(pos.x + 0.5); S64 minx = (S64)(pos.x + 0.5);
S64 miny = (S64)(pos.y + 0.5); S64 miny = (S64)(pos.y + 0.5);
if (size.x == F32MAX || size.y == F32MAX) {
S64 maxx = minx + src->x; S64 maxx = minx + src->x;
S64 maxy = miny + src->y; S64 maxy = miny + src->y;
S64 offsetx = 0; S64 offsetx = 0;
@@ -203,8 +201,12 @@ void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos, Vec2 size=vec2(F32MAX, F32M
*dst_pixel = color32; *dst_pixel = color32;
} }
} }
} }
else {
function
void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos, Vec2 size) {
S64 minx = (S64)(pos.x + 0.5);
S64 miny = (S64)(pos.y + 0.5);
S64 maxx = minx + (S64)(size.x + 0.5f); S64 maxx = minx + (S64)(size.x + 0.5f);
S64 maxy = miny + (S64)(size.y + 0.5f); S64 maxy = miny + (S64)(size.y + 0.5f);
S64 offsetx = 0; S64 offsetx = 0;
@@ -238,7 +240,6 @@ void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos, Vec2 size=vec2(F32MAX, F32M
*dst_pixel = color32; *dst_pixel = color32;
} }
} }
}
} }
function function
@@ -465,6 +466,9 @@ void draw_triangle_bilinear(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 li
U32 *pixel = src->pixels + (ui + (src->y - 1ll - vi) * src->x); U32 *pixel = src->pixels + (ui + (src->y - 1ll - vi) * src->x);
U32 *dst_pixel = dst->pixels + (x + y * dst->x); U32 *dst_pixel = dst->pixels + (x + y * dst->x);
#if 0
Vec4 result_color = vec4abgr(*pixel);
#else
Vec4 pixelx1y1 = vec4abgr(*pixel); Vec4 pixelx1y1 = vec4abgr(*pixel);
Vec4 pixelx2y1 = vec4abgr(*(pixel + 1)); Vec4 pixelx2y1 = vec4abgr(*(pixel + 1));
Vec4 pixelx1y2 = vec4abgr(*(pixel - src->x)); Vec4 pixelx1y2 = vec4abgr(*(pixel - src->x));
@@ -476,6 +480,7 @@ void draw_triangle_bilinear(Bitmap* dst, F32 *depth_buffer, Bitmap *src, Vec3 li
Vec4 blendx1 = lerp(pixelx1y1, pixelx2y1, udiff); Vec4 blendx1 = lerp(pixelx1y1, pixelx2y1, udiff);
Vec4 blendx2 = lerp(pixelx1y2, pixelx2y2, udiff); Vec4 blendx2 = lerp(pixelx1y2, pixelx2y2, udiff);
Vec4 result_color = lerp(blendx1, blendx2, vdiff); Vec4 result_color = lerp(blendx1, blendx2, vdiff);
#endif
Vec3 light_color = vec3(0.8,0.8,1); Vec3 light_color = vec3(0.8,0.8,1);
constexpr F32 ambient_strength = 0.1f; { constexpr F32 ambient_strength = 0.1f; {
@@ -629,19 +634,14 @@ void draw_mesh(Render *r, String scene_name, Obj_Material *materials, Obj_Mesh *
} }
draw_triangle_bilinear(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[1].pos, in[2].pos, in[0].tex, in[1].tex, in[2].tex, in[0].norm, in[1].norm, in[2].norm); draw_triangle_nearest(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[1].pos, in[2].pos, in[0].tex, in[1].tex, in[2].tex, in[0].norm, in[1].norm, in[2].norm);
if (in_count > 3) { if (in_count > 3) {
draw_triangle_bilinear(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[2].pos, in[3].pos, in[0].tex, in[2].tex, in[3].tex, in[0].norm, in[2].norm, in[3].norm); draw_triangle_nearest(&r->screen320, r->depth320, image, light_direction, in[0].pos, in[2].pos, in[3].pos, in[0].tex, in[2].tex, in[3].tex, in[0].norm, in[2].norm, in[3].norm);
} }
#if 0
ProfileScope *scope = profile_scopes + ProfileScopeName_draw_triangle;
LOCAL_PERSIST B32 profile_flag;
if (!profile_flag && scope->i > 2000) {
profile_flag = 1;
save_profile_data(scope, scene_name, LIT("draw_triangle"));
}
#endif
} }
} }
} }
@@ -692,8 +692,8 @@ main(int argc, char **argv) {
sponza = load_obj_dump(os.perm_arena, "sponza.bin"_s); sponza = load_obj_dump(os.perm_arena, "sponza.bin"_s);
scene_callback(); scene_callback();
int screen_x = 320; int screen_x = 1280;
int screen_y = 180; int screen_y = 720;
r.camera_pos = {0,0,-2}; r.camera_pos = {0,0,-2};
r.screen320 = {(U32 *)arena_push_size(os.perm_arena, screen_x*screen_y*sizeof(U32)), screen_x, screen_y}; r.screen320 = {(U32 *)arena_push_size(os.perm_arena, screen_x*screen_y*sizeof(U32)), screen_x, screen_y};

18
obj.cpp
View File

@@ -61,8 +61,8 @@ function Obj *
load_obj_dump(Allocator *allocator, String filename){ load_obj_dump(Allocator *allocator, String filename){
String string = os_read_file(allocator, filename); String string = os_read_file(allocator, filename);
Obj *obj = (Obj *)string.str; Stream stream = {string.str, string.str + string.len};
Stream stream = {(U8 *)(obj+1), string.str + string.len}; Obj *obj = stream_read_struct(&stream, Obj);
obj->name.str = stream_read_array(&stream, U8, obj->name.len); obj->name.str = stream_read_array(&stream, U8, obj->name.len);
obj->vertices.data = stream_read_array(&stream, Vec3, obj->vertices.len); obj->vertices.data = stream_read_array(&stream, Vec3, obj->vertices.len);
obj->texture_coordinates.data = stream_read_array(&stream, Vec2, obj->texture_coordinates.len); obj->texture_coordinates.data = stream_read_array(&stream, Vec2, obj->texture_coordinates.len);
@@ -70,15 +70,15 @@ load_obj_dump(Allocator *allocator, String filename){
obj->mesh.data = stream_read_array(&stream, Obj_Mesh, obj->mesh.len); obj->mesh.data = stream_read_array(&stream, Obj_Mesh, obj->mesh.len);
obj->materials.data = stream_read_array(&stream, Obj_Material, obj->materials.len); obj->materials.data = stream_read_array(&stream, Obj_Material, obj->materials.len);
Iter(obj->mesh){ For(obj->mesh){
it->indices.data = stream_read_array(&stream, Obj_Index, it->indices.len); it.indices.data = stream_read_array(&stream, Obj_Index, it.indices.len);
} }
Iter(obj->materials){ For(obj->materials){
it->texture_ambient.pixels = stream_read_array(&stream, U32, it->texture_ambient.x*it->texture_ambient.y); it.texture_ambient.pixels = stream_read_array(&stream, U32, it.texture_ambient.x*it.texture_ambient.y);
it->texture_diffuse.pixels = stream_read_array(&stream, U32, it->texture_diffuse.x*it->texture_diffuse.y); it.texture_diffuse.pixels = stream_read_array(&stream, U32, it.texture_diffuse.x*it.texture_diffuse.y);
it->texture_dissolve.pixels = stream_read_array(&stream, U32, it->texture_dissolve.x*it->texture_dissolve.y); it.texture_dissolve.pixels = stream_read_array(&stream, U32, it.texture_dissolve.x*it.texture_dissolve.y);
it->texture_displacement.pixels = stream_read_array(&stream, U32, it->texture_displacement.x*it->texture_displacement.y); it.texture_displacement.pixels = stream_read_array(&stream, U32, it.texture_displacement.x*it.texture_displacement.y);
} }
return obj; return obj;

View File

@@ -21,33 +21,3 @@ _profile_scope->samples[_profile_scope->i] = __rdtsc() - _profile_scope->samples
_profile_scope->i = (_profile_scope->i + 1) % 5096; \ _profile_scope->i = (_profile_scope->i + 1) % 5096; \
}while (0) }while (0)
function void save_profile_data(ProfileScope *scope, S8 scenario_name, S8 scope_name) {
/*for (S64 si = 1; si < scope->i; si++) {
for (S64 sj = 1; sj < scope->i; sj++) {
if (scope->samples[sj] < scope->samples[sj - 1]) {
F64 temp = scope->samples[sj];
scope->samples[sj] = scope->samples[sj-1];
scope->samples[sj-1] = temp;
}
}
}*/
/*
Scratch scratch;
scenario_name = string_chop_last_period(scenario_name);
scenario_name = string_skip_to_last_slash(scenario_name);
U8 *string_pointer = string_begin(scratch);
string_fmt(scratch, "%s %s\n", build_name, scenario_name);
S64 one_past_last = scope->i;
for (S64 si = 0; si < one_past_last; si++) {
string_fmt(scratch, "%u\n", scope->samples[si]);
}
S8 data = string_end(scratch, string_pointer);
Date date = os_date();
os_make_dir(LIT("stats"));
S8 name = string_fmt(scratch, "stats/%s_%s_%s_%u_%u_%u_%u_%u_%u.txt", scope_name, build_name, scenario_name, date.year, date.month, date.day, date.hour, date.minute, date.second);
os_append_file(name, data);
*/
}

2
ui.cpp
View File

@@ -56,7 +56,7 @@ struct UI : UIWidget {
}; };
function UIWidget *ui_new_widget(Allocator *arena, UIWidgetKind kind) { function UIWidget *ui_new_widget(Allocator *arena, UIWidgetKind kind) {
UIWidget *result = exp_alloc_type(arena, UIWidget); UIWidget *result = exp_alloc_type(arena, UIWidget, AF_ZeroMemory);
result->kind = kind; result->kind = kind;
return result; return result;
} }