Updating renderer to work with new base version
This commit is contained in:
10
README.md
10
README.md
@@ -25,6 +25,8 @@
|
||||
- [x] FPS Camera
|
||||
- [ ] Quarternions for rotations
|
||||
- [x] Reading OBJ models
|
||||
- [x] Dumping raw obj files
|
||||
- [x] Loading raw obj files, big startup speedup!
|
||||
- [ ] Reading more OBJ formats
|
||||
- [x] Reading OBJ .mtl files
|
||||
- [x] Loading materials
|
||||
@@ -41,7 +43,7 @@
|
||||
- [ ] Outlines
|
||||
- [ ] Lightning
|
||||
- [ ] Proper normal interpolation
|
||||
* https://hero.handmade.network/episode/code/day101/#105
|
||||
* `https://hero.handmade.network/episode/code/day101/#105
|
||||
- [ ] Phong
|
||||
- [x] diffuse
|
||||
- [x] ambient
|
||||
@@ -76,6 +78,12 @@
|
||||
- [x] Simple scatter plot
|
||||
|
||||
|
||||
### Urgent:
|
||||
|
||||
- [ ] Simplify the code, especially for the 2d routines
|
||||
- [x] Asset processor as second program
|
||||
|
||||
|
||||
## Clipping
|
||||
|
||||
There are 3 clipping stages, 2 clipping stages in 3D space against zfar and znear and 1 clipping
|
||||
|
||||
31
assets.cpp
31
assets.cpp
@@ -336,6 +336,7 @@ dump_bitmap_image(String_Builder *sb, Bitmap *bm){
|
||||
sb->append_data(bm->pixels, sizeof(U32)*bm->x*bm->y);
|
||||
}
|
||||
|
||||
|
||||
function B32
|
||||
_os_write_file(String file, String data, B32 append = false) {
|
||||
B32 result = false;
|
||||
@@ -368,7 +369,7 @@ _os_write_file(String file, String data, B32 append = false) {
|
||||
}
|
||||
|
||||
function B32
|
||||
os_write_file(String file, String data) {
|
||||
os_write_file2(String file, String data) {
|
||||
return _os_write_file(file, data, false);
|
||||
}
|
||||
function B32
|
||||
@@ -393,9 +394,9 @@ dump_obj_to_file(Obj *obj, String out_name){
|
||||
obj->materials.allocator = 0;
|
||||
obj->materials.cap = obj->materials.len;
|
||||
|
||||
Iter(obj->mesh){
|
||||
it->indices.allocator = 0;
|
||||
it->indices.cap = it->indices.len;
|
||||
For(obj->mesh){
|
||||
it.indices.allocator = 0;
|
||||
it.indices.cap = it.indices.len;
|
||||
}
|
||||
|
||||
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->materials.data, obj->materials.len*sizeof(Obj_Material));
|
||||
|
||||
Iter(obj->mesh){
|
||||
sb.append_data(it->indices.data, sizeof(Obj_Index)*it->indices.len);
|
||||
For(obj->mesh){
|
||||
sb.append_data(it.indices.data, sizeof(Obj_Index)*it.indices.len);
|
||||
}
|
||||
|
||||
Iter(obj->materials){
|
||||
sb.append_data(it, sizeof(Obj_Material));
|
||||
dump_bitmap_image(&sb, &it->texture_ambient);
|
||||
dump_bitmap_image(&sb, &it->texture_diffuse);
|
||||
dump_bitmap_image(&sb, &it->texture_dissolve);
|
||||
dump_bitmap_image(&sb, &it->texture_displacement);
|
||||
For(obj->materials){
|
||||
sb.append_data(&it, sizeof(Obj_Material));
|
||||
dump_bitmap_image(&sb, &it.texture_ambient);
|
||||
dump_bitmap_image(&sb, &it.texture_diffuse);
|
||||
dump_bitmap_image(&sb, &it.texture_dissolve);
|
||||
dump_bitmap_image(&sb, &it.texture_displacement);
|
||||
}
|
||||
|
||||
String result = string_flatten(arena, &sb);
|
||||
os_write_file(out_name, result);
|
||||
os_write_file2(out_name, result);
|
||||
}
|
||||
|
||||
global FILE *output_file;
|
||||
@@ -451,8 +452,8 @@ main(int argc, char **argv){
|
||||
material.texture_ambient = load_image("assets/bricksx64.png"_s);
|
||||
plane_obj.materials.add(material);
|
||||
For(plane_obj.mesh){
|
||||
IFor(it->indices, jt, j){
|
||||
jt->material_id = 0;
|
||||
For_Named(it.indices, jt){
|
||||
jt.material_id = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
@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
|
||||
|
||||
rem assets.exe
|
||||
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
|
||||
|
||||
clang main.cpp -Wall -Wno-unused-function -Wno-missing-braces -fno-exceptions -fdiagnostics-absolute-paths -g -I".." -o main.exe -Wl,user32.lib
|
||||
158
main.cpp
158
main.cpp
@@ -164,79 +164,80 @@ void draw_rect(Bitmap* dst, F32 X, F32 Y, F32 w, F32 h, Vec4 color) {
|
||||
}
|
||||
}
|
||||
|
||||
function
|
||||
void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos, Vec2 size=vec2(F32MAX, F32MAX)) {
|
||||
function void
|
||||
draw_bitmap(Bitmap *dst, Bitmap *src, Vec2 pos){
|
||||
S64 minx = (S64)(pos.x + 0.5);
|
||||
S64 miny = (S64)(pos.y + 0.5);
|
||||
S64 maxx = minx + src->x;
|
||||
S64 maxy = miny + src->y;
|
||||
S64 offsetx = 0;
|
||||
S64 offsety = 0;
|
||||
|
||||
if (size.x == F32MAX || size.y == F32MAX) {
|
||||
S64 maxx = minx + src->x;
|
||||
S64 maxy = miny + src->y;
|
||||
S64 offsetx = 0;
|
||||
S64 offsety = 0;
|
||||
|
||||
if (maxx > dst->x) {
|
||||
maxx = dst->x;
|
||||
}
|
||||
if (maxy > dst->y) {
|
||||
maxy = dst->y;
|
||||
}
|
||||
if (minx < 0) {
|
||||
offsetx = -minx;
|
||||
minx = 0;
|
||||
}
|
||||
if (miny < 0) {
|
||||
offsety = -miny;
|
||||
miny = 0;
|
||||
}
|
||||
for (S64 y = miny; y < maxy; y++) {
|
||||
for (S64 x = minx; x < maxx; x++) {
|
||||
S64 tx = x - minx + offsetx;
|
||||
S64 ty = y - miny + offsety;
|
||||
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
|
||||
U32 *pixel = src->pixels + (tx + ty * src->x);
|
||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||
result_color = premultiplied_alpha(dst_color, result_color);
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
*dst_pixel = color32;
|
||||
}
|
||||
if (maxx > dst->x) {
|
||||
maxx = dst->x;
|
||||
}
|
||||
if (maxy > dst->y) {
|
||||
maxy = dst->y;
|
||||
}
|
||||
if (minx < 0) {
|
||||
offsetx = -minx;
|
||||
minx = 0;
|
||||
}
|
||||
if (miny < 0) {
|
||||
offsety = -miny;
|
||||
miny = 0;
|
||||
}
|
||||
for (S64 y = miny; y < maxy; y++) {
|
||||
for (S64 x = minx; x < maxx; x++) {
|
||||
S64 tx = x - minx + offsetx;
|
||||
S64 ty = y - miny + offsety;
|
||||
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
|
||||
U32 *pixel = src->pixels + (tx + ty * src->x);
|
||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||
result_color = premultiplied_alpha(dst_color, result_color);
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
*dst_pixel = color32;
|
||||
}
|
||||
}
|
||||
else {
|
||||
S64 maxx = minx + (S64)(size.x + 0.5f);
|
||||
S64 maxy = miny + (S64)(size.y + 0.5f);
|
||||
S64 offsetx = 0;
|
||||
S64 offsety = 0;
|
||||
maxx = clamp_top(maxx, (S64)dst->x);
|
||||
maxy = clamp_top(maxy, (S64)dst->y);
|
||||
if (minx < 0) {
|
||||
offsetx = -minx;
|
||||
minx = 0;
|
||||
}
|
||||
if (miny < 0) {
|
||||
offsety = -miny;
|
||||
miny = 0;
|
||||
}
|
||||
}
|
||||
|
||||
F32 distx = (F32)(maxx - minx);
|
||||
F32 disty = (F32)(maxy - miny);
|
||||
for (S64 y = miny; y < maxy; y++) {
|
||||
for (S64 x = minx; x < maxx; x++) {
|
||||
F32 u = (F32)(x - minx) / distx;
|
||||
F32 v = (F32)(y - miny) / disty;
|
||||
S64 tx = (S64)(u * src->x + 0.5f);
|
||||
S64 ty = (S64)(v * src->y + 0.5f);
|
||||
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
|
||||
U32 *pixel = src->pixels + (tx + ty * src->x);
|
||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||
result_color = premultiplied_alpha(dst_color, result_color);
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
*dst_pixel = color32;
|
||||
}
|
||||
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 maxy = miny + (S64)(size.y + 0.5f);
|
||||
S64 offsetx = 0;
|
||||
S64 offsety = 0;
|
||||
maxx = clamp_top(maxx, (S64)dst->x);
|
||||
maxy = clamp_top(maxy, (S64)dst->y);
|
||||
if (minx < 0) {
|
||||
offsetx = -minx;
|
||||
minx = 0;
|
||||
}
|
||||
if (miny < 0) {
|
||||
offsety = -miny;
|
||||
miny = 0;
|
||||
}
|
||||
|
||||
F32 distx = (F32)(maxx - minx);
|
||||
F32 disty = (F32)(maxy - miny);
|
||||
for (S64 y = miny; y < maxy; y++) {
|
||||
for (S64 x = minx; x < maxx; x++) {
|
||||
F32 u = (F32)(x - minx) / distx;
|
||||
F32 v = (F32)(y - miny) / disty;
|
||||
S64 tx = (S64)(u * src->x + 0.5f);
|
||||
S64 ty = (S64)(v * src->y + 0.5f);
|
||||
U32 *dst_pixel = dst->pixels + (x + y * dst->x);
|
||||
U32 *pixel = src->pixels + (tx + ty * src->x);
|
||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||
result_color = premultiplied_alpha(dst_color, result_color);
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
*dst_pixel = color32;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 *dst_pixel = dst->pixels + (x + y * dst->x);
|
||||
|
||||
#if 0
|
||||
Vec4 result_color = vec4abgr(*pixel);
|
||||
#else
|
||||
Vec4 pixelx1y1 = vec4abgr(*pixel);
|
||||
Vec4 pixelx2y1 = vec4abgr(*(pixel + 1));
|
||||
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 blendx2 = lerp(pixelx1y2, pixelx2y2, udiff);
|
||||
Vec4 result_color = lerp(blendx1, blendx2, vdiff);
|
||||
#endif
|
||||
|
||||
Vec3 light_color = vec3(0.8,0.8,1);
|
||||
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) {
|
||||
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);
|
||||
scene_callback();
|
||||
|
||||
int screen_x = 320;
|
||||
int screen_y = 180;
|
||||
int screen_x = 1280;
|
||||
int screen_y = 720;
|
||||
|
||||
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};
|
||||
|
||||
18
obj.cpp
18
obj.cpp
@@ -61,8 +61,8 @@ function Obj *
|
||||
load_obj_dump(Allocator *allocator, String filename){
|
||||
String string = os_read_file(allocator, filename);
|
||||
|
||||
Obj *obj = (Obj *)string.str;
|
||||
Stream stream = {(U8 *)(obj+1), string.str + string.len};
|
||||
Stream stream = {string.str, string.str + string.len};
|
||||
Obj *obj = stream_read_struct(&stream, Obj);
|
||||
obj->name.str = stream_read_array(&stream, U8, obj->name.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);
|
||||
@@ -70,15 +70,15 @@ load_obj_dump(Allocator *allocator, String filename){
|
||||
obj->mesh.data = stream_read_array(&stream, Obj_Mesh, obj->mesh.len);
|
||||
obj->materials.data = stream_read_array(&stream, Obj_Material, obj->materials.len);
|
||||
|
||||
Iter(obj->mesh){
|
||||
it->indices.data = stream_read_array(&stream, Obj_Index, it->indices.len);
|
||||
For(obj->mesh){
|
||||
it.indices.data = stream_read_array(&stream, Obj_Index, it.indices.len);
|
||||
}
|
||||
|
||||
Iter(obj->materials){
|
||||
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_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);
|
||||
For(obj->materials){
|
||||
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_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);
|
||||
}
|
||||
|
||||
return obj;
|
||||
|
||||
30
profile.cpp
30
profile.cpp
@@ -21,33 +21,3 @@ _profile_scope->samples[_profile_scope->i] = __rdtsc() - _profile_scope->samples
|
||||
_profile_scope->i = (_profile_scope->i + 1) % 5096; \
|
||||
}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);
|
||||
*/
|
||||
}
|
||||
Reference in New Issue
Block a user