mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Trying noise but poorly for now
This commit is contained in:
@ -44,7 +44,7 @@ class Camera
|
|||||||
glm::vec3 _velocity;
|
glm::vec3 _velocity;
|
||||||
glm::vec3 _acceleration;
|
glm::vec3 _acceleration;
|
||||||
|
|
||||||
float _maxSpeed = 10.0f;
|
float _maxSpeed = 8.0f;
|
||||||
float _acceleration_rate = 40.0f;
|
float _acceleration_rate = 40.0f;
|
||||||
float _deceleration_rate = 40.0f;
|
float _deceleration_rate = 40.0f;
|
||||||
float _sensitivity = 0.2f;
|
float _sensitivity = 0.2f;
|
||||||
|
@ -23,9 +23,6 @@ class Window
|
|||||||
Window(Scene *scene, int width, int height, const char *title, int sleep);
|
Window(Scene *scene, int width, int height, const char *title, int sleep);
|
||||||
~Window(void);
|
~Window(void);
|
||||||
|
|
||||||
GLFWwindow *getWindow(void) const;
|
|
||||||
float getFps(void) const;
|
|
||||||
|
|
||||||
void display();
|
void display();
|
||||||
void pollEvents();
|
void pollEvents();
|
||||||
bool shouldClose();
|
bool shouldClose();
|
||||||
@ -35,11 +32,16 @@ class Window
|
|||||||
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
|
static void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
|
||||||
static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos);
|
static void mouseMoveCallback(GLFWwindow *window, double xpos, double ypos);
|
||||||
static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
|
static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
|
||||||
|
|
||||||
|
GLFWwindow *getWindow(void) const;
|
||||||
|
float getFps(void) const;
|
||||||
|
int getFrameCount(void) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
GLFWwindow *_window;
|
GLFWwindow *_window;
|
||||||
Scene *_scene;
|
Scene *_scene;
|
||||||
|
|
||||||
|
int _frameCount;
|
||||||
float _fps;
|
float _fps;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
|
|
||||||
MAT 255 255 255 0.0 1.0 2.0
|
MAT 255 255 255 0.0 1.0 2.0
|
||||||
MAT 255 0 0 0.0 1.0 2.0
|
MAT 255 255 255 1.0 1.0 2.0
|
||||||
|
|
||||||
sp 0 -1 -6 1.0 0
|
sp 0 -1 -6 1.0 0
|
||||||
sp 2 -1 -6 1.0 1
|
|
||||||
sp -2 -1 -6 1.0 1
|
sp 0 5 -6 3.0 1
|
||||||
|
|
||||||
|
|
||||||
R 1.0 -2.0 10
|
R 1.0 -2.0 10
|
||||||
|
@ -7,101 +7,112 @@ layout(local_size_x = 16, local_size_y = 16) in;
|
|||||||
layout(binding = 0, rgba32f) uniform image2D outputImage;
|
layout(binding = 0, rgba32f) uniform image2D outputImage;
|
||||||
|
|
||||||
struct GPUObject {
|
struct GPUObject {
|
||||||
vec3 position; // 12 + 4
|
vec3 position; // 12 + 4
|
||||||
vec3 color; // 12 + 4
|
vec3 color; // 12 + 4
|
||||||
float emission; // 4
|
float emission; // 4
|
||||||
float roughness; // 4
|
float roughness; // 4
|
||||||
float specular; // 4
|
float specular; // 4
|
||||||
float radius; // 4
|
float radius; // 4
|
||||||
int type; // 4 + 12
|
int type; // 4 + 12
|
||||||
};
|
};
|
||||||
|
|
||||||
layout(std430, binding = 1) buffer ObjectBuffer
|
layout(std430, binding = 1) buffer ObjectBuffer
|
||||||
{
|
{
|
||||||
GPUObject objects[];
|
GPUObject objects[];
|
||||||
};
|
};
|
||||||
|
|
||||||
uniform int u_objectsNum;
|
uniform int u_objectsNum;
|
||||||
uniform vec2 u_resolution;
|
uniform vec2 u_resolution;
|
||||||
uniform vec3 u_cameraPosition;
|
uniform vec3 u_cameraPosition;
|
||||||
uniform mat4 u_viewMatrix;
|
uniform mat4 u_viewMatrix;
|
||||||
|
uniform int u_frameCount;
|
||||||
|
|
||||||
vec3 lightPos = vec3(5.0, 5.0, 5.0);
|
vec3 lightPos = vec3(5.0, 5.0, 5.0);
|
||||||
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
vec3 lightColor = vec3(1.0, 1.0, 1.0);
|
||||||
|
|
||||||
vec3 sphereCenter = vec3(0.0, 0.0, -5.0);
|
|
||||||
float sphereRadius = 1.0;
|
const float PHI = 1.61803398874989484820459; // Φ = Golden Ratio
|
||||||
vec3 objectColor = vec3(0.4, 0.7, 0.9);
|
float getRandom(in vec2 xy, float seed)
|
||||||
|
{
|
||||||
|
return fract(tan(distance(xy*PHI, xy)*seed)*xy.x);
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 randomVec3(vec2 uv, int frame)
|
||||||
|
{
|
||||||
|
float x = getRandom(uv, float(frame) + 0.1);
|
||||||
|
float y = getRandom(uv + vec2(1.0), float(frame) + 0.2);
|
||||||
|
float z = getRandom(uv + vec2(2.0), float(frame) + 0.3);
|
||||||
|
return vec3(x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
struct Ray {
|
struct Ray {
|
||||||
vec3 origin;
|
vec3 origin;
|
||||||
vec3 direction;
|
vec3 direction;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool intersectSphere(Ray ray, vec3 center, float radius, out float t)
|
bool intersectSphere(Ray ray, vec3 center, float radius, out float t)
|
||||||
{
|
{
|
||||||
vec3 oc = ray.origin - center;
|
vec3 oc = ray.origin - center;
|
||||||
float a = dot(ray.direction, ray.direction);
|
float a = dot(ray.direction, ray.direction);
|
||||||
float b = 2.0 * dot(oc, ray.direction);
|
float b = 2.0 * dot(oc, ray.direction);
|
||||||
float c = dot(oc, oc) - radius * radius;
|
float c = dot(oc, oc) - radius * radius;
|
||||||
float discriminant = b * b - 4.0 * a * c;
|
float discriminant = b * b - 4.0 * a * c;
|
||||||
|
|
||||||
if (discriminant < 0.0) {
|
if (discriminant < 0.0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
float t1 = (-b - sqrt(discriminant)) / (2.0 * a);
|
float t1 = (-b - sqrt(discriminant)) / (2.0 * a);
|
||||||
if (t1 > 0.001) {
|
if (t1 > 0.001) {
|
||||||
t = t1;
|
t = t1;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
vec3 computeLighting(vec3 point, vec3 normal, vec3 viewDir) {
|
vec3 pathtrace(Ray ray)
|
||||||
vec3 lightDir = normalize(lightPos - point);
|
{
|
||||||
float diff = max(dot(normal, lightDir), 0.0);
|
vec3 color = vec3(0.0);
|
||||||
vec3 diffuse = diff * lightColor;
|
vec3 light = vec3(0.0);
|
||||||
return objectColor * diffuse;
|
|
||||||
|
float closest_t = 1e30;
|
||||||
|
for (int i = 0; i < u_objectsNum; i++)
|
||||||
|
{
|
||||||
|
float t;
|
||||||
|
if (intersectSphere(ray, objects[i].position, objects[i].radius, t))
|
||||||
|
{
|
||||||
|
if (t < closest_t)
|
||||||
|
{
|
||||||
|
closest_t = t;
|
||||||
|
|
||||||
|
vec3 hitPoint = ray.origin + t * ray.direction;
|
||||||
|
vec3 normal = normalize(hitPoint - objects[i].position);
|
||||||
|
|
||||||
|
color = objects[i].color * normal.y;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
ivec2 pixelCoords = ivec2(gl_GlobalInvocationID.xy);
|
ivec2 pixelCoords = ivec2(gl_GlobalInvocationID.xy);
|
||||||
if (pixelCoords.x >= int(u_resolution.x) || pixelCoords.y >= int(u_resolution.y)) {
|
if (pixelCoords.x >= int(u_resolution.x) || pixelCoords.y >= int(u_resolution.y))
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
vec2 uv = vec2(pixelCoords) / u_resolution;
|
vec2 uv = vec2(pixelCoords) / u_resolution;
|
||||||
uv = uv * 2.0 - 1.0;
|
uv = uv * 2.0 - 1.0;
|
||||||
uv.x *= u_resolution.x / u_resolution.y;
|
uv.x *= u_resolution.x / u_resolution.y;
|
||||||
|
|
||||||
float fov = 90.0;
|
float fov = 90.0;
|
||||||
float focal_length = 1.0 / tan(radians(fov) / 2.0);
|
float focal_length = 1.0 / tan(radians(fov) / 2.0);
|
||||||
vec3 viewSpaceRay = normalize(vec3(uv.x, uv.y, -focal_length));
|
vec3 viewSpaceRay = normalize(vec3(uv.x, uv.y, -focal_length));
|
||||||
|
|
||||||
vec3 rayDirection = (inverse(u_viewMatrix) * vec4(viewSpaceRay, 0.0)).xyz;
|
vec3 rayDirection = (inverse(u_viewMatrix) * vec4(viewSpaceRay, 0.0)).xyz;
|
||||||
rayDirection = normalize(rayDirection);
|
rayDirection = normalize(rayDirection);
|
||||||
Ray ray = Ray(u_cameraPosition, rayDirection);
|
Ray ray = Ray(u_cameraPosition, rayDirection);
|
||||||
|
|
||||||
|
vec3 color = pathtrace(ray);
|
||||||
|
|
||||||
vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
|
// Write to the output image
|
||||||
float closest_t = 1e30;
|
imageStore(outputImage, pixelCoords, vec4(randomVec3(uv, u_frameCount), 1.0));
|
||||||
for (int i = 0; i < u_objectsNum; i++)
|
|
||||||
{
|
|
||||||
float t;
|
|
||||||
if (intersectSphere(ray, objects[i].position, objects[i].radius, t))
|
|
||||||
{
|
|
||||||
if (t < closest_t)
|
|
||||||
{
|
|
||||||
closest_t = t;
|
|
||||||
|
|
||||||
vec3 hitPoint = ray.origin + t * ray.direction;
|
|
||||||
vec3 normal = normalize(hitPoint - objects[i].position);
|
|
||||||
|
|
||||||
color = vec4(objects[i].color * normal.y, 1.0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Write to the output image
|
|
||||||
imageStore(outputImage, pixelCoords, color);
|
|
||||||
}
|
}
|
@ -56,6 +56,7 @@ int main(int argc, char **argv)
|
|||||||
glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_data.size() * sizeof(GPUObject), gpu_data.data(), GL_DYNAMIC_DRAW);
|
glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_data.size() * sizeof(GPUObject), gpu_data.data(), GL_DYNAMIC_DRAW);
|
||||||
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, objectSSBO);
|
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, objectSSBO);
|
||||||
|
|
||||||
|
shader.set_int("u_frameCount", window.getFrameCount());
|
||||||
shader.set_int("u_objectsNum", gpu_data.size());
|
shader.set_int("u_objectsNum", gpu_data.size());
|
||||||
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
shader.set_vec2("u_resolution", glm::vec2(WIDTH, HEIGHT));
|
||||||
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position());
|
shader.set_vec3("u_cameraPosition", scene.getCamera()->get_position());
|
||||||
|
@ -118,6 +118,8 @@ void Window::display()
|
|||||||
lastTime = currentTime;
|
lastTime = currentTime;
|
||||||
_fps = 1.0f / delta;
|
_fps = 1.0f / delta;
|
||||||
|
|
||||||
|
_frameCount++;
|
||||||
|
|
||||||
glfwSwapBuffers(_window);
|
glfwSwapBuffers(_window);
|
||||||
}
|
}
|
||||||
void Window::pollEvents()
|
void Window::pollEvents()
|
||||||
@ -140,4 +142,9 @@ GLFWwindow *Window::getWindow(void) const
|
|||||||
float Window::getFps(void) const
|
float Window::getFps(void) const
|
||||||
{
|
{
|
||||||
return (_fps);
|
return (_fps);
|
||||||
|
}
|
||||||
|
|
||||||
|
int Window::getFrameCount(void) const
|
||||||
|
{
|
||||||
|
return (_frameCount);
|
||||||
}
|
}
|
Reference in New Issue
Block a user