Implemented basic normal-dot product shading, preparing for more complex methods such as Blinn-Phong and PBR

This commit is contained in:
Lillian Salehi 2024-12-04 23:11:15 -06:00
parent c42133f426
commit 454b8e6c28
14 changed files with 2140 additions and 24 deletions

View File

@ -0,0 +1,2 @@
# Blender 4.2.3 LTS MTL File: 'None'
# www.blender.org

2070
assets/models/UVSphere.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,2 @@
# Blender 4.2.3 LTS MTL File: 'None'
# www.blender.org
newmtl Material
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2
map_Kd /home/lillian/Downloads/d41777258e68c9d7046fc03c4d1d1e89.png

View File

@ -0,0 +1,39 @@
# Blender 4.2.3 LTS
# www.blender.org
mtllib untitled.mtl
o Cube
v -6.996062 0.060240 6.002291
v -6.996062 0.123414 6.002291
v -6.996062 0.060240 -6.002291
v -6.996062 0.123414 -6.002291
v 6.996062 0.060240 6.002291
v 6.996062 0.123414 6.002291
v 6.996062 0.060240 -6.002291
v 6.996062 0.123414 -6.002291
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -0.0000 -1.0000 -0.0000
vn -0.0000 1.0000 -0.0000
vt 0.561014 0.000000
vt 0.625000 0.000000
vt 0.625000 0.250000
vt 0.561014 0.250000
vt 0.625000 0.500000
vt 0.561014 0.500000
vt 0.625000 0.750000
vt 0.561014 0.750000
vt 0.625000 1.000000
vt 0.561014 1.000000
vt 0.125000 0.500000
vt 0.125000 0.750000
vt 0.875000 0.500000
vt 0.875000 0.750000
s 0
f 1/1/1 2/2/1 4/3/1 3/4/1
f 3/4/2 4/3/2 8/5/2 7/6/2
f 7/6/3 8/5/3 6/7/3 5/8/3
f 5/8/4 6/7/4 2/9/4 1/10/4
f 3/11/5 7/6/5 5/8/5 1/12/5
f 8/5/6 4/13/6 2/14/6 6/7/6

View File

@ -3,6 +3,6 @@ Pos=60,60
Size=400,400
[Window][Agnosia Debug]
Pos=59,322
Pos=57,392
Size=623,438

View File

@ -23,8 +23,8 @@ void initImGuiWindow() {
}
ImGui::DragFloat3("Camera Position", Graphics::getCamPos());
ImGui::DragFloat3("Light Position", Graphics::getLightPos());
ImGui::DragFloat3("Center Position", Graphics::getCenterPos());
ImGui::DragFloat3("Up Direction", Graphics::getUpDir());
ImGui::DragFloat("Depth of Field", &Graphics::getDepthField(), 0.1f, 1.0f,
180.0f, NULL, ImGuiSliderFlags_AlwaysClamp);
ImGui::DragFloat2("Near and Far fields", Graphics::getDistanceField());

View File

@ -88,15 +88,15 @@ void createInstance() {
}
}
void initAgnosia() {
Material *vikingRoomMaterial =
new Material("vikingRoomMaterial", "assets/textures/viking_room.png");
Material *sphereMaterial =
new Material("sphereMaterial", "assets/textures/checkermap.png");
Material *stanfordDragonMaterial =
new Material("stanfordDragonMaterial", "assets/textures/checkermap.png");
Material *teapotMaterial =
new Material("teapotMaterial", "assets/textures/checkermap.png");
Model *vikingRoom =
new Model("vikingRoom", *vikingRoomMaterial,
"assets/models/viking_room.obj", glm::vec3(0.0f, 0.0f, 0.0f));
Model *uvSphere =
new Model("uvSphere", *sphereMaterial, "assets/models/UVSphere.obj",
glm::vec3(0.0f, 0.0f, 0.0f));
Model *stanfordDragon = new Model("stanfordDragon", *stanfordDragonMaterial,
"assets/models/StanfordDragon800k.obj",
glm::vec3(0.0f, 2.0f, 0.0f));

View File

@ -13,6 +13,7 @@
#include <iostream>
#include <vulkan/vulkan_core.h>
float lightPos[4] = {5.0f, 5.0f, 5.0f, 0.44f};
float camPos[4] = {3.0f, 3.0f, 3.0f, 0.44f};
float centerPos[4] = {0.0f, 0.0f, 0.0f, 0.44f};
float upDir[4] = {0.0f, 0.0f, 1.0f, 0.44f};
@ -363,6 +364,7 @@ void Graphics::recordCommandBuffer(VkCommandBuffer commandBuffer,
Agnosia_T::GPUPushConstants pushConsts;
pushConsts.vertexBuffer = model->getBuffers().vertexBufferAddress;
pushConsts.objPosition = model->getPos();
pushConsts.lightPos = glm::vec3(lightPos[0], lightPos[1], lightPos[2]);
pushConsts.textureID = texID;
pushConsts.model =
@ -433,6 +435,7 @@ void Graphics::recordCommandBuffer(VkCommandBuffer commandBuffer,
}
float *Graphics::getCamPos() { return camPos; }
float *Graphics::getLightPos() { return lightPos; }
float *Graphics::getCenterPos() { return centerPos; }
float *Graphics::getUpDir() { return upDir; }
float &Graphics::getDepthField() { return depthField; }

View File

@ -15,6 +15,7 @@ public:
uint32_t imageIndex);
static float *getCamPos();
static float *getLightPos();
static float *getCenterPos();
static float *getUpDir();
static float &getDepthField();

View File

@ -15,6 +15,7 @@ layout(buffer_reference, scalar) readonly buffer VertexBuffer{
layout( push_constant, scalar ) uniform constants {
VertexBuffer vertBuffer;
vec3 objPos;
vec3 lightPos;
int textureID;
mat4 model;
mat4 view;

View File

@ -5,11 +5,18 @@
layout(binding = 1) uniform sampler2D texSampler[];
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 0) in vec3 v_norm;
layout(location = 1) in vec3 v_pos;
layout(location = 2) in vec2 texCoord;
layout(location = 0) out vec4 outColor;
void main() {
outColor = texture(texSampler[PushConstants.textureID], fragTexCoord) * vec4(fragColor, 1.0f);
vec3 diffuseColor = texture(texSampler[PushConstants.textureID], texCoord).rgb;
vec3 ambientColor = vec3(0.05f,0.05f, 0.05f) * diffuseColor;
float lightPower = 5;
vec3 lightColor = vec3(1.0f, 1.0f, 1.0f);
float cosTheta = dot(PushConstants.lightPos, v_norm);
float sqrDist = distance(v_pos, PushConstants.lightPos)*distance(v_pos, PushConstants.lightPos);
outColor = vec4(ambientColor + clamp(diffuseColor * lightColor * lightPower * cosTheta / sqrDist, vec3(0,0,0), vec3(1,1,1)), 1.0f);
}

View File

@ -2,14 +2,16 @@
#extension GL_GOOGLE_include_directive : enable
#include "common.glsl"
layout(location = 0) out vec3 fragColor;
layout(location = 1) out vec2 fragTexCoord;
layout(location = 0) out vec3 v_norm;
layout(location = 1) out vec3 v_pos;
layout(location = 2) out vec2 texCoord;
void main() {
Vertex vertex = PushConstants.vertBuffer.vertices[gl_VertexIndex];
gl_Position = PushConstants.proj * PushConstants.view * PushConstants.model *
vec4(vertex.pos + PushConstants.objPos, 1.0f);
fragColor = vertex.color.rgb;
fragTexCoord = vertex.texCoord;
v_norm = vertex.normal;
v_pos = vertex.pos;
texCoord = vertex.texCoord;
}

View File

@ -32,6 +32,7 @@ public:
struct GPUPushConstants {
VkDeviceAddress vertexBuffer;
glm::vec3 objPosition;
glm::vec3 lightPos;
int textureID;
glm::mat4 model;
glm::mat4 view;