Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*____ ___ ___ __ _________ _____ ___ _____ _____
- || \\ / .\ \ \ // |___*___| || \\ / .\ / ---_\ |*****|
- ||__// / ___.\ \ // |*| ||__// / ___.\ | | |**-/
- ||--\\ / -----.\ / // |*| ||--\\ / -----.\ | |__/' |**-\
- || \\ / .\ /_// |*| || \\ / .\ \_____/ |*****|
- */
- // RAYTRACE CYCLE:
- RTCRay ray;
- //constants:
- float max_rad = 0;
- auto worldLight = 0.5f;
- auto white = glm::vec3(255.0f, 255.0f, 255.0f);
- auto red = glm::vec3(255.0f, 2.0f, 2.0f);
- auto alpha = 0.05f;
- // main cycle:
- for (int y = 0; y < H; ++y) {
- for (int x = 0; x < W; ++x) {
- float radiance = 0;
- glm::vec3 voxel_color(0,0,0);
- glm::vec3 color(0, 0, 0);
- float j = x - W/2.0 + 0.5f;
- float i = y - H/2.0 + 0.5f;
- auto direction = camera_dir + j * dx + i * dy;
- memcpy(ray.org, &camera_pos, sizeof(camera_pos)); //ray.org = camera_pos;
- memcpy(ray.dir, &direction, sizeof(direction)); //ray.dir = camera_dir;
- ray.tnear = 0.00001f;
- ray.tfar = 1000.0f;
- ray.geomID = RTC_INVALID_GEOMETRY_ID;
- rtcIntersect(main_scene, ray);
- // RayTrace itself:
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
- glm::vec3 norm(ray.Ng[0], ray.Ng[1], ray.Ng[2]);
- norm = glm::normalize(norm);
- norm = -norm;
- auto intensity = -glm::dot(norm, light);
- intensity = glm::clamp(intensity, 0.0f, 1.0f);
- color = white * (worldLight + intensity) / (worldLight + 1.0f);
- if (ray.geomID == 2 || ray.geomID == 0) {
- //color += glm::vec3(white.x/40, white.y/4, white.z/40);
- ray = continue_ray(ray);
- rtcIntersect(main_scene, ray);
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
- glm::vec3 norm(ray.Ng[0], ray.Ng[1], ray.Ng[2]);
- norm = glm::normalize(norm);
- norm = -norm;
- intensity = -glm::dot(norm, light);
- intensity = glm::clamp(intensity, 0.0f, 1.0f);
- color = white * (worldLight + intensity) / (worldLight + 1.0f);
- }
- else {
- color.x = 0;
- color.y = 0;
- color.z = 0;
- }
- }
- // Voxel part:
- glm::vec3 org(ray.org[0], ray.org[1], ray.org[2]);
- glm::vec3 dir(ray.dir[0], ray.dir[1], ray.dir[2]);
- if (ray.geomID != RTC_INVALID_GEOMETRY_ID) {
- auto hit = org + ray.tfar * dir;
- auto segment = hit - toVec3(ray.org);
- auto step = glm::normalize(segment);
- for (float len = glm::length(segment); len > 0; len -= voxel_size) {
- auto point = toVec3(ray.org) + step * len;
- int x = (point.x - bounds.lower_x) / voxel_size;
- int y = (point.y - bounds.lower_y) / voxel_size;
- int z = (point.z - bounds.lower_z) / voxel_size;
- if ((z*Qy+y)*Qx+x < size && (z*Qy+y)*Qx+x >= 0) {
- radiance = (voxels[(z*Qy+y)*Qx+x]);
- voxel_color = converter(radiance);
- color = alpha * voxel_color + (1.0f - alpha) * color;
- }
- }
- }
- // Voxel part ends.
- // Color output:
- color = glm::clamp(color, glm::vec3(0,0,0), glm::vec3(255,255,255));
- out << (int)color.x << " " << (int)color.y << " " << (int)color.z << "\n";
- }
- else { out << 0 << " " << 0 << " " << 0 << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment