Skip to content

Commit

Permalink
BlockModelStorage update
Browse files Browse the repository at this point in the history
  • Loading branch information
adaf committed Dec 5, 2024
1 parent abbf179 commit 6f9e9bf
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 67 deletions.
4 changes: 2 additions & 2 deletions Geome3Dash/Geome3Dash.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="src\BlockModelsStorage.cpp" />
<ClCompile Include="src\BlockModelStorage.cpp" />
<ClCompile Include="src\CocosShaderProgram.cpp" />
<ClCompile Include="src\game\component\G3DProgressBar.cpp" />
<ClCompile Include="src\game\editor\G3DCurveEditorLayer.cpp" />
Expand Down Expand Up @@ -192,7 +192,7 @@
<ClInclude Include="lib\tinyfiledialogs\tinyfiledialogs.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="src\BezierManager.h" />
<ClInclude Include="src\BlockModelsStorage.h" />
<ClInclude Include="src\BlockModelStorage.h" />
<ClInclude Include="src\CameraAction.h" />
<ClInclude Include="src\CocosShaderProgram.h" />
<ClInclude Include="src\delegate\CustomKeyboard.h" />
Expand Down
4 changes: 2 additions & 2 deletions Geome3Dash/Geome3Dash.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
<ClCompile Include="src\helper\spline\Spline.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\BlockModelsStorage.cpp">
<ClCompile Include="src\BlockModelStorage.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="lib\imgui-cocos\imgui.cpp">
Expand Down Expand Up @@ -341,7 +341,7 @@
<ClInclude Include="src\helper\spline\Spline.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\BlockModelsStorage.h">
<ClInclude Include="src\BlockModelStorage.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="lib\imgui-cocos\imconfig.h">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "pch.h"

#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

#include "engine/sus3d/ShaderProgram.h"
#include "engine/sus3d/Shader.h"
Expand All @@ -17,39 +17,41 @@

namespace g3d
{
bool BlockModelsStorage::init()
void BlockModelStorage::loadAllShaders()
{
basePath = geode::Mod::get()->getResourcesDir() / "model3d";
OpenGLStateHelper::saveState();

const auto shaderPath = basePath / "planet" / "shader";

auto vertexShader = sus3d::Shader::createWithString(
sus3d::shaders::vertexShaderSource,
sus3d::shaders::vertexShaderSource,
sus3d::ShaderType::kVertexShader);
auto fragmentShader = sus3d::Shader::createWithString(
sus3d::shaders::fragmentShaderSource,
sus3d::shaders::fragmentShaderSource,
sus3d::ShaderType::kFragmentShader);
//if (blockShaderProgram) { delete blockShaderProgram; }
blockShaderProgram = CocosShaderProgram::create(vertexShader, fragmentShader);
delete vertexShader;
delete fragmentShader;

auto vertexShader2 = sus3d::Shader::createWithString(
sus3d::shaders::vertexShaderSource,
sus3d::shaders::vertexShaderSource,
sus3d::ShaderType::kVertexShader);
auto fragmentShader2 = sus3d::Shader::createWithFile(
shaderPath / "water2.fsh",
shaderPath / "water2.fsh",
sus3d::ShaderType::kFragmentShader);
//if (waterShaderProgram) { delete waterShaderProgram; }
waterShaderProgram = CocosShaderProgram::create(vertexShader2, fragmentShader2);
delete vertexShader2;
delete fragmentShader2;

auto vertexShader3 = sus3d::Shader::createWithFile(
shaderPath / "cloud.vsh",
shaderPath / "cloud.vsh",
sus3d::ShaderType::kVertexShader);
auto fragmentShader3 = sus3d::Shader::createWithFile(
shaderPath / "cloud.fsh",
shaderPath / "cloud.fsh",
sus3d::ShaderType::kFragmentShader);
//if (cloudShaderProgram) { delete cloudShaderProgram; }
cloudShaderProgram = CocosShaderProgram::create(vertexShader3, fragmentShader3);
delete vertexShader3;
delete fragmentShader3;
Expand All @@ -58,20 +60,26 @@ namespace g3d
sus3d::shaders::idBufferingVertexShader,
sus3d::ShaderType::kVertexShader);
auto fragmentShader4 = sus3d::Shader::createWithString(
sus3d::shaders::idBufferingFragmentShader,
sus3d::shaders::idBufferingFragmentShader,
sus3d::ShaderType::kFragmentShader);
//if (idBufferShaderProgram) { delete idBufferShaderProgram; }
idBufferShaderProgram = CocosShaderProgram::create(vertexShader4, fragmentShader4);
delete vertexShader4;
delete fragmentShader4;

OpenGLStateHelper::pushState();
}

bool BlockModelStorage::init()
{
basePath = geode::Mod::get()->getResourcesDir() / "model3d";
loadAllShaders();
loadAllModels();
OpenGLStateHelper::pushState();
return true;
}

// mtl model path fix (model path must be absolute)
void BlockModelsStorage::parseMtlPath(const std::filesystem::path& path)
void BlockModelStorage::parseMtlPath(const std::filesystem::path& path)
{
if (std::filesystem::exists(path))
{
Expand All @@ -84,7 +92,7 @@ namespace g3d
}
}

void BlockModelsStorage::loadAllModels()
void BlockModelStorage::loadAllModels()
{
try
{
Expand Down Expand Up @@ -112,21 +120,21 @@ namespace g3d
}
}

sus3d::Model* BlockModelsStorage::getBlockModel(const int id)
sus3d::Model* BlockModelStorage::getBlockModel(const int id)
{
auto it = blockModels.find(id);
if (it == blockModels.end()) { return nullptr; }
return it->second;
}

sus3d::Model* BlockModelsStorage::getModel(const std::filesystem::path& path)
sus3d::Model* BlockModelStorage::getModel(const std::filesystem::path& path)
{
auto it = allModels.find(path);
if (it == allModels.end()) { return nullptr; }
return it->second;
}

void BlockModelsStorage::tryRenderBlock(
void BlockModelStorage::tryRenderBlock(
const int objectId,
sus3d::Camera* camera,
sus3d::Light* light)
Expand All @@ -143,16 +151,15 @@ namespace g3d
}
}

BlockModelsStorage* BlockModelsStorage::getInstance()
BlockModelStorage* BlockModelStorage::get()
{

if (!instance)
{
instance = new BlockModelsStorage;
instance = new BlockModelStorage;
instance->init();
}
return instance;
}

BlockModelsStorage* BlockModelsStorage::instance = nullptr;
BlockModelStorage* BlockModelStorage::instance = nullptr;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ namespace sus3d

namespace g3d
{
class BlockModelsStorage
class BlockModelStorage
{
private:
BlockModelStorage() {}
protected:
std::unordered_map<int, sus3d::Model*> blockModels;
std::unordered_map<std::filesystem::path, sus3d::Model*> allModels;
sus3d::ShaderProgram* blockShaderProgram;
sus3d::ShaderProgram* waterShaderProgram;
sus3d::ShaderProgram* cloudShaderProgram;
sus3d::ShaderProgram* idBufferShaderProgram;
static BlockModelsStorage* instance;
static BlockModelStorage* instance;
void loadAllShaders();
void loadAllModels();
bool init();
BlockModelsStorage() {}
std::filesystem::path basePath;
public:
sus3d::ShaderProgram* getBlockSP() { return blockShaderProgram; }
Expand All @@ -42,7 +44,7 @@ namespace g3d
void parseMtlPath(const std::filesystem::path& path);
sus3d::Model* getModel(const std::filesystem::path& path);
sus3d::Model* getBlockModel(const int id);
static BlockModelsStorage* getInstance();
static BlockModelStorage* get();

template <typename ModelT = sus3d::Model>
ModelT* loadAndParseMtl(const std::filesystem::path& path)
Expand Down
4 changes: 2 additions & 2 deletions Geome3Dash/src/CocosShaderProgram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace g3d
{
void CocosShaderProgram::setBasicUniforms() {
void CocosShaderProgram::setBasicUniforms()
{
auto screenSize = CCDirector::sharedDirector()->getWinSize();
float screenWidth = screenSize.width;
float screenHeight = screenSize.height;
float time = CCDirector::sharedDirector()->getTotalFrames() * CCDirector::sharedDirector()->getAnimationInterval();

setVec2("iResolution", glm::vec2(screenWidth, screenHeight));
setFloat("iTime", time);
}
Expand Down
4 changes: 2 additions & 2 deletions Geome3Dash/src/game/component/G3DBaseNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "helper/OpenGLStateHelper.h"
#include "helper/CommonHelper.h"

#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

namespace g3d
{
Expand Down Expand Up @@ -105,7 +105,7 @@ namespace g3d

OpenGLStateHelper::saveState();

auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();

shaderProgram = bms->getBlockSP();
getObjectIDByMousePositionShader = bms->getIdBufferSP();
Expand Down
4 changes: 2 additions & 2 deletions Geome3Dash/src/game/component/G3DBaseNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "helper/CommonHelper.h"

#include "CocosShaderProgram.h"
#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

namespace g3d
{
Expand Down Expand Up @@ -53,7 +53,7 @@ namespace g3d
template <typename T>
T* loadAndAddModel(const std::filesystem::path& filePath)
{
auto model = BlockModelsStorage::getInstance()->getModelT<T>(filePath);
auto model = BlockModelStorage::get()->getModelT<T>(filePath);
if (model) { models.push_back(model); }
return model;
}
Expand Down
6 changes: 3 additions & 3 deletions Geome3Dash/src/game/editor/G3DCurveEditorLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include "engine/sus3d/Shader.h"
#include "engine/sus3d/Shaders.h"

#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

namespace g3d
{
Expand Down Expand Up @@ -195,7 +195,7 @@ namespace g3d

setKeyboardEnabled(true);

auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();
pointModel = bms->getModel(bms->getBP() / "editor" / "model" / "point.obj");
pointModel->setScale(glm::vec3(0.2f));
layer3d->models.push_back(pointModel);
Expand Down Expand Up @@ -286,7 +286,7 @@ namespace g3d
glm::quat rotationQuat = glm::quat_cast(rotationMatrix);
glm::vec3 eulerDegrees = glm::degrees(glm::eulerAngles(rotationQuat * firstRotationQuat));

auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();
if (auto blockModel = bms->getBlockModel(block->m_objectID))
{
blockModel->setPosition(pos + (normal * lengthScaleFactor * (block->getPositionY() - 110)));
Expand Down
6 changes: 3 additions & 3 deletions Geome3Dash/src/game/editor/G3DEditorPopup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "helper/OpenGLStateHelper.h"

#include "LevelDataManager.h"
#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

namespace g3d
{
Expand All @@ -28,7 +28,7 @@ namespace g3d

void G3DEditorScene::loadModel()
{
auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();

cube = bms->getModel(bms->getBP() / "player" / "cube" / "0" / "model.obj");
cube->setScale(glm::vec3(0.75));
Expand Down Expand Up @@ -93,7 +93,7 @@ namespace g3d
}


auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();

// End jumping logic

Expand Down
4 changes: 2 additions & 2 deletions Geome3Dash/src/game/planet/G3DPlanetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ namespace g3d
// Static animation state
static bool isAnimating = false;
static double timeElapsed = 0.0;
constexpr double duration = 1.0; // Animation duration
constexpr double duration = 1.25; // Animation duration

// Trigger animation if the scale difference exceeds the threshold
if (!isAnimating && (waterScaleDiff > threshold || planetScaleDiff > threshold)) {
Expand Down Expand Up @@ -318,7 +318,7 @@ namespace g3d
setKeyboardEnabled(true);


auto bms = BlockModelsStorage::getInstance();
auto bms = BlockModelStorage::get();

const auto planetPath = bms->getBP() / "planet";
const auto shaderPath = planetPath / "shader";
Expand Down
8 changes: 4 additions & 4 deletions Geome3Dash/src/game/playing/G3DPlayLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

#include "game/playing/G3DPlayLayer.h"
#include "LevelDataManager.h"
#include "BlockModelsStorage.h"
#include "BlockModelStorage.h"

namespace g3d
{
G3DPlayLayer* G3DPlayLayer::instance = nullptr;

std::filesystem::path G3DPlayerObject::getPlayerModelPath(const std::string& type, const int id)
{
return geode::Mod::get()->getResourcesDir() / "model3d" / "player" / type / std::to_string(id) / "model.obj";
return BlockModelStorage::get()->getBP() / "player" / type / std::to_string(id) / "model.obj";
}

std::filesystem::path G3DPlayerObject::getFixedPlayerModelPath(const std::string& type, const int id)
Expand All @@ -30,7 +30,7 @@ namespace g3d

void G3DPlayerObject::loadPlayerModel(sus3d::Model** model, const std::string& type, const int id)
{
*model = BlockModelsStorage::getInstance()->loadAndParseMtl(getFixedPlayerModelPath(type, id));
*model = BlockModelStorage::get()->loadAndParseMtl(getFixedPlayerModelPath(type, id));
(*model)->setScale(glm::vec3(0.75));
}

Expand Down Expand Up @@ -229,7 +229,7 @@ namespace g3d
auto distance = std::abs(playLayer->m_player1->m_position.x - obj->getPositionX());
if (distance < maxRender)
{
if (auto model = BlockModelsStorage::getInstance()->getBlockModel(obj->m_objectID))
if (auto model = BlockModelStorage::get()->getBlockModel(obj->m_objectID))
{
updateBlock(obj, model);
// apply fade
Expand Down
Loading

0 comments on commit 6f9e9bf

Please sign in to comment.