+ | Multiple sphere sent to GPU working

This commit is contained in:
TheRedShip
2024-12-24 01:44:53 +01:00
parent 7b56daf149
commit 216e9a684a
10 changed files with 166 additions and 62 deletions

View File

@ -16,7 +16,6 @@
# define WIDTH 1920 # define WIDTH 1920
# define HEIGHT 1080 # define HEIGHT 1080
# include "glm/glm.hpp" # include "glm/glm.hpp"
# include "glm/gtc/matrix_transform.hpp" # include "glm/gtc/matrix_transform.hpp"
# include "glm/gtc/type_ptr.hpp" # include "glm/gtc/type_ptr.hpp"
@ -24,14 +23,17 @@
# include "glad/gl.h" # include "glad/gl.h"
# include "GLFW/glfw3.h" # include "GLFW/glfw3.h"
# include <iostream> # include <iostream>
# include <memory>
struct Vertex { struct Vertex {
glm::vec2 position; glm::vec2 position;
glm::vec2 texCoord; glm::vec2 texCoord;
}; };
# include "Object.hpp"
# include "objects/Sphere.hpp"
# include "Camera.hpp" # include "Camera.hpp"
# include "Window.hpp" # include "Window.hpp"
# include "Shader.hpp" # include "Shader.hpp"

View File

@ -13,20 +13,33 @@
#ifndef RT_OBJECT__HPP #ifndef RT_OBJECT__HPP
# define RT_OBJECT__HPP # define RT_OBJECT__HPP
#include "RT.hpp" # include "RT.hpp"
typedef struct s_Material
{
glm::vec3 color;
float roughness;
float specular;
} Material;
class Object class Object
{ {
public: protected:
glm::vec3 position; glm::vec3 _position;
glm::vec3 color; Material _material;
Object(const glm::vec3& pos, const glm::vec3& col) : position(pos), color(col) {}
virtual ~Object() = default;
// virtual bool hit(const glm::vec3& rayOrigin, const glm::vec3& rayDir, float& t, glm::vec3& hitNormal) const = 0;
public:
Object(const glm::vec3& position, const Material& material) : _position(position), _material(material) {}
virtual ~Object() = default;
const glm::vec3& getPosition() const { return (_position); }
const Material& getMaterial() const { return (_material); }
enum class Type {
SPHERE,
};
virtual Type getType() const = 0;
}; };
#endif #endif

View File

@ -15,6 +15,19 @@
# include "RT.hpp" # include "RT.hpp"
struct GPUObject
{
glm::vec3 position;
int padding_1;
glm::vec3 color;
int padding_2;
float roughness;
float specular;
float radius;
int type;
};
class Sphere;
class Camera; class Camera;
class Scene class Scene
@ -23,11 +36,16 @@ class Scene
Scene(); Scene();
~Scene(); ~Scene();
Camera *getCamera(void) const; Camera *getCamera(void) const;
// Object *getObjects(void) const; void addObject(std::unique_ptr<Object> object);
void updateGPUData();
const std::vector<GPUObject>& getGPUData() const;
private: private:
// Object *_objects; std::vector<std::unique_ptr<Object>> _objects;
std::vector<GPUObject> _gpuObjects;
Camera *_camera; Camera *_camera;
}; };

View File

@ -21,7 +21,6 @@ class Shader
Shader(std::string vertexPath, std::string fragmentPath, std::string computePath); Shader(std::string vertexPath, std::string fragmentPath, std::string computePath);
~Shader(void); ~Shader(void);
// void compile(const char *vertexSource, const char *fragmentSource);
void attach(void); void attach(void);
void setupVertexBuffer(const Vertex* vertices, size_t size); void setupVertexBuffer(const Vertex* vertices, size_t size);
void drawTriangles(size_t size); void drawTriangles(size_t size);
@ -29,7 +28,7 @@ class Shader
// void setBool(const std::string &name, bool value) const; // void setBool(const std::string &name, bool value) const;
// void setInt(const std::string &name, int value) const; void set_int(const std::string &name, int value) const;
// void setFloat(const std::string &name, float value) const; // void setFloat(const std::string &name, float value) const;
void set_vec2(const std::string &name, const glm::vec2 &value) const; void set_vec2(const std::string &name, const glm::vec2 &value) const;
void set_vec3(const std::string &name, const glm::vec3 &value) const; void set_vec3(const std::string &name, const glm::vec3 &value) const;

View File

@ -18,13 +18,14 @@
class Sphere : public Object class Sphere : public Object
{ {
public: public:
float radius; Sphere(const glm::vec3& position, float radius, const Material& material)
glm::vec3 position; : Object(position, material), _radius(radius) {}
float getRadius() const { return (_radius); }
Type getType() const override { return Type::SPHERE; }
private: private:
float _radius;
}; };
#endif #endif

View File

@ -6,14 +6,30 @@ layout(local_size_x = 16, local_size_y = 16) in;
// Output image // Output image
layout(binding = 0, rgba32f) uniform image2D outputImage; layout(binding = 0, rgba32f) uniform image2D outputImage;
// Uniforms for camera and scene struct GPUObject {
uniform vec2 u_resolution; vec3 position; // 12 + 4
uniform vec3 u_cameraPosition; float padding_1; // 4
uniform mat4 u_viewMatrix; vec3 color; // 12 + 4
float padding_2; // 4
float roughness; // 4
float specular; // 4
float radius; // 4
int type; // 4
};
layout(std430, binding = 1) buffer ObjectBuffer
{
GPUObject objects[];
};
uniform int u_objectsNum;
uniform vec2 u_resolution;
uniform vec3 u_cameraPosition;
uniform mat4 u_viewMatrix;
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);
// Scene definition
vec3 sphereCenter = vec3(0.0, 0.0, -5.0); vec3 sphereCenter = vec3(0.0, 0.0, -5.0);
float sphereRadius = 1.0; float sphereRadius = 1.0;
vec3 objectColor = vec3(0.4, 0.7, 0.9); vec3 objectColor = vec3(0.4, 0.7, 0.9);
@ -23,7 +39,8 @@ struct Ray {
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);
@ -32,10 +49,13 @@ bool intersectSphere(Ray ray, vec3 center, float radius, out float t) {
if (discriminant < 0.0) { if (discriminant < 0.0) {
return false; return false;
} else { }
t = (-b - sqrt(discriminant)) / (2.0 * a); float t1 = (-b - sqrt(discriminant)) / (2.0 * a);
if (t1 > 0.001) {
t = t1;
return true; return true;
} }
return false;
} }
vec3 computeLighting(vec3 point, vec3 normal, vec3 viewDir) { vec3 computeLighting(vec3 point, vec3 normal, vec3 viewDir) {
@ -46,7 +66,6 @@ vec3 computeLighting(vec3 point, vec3 normal, vec3 viewDir) {
} }
void main() { void main() {
// Compute pixel coordinates
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;
@ -64,14 +83,24 @@ void main() {
rayDirection = normalize(rayDirection); rayDirection = normalize(rayDirection);
Ray ray = Ray(u_cameraPosition, rayDirection); Ray ray = Ray(u_cameraPosition, rayDirection);
float t;
vec4 color = vec4(0.0, 0.0, 0.0, 1.0); vec4 color = vec4(0.0, 0.0, 0.0, 1.0);
if (intersectSphere(ray, sphereCenter, sphereRadius, t)) { float closest_t = 1e30;
vec3 hitPoint = ray.origin + t * ray.direction; for (int i = 0; i < u_objectsNum; i++)
vec3 normal = normalize(hitPoint - sphereCenter); {
vec3 viewDir = normalize(-ray.direction); float t;
vec3 lighting = computeLighting(hitPoint, normal, viewDir); if (intersectSphere(ray, objects[i].position, objects[i].radius, t))
color = vec4(lighting, 1.0); {
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 // Write to the output image

View File

@ -12,21 +12,33 @@
#include "RT.hpp" #include "RT.hpp"
int main(void) int main(void)
{ {
Window window(WIDTH, HEIGHT, "RT_GPU", 0); Window window(WIDTH, HEIGHT, "RT_GPU", 1);
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl"); Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
Vertex vertices[3] = { Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0};
{{-1.0f, -1.0f}, {0.0f, 0.0f}}, float radius = 30.0f;
{{3.0f, -1.0f}, {2.0f, 0.0f}}, for (int i = 0; i < 150; i++) {
{{-1.0f, 3.0f}, {0.0f, 2.0f}} float angle = (2.0f * M_PI * i) / 150.0f;
};
float x = radius * cos(angle);
float z = radius * sin(angle);
float y = 2.0f * sin(angle * 3.0f);
glm::vec3 position(x, y, z);
float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f);
window.getScene()->addObject(std::make_unique<Sphere>(position, sphereSize, redMaterial));
}
GLuint objectSSBO;
glGenBuffers(1, &objectSSBO);
shader.attach(); shader.attach();
Vertex vertices[3] = {{{-1.0f, -1.0f}, {0.0f, 0.0f}},{{3.0f, -1.0f}, {2.0f, 0.0f}},{{-1.0f, 3.0f}, {0.0f, 2.0f}}};
size_t size = sizeof(vertices) / sizeof(Vertex) / 3; size_t size = sizeof(vertices) / sizeof(Vertex) / 3;
shader.setupVertexBuffer(vertices, size); shader.setupVertexBuffer(vertices, size);
@ -35,6 +47,14 @@ int main(void)
{ {
glUseProgram(shader.getProgramCompute()); glUseProgram(shader.getProgramCompute());
const std::vector<GPUObject> &gpu_data = window.getScene()->getGPUData();
// Update SSBO with latest object data
glBindBuffer(GL_SHADER_STORAGE_BUFFER, objectSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, gpu_data.size() * sizeof(GPUObject), gpu_data.data(), GL_DYNAMIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 1, objectSSBO);
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", window.getScene()->getCamera()->get_position()); shader.set_vec3("u_cameraPosition", window.getScene()->getCamera()->get_position());
shader.set_mat4("u_viewMatrix", window.getScene()->getCamera()->get_view_matrix()); shader.set_mat4("u_viewMatrix", window.getScene()->getCamera()->get_view_matrix());

View File

@ -1,12 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Object.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:44:00 by ycontre #+# #+# */
/* Updated: 2024/12/23 18:44:01 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -19,10 +19,43 @@ Scene::Scene()
Scene::~Scene() Scene::~Scene()
{ {
delete _camera; delete (_camera);
} }
Camera *Scene::getCamera(void) const Camera *Scene::getCamera(void) const
{ {
return (_camera); return (_camera);
}
void Scene::addObject(std::unique_ptr<Object> object)
{
_objects.push_back(std::move(object));
this->updateGPUData();
}
void Scene::updateGPUData()
{
_gpuObjects.clear();
for (const auto& obj : _objects)
{
GPUObject gpuObj;
gpuObj.position = obj->getPosition();
gpuObj.color = obj->getMaterial().color;
gpuObj.roughness = obj->getMaterial().roughness;
gpuObj.specular = obj->getMaterial().specular;
gpuObj.type = static_cast<int>(obj->getType());
if (obj->getType() == Object::Type::SPHERE) {
auto sphere = static_cast<const Sphere*>(obj.get());
gpuObj.radius = sphere->getRadius();
}
_gpuObjects.push_back(gpuObj);
}
}
const std::vector<GPUObject>& Scene::getGPUData() const
{
return (_gpuObjects);
} }

View File

@ -142,17 +142,18 @@ void Shader::drawTriangles(size_t size)
glDrawArrays(GL_TRIANGLES, 0, size * 3); glDrawArrays(GL_TRIANGLES, 0, size * 3);
} }
void Shader::set_int(const std::string &name, int value) const
{
glUniform1i(glGetUniformLocation(_program_compute, name.c_str()), value);
}
void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const void Shader::set_vec2(const std::string &name, const glm::vec2 &value) const
{ {
glUniform2fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value)); glUniform2fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
} }
void Shader::set_vec3(const std::string &name, const glm::vec3 &value) const void Shader::set_vec3(const std::string &name, const glm::vec3 &value) const
{ {
glUniform3fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value)); glUniform3fv(glGetUniformLocation(_program_compute, name.c_str()), 1, glm::value_ptr(value));
} }
void Shader::set_mat4(const std::string &name, const glm::mat4 &value) const void Shader::set_mat4(const std::string &name, const glm::mat4 &value) const
{ {
glUniformMatrix4fv(glGetUniformLocation(_program_compute, name.c_str()), 1, GL_FALSE, glm::value_ptr(value)); glUniformMatrix4fv(glGetUniformLocation(_program_compute, name.c_str()), 1, GL_FALSE, glm::value_ptr(value));