From 7f7ecfa37d0ed08e53f0840814c5f955b59f9e14 Mon Sep 17 00:00:00 2001 From: kj16609 Date: Sat, 16 Mar 2024 23:44:20 -0400 Subject: [PATCH] Add in Z scaling to texture map --- shaders/terrain/terrain.tesc | 7 ++-- shaders/terrain/terrain.tese | 6 ++-- shaders/terrain/terrain.vert | 5 ++- src/engine/EngineContext.cpp | 23 +++--------- src/engine/model/prebuilt/terrainModel.cpp | 37 ++++++++++++++++++++ src/engine/model/prebuilt/terrainModel.hpp | 17 +++++++++ src/engine/primitives/TransformComponent.cpp | 15 ++------ 7 files changed, 74 insertions(+), 36 deletions(-) create mode 100644 src/engine/model/prebuilt/terrainModel.cpp create mode 100644 src/engine/model/prebuilt/terrainModel.hpp diff --git a/shaders/terrain/terrain.tesc b/shaders/terrain/terrain.tesc index 0461913..55c5df0 100644 --- a/shaders/terrain/terrain.tesc +++ b/shaders/terrain/terrain.tesc @@ -3,13 +3,15 @@ layout(location = 0) in vec3 in_normal[]; layout(location = 1) in vec3 in_color[]; layout(location = 2) in vec2 in_uv[]; -layout(location = 3) in uint in_tex_idx[]; +layout(location = 3) in flat uint in_tex_idx[]; +layout(location = 4) in flat float in_scale_z[]; layout(vertices = 4) out; layout(location = 0) out vec3 out_normal[4]; layout(location = 1) out vec2 out_uv[4]; -layout(location = 2) out uint out_tex_idx[4]; +layout(location = 2) out flat uint out_tex_idx[4]; +layout(location = 3) out flat float out_scale_z[4]; layout (set = 0, binding = 0) uniform CameraInfo { mat4 projection; @@ -50,4 +52,5 @@ void main() out_normal[gl_InvocationID] = in_normal[gl_InvocationID]; out_uv[gl_InvocationID] = in_uv[gl_InvocationID]; out_tex_idx[gl_InvocationID] = in_tex_idx[gl_InvocationID]; + out_scale_z[gl_InvocationID] = in_scale_z[gl_InvocationID]; } \ No newline at end of file diff --git a/shaders/terrain/terrain.tese b/shaders/terrain/terrain.tese index 2603c3f..16fa3b4 100644 --- a/shaders/terrain/terrain.tese +++ b/shaders/terrain/terrain.tese @@ -5,7 +5,8 @@ layout(quads, equal_spacing, cw) in; layout(location = 0) in vec3 in_normal[]; layout(location = 1) in vec2 in_uv[]; -layout(location = 2) in uint in_tex_idx[]; +layout(location = 2) in flat uint in_tex_idx[]; +layout(location = 3) in flat float in_scale_z[]; layout(location = 0) out vec3 out_normal; layout(location = 1) out vec2 out_uv; @@ -39,7 +40,8 @@ void main() vec4 world_pos = ubo.projection * ubo.view * pos; - world_pos.y -= (texture(tex[out_tex_idx], out_uv).r - 0.5); + // Doesn't matter which in_scale_z we take since it'll be the same for the entire model. + world_pos.y -= (texture(tex[out_tex_idx], out_uv).r - 0.5) * in_scale_z[0]; out_world_pos = vec3(world_pos); gl_Position = world_pos; diff --git a/shaders/terrain/terrain.vert b/shaders/terrain/terrain.vert index bef017a..53f0b45 100644 --- a/shaders/terrain/terrain.vert +++ b/shaders/terrain/terrain.vert @@ -12,11 +12,14 @@ layout (location = 8) in uint in_texture_id; layout (location = 0) out vec3 out_normal; layout (location = 1) out vec3 out_color; layout (location = 2) out vec2 out_tex_coord; -layout (location = 3) out uint out_texture_idx; +layout (location = 3) out flat uint out_texture_idx; +layout (location = 4) out flat float out_scale_z; void main() { gl_Position = instance_model_matrix * vec4(position, 1.0f); + //We want to grab the scale value + out_scale_z = length(instance_model_matrix[2]); mat3 normal_matrix = transpose(inverse(mat3(instance_model_matrix))); diff --git a/src/engine/EngineContext.cpp b/src/engine/EngineContext.cpp index 45d8cc2..39d5c5b 100644 --- a/src/engine/EngineContext.cpp +++ b/src/engine/EngineContext.cpp @@ -8,6 +8,7 @@ #include #include +#include "engine/model/prebuilt/terrainModel.hpp" #include #include #include @@ -291,25 +292,9 @@ namespace fgl::engine }*/ { - std::vector< Vertex > verts {}; - - verts.emplace_back( - glm::vec3( -1.0f, -1.0f, 0.0f ), glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.0f, 0.5f ) ); - verts.emplace_back( - glm::vec3( -1.0f, 1.0f, 0.0f ), glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.5f, .5f ) ); - verts.emplace_back( - glm::vec3( 1.0f, 1.0f, 0.0f ), glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.5f, 0.0f ) ); - verts.emplace_back( - glm::vec3( 1.0f, -1.0f, 0.0f ), glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.0f, 0.0f ) ); - - std::vector< std::uint32_t > indicies { 0, 1, 2, 3 }; - - std::shared_ptr< Model > model { Model::createModelFromVerts( - Device::getInstance(), - std::move( verts ), - std::move( indicies ), - m_terrain_system.getVertexBuffer(), - m_terrain_system.getIndexBuffer() ) }; + auto model { + generateTerrainModel( m_terrain_system.getVertexBuffer(), m_terrain_system.getIndexBuffer() ) + }; Texture texture { Texture::loadFromFile( "models/textures/heightmap.png" ) }; Sampler sampler { vk::Filter::eLinear, diff --git a/src/engine/model/prebuilt/terrainModel.cpp b/src/engine/model/prebuilt/terrainModel.cpp new file mode 100644 index 0000000..ff7441d --- /dev/null +++ b/src/engine/model/prebuilt/terrainModel.cpp @@ -0,0 +1,37 @@ +// +// Created by kj16609 on 3/16/24. +// + +#include "terrainModel.hpp" + +#include "engine/model/Model.hpp" +#include "engine/rendering/Device.hpp" + +namespace fgl::engine +{ + + std::shared_ptr< Model > generateTerrainModel( Buffer& vertex_buffer, Buffer& index_buffer ) + { + std::vector< Vertex > verts {}; + + constexpr glm::vec3 TOP_LEFT { -0.5f, 0.5f, 0.0f }; + constexpr glm::vec3 TOP_RIGHT { 0.5f, 0.5f, 0.0f }; + constexpr glm::vec3 BOTTOM_RIGHT { 0.5f, -0.5f, 0.0f }; + constexpr glm::vec3 BOTTOM_LEFT { -0.5f, -0.5f, 0.0f }; + constexpr float dist_mulpt { 2.0f }; + + verts.emplace_back( TOP_LEFT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.0f, 0.0f ) ); + verts.emplace_back( TOP_RIGHT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 1.0f, 0.0f ) ); + verts + .emplace_back( BOTTOM_RIGHT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 1.0f, 1.0f ) ); + verts.emplace_back( BOTTOM_LEFT * dist_mulpt, glm::vec3( 1.0f ), constants::WORLD_UP, glm::vec2( 0.0f, 1.0f ) ); + + std::vector< std::uint32_t > indicies { 0, 1, 2, 3 }; + + std::shared_ptr< Model > model { Model::createModelFromVerts( + Device::getInstance(), std::move( verts ), std::move( indicies ), vertex_buffer, index_buffer ) }; + + return model; + } + +} // namespace fgl::engine diff --git a/src/engine/model/prebuilt/terrainModel.hpp b/src/engine/model/prebuilt/terrainModel.hpp new file mode 100644 index 0000000..58195c0 --- /dev/null +++ b/src/engine/model/prebuilt/terrainModel.hpp @@ -0,0 +1,17 @@ +// +// Created by kj16609 on 3/16/24. +// + +#pragma once + +#include + +namespace fgl::engine +{ + class Model; + + class Buffer; + + std::shared_ptr< Model > generateTerrainModel( Buffer& vertex_buffer, Buffer& index_buffer ); + +} // namespace fgl::engine diff --git a/src/engine/primitives/TransformComponent.cpp b/src/engine/primitives/TransformComponent.cpp index 45e839f..35859f7 100644 --- a/src/engine/primitives/TransformComponent.cpp +++ b/src/engine/primitives/TransformComponent.cpp @@ -15,18 +15,9 @@ namespace fgl::engine //TODO: This uses the Tait-Bryan angles stuff, It should use the respective function. // Or maybe something else can be done here - return glm::mat4 { { scale.x * rotation_mat[ 0 ][ 0 ], - scale.x * rotation_mat[ 0 ][ 1 ], - scale.x * rotation_mat[ 0 ][ 2 ], - 0.0f }, - { scale.y * rotation_mat[ 1 ][ 0 ], - scale.y * rotation_mat[ 1 ][ 1 ], - scale.y * rotation_mat[ 1 ][ 2 ], - 0.0f }, - { scale.z * rotation_mat[ 2 ][ 0 ], - scale.z * rotation_mat[ 2 ][ 1 ], - scale.z * rotation_mat[ 2 ][ 2 ], - 0.0f }, + return glm::mat4 { { scale.x * rotation_mat[ 0 ], 0.0f }, + { scale.y * rotation_mat[ 1 ], 0.0f }, + { scale.z * rotation_mat[ 2 ], 0.0f }, { translation.vec().x, translation.vec().y, translation.vec().z, 1.0f } }; }