3D scene stable

This commit is contained in:
IlyaShurupov 2024-06-18 17:57:42 +03:00
parent 5d96d6905f
commit 119e610db6
14 changed files with 96 additions and 45 deletions

View file

@ -13,7 +13,7 @@ public:
auto canvas = this->mGraphics->getCanvas(); auto canvas = this->mGraphics->getCanvas();
mGui = new EditorWidget<EventHandler, Canvas>(canvas, &mScene, renderResolution); mGui = new EditorWidget<EventHandler, Canvas>(canvas, &mScene, renderResolution);
mScene.load("rsc/scene.obj"); mScene.load("rsc/scene/script.lua");
mScene.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 }); mScene.mCamera.lookAtPoint({ 0, 0, 0 }, { 3, 3, 2 }, { 0, 0, 1 });
} }

View file

@ -0,0 +1,20 @@
# Blender MTL File: 'scene.blend'
# Material Count: 2
newmtl Material
Ns 323.999994
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
newmtl None
Ns 500
Ka 0.8 0.8 0.8
Kd 0.8 0.8 0.8
Ks 0.8 0.8 0.8
d 1
illum 2

View file

@ -0,0 +1,25 @@
Meshes = "meshes.obj"
Camera = {
pos = { 0.5, 4.5, 0.2 },
size_x = 600,
size_y = 800,
}
Lights = {
{
pos = { -0.5, 3.5, 1 },
intensity = 1
},
{
pos = { 0, 0, 1 },
intensity = 0.5
},
}
RenderSettings = {
depth = 1,
spray = 3,
multisampling = 16,
}

View file

@ -10,4 +10,4 @@ target_include_directories(${PROJECT_NAME} PUBLIC ./public/)
target_include_directories(${PROJECT_NAME} PRIVATE ../Externals/) target_include_directories(${PROJECT_NAME} PRIVATE ../Externals/)
target_link_libraries(${PROJECT_NAME} PUBLIC Math) target_link_libraries(${PROJECT_NAME} PUBLIC Math)
target_link_libraries(${PROJECT_NAME} PUBLIC ${BINDINGS_LIBS}) target_link_libraries(${PROJECT_NAME} PRIVATE Lua)

View file

@ -1,5 +1,5 @@
#include "Rayt.hpp" #include "Scene.hpp"
extern "C" { extern "C" {
#include "lauxlib.h" #include "lauxlib.h"
@ -9,7 +9,7 @@ extern "C" {
#include <filesystem> #include <filesystem>
// Function to read a Lua table representing RenderSettings // Function to read a Lua table representing RenderSettings
int readRenderSettings(lua_State* L, tp::RayTracer::RenderSettings& settings) { int readRenderSettings(lua_State* L, tp::RenderSettings& settings) {
lua_getglobal(L, "RenderSettings"); lua_getglobal(L, "RenderSettings");
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
printf("RenderSettings is not a table.\n"); printf("RenderSettings is not a table.\n");
@ -83,7 +83,7 @@ int readLight(lua_State* L, tp::PointLight* light) {
return 1; // Success return 1; // Success
} }
void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::RenderSettings& settings) { bool tp::Scene::loadLuaFormat(const std::string& scenePath) {
lua_State* L = luaL_newstate(); lua_State* L = luaL_newstate();
luaL_openlibs(L); luaL_openlibs(L);
@ -100,7 +100,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (luaL_dofile(L, scenePath.c_str()) != 0) { if (luaL_dofile(L, scenePath.c_str()) != 0) {
lua_close(L); lua_close(L);
printf("Cant open scene script.\n"); printf("Cant open scene script.\n");
return; return false;
} }
lua_getglobal(L, "Meshes"); lua_getglobal(L, "Meshes");
@ -110,14 +110,14 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
directoryPath /= meshesPath; directoryPath /= meshesPath;
if (!scene.load(directoryPath.string())) { if (!loadOBJFormat(directoryPath.string())) {
printf("No 'meshes' loaded - check ur .obj path and validate content of .obj .\n"); printf("No 'meshes' loaded - check ur .obj path and validate content of .obj .\n");
return; return false;
} }
} else { } else {
printf("No 'meshes' path given.\n"); printf("No 'meshes' path given.\n");
return; return false;
} }
// --- camera // --- camera
@ -127,7 +127,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
printf("Camera is not a table.\n"); printf("Camera is not a table.\n");
lua_close(L); lua_close(L);
return; return false;
} }
// Verify you are inside the "Camera" table // Verify you are inside the "Camera" table
@ -138,7 +138,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!lua_istable(L, -1) || lua_rawlen(L, -1) != 3) { if (!lua_istable(L, -1) || lua_rawlen(L, -1) != 3) {
printf("Invalid 'pos' field in Camera table.\n"); printf("Invalid 'pos' field in Camera table.\n");
lua_close(L); lua_close(L);
return; return false;
} }
// Read the values from the table // Read the values from the table
@ -150,7 +150,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
} else { } else {
printf("Invalid 'pos' field value at index %d.\n", i + 1); printf("Invalid 'pos' field value at index %d.\n", i + 1);
lua_close(L); lua_close(L);
return; return false;
} }
lua_pop(L, 1); lua_pop(L, 1);
} }
@ -160,7 +160,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!lua_isnumber(L, -1)) { if (!lua_isnumber(L, -1)) {
printf("Invalid or missing 'size_x' field in Camera table.\n"); printf("Invalid or missing 'size_x' field in Camera table.\n");
lua_close(L); lua_close(L);
return; return false;
} }
int size_x = lua_tointeger(L, -1); int size_x = lua_tointeger(L, -1);
lua_pop(L, 1); // Pop the 'size_x' value from the stack lua_pop(L, 1); // Pop the 'size_x' value from the stack
@ -170,16 +170,16 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!lua_isnumber(L, -1)) { if (!lua_isnumber(L, -1)) {
printf("Invalid or missing 'size_y' field in Camera table.\n"); printf("Invalid or missing 'size_y' field in Camera table.\n");
lua_close(L); lua_close(L);
return; return false;
} }
int size_y = lua_tointeger(L, -1); int size_y = lua_tointeger(L, -1);
settings.size = { (tp::halnf) size_x, (tp::halnf) size_y }; mRenderSettings.size = { (tp::halnf) size_x, (tp::halnf) size_y };
scene.mCamera.lookAtPoint({ 0, 0, 0 }, { pos[0], pos[1], pos[2] }, { 0, 0, 1 }); mCamera.lookAtPoint({ 0, 0, 0 }, { pos[0], pos[1], pos[2] }, { 0, 0, 1 });
scene.mCamera.setFOV(3.14 / 4); mCamera.setFOV(3.14 / 4);
scene.mCamera.setFar(100); mCamera.setFar(100);
scene.mCamera.setRatio((tp::halnf) size_y / (tp::halnf) size_x); mCamera.setRatio((tp::halnf) size_y / (tp::halnf) size_x);
// ---------- LIGHTS // ---------- LIGHTS
{ {
@ -187,7 +187,7 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!lua_istable(L, -1)) { if (!lua_istable(L, -1)) {
printf("Lights is not a table.\n"); printf("Lights is not a table.\n");
lua_close(L); lua_close(L);
return; // Error return false; // Error
} }
// Read and process each light in the "Lights" table // Read and process each light in the "Lights" table
@ -199,20 +199,22 @@ void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::Re
if (!readLight(L, &light)) { if (!readLight(L, &light)) {
printf("Cant read lights data\n"); printf("Cant read lights data\n");
lua_close(L); lua_close(L);
return; // Error return false; // Error
} }
scene.mLights.append(light); mLights.append(light);
} }
lua_pop(L, 1); // Pop the i-th light table lua_pop(L, 1); // Pop the i-th light table
} }
} }
// ----------- settings -------------- // ----------- settings --------------
if (!readRenderSettings(L, settings)) { if (!readRenderSettings(L, mRenderSettings)) {
printf("Cant Read Render Settings"); printf("Cant Read Render Settings");
lua_close(L); lua_close(L);
return; // Error return false; // Error
} }
lua_close(L); lua_close(L);
return true;
} }

View file

@ -10,7 +10,7 @@ extern "C" {
#include <filesystem> #include <filesystem>
bool tp::Scene::load(const std::string& objetsPath) { bool tp::Scene::loadOBJFormat(const std::string& objetsPath) {
using namespace tp; using namespace tp;
objl::Loader Loader; objl::Loader Loader;

View file

@ -1,2 +1,6 @@
#include "Scene.hpp" #include "Scene.hpp"
bool tp::Scene::load(const std::string& scenePath) {
return loadLuaFormat(scenePath);
}

View file

@ -7,6 +7,13 @@
#include <string> #include <string>
namespace tp { namespace tp {
struct RenderSettings {
uhalni depth = 2;
uhalni spray = 1;
ualni multisampling = 1;
Vec2<ualni> size;
};
class GPUBuffers { class GPUBuffers {
public: public:
GPUBuffers() = default; GPUBuffers() = default;
@ -41,9 +48,14 @@ namespace tp {
bool load(const std::string& scenePath); bool load(const std::string& scenePath);
bool loadLuaFormat(const std::string& scenePath);
bool loadOBJFormat(const std::string& objectsPath);
public: public:
Buffer<Object> mObjects; Buffer<Object> mObjects;
Buffer<PointLight> mLights; Buffer<PointLight> mLights;
Camera mCamera; Camera mCamera;
RenderSettings mRenderSettings;
}; };
} }

View file

@ -10,7 +10,7 @@ target_link_libraries(${PROJECT_NAME} PUBLIC 3DScene Connection)
### -------------------------- Applications -------------------------- ### ### -------------------------- Applications -------------------------- ###
file(GLOB APP_SOURCES "./applications/*.cpp") file(GLOB APP_SOURCES "./applications/*.cpp")
add_executable(rayt ${APP_SOURCES}) add_executable(rayt ${APP_SOURCES})
target_link_libraries(rayt ${PROJECT_NAME} Lua ImageIO) target_link_libraries(rayt ${PROJECT_NAME} ImageIO)
file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/") file(COPY "rsc" DESTINATION "${CMAKE_BINARY_DIR}/${PROJECT_NAME}/")
### -------------------------- Tests -------------------------- ### ### -------------------------- Tests -------------------------- ###

View file

@ -1,7 +1,7 @@
// #include "NewPlacement.hpp" // #include "NewPlacement.hpp"
#include "Rayt.hpp" #include "RayTracer.hpp"
#include "Timing.hpp" #include "Timing.hpp"
@ -53,9 +53,11 @@ void printStatus(const halnf* percentage) {
void renderCommand(const std::string& scenePath) { void renderCommand(const std::string& scenePath) {
Scene scene; Scene scene;
RayTracer::RenderSettings settings; RenderSettings& settings = scene.mRenderSettings;
loadScene(scene, scenePath, settings); if (!scene.load(scenePath)) {
return;
}
RayTracer::OutputBuffers output; RayTracer::OutputBuffers output;
RayTracer rayt; RayTracer rayt;

View file

@ -1,7 +0,0 @@
#pragma once
#include "RayTracer.hpp"
#include <string>
void loadScene(tp::Scene& scene, const std::string& scenePath, tp::RayTracer::RenderSettings& settings);

View file

@ -16,13 +16,6 @@ namespace tp {
halnf percentage = 0.f; halnf percentage = 0.f;
} mProgress; } mProgress;
struct RenderSettings {
uhalni depth = 2;
uhalni spray = 1;
ualni multisampling = 1;
Vec2<ualni> size;
};
struct OutputBuffers { struct OutputBuffers {
RenderBuffer normals; RenderBuffer normals;
RenderBuffer color; RenderBuffer color;

View file

@ -80,7 +80,7 @@ SUITE(RayTracer) {
object.mCache.Source = &object.mTopology; object.mCache.Source = &object.mTopology;
object.mCache.updateCache(); object.mCache.updateCache();
RayTracer::RenderSettings settings = { RenderSettings settings = {
0, 0,
0, 0,
1, 1,