Skip to content

Commit eb10003

Browse files
committed
Phong illumination
1 parent 32b8ae9 commit eb10003

File tree

5 files changed

+24
-5
lines changed

5 files changed

+24
-5
lines changed

shaders/model_frag.glsl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,28 @@
22

33
layout(location = 0) in vec3 in_normal;
44
layout(location = 1) in vec2 in_texcoord;
5+
layout(location = 2) in vec3 in_position;
56

67
layout(set = 0, binding = 1) uniform sampler2D in_albedoSampledTexture;
78

89
layout(location = 0) out vec4 out_color;
910

1011
void main() {
12+
vec3 view = normalize(-in_position);
13+
1114
vec4 texSample = texture(in_albedoSampledTexture, in_texcoord);
1215
vec3 normal = normalize(in_normal);
16+
17+
vec3 light = normalize(vec3(1, 1, -1));
18+
vec3 reflection = reflect(-light, normal);
19+
float shininess = 16.0f;
20+
21+
vec3 diffuse = max(dot(normal, light), 0.0) * texSample.rgb;
22+
vec3 specular = pow(max(dot(view, reflection), 0.0), shininess) * vec3(1.0);
23+
vec3 ambient = 0.2f * texSample.rgb;
24+
25+
vec3 combined = 0.5 * diffuse + ambient + 0.4 * specular;
26+
1327
//out_color = vec4(normal * 0.5 + 0.5, 1.0);
14-
out_color = texSample;
28+
out_color = vec4(combined, texSample.a);
1529
}

shaders/model_frag.spv

1.36 KB
Binary file not shown.

shaders/model_vert.glsl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ layout(location = 2) in vec2 in_texcoord;
66

77
layout(set = 0, binding = 0) uniform transform {
88
mat4 modelViewProj;
9+
mat4 modelView;
910
} u_transforms;
1011

1112
layout(location = 0) out vec3 out_normal;
1213
layout(location = 1) out vec2 out_texcoord;
14+
layout(location = 2) out vec3 out_position;
1315

1416
void main() {
1517
mat4 modelViewProj = u_transforms.modelViewProj;
1618
gl_Position = modelViewProj * vec4(in_position, 1.0);
17-
out_normal = in_normal;
1819
out_texcoord = in_texcoord;
20+
out_normal = mat3(transpose(inverse(u_transforms.modelView))) * in_normal;
21+
out_position = (u_transforms.modelView * vec4(in_position, 1.0)).xyz;
1922
}

shaders/model_vert.spv

656 Bytes
Binary file not shown.

src/main.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ void initApplication(SDL_Window* window) {
239239
VKA(vkCreateDescriptorPool(context->device, &createInfo, 0, &modelDescriptorPool));
240240
}
241241
for(uint32_t i = 0; i < FRAMES_IN_FLIGHT; ++i) {
242-
createBuffer(context, &modelUniformBuffers[i], sizeof(glm::mat4), VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
242+
createBuffer(context, &modelUniformBuffers[i], sizeof(glm::mat4)*2, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
243243
}
244244
{
245245
VkDescriptorSetLayoutBinding bindings[] = {
@@ -258,7 +258,7 @@ void initApplication(SDL_Window* window) {
258258
allocateInfo.pSetLayouts = &modelDescriptorSetLayout;
259259
VKA(vkAllocateDescriptorSets(context->device, &allocateInfo, &modelDescriptorSets[i]));
260260

261-
VkDescriptorBufferInfo bufferInfo = {modelUniformBuffers[i].buffer, 0, sizeof(glm::mat4)};
261+
VkDescriptorBufferInfo bufferInfo = {modelUniformBuffers[i].buffer, 0, sizeof(glm::mat4)*2};
262262
VkDescriptorImageInfo imageInfo = {sampler, model.albedoTexture.view, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL};
263263
VkWriteDescriptorSet descriptorWrites[2];
264264
descriptorWrites[0] = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
@@ -468,10 +468,12 @@ void renderApplication() {
468468
glm::mat4 modelMatrix = translationMatrix * scaleMatrix * rotationMatrix;
469469

470470
glm::mat4 modelViewProj = camera.viewProj * modelMatrix;
471+
glm::mat4 modelView = camera.view * modelMatrix;
471472

472473
void* mapped;
473-
VK(vkMapMemory(context->device, modelUniformBuffers[frameIndex].memory, 0, sizeof(glm::mat4), 0, &mapped));
474+
VK(vkMapMemory(context->device, modelUniformBuffers[frameIndex].memory, 0, sizeof(glm::mat4)*2, 0, &mapped));
474475
memcpy(mapped, &modelViewProj, sizeof(modelViewProj));
476+
memcpy(((uint8_t*)mapped)+sizeof(glm::mat4), &modelView, sizeof(modelView));
475477
VK(vkUnmapMemory(context->device, modelUniformBuffers[frameIndex].memory));
476478

477479
vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, modelPipeline.pipeline);

0 commit comments

Comments
 (0)