diff options
author | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-31 12:26:22 +1000 |
---|---|---|
committer | omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-07-31 12:26:22 +1000 |
commit | fa04ebc603a7bf742c64724ede23cfa010fb3c4c (patch) | |
tree | 59f000765e5528bda36194a91b21c187c9e988bd | |
parent | c2fec36353c29d31ed95451e42413983808fd62e (diff) |
grid is not really working
-rw-r--r-- | assets/shaders/grid.frag | 55 | ||||
-rw-r--r-- | assets/shaders/grid.vert | 29 | ||||
-rw-r--r-- | examples/game_demo/game_demo.c | 2 | ||||
-rw-r--r-- | src/resources/gltf.c | 40 | ||||
-rw-r--r-- | src/systems/grid.c | 10 |
5 files changed, 108 insertions, 28 deletions
diff --git a/assets/shaders/grid.frag b/assets/shaders/grid.frag index 06eebc3..6cf8a7c 100644 --- a/assets/shaders/grid.frag +++ b/assets/shaders/grid.frag @@ -2,6 +2,59 @@ out vec4 FragColor; +in float near; //0.01 +in float far; //100 +in vec3 nearPoint; +in vec3 farPoint; +in mat4 fragView; +in mat4 fragProj; + +// void main() { +// // FragColor = vec4(1.0, 0.0, 0.0, 1.0); +// float t = -nearPoint.y / (farPoint.y - nearPoint.y); +// if (t > 0) { +// FragColor = vec4(1.0, 0.0, 0.0, 1.0); +// } else { +// FragColor = vec4(0.0); +// } +// // FragColor = vec4(1.0, 0.0, 0.0, 1.0 * float(t > 0)); // opacity = 1 when t > 0, opacity = 0 otherwise +// } + +vec4 grid(vec3 fragPos3D, float scale, bool drawAxis) { + vec2 coord = fragPos3D.xz * scale; + vec2 derivative = fwidth(coord); + vec2 grid = abs(fract(coord - 0.5) - 0.5) / derivative; + float line = min(grid.x, grid.y); + float minimumz = min(derivative.y, 1); + float minimumx = min(derivative.x, 1); + vec4 color = vec4(0.2, 0.2, 0.2, 1.0 - min(line, 1.0)); + // z axis + if(fragPos3D.x > -0.1 * minimumx && fragPos3D.x < 0.1 * minimumx) + color.z = 1.0; + // x axis + if(fragPos3D.z > -0.1 * minimumz && fragPos3D.z < 0.1 * minimumz) + color.x = 1.0; + return color; +} +float computeDepth(vec3 pos) { + vec4 clip_space_pos = fragProj * fragView * vec4(pos.xyz, 1.0); + return (clip_space_pos.z / clip_space_pos.w); +} +float computeLinearDepth(vec3 pos) { + vec4 clip_space_pos = fragProj * fragView * vec4(pos.xyz, 1.0); + float clip_space_depth = (clip_space_pos.z / clip_space_pos.w) * 2.0 - 1.0; // put back between -1 and 1 + float linearDepth = (2.0 * near * far) / (far + near - clip_space_depth * (far - near)); // get linear value between 0.01 and 100 + return linearDepth / far; // normalize +} void main() { - FragColor = vec4(1.0, 0.0, 0.0, 1.0); + float t = -nearPoint.y / (farPoint.y - nearPoint.y); + vec3 fragPos3D = nearPoint + t * (farPoint - nearPoint); + + gl_FragDepth = computeDepth(fragPos3D); + + float linearDepth = computeLinearDepth(fragPos3D); + float fading = max(0, (0.5 - linearDepth)); + + FragColor = (grid(fragPos3D, 10, true) + grid(fragPos3D, 1, true))* float(t > 0); // adding multiple resolution for the grid + FragColor.a *= fading; }
\ No newline at end of file diff --git a/assets/shaders/grid.vert b/assets/shaders/grid.vert index bdfa477..592cbfc 100644 --- a/assets/shaders/grid.vert +++ b/assets/shaders/grid.vert @@ -6,12 +6,37 @@ uniform Camera { vec4 viewPos; } cam; +out vec3 nearPoint; +out vec3 farPoint; +out float near; +out float far; +out mat4 fragView; +out mat4 fragProj; + // Grid position are in xy clipped space vec3 gridPlane[6] = vec3[]( vec3(1, 1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0), vec3(-1, -1, 0), vec3(1, 1, 0), vec3(1, -1, 0) ); -// normal vertice projection + +vec3 UnprojectPoint(float x, float y, float z, mat4 view, mat4 projection) { + mat4 viewInv = inverse(view); + mat4 projInv = inverse(projection); + vec4 unprojectedPoint = viewInv * projInv * vec4(x, y, z, 1.0); + return unprojectedPoint.xyz / unprojectedPoint.w; +} + +// normal vertex projection void main() { - gl_Position = cam.proj * cam.view * vec4(gridPlane[gl_VertexID].xyz, 1.0); + // gl_Position = cam.proj * cam.view * vec4(gridPlane[gl_VertexID].xyz, 1.0); + vec3 p = gridPlane[gl_VertexID].xyz; + nearPoint = UnprojectPoint(p.x, p.y, -1.0, cam.view, cam.proj).xyz; // unprojecting on the near plane + farPoint = UnprojectPoint(p.x, p.y, 1.0, cam.view, cam.proj).xyz; // unprojecting on the far plane + + fragView = cam.view; + fragProj = cam.proj; + near = 0.01; + far = 100.0; + + gl_Position = vec4(p, 1.0); // using directly the clipped coordinates }
\ No newline at end of file diff --git a/examples/game_demo/game_demo.c b/examples/game_demo/game_demo.c index dfb70d1..e81f6e1 100644 --- a/examples/game_demo/game_demo.c +++ b/examples/game_demo/game_demo.c @@ -106,7 +106,7 @@ int main() { if (draw_debug) { // draw the player model with shadows - // Render_RenderEntities(render_entities->data, render_entities->len); + Render_RenderEntities(render_entities->data, render_entities->len); // Render_DrawTerrain(); Skybox_Draw(&skybox, cam); } else { diff --git a/src/resources/gltf.c b/src/resources/gltf.c index 41ff9e2..91816d8 100644 --- a/src/resources/gltf.c +++ b/src/resources/gltf.c @@ -118,6 +118,24 @@ void load_texcoord_components(Vec2_darray *texcoords, cgltf_accessor *accessor) } } +void load_joint_index_components(Vec4u_darray *joint_indices, cgltf_accessor *accessor) { + TRACE("Load joint indices from accessor"); + CASSERT(accessor->component_type == cgltf_component_type_r_16u); + CASSERT_MSG(accessor->type == cgltf_type_vec4, "Joint indices should be a vec4"); + Vec4u tmp_joint_index; + Vec4 joints_as_floats; + for (cgltf_size v = 0; v < accessor->count; ++v) { + cgltf_accessor_read_float(accessor, v, &joints_as_floats.x, 4); + tmp_joint_index.x = (u32)joints_as_floats.x; + tmp_joint_index.y = (u32)joints_as_floats.y; + tmp_joint_index.z = (u32)joints_as_floats.z; + tmp_joint_index.w = (u32)joints_as_floats.w; + printf("Joints affecting vertex %d : %d %d %d %d\n", v, tmp_joint_index.x, tmp_joint_index.y, + tmp_joint_index.z, tmp_joint_index.w); + Vec4u_darray_push(joint_indices, tmp_joint_index); + } +} + bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 relative_path, Model *out_model, bool invert_textures_y) { TRACE("Load GLTF from string"); @@ -222,25 +240,9 @@ bool model_load_gltf_str(const char *file_string, const char *filepath, Str8 rel cgltf_accessor *accessor = attribute.data; load_texcoord_components(tmp_uvs, accessor); } else if (attribute.type == cgltf_attribute_type_joints) { - // FIXME: joints - // TRACE("Load joint indices from accessor"); - // cgltf_accessor *accessor = attribute.data; - // assert(accessor->component_type == cgltf_component_type_r_16u); - // assert(accessor->type == cgltf_type_vec4); - // vec4u joint_indices; - // vec4 joints_as_floats; - // for (cgltf_size v = 0; v < accessor->count; ++v) { - // cgltf_accessor_read_float(accessor, v, &joints_as_floats.x, 4); - // joint_indices.x = (u32)joints_as_floats.x; - // joint_indices.y = (u32)joints_as_floats.y; - // joint_indices.z = (u32)joints_as_floats.z; - // joint_indices.w = (u32)joints_as_floats.w; - // printf("Joints affecting vertex %d : %d %d %d %d\n", v, joint_indices.x, - // joint_indices.y, - // joint_indices.z, joint_indices.w); - // vec4u_darray_push(tmp_joint_indices, joint_indices); - // } - + TRACE("Load joint indices from accessor"); + cgltf_accessor *accessor = attribute.data; + load_joint_index_components(tmp_joint_indices, accessor); } else if (attribute.type == cgltf_attribute_type_weights) { // FIXME: weights // TRACE("Load joint weights from accessor"); diff --git a/src/systems/grid.c b/src/systems/grid.c index e095d22..70092e0 100644 --- a/src/systems/grid.c +++ b/src/systems/grid.c @@ -8,8 +8,8 @@ #include "ral_impl.h" #include "ral_types.h" #include "render.h" -#include "render_types.h" #include "render_scene.h" +#include "render_types.h" #include "shader_layouts.h" void Grid_Init(Grid_Storage* storage) { @@ -18,8 +18,9 @@ void Grid_Init(Grid_Storage* storage) { Mesh plane_mesh = Mesh_Create(&plane_geo, true); storage->plane_vertices = plane_mesh.vertex_buffer; - u32 indices[6] = { 5,4,3,2,1,0}; - storage->plane_indices = GPU_BufferCreate(6 * sizeof(u32),BUFFER_INDEX, BUFFER_FLAG_GPU, &indices); + u32 indices[6] = { 5, 4, 3, 2, 1, 0 }; + storage->plane_indices = + GPU_BufferCreate(6 * sizeof(u32), BUFFER_INDEX, BUFFER_FLAG_GPU, &indices); GPU_RenderpassDesc rpass_desc = { .default_framebuffer = true, @@ -63,8 +64,7 @@ void Grid_Draw() { Grid_Execute(grid); } -void Grid_Execute(Grid_Storage *storage) { - WARN("Draw Grid"); +void Grid_Execute(Grid_Storage* storage) { RenderScene* scene = Render_GetScene(); GPU_CmdEncoder* enc = GPU_GetDefaultEncoder(); GPU_CmdEncoder_BeginRender(enc, storage->renderpass); |