Updated texture example using different texture and lighting (diffuse + specular)
This commit is contained in:
parent
a72df2bec9
commit
cb96eaf6bd
11 changed files with 101 additions and 59 deletions
|
|
@ -4,12 +4,11 @@ if %ERRORLEVEL% EQU 0 (
|
||||||
echo ndk-build has failed, build cancelled
|
echo ndk-build has failed, build cancelled
|
||||||
cd..
|
cd..
|
||||||
|
|
||||||
mkdir "assets\shaders"
|
mkdir "assets\shaders\texture"
|
||||||
xcopy "..\..\data\shaders\texture.vert.spv" "assets\shaders" /Y
|
xcopy "..\..\data\shaders\texture\*.spv" "assets\shaders\texture" /Y
|
||||||
xcopy "..\..\data\shaders\texture.frag.spv" "assets\shaders" /Y
|
|
||||||
|
|
||||||
mkdir "assets\textures"
|
mkdir "assets\textures"
|
||||||
xcopy "..\..\data\textures\igor_and_pal_bc3.ktx" "assets\textures" /Y
|
xcopy "..\..\data\textures\pattern_02_bc2.ktx" "assets\textures" /Y
|
||||||
|
|
||||||
mkdir "res\drawable"
|
mkdir "res\drawable"
|
||||||
xcopy "..\..\android\images\icon.png" "res\drawable" /Y
|
xcopy "..\..\android\images\icon.png" "res\drawable" /Y
|
||||||
|
|
|
||||||
|
|
@ -1,16 +0,0 @@
|
||||||
#version 450
|
|
||||||
|
|
||||||
#extension GL_ARB_separate_shader_objects : enable
|
|
||||||
#extension GL_ARB_shading_language_420pack : enable
|
|
||||||
|
|
||||||
layout (binding = 1) uniform sampler2D samplerColor;
|
|
||||||
|
|
||||||
layout (location = 0) in vec2 inUV;
|
|
||||||
layout (location = 1) in float inLodBias;
|
|
||||||
|
|
||||||
layout (location = 0) out vec4 outFragColor;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
outFragColor = texture(samplerColor, inUV, inLodBias);
|
|
||||||
}
|
|
||||||
Binary file not shown.
|
|
@ -1,24 +0,0 @@
|
||||||
#version 450
|
|
||||||
|
|
||||||
#extension GL_ARB_separate_shader_objects : enable
|
|
||||||
#extension GL_ARB_shading_language_420pack : enable
|
|
||||||
|
|
||||||
layout (location = 0) in vec3 inPos;
|
|
||||||
layout (location = 1) in vec2 inUV;
|
|
||||||
|
|
||||||
layout (binding = 0) uniform UBO
|
|
||||||
{
|
|
||||||
mat4 projection;
|
|
||||||
mat4 model;
|
|
||||||
float lodBias;
|
|
||||||
} ubo;
|
|
||||||
|
|
||||||
layout (location = 0) out vec2 outUV;
|
|
||||||
layout (location = 1) out float outLodBias;
|
|
||||||
|
|
||||||
void main()
|
|
||||||
{
|
|
||||||
outUV = inUV;
|
|
||||||
outLodBias = ubo.lodBias;
|
|
||||||
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
|
|
||||||
}
|
|
||||||
Binary file not shown.
28
data/shaders/texture/texture.frag
Normal file
28
data/shaders/texture/texture.frag
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
#extension GL_ARB_separate_shader_objects : enable
|
||||||
|
#extension GL_ARB_shading_language_420pack : enable
|
||||||
|
|
||||||
|
layout (binding = 1) uniform sampler2D samplerColor;
|
||||||
|
|
||||||
|
layout (location = 0) in vec2 inUV;
|
||||||
|
layout (location = 1) in float inLodBias;
|
||||||
|
layout (location = 2) in vec3 inNormal;
|
||||||
|
layout (location = 3) in vec3 inViewVec;
|
||||||
|
layout (location = 4) in vec3 inLightVec;
|
||||||
|
|
||||||
|
layout (location = 0) out vec4 outFragColor;
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
vec4 color = texture(samplerColor, inUV, inLodBias);
|
||||||
|
|
||||||
|
vec3 N = normalize(inNormal);
|
||||||
|
vec3 L = normalize(inLightVec);
|
||||||
|
vec3 V = normalize(inViewVec);
|
||||||
|
vec3 R = reflect(-L, N);
|
||||||
|
vec3 diffuse = max(dot(N, L), 0.0) * vec3(1.0);
|
||||||
|
float specular = pow(max(dot(R, V), 0.0), 16.0) * color.a;
|
||||||
|
|
||||||
|
outFragColor = vec4(diffuse * color.rgb + specular, 1.0);
|
||||||
|
}
|
||||||
BIN
data/shaders/texture/texture.frag.spv
Normal file
BIN
data/shaders/texture/texture.frag.spv
Normal file
Binary file not shown.
44
data/shaders/texture/texture.vert
Normal file
44
data/shaders/texture/texture.vert
Normal file
|
|
@ -0,0 +1,44 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
#extension GL_ARB_separate_shader_objects : enable
|
||||||
|
#extension GL_ARB_shading_language_420pack : enable
|
||||||
|
|
||||||
|
layout (location = 0) in vec3 inPos;
|
||||||
|
layout (location = 1) in vec2 inUV;
|
||||||
|
layout (location = 2) in vec3 inNormal;
|
||||||
|
|
||||||
|
layout (binding = 0) uniform UBO
|
||||||
|
{
|
||||||
|
mat4 projection;
|
||||||
|
mat4 model;
|
||||||
|
vec4 viewPos;
|
||||||
|
float lodBias;
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
layout (location = 0) out vec2 outUV;
|
||||||
|
layout (location = 1) out float outLodBias;
|
||||||
|
layout (location = 2) out vec3 outNormal;
|
||||||
|
layout (location = 3) out vec3 outViewVec;
|
||||||
|
layout (location = 4) out vec3 outLightVec;
|
||||||
|
|
||||||
|
out gl_PerVertex
|
||||||
|
{
|
||||||
|
vec4 gl_Position;
|
||||||
|
};
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
outUV = inUV;
|
||||||
|
outLodBias = ubo.lodBias;
|
||||||
|
|
||||||
|
vec3 worldPos = vec3(ubo.model * vec4(inPos, 1.0));
|
||||||
|
|
||||||
|
gl_Position = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0);
|
||||||
|
|
||||||
|
vec4 pos = ubo.model * vec4(inPos, 1.0);
|
||||||
|
outNormal = mat3(inverse(transpose(ubo.model))) * inNormal;
|
||||||
|
vec3 lightPos = vec3(0.0);
|
||||||
|
vec3 lPos = mat3(ubo.model) * lightPos.xyz;
|
||||||
|
outLightVec = lPos - pos.xyz;
|
||||||
|
outViewVec = ubo.viewPos.xyz - pos.xyz;
|
||||||
|
}
|
||||||
BIN
data/shaders/texture/texture.vert.spv
Normal file
BIN
data/shaders/texture/texture.vert.spv
Normal file
Binary file not shown.
|
|
@ -27,6 +27,7 @@
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
float pos[3];
|
float pos[3];
|
||||||
float uv[2];
|
float uv[2];
|
||||||
|
float normal[3];
|
||||||
};
|
};
|
||||||
|
|
||||||
class VulkanExample : public VulkanExampleBase
|
class VulkanExample : public VulkanExampleBase
|
||||||
|
|
@ -65,6 +66,7 @@ public:
|
||||||
struct {
|
struct {
|
||||||
glm::mat4 projection;
|
glm::mat4 projection;
|
||||||
glm::mat4 model;
|
glm::mat4 model;
|
||||||
|
glm::vec4 viewPos;
|
||||||
float lodBias = 0.0f;
|
float lodBias = 0.0f;
|
||||||
} uboVS;
|
} uboVS;
|
||||||
|
|
||||||
|
|
@ -79,7 +81,7 @@ public:
|
||||||
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
|
||||||
{
|
{
|
||||||
zoom = -2.5f;
|
zoom = -2.5f;
|
||||||
rotation = { 45.0f, 0.0f, 0.0f };
|
rotation = { 0.0f, 15.0f, 0.0f };
|
||||||
title = "Vulkan Example - Texturing";
|
title = "Vulkan Example - Texturing";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -537,15 +539,17 @@ public:
|
||||||
void generateQuad()
|
void generateQuad()
|
||||||
{
|
{
|
||||||
// Setup vertices for a single uv-mapped quad
|
// Setup vertices for a single uv-mapped quad
|
||||||
#define dim 1.0f
|
#define DIM 1.0f
|
||||||
|
#define NORMAL { 0.0f, 0.0f, 1.0f }
|
||||||
std::vector<Vertex> vertexBuffer =
|
std::vector<Vertex> vertexBuffer =
|
||||||
{
|
{
|
||||||
{ { dim, dim, 0.0f },{ 1.0f, 1.0f } },
|
{ { DIM, DIM, 0.0f }, { 1.0f, 1.0f }, NORMAL },
|
||||||
{ { -dim, dim, 0.0f },{ 0.0f, 1.0f } },
|
{ { -DIM, DIM, 0.0f }, { 0.0f, 1.0f }, NORMAL },
|
||||||
{ { -dim, -dim, 0.0f },{ 0.0f, 0.0f } },
|
{ { -DIM, -DIM, 0.0f }, { 0.0f, 0.0f }, NORMAL },
|
||||||
{ { dim, -dim, 0.0f },{ 1.0f, 0.0f } }
|
{ { DIM, -DIM, 0.0f }, { 1.0f, 0.0f }, NORMAL }
|
||||||
};
|
};
|
||||||
#undef dim
|
#undef dim
|
||||||
|
#undef normal
|
||||||
createBuffer(
|
createBuffer(
|
||||||
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
|
||||||
vertexBuffer.size() * sizeof(Vertex),
|
vertexBuffer.size() * sizeof(Vertex),
|
||||||
|
|
@ -577,7 +581,7 @@ public:
|
||||||
|
|
||||||
// Attribute descriptions
|
// Attribute descriptions
|
||||||
// Describes memory layout and shader positions
|
// Describes memory layout and shader positions
|
||||||
vertices.attributeDescriptions.resize(2);
|
vertices.attributeDescriptions.resize(3);
|
||||||
// Location 0 : Position
|
// Location 0 : Position
|
||||||
vertices.attributeDescriptions[0] =
|
vertices.attributeDescriptions[0] =
|
||||||
vkTools::initializers::vertexInputAttributeDescription(
|
vkTools::initializers::vertexInputAttributeDescription(
|
||||||
|
|
@ -592,6 +596,13 @@ public:
|
||||||
1,
|
1,
|
||||||
VK_FORMAT_R32G32_SFLOAT,
|
VK_FORMAT_R32G32_SFLOAT,
|
||||||
sizeof(float) * 3);
|
sizeof(float) * 3);
|
||||||
|
// Location 1 : Vertex normal
|
||||||
|
vertices.attributeDescriptions[2] =
|
||||||
|
vkTools::initializers::vertexInputAttributeDescription(
|
||||||
|
VERTEX_BUFFER_BIND_ID,
|
||||||
|
2,
|
||||||
|
VK_FORMAT_R32G32B32_SFLOAT,
|
||||||
|
sizeof(float) * 5);
|
||||||
|
|
||||||
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
vertices.inputState = vkTools::initializers::pipelineVertexInputStateCreateInfo();
|
||||||
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
|
vertices.inputState.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
|
||||||
|
|
@ -737,8 +748,8 @@ public:
|
||||||
// Load shaders
|
// Load shaders
|
||||||
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
|
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
|
||||||
|
|
||||||
shaderStages[0] = loadShader(getAssetPath() + "shaders/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
shaderStages[0] = loadShader(getAssetPath() + "shaders/texture/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
|
||||||
shaderStages[1] = loadShader(getAssetPath() + "shaders/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
shaderStages[1] = loadShader(getAssetPath() + "shaders/texture/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
|
||||||
|
|
||||||
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
|
||||||
vkTools::initializers::pipelineCreateInfo(
|
vkTools::initializers::pipelineCreateInfo(
|
||||||
|
|
@ -778,16 +789,16 @@ public:
|
||||||
void updateUniformBuffers()
|
void updateUniformBuffers()
|
||||||
{
|
{
|
||||||
// Vertex shader
|
// Vertex shader
|
||||||
glm::mat4 viewMatrix = glm::mat4();
|
|
||||||
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
|
uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f);
|
||||||
viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom));
|
glm::mat4 viewMatrix = glm::translate(glm::mat4(), glm::vec3(0.0f, 0.0f, zoom));
|
||||||
|
|
||||||
uboVS.model = glm::mat4();
|
uboVS.model = viewMatrix * glm::translate(glm::mat4(), cameraPos);
|
||||||
uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0));
|
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f));
|
||||||
|
|
||||||
|
uboVS.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f);
|
||||||
|
|
||||||
uint8_t *pData;
|
uint8_t *pData;
|
||||||
VK_CHECK_RESULT(vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
VK_CHECK_RESULT(vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
|
||||||
memcpy(pData, &uboVS, sizeof(uboVS));
|
memcpy(pData, &uboVS, sizeof(uboVS));
|
||||||
|
|
@ -801,8 +812,8 @@ public:
|
||||||
setupVertexDescriptions();
|
setupVertexDescriptions();
|
||||||
prepareUniformBuffers();
|
prepareUniformBuffers();
|
||||||
loadTexture(
|
loadTexture(
|
||||||
getAssetPath() + "textures/igor_and_pal_bc3.ktx",
|
getAssetPath() + "textures/pattern_02_bc2.ktx",
|
||||||
VK_FORMAT_BC3_UNORM_BLOCK,
|
VK_FORMAT_BC2_UNORM_BLOCK,
|
||||||
false);
|
false);
|
||||||
setupDescriptorSetLayout();
|
setupDescriptorSetLayout();
|
||||||
preparePipelines();
|
preparePipelines();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue