diff options
| author | vimene <vincent.menegaux@gmail.com> | 2026-01-13 02:04:52 +0100 |
|---|---|---|
| committer | vimene <vincent.menegaux@gmail.com> | 2026-01-13 02:04:52 +0100 |
| commit | db41d43345ea73cf7c1bbb29448e52ffb822e3e0 (patch) | |
| tree | 4635d654e301b3f31f8d2626f3bc2c6f2a6e50a8 /src/shaders | |
| parent | 7f08187a46e30925e4563585fab2c6f92400330a (diff) | |
| download | engine-db41d43345ea73cf7c1bbb29448e52ffb822e3e0.tar.gz | |
added textures for the hardware renderer
- removed "using" directive in .hpp
- reverse order of arguments for quaternion rotation, i.e. q.rot(v)
instead of v.rot(q), where q is a quaterinon and v a vector
- pass the inverse of the view matrix to render_and_present_frame, to
allow light calculation in shaders (we used to just pass the matrix of
the quaternion of the transformation, i.e. discard scaling and
translations)
- added another mesh and texture (viking_room) for testing purposes
- added transparent background option
- added Quaternion::look_towards(), which is the equivalent of
Matrix4::look_at() but only for rotations
- various improvement to .obj parsing
- load texture coordinates from .obj file
- merged duplicate vertices in Mesh::linearize_indices()
Diffstat (limited to 'src/shaders')
| -rw-r--r-- | src/shaders/shader.slang | 34 |
1 files changed, 18 insertions, 16 deletions
diff --git a/src/shaders/shader.slang b/src/shaders/shader.slang index e92f5aa..245df4b 100644 --- a/src/shaders/shader.slang +++ b/src/shaders/shader.slang @@ -1,12 +1,11 @@ struct VertexInput { float3 pos; float3 normal; + float2 uv; }; struct UniformBuffer { - float4x4 model, view, proj; - float4x4 camera_rot; - float3 camera_loc; + float4x4 model, view, proj, inv_view; }; ConstantBuffer<UniformBuffer> ubo; @@ -15,6 +14,7 @@ struct VertexOutput { float4 pos : SV_Position; float3 world_loc; float3 normal; + float2 uv; }; [shader("vertex")] @@ -23,7 +23,8 @@ VertexOutput vert_main(VertexInput vi) { float4 world_loc = mul(ubo.model, float4(vi.pos, 1.)); vo.pos = mul(ubo.proj, mul(ubo.view, world_loc)); vo.world_loc = world_loc.xyz; - vo.normal = vi.normal; + vo.normal = mul(ubo.model, float4(vi.normal, 0.)).xyz; + vo.uv = vi.uv; return vo; } @@ -31,16 +32,17 @@ Sampler2D texture; [shader("fragment")] float4 frag_main(VertexOutput vo) : SV_Target { - float3 u = vo.world_loc - ubo.camera_loc; - float u_len_sq = dot(u, u); - u = normalize(u); - float attenuation = dot(mul(ubo.camera_rot, float4(0., 0., -1., 0.)).xyz, u); - if (attenuation < .7) attenuation = 0.f; - else if (attenuation > .8) attenuation = 1.; - else attenuation = (attenuation - .7) / .1; - float light = -dot(vo.normal, u) / u_len_sq; - if (light < 0.) light = 0.; - float final_light = .003 + light * attenuation * .8 * .997; - if (final_light > 1.) final_light = 1.; - return float4(final_light, final_light, final_light, 1.); + // float3 u = vo.world_loc - (ubo.inv_view * float4(0., 0., 0., 1.)).xyz; + // float u_len_sq = dot(u, u); + // u = normalize(u); + // float attenuation = dot(mul(ubo.inv_view, float4(0., 0., -1., 0.)).xyz, u); + // if (attenuation < .7) attenuation = 0.f; + // else if (attenuation > .8) attenuation = 1.; + // else attenuation = (attenuation - .7) / .1; + // float light = -dot(vo.normal, u) / u_len_sq; + // if (light < 0.) light = 0.; + // float final_light = .003 + light * attenuation * .8 * .997; + // if (final_light > 1.) final_light = 1.; + // return float4(final_light, final_light, final_light, 1.); + return float4(texture.Sample(vo.uv).rgb, 1.f); } |
