Updated texture example using different texture and lighting (diffuse + specular)

This commit is contained in:
saschawillems 2016-05-14 15:54:20 +02:00
parent a72df2bec9
commit cb96eaf6bd
11 changed files with 101 additions and 59 deletions

View file

@ -4,12 +4,11 @@ if %ERRORLEVEL% EQU 0 (
echo ndk-build has failed, build cancelled
cd..
mkdir "assets\shaders"
xcopy "..\..\data\shaders\texture.vert.spv" "assets\shaders" /Y
xcopy "..\..\data\shaders\texture.frag.spv" "assets\shaders" /Y
mkdir "assets\shaders\texture"
xcopy "..\..\data\shaders\texture\*.spv" "assets\shaders\texture" /Y
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"
xcopy "..\..\android\images\icon.png" "res\drawable" /Y

View file

@ -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.

View file

@ -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.

View 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);
}

Binary file not shown.

View 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;
}

Binary file not shown.

View file

@ -27,6 +27,7 @@
struct Vertex {
float pos[3];
float uv[2];
float normal[3];
};
class VulkanExample : public VulkanExampleBase
@ -65,6 +66,7 @@ public:
struct {
glm::mat4 projection;
glm::mat4 model;
glm::vec4 viewPos;
float lodBias = 0.0f;
} uboVS;
@ -79,7 +81,7 @@ public:
VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION)
{
zoom = -2.5f;
rotation = { 45.0f, 0.0f, 0.0f };
rotation = { 0.0f, 15.0f, 0.0f };
title = "Vulkan Example - Texturing";
}
@ -537,15 +539,17 @@ public:
void generateQuad()
{
// 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 =
{
{ { dim, dim, 0.0f },{ 1.0f, 1.0f } },
{ { -dim, dim, 0.0f },{ 0.0f, 1.0f } },
{ { -dim, -dim, 0.0f },{ 0.0f, 0.0f } },
{ { dim, -dim, 0.0f },{ 1.0f, 0.0f } }
{ { DIM, DIM, 0.0f }, { 1.0f, 1.0f }, NORMAL },
{ { -DIM, DIM, 0.0f }, { 0.0f, 1.0f }, NORMAL },
{ { -DIM, -DIM, 0.0f }, { 0.0f, 0.0f }, NORMAL },
{ { DIM, -DIM, 0.0f }, { 1.0f, 0.0f }, NORMAL }
};
#undef dim
#undef normal
createBuffer(
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT,
vertexBuffer.size() * sizeof(Vertex),
@ -577,7 +581,7 @@ public:
// Attribute descriptions
// Describes memory layout and shader positions
vertices.attributeDescriptions.resize(2);
vertices.attributeDescriptions.resize(3);
// Location 0 : Position
vertices.attributeDescriptions[0] =
vkTools::initializers::vertexInputAttributeDescription(
@ -592,6 +596,13 @@ public:
1,
VK_FORMAT_R32G32_SFLOAT,
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.vertexBindingDescriptionCount = vertices.bindingDescriptions.size();
@ -737,8 +748,8 @@ public:
// Load shaders
std::array<VkPipelineShaderStageCreateInfo,2> shaderStages;
shaderStages[0] = loadShader(getAssetPath() + "shaders/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
shaderStages[0] = loadShader(getAssetPath() + "shaders/texture/texture.vert.spv", VK_SHADER_STAGE_VERTEX_BIT);
shaderStages[1] = loadShader(getAssetPath() + "shaders/texture/texture.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT);
VkGraphicsPipelineCreateInfo pipelineCreateInfo =
vkTools::initializers::pipelineCreateInfo(
@ -778,16 +789,16 @@ public:
void updateUniformBuffers()
{
// Vertex shader
glm::mat4 viewMatrix = glm::mat4();
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(uboVS.model, glm::vec3(0, 0, 0));
uboVS.model = viewMatrix * glm::translate(glm::mat4(), cameraPos);
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.z), glm::vec3(0.0f, 0.0f, 1.0f));
uboVS.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f);
uint8_t *pData;
VK_CHECK_RESULT(vkMapMemory(device, uniformDataVS.memory, 0, sizeof(uboVS), 0, (void **)&pData));
memcpy(pData, &uboVS, sizeof(uboVS));
@ -801,8 +812,8 @@ public:
setupVertexDescriptions();
prepareUniformBuffers();
loadTexture(
getAssetPath() + "textures/igor_and_pal_bc3.ktx",
VK_FORMAT_BC3_UNORM_BLOCK,
getAssetPath() + "textures/pattern_02_bc2.ktx",
VK_FORMAT_BC2_UNORM_BLOCK,
false);
setupDescriptorSetLayout();
preparePipelines();