~ | Emissive texture working

This commit is contained in:
TheRedShip
2025-02-02 17:10:06 +01:00
parent f03d48e62d
commit 3a70a08ea6
5 changed files with 26 additions and 27 deletions

View File

@ -202,8 +202,8 @@ vec3 pathtrace(Ray ray, inout uint rng_state)
GPUMaterial mat = materials[hit.mat_index]; GPUMaterial mat = materials[hit.mat_index];
calculateLightColor(mat, hit, color, light, rng_state); calculateLightColor(mat, hit, color, light, rng_state);
// if (mat.emission > 0.0 && mat.emission_texture_index == -1) if (mat.emission > 0.0 && mat.emission_texture_index == -1)
// break; break;
ray = newRay(hit, ray, rng_state); ray = newRay(hit, ray, rng_state);
ray.inv_direction = 1.0 / ray.direction; ray.inv_direction = 1.0 / ray.direction;

View File

@ -1,27 +1,28 @@
bool intersectSphere(Ray ray, GPUObject obj, out hitInfo hit) bool intersectSphere(Ray ray, GPUObject obj, out hitInfo hit)
{ {
vec3 oc = ray.origin - obj.position; vec3 oc = ray.origin - obj.position;
float b = dot(oc, ray.direction); float b = dot(oc, ray.direction);
float c = dot(oc, oc) - obj.radius * obj.radius; float c = dot(oc, oc) - obj.radius * obj.radius;
float h = b * b - c; float discriminant = b * b - c;
float t = -b - sqrt(h);
float last_t = -b + sqrt(h);
if (t > last_t) float sqrtD = sqrt(max(0.0, discriminant));
{ float t0 = -b - sqrtD;
float temp = t; float t1 = -b + sqrtD;
t = last_t;
last_t = temp;
}
hit.t = t; float temp = min(t0, t1);
hit.last_t = last_t; t1 = max(t0, t1);
hit.position = ray.origin + ray.direction * t; t0 = temp;
bool isInside = c < 0.0;
t0 = isInside ? t1 : t0;
hit.t = t0;
hit.last_t = t1;
hit.position = ray.origin + ray.direction * t0;
hit.normal = normalize(hit.position - obj.position); hit.normal = normalize(hit.position - obj.position);
hit.normal *= (isInside ? -1.0 : 1.0);
return (h >= 0.0 && t > 0.0);
return (discriminant >= 0.0) && (t0 > 0.0);
} }
bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit) bool intersectPlane(Ray ray, GPUObject obj, out hitInfo hit)

View File

@ -90,11 +90,9 @@ vec3 sampleLights(vec3 position, inout uint rng_state)
vec2 getSphereUV(vec3 surfacePoint) vec2 getSphereUV(vec3 surfacePoint)
{ {
// Convert 3D point to spherical coordinates
float phi = atan(surfacePoint.z, surfacePoint.x); float phi = atan(surfacePoint.z, surfacePoint.x);
float theta = acos(surfacePoint.y); float theta = acos(surfacePoint.y);
// Map to [0, 1] UV space
float u = (phi + M_PI) / (2.0 * M_PI); float u = (phi + M_PI) / (2.0 * M_PI);
float v = theta / M_PI; float v = theta / M_PI;
@ -113,11 +111,9 @@ vec2 getTextureColor(hitInfo hit)
else if (hit.obj_type == 3) else if (hit.obj_type == 3)
{ {
GPUTriangle tri = triangles[hit.obj_index]; GPUTriangle tri = triangles[hit.obj_index];
uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1; uv = hit.u * tri.texture_vertex2 + hit.v * tri.texture_vertex3 + (1 - (hit.u + hit.v)) * tri.texture_vertex1;
uv = vec2(uv.x, 1 - uv.y);
} }
return (uv); return (vec2(uv.x, 1 - uv.y));
} }
void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state) void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inout vec3 light, inout uint rng_state)
@ -136,8 +132,8 @@ void calculateLightColor(GPUMaterial mat, hitInfo hit, inout vec3 color, inou
} }
else else
{ {
light += mat.emission * mat.color;
color *= mat.color; color *= mat.color;
light += mat.emission * mat.color;
} }
// light += sampleLights(hit.position, rng_state); // light += sampleLights(hit.position, rng_state);
} }

BIN
skymap.hdr Normal file

Binary file not shown.

View File

@ -333,11 +333,13 @@ void ObjParser::parseMtl(std::stringstream &input_line, Scene &scene)
throw std::runtime_error("OBJ: syntax error while getting material texture"); throw std::runtime_error("OBJ: syntax error while getting material texture");
mat->emission_texture_index = scene.getEmissionTextures().size(); mat->emission_texture_index = scene.getEmissionTextures().size();
std::cout << "path " << mat->emission_texture_index << " : " << getFilePath(_filename) + path << std::endl;
scene.addEmissionTexture(getFilePath(_filename) + path); scene.addEmissionTexture(getFilePath(_filename) + path);
if (mat->emission == 0)
mat->emission = 1;
} }
else // else
std::cerr << "unsupported material setting : " << identifier << std::endl; // std::cerr << "unsupported material setting : " << identifier << std::endl;
} }
if(mat) if(mat)
{ {