Skip to content

Commit

Permalink
Cell rendering now internally uses a 3D array. Visualization is buggy…
Browse files Browse the repository at this point in the history
… for positions unequal 0
  • Loading branch information
Zang3th committed Oct 7, 2023
1 parent 5ab948f commit 9fac00e
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 60 deletions.
29 changes: 19 additions & 10 deletions Apps/CellSim/src/CellSimApp.cpp → Apps/CellSim/CellSimApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,22 +142,31 @@ namespace CS
{
//ToDo: Add profiling

Engine::AppSettings::cellsAlive = _cellRenderer->GetAliveCellAmount();

if(Engine::AppSettings::spawnNewCell)
{
_cellRenderer->SpawnCell
(
Engine::AppSettings::selectedCellType,
Engine::AppSettings::selectedCellAmount,
glm::vec3(Engine::AppSettings::selectedCellCoords[0],
Engine::AppSettings::selectedCellCoords[1],
Engine::AppSettings::selectedCellCoords[2])
);
if(Engine::AppSettings::selectedCellAmount > 0)
{
_cellRenderer->SpawnCell
(
Engine::AppSettings::selectedCellType,
Engine::AppSettings::selectedCellAmount,
glm::u32vec3(Engine::AppSettings::selectedCellCoords[0],
Engine::AppSettings::selectedCellCoords[1],
Engine::AppSettings::selectedCellCoords[2])
);
}

Engine::AppSettings::spawnNewCell = false;
}

if(Engine::AppSettings::deleteAllCells)
{
_cellRenderer->DeleteAllCells();
Engine::AppSettings::deleteAllCells = false;
}

Engine::AppSettings::cellsAlive = _cellRenderer->GetAliveCellAmount();

if(Engine::AppSettings::cellsAlive > 0)
{
_cellRenderer->CalculateCellPhysics();
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ namespace CS
ImGui::PopItemWidth();
CheckCellBoundaries();

// Cell spawn amount
// --- Cell spawn amount
ImGui::Text("Amount:\t");
ImGui::SameLine();
ImGui::RadioButton("1", &Engine::AppSettings::selectedCellAmount, 1);
Expand All @@ -179,14 +179,22 @@ namespace CS
ImGui::RadioButton("49", &Engine::AppSettings::selectedCellAmount, 49);

// --- Spawn button
CenterCursor("Spawn");
if (ImGui::Button("Spawn"))
ImGui::Text("\t\t");
ImGui::SameLine();
if(ImGui::Button("Spawn"))
{
Engine::AppSettings::spawnNewCell = true;
}
ImGui::Separator();
ImGui::SameLine();

//ToDo: Add "delete all cells" Button
// --- Delete all cells
ImGui::Text("\t\t");
ImGui::SameLine();
if(ImGui::Button("Delete all"))
{
Engine::AppSettings::deleteAllCells = true;
}
ImGui::Separator();

//ToDo: Add "generate stone ground" Button

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 6 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,17 @@ target_link_libraries(GameEngine PUBLIC ${ENGINE_LINKER_FLAGS})
find_package(OpenGL REQUIRED)

### Source files ###
file(GLOB_RECURSE SRC_GREENWORLD Apps/GreenWorld/src/*.cpp)
file(GLOB_RECURSE SRC_CELLSIM Apps/CellSim/src/*.cpp)
file(GLOB_RECURSE SRC_GREENWORLD Apps/GreenWorld/*.cpp)
file(GLOB_RECURSE SRC_CELLSIM Apps/CellSim/*.cpp)

### Compile to an executable ###
add_executable(GreenWorld Apps/GreenWorld/GreenWorld.cpp ${SRC_GREENWORLD})
add_executable(CellSim Apps/CellSim/CellSim.cpp ${SRC_CELLSIM})
add_executable(GreenWorld ${SRC_GREENWORLD})
add_executable(CellSim ${SRC_CELLSIM})

### Include directories ###
target_include_directories(GreenWorld PUBLIC Apps/GreenWorld/src)
target_include_directories(GreenWorld PUBLIC Apps/GreenWorld)
target_include_directories(GreenWorld PUBLIC Engine)
target_include_directories(CellSim PUBLIC Apps/CellSim/src)
target_include_directories(CellSim PUBLIC Apps/CellSim)
target_include_directories(CellSim PUBLIC Engine)

### Link against static libraries ###
Expand Down
1 change: 1 addition & 0 deletions Engine/Core/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ namespace Engine
inline static int32 selectedCellAmount = 0;
inline static uint32 cellsAlive = 0;
inline static bool spawnNewCell = false;
inline static bool deleteAllCells = false;

AppSettings() = delete;
};
Expand Down
4 changes: 2 additions & 2 deletions Engine/Core/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ namespace Engine
}

//Log version
std::string rendererString(reinterpret_cast<const char *>(glGetString(GL_RENDERER)));
std::string versionString(reinterpret_cast<const char *>(glGetString(GL_VERSION)));
std::string rendererString(reinterpret_cast<const char*>(glGetString(GL_RENDERER)));
std::string versionString(reinterpret_cast<const char*>(glGetString(GL_VERSION)));
Logger::Info("Loaded", "OpenGL", std::string(rendererString + ", " + versionString));

GLRenderSettings::SetViewport(AppSettings::WINDOW_WIDTH, AppSettings::WINDOW_HEIGHT);
Expand Down
59 changes: 30 additions & 29 deletions Engine/Rendering/Renderer/CellRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Engine
_cellCount(0), _shader(shader), _worldSpawnPos(worldSpawnPos)
{
Logger::Info("Created", "Renderer",__func__);
//GenerateAllCells();
InitCellStorage();
InitGpuStorage();
}

Expand Down Expand Up @@ -48,51 +48,46 @@ namespace Engine
void CellRenderer::UpdateGpuStorage()
{
_vboModel->Bind();
_vboModel->Update(&_modelViewStorage[0], _cellCount * 16 * sizeof(float));
_vboModel->Update(&_modelViewStorage[0], _modelViewStorage.size() * 16 * sizeof(float));
_vboModel->Unbind();
}

void CellRenderer::UpdateModelViewStorage()
{
glm::mat4 model(1.0f);
uint32 count = 0;

for(uint32 i = 0; i < _cellCount; i++)
for(uint32 x = 0; x < AppSettings::CELL_FRAME_SIZE; x++)
{
model = glm::mat4(1.0f);
model = glm::translate(model, _cellStorage.at(i).position);
model = glm::scale(model, glm::vec3(_cellSize));
_modelViewStorage.at(i) = model;
for(uint32 y = 0; y < AppSettings::CELL_FRAME_SIZE; y++)
{
for(uint32 z = 0; z < AppSettings::CELL_FRAME_SIZE; z++)
{
if(_cellStorage[x][y][z].amount > 0)
{
model = glm::mat4(1.0f);
model = glm::translate(model, _worldSpawnPos + glm::vec3(x, y, z));
model = glm::scale(model, glm::vec3(_cellSize));
_modelViewStorage.at(count) = model;
}
count++;
}
}
}
}

//Function only used for stress testing
void CellRenderer::GenerateAllCells()
void CellRenderer::InitCellStorage()
{
uint32 count = 0;

//Get initial world position
for(uint32 x = 0; x < AppSettings::CELL_FRAME_SIZE; x++)
{
for(uint32 y = 0; y < AppSettings::CELL_FRAME_SIZE; y++)
{
for(uint32 z = 0; z < AppSettings::CELL_FRAME_SIZE; z++)
{
_cellStorage.at(count).position = _worldSpawnPos + glm::vec3(x, y, z);
count++;
_cellStorage[x][y][z] = {None, 0};
}
}
}

_cellCount = count;

//Sort depending on distance to camera
std::sort(_cellStorage.begin(), _cellStorage.end(),
[](const Cell& a, const Cell& b)
{
return glm::length(Camera3D::GetPosition() - a.position) < glm::length(Camera3D::GetPosition() - b.position);
});

UpdateModelViewStorage();
}

// ----- Public -----
Expand Down Expand Up @@ -131,16 +126,22 @@ namespace Engine
AppSettings::renderStats.cellPasses++;
}

uint32 CellRenderer::GetAliveCellAmount()
uint32 CellRenderer::GetAliveCellAmount() const
{
return _cellCount;
}

void CellRenderer::SpawnCell(CellType cellType, int32 cellAmount, const glm::vec3& cellPos)
void CellRenderer::SpawnCell(CellType cellType, uint32 cellAmount, const glm::u32vec3& cellPos)
{
_cellStorage.at(_cellCount) = {cellType, _worldSpawnPos + cellPos};
_cellCount++;
_cellStorage[cellPos.x][cellPos.y][cellPos.z] = {cellType, cellAmount};
_cellCount += cellAmount;
UpdateModelViewStorage();
}

void CellRenderer::DeleteAllCells()
{
InitCellStorage();
_cellCount = 0;
UpdateModelViewStorage();
}

Expand Down
16 changes: 8 additions & 8 deletions Engine/Rendering/Renderer/CellRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ namespace Engine
friend class RenderManager;

private:

struct Cell
{
CellType type;
glm::vec3 position;
uint32 amount;
};

Scope<VertexArray> _vao;
Expand All @@ -35,7 +34,7 @@ namespace Engine
Shader* _shader;
glm::vec3 _worldSpawnPos;

std::array<Cell, AppSettings::MAX_CELL_AMOUNT> _cellStorage;
Cell _cellStorage[AppSettings::CELL_FRAME_SIZE][AppSettings::CELL_FRAME_SIZE][AppSettings::CELL_FRAME_SIZE];
std::array<glm::mat4, AppSettings::MAX_CELL_AMOUNT> _modelViewStorage;

CellRenderer
Expand All @@ -48,12 +47,13 @@ namespace Engine
void InitGpuStorage();
void UpdateGpuStorage();
void UpdateModelViewStorage();
void GenerateAllCells();
void InitCellStorage();

public:
void Flush(Renderer* renderer) final;
uint32 GetAliveCellAmount();
void SpawnCell(CellType cellType, int32 cellAmount, const glm::vec3& cellPos);
void CalculateCellPhysics();
void Flush(Renderer* renderer) final;
[[nodiscard]] uint32 GetAliveCellAmount() const;
void SpawnCell(CellType cellType, uint32 cellAmount, const glm::u32vec3& cellPos);
void DeleteAllCells();
void CalculateCellPhysics();
};
}

0 comments on commit 9fac00e

Please sign in to comment.