Skip to content

Commit

Permalink
Hopefully fixed all cell rendering bugs (for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zang3th committed Oct 8, 2023
1 parent eefe9e3 commit aff9468
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 97 deletions.
20 changes: 8 additions & 12 deletions Apps/CellSim/CellSimInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ namespace CS
{
for(Engine::uint8 i = 0; i < 3; i++)
{
if(Engine::AppSettings::selectedCellCoords[i] < 0)
{
Engine::AppSettings::selectedCellCoords[i] = 0;
}
else if((uint32_t)Engine::AppSettings::selectedCellCoords[i] > Engine::AppSettings::CELL_FRAME_SIZE)
if((uint32_t)Engine::AppSettings::selectedCellCoords[i] > Engine::AppSettings::CELL_FRAME_SIZE)
{
Engine::AppSettings::selectedCellCoords[i] = Engine::AppSettings::CELL_FRAME_SIZE;
}
Expand Down Expand Up @@ -150,33 +146,33 @@ namespace CS

ImGui::Text("X Coordinate:\t");
ImGui::SameLine();
ImGui::InputFloat("##1", &Engine::AppSettings::selectedCellCoords[0], 10, 0);
Input_u32("##1", &Engine::AppSettings::selectedCellCoords[0], 1, 10, ImGuiInputTextFlags_CharsDecimal);
ImGui::Text("Y Coordinate:\t");
ImGui::SameLine();
ImGui::InputFloat("##2", &Engine::AppSettings::selectedCellCoords[1], 10, 0);
Input_u32("##2", &Engine::AppSettings::selectedCellCoords[1], 1, 10, ImGuiInputTextFlags_CharsDecimal);
ImGui::Text("Z Coordinate:\t");
ImGui::SameLine();
ImGui::InputFloat("##3", &Engine::AppSettings::selectedCellCoords[2], 10, 0);
Input_u32("##3", &Engine::AppSettings::selectedCellCoords[2], 1, 10, ImGuiInputTextFlags_CharsDecimal);

ImGui::PopItemWidth();
CheckCellBoundaries();

// --- Cell spawn amount
ImGui::Text("Amount:\t");
ImGui::SameLine();
ImGui::RadioButton("1", &Engine::AppSettings::selectedCellAmount, 1);
RadioButton_u32("1", &Engine::AppSettings::selectedCellAmount, 1);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("9", &Engine::AppSettings::selectedCellAmount, 9);
RadioButton_u32("9", &Engine::AppSettings::selectedCellAmount, 9);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("25", &Engine::AppSettings::selectedCellAmount, 25);
RadioButton_u32("25", &Engine::AppSettings::selectedCellAmount, 25);
ImGui::SameLine();
ImGui::Text("\t");
ImGui::SameLine();
ImGui::RadioButton("49", &Engine::AppSettings::selectedCellAmount, 49);
RadioButton_u32("49", &Engine::AppSettings::selectedCellAmount, 49);

// --- Spawn button
ImGui::Text("\t\t");
Expand Down
14 changes: 14 additions & 0 deletions Engine/Application/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@ namespace Engine
ImGui::SetCursorPosX((windowWidth - textWidth) * 0.5f);
}

bool Interface::RadioButton_u32(const char* label, uint32* v, uint32 v_button)
{
const bool pressed = ImGui::RadioButton(label, *v == v_button);
if (pressed)
*v = v_button;
return pressed;
}

bool Interface::Input_u32(const char* label, uint32* v, uint32 step, uint32 step_fast, ImGuiInputTextFlags flags)
{
const char* format = (flags & ImGuiInputTextFlags_CharsHexadecimal) ? "%08X" : "%d";
return ImGui::InputScalar(label, ImGuiDataType_S32, (void*)v, (void*)(step > 0 ? &step : NULL), (void*)(step_fast > 0 ? &step_fast : NULL), format, flags);
}

// ----- Public -----

Interface::Interface()
Expand Down
2 changes: 2 additions & 0 deletions Engine/Application/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ namespace Engine
static void SetDarkThemeColors();
static void CenterText(const char* text);
static void CenterCursor(const char* characterAmount);
static bool RadioButton_u32(const char* label, uint32* v, uint32 v_button);
static bool Input_u32(const char* label, uint32* v, uint32 step, uint32 step_fast, ImGuiInputTextFlags flags);

public:
Interface();
Expand Down
4 changes: 2 additions & 2 deletions Engine/Core/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ namespace Engine
inline static constexpr uint32 MAX_CELL_AMOUNT = CELL_FRAME_SIZE * CELL_FRAME_SIZE * CELL_FRAME_SIZE;
inline static bool resetCamera = false;
inline static CellType selectedCellType = None;
inline static float selectedCellCoords[3] = {0.0f, 0.0f, 0.0f};
inline static int32 selectedCellAmount = 0;
inline static uint32 selectedCellCoords[3] = {0, 0, 0};
inline static uint32 selectedCellAmount = 0;
inline static uint32 cellsAlive = 0;
inline static bool spawnNewCell = false;
inline static bool deleteAllCells = false;
Expand Down
72 changes: 36 additions & 36 deletions Engine/Core/VerticeData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@ namespace Engine
inline static const float CUBE_VERTICES[] =
{
// Back face
-1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, 1.0f, -1.0f,
-0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
// Front face
-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
// Left face
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, -1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
// Right face
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
// Bottom face
-1.0f, -1.0f, -1.0f,
1.0f, -1.0f, -1.0f,
1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, 1.0f,
-1.0f, -1.0f, -1.0f,
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, -0.5f, -0.5f,
// Top face
-1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f, -1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, -1.0f,
-1.0f, 1.0f, 1.0f
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, 0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, 0.5f, 0.5f
};
}
29 changes: 16 additions & 13 deletions Engine/Rendering/Camera/CameraController3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,28 @@ namespace Engine

void CameraController3D::ProcessInput()
{
auto dt = (float)Window::GetDeltaTime();
if(_windowInFocus)
{
auto dt = (float) Window::GetDeltaTime();

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_W) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::FORWARD, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_W) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::FORWARD, dt);

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_S) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::BACKWARD, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_S) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::BACKWARD, dt);

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_D) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::RIGHT, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_D) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::RIGHT, dt);

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_A) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::LEFT, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_A) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::LEFT, dt);

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_SPACE) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::UP, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_SPACE) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::UP, dt);

if(glfwGetKey(Window::GetWindow(), GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::DOWN, dt);
if (glfwGetKey(Window::GetWindow(), GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
Camera3D::ProcessKeyboard(Camera3D::DOWN, dt);
}
}

void CameraController3D::DeFocusWindow(GLFWwindow* window)
Expand Down
44 changes: 13 additions & 31 deletions Engine/Rendering/Renderer/CellRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,43 +52,25 @@ namespace Engine
_vboModel->Unbind();
}

void CellRenderer::UpdateModelViewStorage()
void CellRenderer::UpdateModelViewStorage(uint32 index, const glm::vec3& pos)
{
glm::mat4 model(1.0f);
uint32 count = 0;

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++)
{
if(_cellStorage[x][y][z].amount > 0)
{
glm::vec3 pos((float)x, (float)y, (float)z);
glm::vec3 pos2(50.0f);
glm::vec3 pos3(AppSettings::selectedCellCoords[0], AppSettings::selectedCellCoords[1],
AppSettings::selectedCellCoords[2]);
model = glm::mat4(1.0f);
model = glm::translate(model, _worldSpawnPos + pos3);
model = glm::scale(model, glm::vec3(_cellSize));
_modelViewStorage.at(count) = model;
}
count++;
}
}
}
model = glm::translate(model, _worldSpawnPos + pos);
model = glm::scale(model, glm::vec3(_cellSize));
_modelViewStorage.at(index) = model;
}

void CellRenderer::InitCellStorage()
{
_cellCount = 0;

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[x][y][z] = {None, 0};
SpawnCell(CellType::None, 1, glm::u32vec3(x, y, z));
}
}
}
Expand Down Expand Up @@ -135,18 +117,18 @@ namespace Engine
return _cellCount;
}

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

//ToDo: Resolve amount > 1 in one cell
}

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

void CellRenderer::CalculateCellPhysics()
Expand Down
4 changes: 2 additions & 2 deletions Engine/Rendering/Renderer/CellRenderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ namespace Engine

void InitGpuStorage();
void UpdateGpuStorage();
void UpdateModelViewStorage();
void UpdateModelViewStorage(uint32 index, const glm::vec3& pos);
void InitCellStorage();

public:
void Flush(Renderer* renderer) final;
[[nodiscard]] uint32 GetAliveCellAmount() const;
void SpawnCell(CellType cellType, uint32 cellAmount, const glm::vec3& cellPos);
void SpawnCell(CellType cellType, uint32 cellAmount, const glm::u32vec3& cellPos);
void DeleteAllCells();
void CalculateCellPhysics();
};
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@

### CellSim

![CellSim](Res/Screenshots/CellSim/Screenshot_CS_006.png)
![CellSim](Res/Screenshots/CellSim/Screenshot_CS_007.png)
Binary file added Res/Screenshots/CellSim/Screenshot_CS_007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit aff9468

Please sign in to comment.