mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 10:48:34 +02:00
+ | Real material shared system + parsing
This commit is contained in:
@ -18,6 +18,7 @@
|
||||
typedef struct s_Material
|
||||
{
|
||||
glm::vec3 color;
|
||||
float emission;
|
||||
float roughness;
|
||||
float specular;
|
||||
} Material;
|
||||
@ -26,14 +27,14 @@ class Object
|
||||
{
|
||||
protected:
|
||||
glm::vec3 _position;
|
||||
const Material *_material;
|
||||
int _mat_index;
|
||||
|
||||
public:
|
||||
Object(const glm::vec3& position, const Material *material) : _position(position), _material(material) {}
|
||||
Object(const glm::vec3& position, const int mat_index) : _position(position), _mat_index(mat_index) {}
|
||||
virtual ~Object() = default;
|
||||
|
||||
int getMaterialIndex() const {return (_mat_index); }
|
||||
const glm::vec3 &getPosition() const { return (_position); }
|
||||
const Material *getMaterial() const { return (_material); }
|
||||
|
||||
enum class Type {
|
||||
SPHERE,
|
||||
|
@ -17,10 +17,9 @@
|
||||
|
||||
struct GPUObject
|
||||
{
|
||||
glm::vec3 position;
|
||||
int padding_1;
|
||||
glm::vec3 color;
|
||||
int padding_2;
|
||||
alignas(16) glm::vec3 position;
|
||||
alignas(16) glm::vec3 color;
|
||||
float emission;
|
||||
float roughness;
|
||||
float specular;
|
||||
float radius;
|
||||
@ -39,16 +38,21 @@ class Scene
|
||||
bool parseScene(char *name);
|
||||
|
||||
void addObject(Object *object);
|
||||
void addMaterial(Material *material);
|
||||
|
||||
void updateGPUData();
|
||||
|
||||
const std::vector<GPUObject> &getGPUData() const;
|
||||
Camera *getCamera(void) const;
|
||||
|
||||
Material *getMaterial(int material_index);
|
||||
|
||||
private:
|
||||
std::vector<Object *> _objects;
|
||||
std::vector<GPUObject> _gpu_objects;
|
||||
|
||||
std::vector<Material *> _materials;
|
||||
|
||||
Camera *_camera;
|
||||
};
|
||||
|
||||
|
@ -10,8 +10,8 @@
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef RT_SceneParser__HPP
|
||||
# define RT_SceneParser__HPP
|
||||
#ifndef RT_SCENEPARSER__HPP
|
||||
# define RT_SCENEPARSER__HPP
|
||||
|
||||
# include "RT.hpp"
|
||||
|
||||
@ -23,6 +23,9 @@ class SceneParser
|
||||
bool parseLine(const std::string &line);
|
||||
|
||||
private:
|
||||
void parseMaterial(std::stringstream &line);
|
||||
|
||||
|
||||
Scene *_scene;
|
||||
|
||||
std::map<std::string, std::function<Object *(std::stringstream &)>> object_parsers;
|
||||
|
@ -17,58 +17,36 @@
|
||||
|
||||
class Sphere : public Object
|
||||
{
|
||||
|
||||
public:
|
||||
struct ParseError : public std::runtime_error
|
||||
Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), -1)
|
||||
{
|
||||
ParseError(const std::string& msg) : std::runtime_error("Sphere parse error: " + msg) {}
|
||||
};
|
||||
|
||||
Sphere(std::stringstream &line) : Object(glm::vec3(0.0f), nullptr)
|
||||
{
|
||||
_mat = new Material;
|
||||
|
||||
try {
|
||||
float x, y, z, radius;
|
||||
float r, g, b, rough, spec;
|
||||
int mat_index;
|
||||
|
||||
if (!(line >> x >> y >> z >> radius))
|
||||
throw std::invalid_argument("Missing position or radius values");
|
||||
throw std::runtime_error("Missing position or radius values");
|
||||
|
||||
if (radius <= 0.0f)
|
||||
throw std::invalid_argument("Radius must be positive");
|
||||
throw std::runtime_error("Radius must be positive");
|
||||
|
||||
if (!(line >> r >> g >> b >> rough >> spec))
|
||||
throw std::invalid_argument("Missing material properties");
|
||||
|
||||
if (r < 0.0f || r > 255.0f || g < 0.0f || g > 255.0f || b < 0.0f || b > 255.0f)
|
||||
throw std::invalid_argument("Color values must be between 0 and 255");
|
||||
|
||||
if (rough < 0.0f || rough > 1.0f)
|
||||
throw std::invalid_argument("Roughness must be between 0 and 1");
|
||||
|
||||
if (spec < 0.0f || spec > 1.0f)
|
||||
throw std::invalid_argument("Specular must be between 0 and 1");
|
||||
if (!(line >> mat_index))
|
||||
throw std::runtime_error("Missing material properties");
|
||||
|
||||
_position = glm::vec3(x, y, z);
|
||||
_radius = radius;
|
||||
|
||||
_mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
|
||||
_mat->roughness = rough;
|
||||
_mat->specular = spec;
|
||||
|
||||
_material = _mat;
|
||||
_mat_index = mat_index;
|
||||
}
|
||||
catch (const std::exception& e) { throw; }
|
||||
}
|
||||
Sphere(const glm::vec3& position, float radius, const Material *material)
|
||||
: Object(position, material), _radius(radius) {}
|
||||
Sphere(const glm::vec3& position, float radius, const int mat_index)
|
||||
: Object(position, mat_index), _radius(radius) {}
|
||||
|
||||
float getRadius() const { return (_radius); }
|
||||
Type getType() const override { return Type::SPHERE; }
|
||||
|
||||
private:
|
||||
Material *_mat;
|
||||
float _radius;
|
||||
};
|
||||
|
||||
|
@ -1,4 +1,9 @@
|
||||
|
||||
sp 0 -1 -6 2.0 255 255 255 0.0 0.0
|
||||
MAT 255 255 255 0.0 1.0 2.0
|
||||
MAT 255 0 0 0.0 1.0 2.0
|
||||
|
||||
sp 0 -1 -6 1.0 0
|
||||
sp 2 -1 -6 1.0 1
|
||||
sp -2 -1 -6 1.0 1
|
||||
|
||||
R 1.0 -2.0 10
|
||||
|
@ -8,13 +8,12 @@ layout(binding = 0, rgba32f) uniform image2D outputImage;
|
||||
|
||||
struct GPUObject {
|
||||
vec3 position; // 12 + 4
|
||||
float padding_1; // 4
|
||||
vec3 color; // 12 + 4
|
||||
float padding_2; // 4
|
||||
float emission; // 4
|
||||
float roughness; // 4
|
||||
float specular; // 4
|
||||
float radius; // 4
|
||||
int type; // 4
|
||||
int type; // 4 + 12
|
||||
};
|
||||
|
||||
layout(std430, binding = 1) buffer ObjectBuffer
|
||||
|
@ -22,8 +22,8 @@ int main(int argc, char **argv)
|
||||
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 1);
|
||||
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
|
||||
|
||||
|
||||
// Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0};
|
||||
// Material redMaterial = {glm::vec3(1.0f, 0.2f, 0.2f), 1.0, 1.0, 0.0};
|
||||
// scene.addMaterial(&redMaterial);
|
||||
|
||||
// for (int i = 0; i < 150; i++)
|
||||
// {
|
||||
@ -33,7 +33,7 @@ int main(int argc, char **argv)
|
||||
// float y = 2.0f * sin(angle * 3.0f);
|
||||
// glm::vec3 position(x, y, z);
|
||||
// float sphereSize = 0.8f + 0.4f * sin(angle * 2.0f);
|
||||
// scene.addObject(new Sphere(position, sphereSize, &redMaterial));
|
||||
// scene.addObject(new Sphere(position, sphereSize, 0));
|
||||
// }
|
||||
|
||||
GLuint objectSSBO;
|
||||
|
@ -56,17 +56,26 @@ void Scene::addObject(Object *object)
|
||||
this->updateGPUData();
|
||||
}
|
||||
|
||||
void Scene::addMaterial(Material *material)
|
||||
{
|
||||
_materials.push_back(material);
|
||||
}
|
||||
|
||||
void Scene::updateGPUData()
|
||||
{
|
||||
GPUObject gpu_obj;
|
||||
Material *mat;
|
||||
|
||||
_gpu_objects.clear();
|
||||
for (const auto& obj : _objects)
|
||||
{
|
||||
GPUObject gpu_obj;
|
||||
mat = getMaterial(obj->getMaterialIndex());
|
||||
|
||||
gpu_obj.position = obj->getPosition();
|
||||
gpu_obj.color = obj->getMaterial()->color;
|
||||
gpu_obj.roughness = obj->getMaterial()->roughness;
|
||||
gpu_obj.specular = obj->getMaterial()->specular;
|
||||
gpu_obj.color = mat->color;
|
||||
gpu_obj.emission = mat->emission;
|
||||
gpu_obj.roughness = mat->roughness;
|
||||
gpu_obj.specular = mat->specular;
|
||||
gpu_obj.type = static_cast<int>(obj->getType());
|
||||
|
||||
if (obj->getType() == Object::Type::SPHERE)
|
||||
@ -75,14 +84,21 @@ void Scene::updateGPUData()
|
||||
gpu_obj.radius = sphere->getRadius();
|
||||
}
|
||||
|
||||
std::cout << gpu_obj.position.x << " " << gpu_obj.position.y << " " << gpu_obj.position.z << " " << gpu_obj.radius << " " << gpu_obj.roughness << " " << gpu_obj.specular << std::endl;
|
||||
|
||||
_gpu_objects.push_back(gpu_obj);
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<GPUObject>& Scene::getGPUData() const
|
||||
{
|
||||
for (const auto& obj : _gpu_objects)
|
||||
{
|
||||
std::cout << "objType: " << obj.type << std::endl;
|
||||
std::cout << "position: " << obj.position.x << " " << obj.position.y << " " << obj.position.z << std::endl;
|
||||
std::cout << "color: " << obj.color.x << " " << obj.color.y << " " << obj.color.z << std::endl;
|
||||
std::cout << "mat: " << obj.emission << " " << obj.roughness << " " << obj.specular << std::endl;
|
||||
std::cout << "Size of GPUObject: " << sizeof(GPUObject) << " bytes" << std::endl;
|
||||
}
|
||||
|
||||
return (_gpu_objects);
|
||||
}
|
||||
|
||||
@ -90,3 +106,10 @@ Camera *Scene::getCamera(void) const
|
||||
{
|
||||
return (_camera);
|
||||
}
|
||||
|
||||
Material *Scene::getMaterial(int material_index)
|
||||
{
|
||||
if (material_index < 0 || material_index >= (int)_materials.size())
|
||||
throw std::runtime_error("Incorrect material index");
|
||||
return (_materials[material_index]);
|
||||
}
|
@ -21,6 +21,27 @@ SceneParser::SceneParser(Scene *scene) : _scene(scene)
|
||||
};
|
||||
}
|
||||
|
||||
void SceneParser::parseMaterial(std::stringstream &line)
|
||||
{
|
||||
float r,g,b;
|
||||
float emission;
|
||||
float roughness;
|
||||
float specular;
|
||||
Material *mat;
|
||||
|
||||
if (!(line >> r >> g >> b >> emission >> roughness >> specular))
|
||||
throw std::runtime_error("Material: Missing material properties");
|
||||
|
||||
mat = new Material;
|
||||
|
||||
mat->color = glm::vec3(r / 255.0f, g / 255.0f, b / 255.0f);
|
||||
mat->emission = emission;
|
||||
mat->roughness = roughness;
|
||||
mat->specular = specular;
|
||||
|
||||
_scene->addMaterial(mat);
|
||||
}
|
||||
|
||||
bool SceneParser::parseLine(const std::string &line)
|
||||
{
|
||||
if (line.empty() || line[0] == '#')
|
||||
@ -31,16 +52,24 @@ bool SceneParser::parseLine(const std::string &line)
|
||||
|
||||
ss >> identifier;
|
||||
|
||||
try
|
||||
{
|
||||
auto it = object_parsers.find(identifier);
|
||||
if (it != object_parsers.end())
|
||||
{
|
||||
try {
|
||||
Object *obj = it->second(ss);
|
||||
(void) _scene->getMaterial(obj->getMaterialIndex()); //verify material
|
||||
|
||||
_scene->addObject(obj);
|
||||
} catch (const std::exception& e) {
|
||||
std::cerr << "Error parsing sphere: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (identifier == "MAT")
|
||||
this->parseMaterial(ss);
|
||||
}
|
||||
catch (const std::exception& e)
|
||||
{
|
||||
std::cerr << e.what() << std::endl;
|
||||
return (false);
|
||||
}
|
||||
|
||||
return (true);
|
||||
|
Reference in New Issue
Block a user