+ | texture

This commit is contained in:
RedShip
2025-01-28 19:19:46 +01:00
parent ff9ab9251f
commit 8466e672f6
10 changed files with 43 additions and 17 deletions

View File

@ -153,6 +153,9 @@ struct hitInfo
vec3 position;
int obj_index;
int mat_index;
float u;
float v;
};
#include "shaders/random.glsl"

View File

@ -87,10 +87,12 @@ bool intersectTriangle(Ray ray, GPUTriangle obj, out hitInfo hit)
v >= 0.0 && (u + v) <= 1.0 &&
t > 0.0;
hit.u = u;
hit.v = v;
hit.t = t;
hit.position = ray.origin + ray.direction * t;
hit.normal = vec3(u, v, 1 - (u + v)); //texture mapping
// hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
// hit.normal = vec3(u, v, 1 - (u + v)); //texture mapping
hit.normal = obj.normal * sign(-dot(ray.direction, obj.normal));
return (valid);
}

View File

@ -105,12 +105,15 @@ uniform sampler2D textures[16];
vec3 getTextureColor(int texture_index, hitInfo hit)
{
vec2 uv = getSphereUV(hit.normal);
GPUTriangle tri = triangles[hit.obj_index];
vec2 uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1;
return vec3(uv, 0.);
return (texture(textures[texture_index], uv).rgb);
}
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
{
color *= mat.texture_index;
if (mat.texture_index != -1)
color *= getTextureColor(mat.texture_index, hit);

View File

@ -80,6 +80,8 @@ hitInfo traceBVH(Ray ray, GPUBvhData bvh_data)
hitInfo temp_hit;
if (intersectTriangle(ray, obj, temp_hit) && temp_hit.t < hit.t)
{
hit.u = temp_hit.u;
hit.v = temp_hit.v;
hit.t = temp_hit.t;
hit.last_t = temp_hit.last_t;
hit.obj_index = bvh_data.triangle_start_index + node.index + i;