Skip to content

Commit

Permalink
Added a grass material and changed clear colour.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Mollard committed Aug 1, 2024
1 parent 2aaa8df commit 737a68a
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 24 deletions.
8 changes: 6 additions & 2 deletions SlimeGame/src/DebugScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int DebugScene::Enter(VulkanContext& vulkanContext, ModelManager& modelManager,
pbrMaterialResource = descriptorManager.CreatePBRMaterial(vulkanContext, modelManager, "PBR Planet Material", "planet_surface/albedo.png", "planet_surface/normal.png", "planet_surface/metallic.png", "planet_surface/roughness.png", "planet_surface/ao.png");
m_pbrMaterials.push_back(pbrMaterialResource);

pbrMaterialResource = descriptorManager.CreatePBRMaterial(vulkanContext, modelManager, "Grass Material", "grass/albedo.png", "grass/normal.png", "planet_surface/metallic.png", "grass/roughness.png", "grass/ao.png");
m_pbrMaterials.push_back(pbrMaterialResource);

InitializeDebugObjects(vulkanContext, modelManager);

return 0;
Expand Down Expand Up @@ -63,6 +66,7 @@ void DebugScene::InitializeDebugObjects(VulkanContext& vulkanContext, ModelManag
// Light
auto lightEntity = std::make_shared<Entity>("Light");
DirectionalLight& light = lightEntity->AddComponent<DirectionalLightObject>().light;
light.color = glm::vec4(0.98f, 0.506f, 0.365f, 1.0f);
m_entityManager.AddEntity(lightEntity);

VmaAllocator allocator = vulkanContext.GetAllocator();
Expand All @@ -73,13 +77,13 @@ void DebugScene::InitializeDebugObjects(VulkanContext& vulkanContext, ModelManag
auto bunnyMesh = modelManager.LoadModel("stanford-bunny.obj", "pbr");
modelManager.CreateBuffersForMesh(allocator, *bunnyMesh);

auto groundPlane = modelManager.CreatePlane(allocator, 300.0f, 10);
auto groundPlane = modelManager.CreatePlane(allocator, 50.0f, 25);
modelManager.CreateBuffersForMesh(allocator, *groundPlane);

// Create the groundPlane
Entity ground = Entity("Ground");
ground.AddComponent<Model>(groundPlane);
ground.AddComponent<PBRMaterial>(m_pbrMaterials[1]);
ground.AddComponent<PBRMaterial>(m_pbrMaterials[2]);
auto& groundTransform = ground.AddComponent<Transform>();
groundTransform.position = glm::vec3(0.0f, 0.2f, 0.0f); // Slightly above the grid
m_entityManager.AddEntity(ground);
Expand Down
2 changes: 1 addition & 1 deletion SlimeOdyssey/include/Light.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct PointLightObject : public Component
struct DirectionalLight
{
glm::vec3 direction = glm::vec3(-20.0f, 15.0f, 20.0f);
float ambientStrength = 0.1f;
float ambientStrength = 0.075f;
glm::vec3 color = glm::vec3(1.0f);
float padding;
glm::mat4 lightSpaceMatrix = glm::mat4();
Expand Down
1 change: 1 addition & 0 deletions SlimeOdyssey/include/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Renderer
} m_mvp;

bool m_forceInvalidateDecriptorSets = false;
glm::vec4 m_clearColour = glm::vec4(0.98f, 0.506f, 0.365f, 1.0f);

void UpdateCommonBuffers(VulkanDebugUtils& debugUtils, VmaAllocator allocator, VkCommandBuffer& cmd, Scene* scene);
void UpdateLightBuffer(EntityManager& entityManager, VmaAllocator allocator);
Expand Down
14 changes: 7 additions & 7 deletions SlimeOdyssey/resources/shaders/basic.frag
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ void main()
vec3 normalMap = texture(normalMap, TexCoords).rgb * 2.0 - 1.0;
vec3 N = normalize(TBN * normalMap);

// Debug normals
// FragColor = vec4(N * 0.5 + 0.5, 1.0);
// return;

// Correct view and light vectors
vec3 V = normalize(camera.viewPos - FragPos);
vec3 L = normalize(-light.direction);
Expand All @@ -88,9 +92,6 @@ void main()
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;
specular *= vec3(1.0 - ao);
specular *= vec3(1.0 - metallic);
specular *= light.color;

vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
Expand All @@ -102,7 +103,7 @@ void main()
float shadow = ShadowCalculation(FragPosLightSpace);

// Combine lighting
vec3 Lo = (kD * albedo / PI + specular) * light.color * NdotL * (1 - shadow);
vec3 Lo = (kD * albedo / PI + specular) * light.color * NdotL * (1.0 - shadow) * ao;
vec3 ambient = light.ambientStrength * albedo * ao;

vec3 color = ambient + Lo;
Expand Down Expand Up @@ -149,9 +150,8 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
return ggx1 * ggx2;
}

vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
vec3 fresnelSchlick(float cosTheta, vec3 F0) {
return F0 + (1.0 - F0) * pow(max(1.0 - cosTheta, 0.0), 5.0);
}

float ShadowCalculation(vec4 fragPosLightSpace)
Expand Down
Binary file added SlimeOdyssey/resources/textures/grass/albedo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SlimeOdyssey/resources/textures/grass/ao.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SlimeOdyssey/resources/textures/grass/height.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added SlimeOdyssey/resources/textures/grass/normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions SlimeOdyssey/src/ModelManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,10 @@ ModelResource* ModelManager::CreatePlane(VmaAllocator allocator, float size, int
Vertex vertex;
vertex.pos = glm::vec3(x, 0.0f, z);
vertex.normal = glm::vec3(0.0f, 1.0f, -1.0f);
vertex.texCoord = glm::vec2(static_cast<float>(i % 2), static_cast<float>(j % 2));
// Calculate UV coordinates to be 0-1 for each quad, but uniform direction
vertex.texCoord = glm::vec2(static_cast<float>(i % 2), static_cast<float>(1 - (j % 2)));
vertex.tangent = glm::vec3(1.0f, 0.0f, 0.0f);
vertex.bitangent = glm::vec3(0.0f, 0.0f, -1.0f); // Inverted bitangent
vertex.bitangent = glm::vec3(0.0f, 0.0f, -1.0f);
model.vertices.push_back(vertex);
}
}
Expand All @@ -969,11 +970,11 @@ ModelResource* ModelManager::CreatePlane(VmaAllocator allocator, float size, int
int topRight = topLeft + 1;
int bottomLeft = (i + 1) * (divisions + 1) + j;
int bottomRight = bottomLeft + 1;
// First triangle (changed winding order)
// First triangle
model.indices.push_back(topLeft);
model.indices.push_back(topRight);
model.indices.push_back(bottomLeft);
// Second triangle (changed winding order)
// Second triangle
model.indices.push_back(topRight);
model.indices.push_back(bottomRight);
model.indices.push_back(bottomLeft);
Expand Down Expand Up @@ -1078,7 +1079,6 @@ ModelResource* ModelManager::CreateCube(VmaAllocator allocator, float size)
return &m_modelResources[name];
}


ModelResource* ModelManager::CreateSphere(VmaAllocator allocator, float radius, int segments, int rings)
{
std::string name = "debug_sphere" + std::to_string(radius) + "_" + std::to_string(segments) + "_" + std::to_string(rings);
Expand Down
26 changes: 17 additions & 9 deletions SlimeOdyssey/src/Renderer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "Renderer.h"

#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_vulkan.h>
#include <Camera.h>
#include <Light.h>

Expand All @@ -10,8 +12,6 @@
#include "vk_mem_alloc.h"
#include "VulkanContext.h"
#include "VulkanUtil.h"
#include <backends/imgui_impl_vulkan.h>
#include <backends/imgui_impl_glfw.h>

void Renderer::SetUp(vkb::DispatchTable& disp, VmaAllocator allocator, vkb::Swapchain swapchain, VulkanDebugUtils& debugUtils)
{
Expand Down Expand Up @@ -47,11 +47,10 @@ int Renderer::Draw(vkb::DispatchTable& disp,
m_shadowMapWidth = m_newShadowMapWidth;
m_shadowMapHeight = m_newShadowMapHeight;
CreateShadowMap(disp, allocator, debugUtils);
ImGui_ImplVulkan_RemoveTexture((VkDescriptorSet)m_shadowMapId);
ImGui_ImplVulkan_RemoveTexture((VkDescriptorSet) m_shadowMapId);
m_shadowMapId = ImGui_ImplVulkan_AddTexture(m_shadowMap.sampler, m_shadowMap.imageView, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}


if (SlimeUtil::BeginCommandBuffer(disp, cmd) != 0)
return -1;

Expand All @@ -72,7 +71,9 @@ int Renderer::Draw(vkb::DispatchTable& disp,
colorAttachmentInfo.imageLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
colorAttachmentInfo.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachmentInfo.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachmentInfo.clearValue = { .color = { { 0.05f, 0.05f, 0.05f, 0.0f } } };
colorAttachmentInfo.clearValue = {
.color = {m_clearColour.r, m_clearColour.g, m_clearColour.b, m_clearColour.a}
};

VkRenderingAttachmentInfo depthAttachmentInfo = {};
depthAttachmentInfo.sType = VK_STRUCTURE_TYPE_RENDERING_ATTACHMENT_INFO;
Expand Down Expand Up @@ -149,7 +150,7 @@ void Renderer::SetupViewportAndScissor(vkb::Swapchain swapchain, vkb::DispatchTa
VkViewport viewport = { .x = 0.0f, .y = 0.0f, .width = static_cast<float>(swapchain.extent.width), .height = static_cast<float>(swapchain.extent.height), .minDepth = 0.0f, .maxDepth = 1.0f };

VkRect2D scissor = {
.offset = { 0, 0 },
.offset = {0, 0},
.extent = swapchain.extent
};

Expand Down Expand Up @@ -181,7 +182,6 @@ std::vector<glm::vec3> calculateFrustumCorners(float fov, float aspect, float ne
return corners;
}


void Renderer::calculateFrustumSphere(const std::vector<glm::vec3>& frustumCorners, glm::vec3& center, float& radius)
{
center = glm::vec3(0.0f);
Expand Down Expand Up @@ -435,6 +435,14 @@ void Renderer::DrawModels(vkb::DispatchTable& disp, VkCommandBuffer& cmd, ModelM
void Renderer::DrawImguiDebugger(vkb::DispatchTable& disp, VmaAllocator allocator, VkCommandPool commandPool, VkQueue graphicsQueue, ModelManager& modelManager, VulkanDebugUtils& debugUtils)
{
RenderShadowMapInspector(disp, allocator, commandPool, graphicsQueue, modelManager, debugUtils);

// Render the ImGui debugger
ImGui::Begin("Renderer Debugger");

// clear colour
ImGui::ColorEdit4("Clear Colour", &m_clearColour.r);

ImGui::End();
}

void Renderer::UpdateCommonBuffers(VulkanDebugUtils& debugUtils, VmaAllocator allocator, VkCommandBuffer& cmd, Scene* scene)
Expand Down Expand Up @@ -719,7 +727,7 @@ void Renderer::GenerateShadowMap(vkb::DispatchTable& disp, VkCommandBuffer& cmd,
disp.cmdBeginRendering(cmd, &renderingInfo);

// Set viewport and scissor for shadow map
VkViewport viewport = { 0, 0, (float)m_shadowMapWidth, (float)m_shadowMapHeight, 0.0f, 1.0f };
VkViewport viewport = { 0, 0, (float) m_shadowMapWidth, (float) m_shadowMapHeight, 0.0f, 1.0f };
VkRect2D scissor = {
{ 0, 0},
{m_shadowMapWidth, m_shadowMapHeight}
Expand Down Expand Up @@ -836,7 +844,7 @@ void Renderer::RenderShadowMapInspector(vkb::DispatchTable& disp, VmaAllocator a
ImGui::SameLine();
ImGui::DragScalar("Height", ImGuiDataType_U32, &m_newShadowMapHeight);
ImGui::PopItemWidth();

// Shadow near and far
ImGui::Text("Shadow Near:");
ImGui::SameLine();
Expand Down

0 comments on commit 737a68a

Please sign in to comment.