Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple objects #144

Merged
merged 20 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ env:
BUILD_DIR: build
BUILD_TYPE: Release
SCCACHE_GHA_ENABLED: true
VCPKG_GIT_COMMIT_ID: '5a3e638f449206ac10f82f2171a21333004d7306'
VCPKG_GIT_COMMIT_ID: '7fd612ee1c57192d49028ed6a922e5e8bdb257f3'

permissions:
contents: read
Expand Down
2 changes: 0 additions & 2 deletions assets/models/cube/colored_cube.obj
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Blender v2.90.1 OBJ File: ''
# www.blender.org
mtllib untitled.mtl
o Cube
v 1.000000 -1.000000 1.000000 1.0 0.0 0.0
v 1.000000 1.000000 1.000000 0.0 0.0 1.0
Expand Down Expand Up @@ -30,7 +29,6 @@ vn -1.0000 0.0000 0.0000
vn 0.0000 1.0000 0.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
usemtl Material
s off
f 5/1/1 3/2/1 1/3/1
f 3/2/2 8/4/2 4/5/2
Expand Down
17 changes: 17 additions & 0 deletions assets/models/floor/floor.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Blender 4.2.2 LTS MTL File: 'floor.blend'
# www.blender.org

newmtl Material.001
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.500000
d 1.000000
illum 2
Pr 0.500000
Pm 0.000000
Ps 0.000000
Pc 0.000000
Pcr 0.030000
aniso 0.000000
anisor 0.000000
map_Kd wood.png
19 changes: 19 additions & 0 deletions assets/models/floor/floor.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Blender 4.2.2 LTS
# www.blender.org
mtllib floor.mtl
o Plane
v -5.000000 0.000000 5.000000
v 5.000000 0.000000 5.000000
v -5.000000 0.000000 -5.000000
v 5.000000 0.000000 -5.000000
vn -0.0000 1.0000 -0.0000
vt 3.000000 -2.000000
vt -2.000000 3.000000
vt -2.000000 -2.000000
vt 3.000000 3.000000
s 0
g off
g Plane_Material.001
usemtl Material.001
f 2/1/1 3/2/1 1/3/1
f 2/1/1 4/4/1 3/2/1
Binary file added assets/models/floor/wood.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 0 additions & 12 deletions assets/models/quad/quad.obj

This file was deleted.

2 changes: 2 additions & 0 deletions assets/models/sphere/sphere_smooth.mtl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Blender 3.6.2 MTL File: 'None'
# www.blender.org
100 changes: 49 additions & 51 deletions assets/shaders/pbr.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@

layout (location = 0) out vec4 FragColor;

in vec3 gPosition;
in vec2 gTexCoord;
in vec3 gNormal;
in vec3 gColor;
noperspective in vec3 gEdgeDistance;
in vec3 vPosition;
in vec2 vTexCoord;
in vec3 vNormal;
in vec3 vColor;

// material parameters
uniform float metallic;
uniform float roughness;
uniform float ao;

// lights
uniform vec3 lightPos;
uniform vec3 lightColor;
struct PointLight {
vec3 position;
vec3 color;
vec3 attenuation;
};

uniform int numLights = 1;
uniform PointLight pointLights[6];

uniform sampler2D texture_diffuse1;
uniform vec3 viewPos;
uniform float ambientStrength;
uniform bool hasNoTexture;
uniform bool showWireframe;
uniform vec3 lineColor;
uniform float lineWidth;

const float M_PI = 3.14159265359;

// Attenuation intensity; see https://learnopengl.com/Lighting/Light-casters
const float LIGHT_CONSTANT = 1.0;
const float LIGHT_LINEAR = 0.07;
const float LIGHT_QUADRATIC = 0.017;

float distributionGGX(vec3 N, vec3 H, float roughness) {
float a = roughness * roughness;
float a2 = a * a;
Expand Down Expand Up @@ -72,13 +69,14 @@ void main() {
vec3 albedo;

if (hasNoTexture) {
albedo = pow(gColor, vec3(2.2));
// albedo = pow(vColor, vec3(2.2));
albedo = vColor;
} else {
albedo = texture(texture_diffuse1, gTexCoord).rgb;
albedo = texture(texture_diffuse1, vTexCoord).rgb;
}

vec3 N = normalize(gNormal);
vec3 V = normalize(viewPos - gPosition);
vec3 N = normalize(vNormal);
vec3 V = normalize(viewPos - vPosition);

// calculate reflectance at normal incidence; if dia-electric (like plastic) use F0
// of 0.04 and if it's a metal, use the albedo color as F0 (metallic workflow)
Expand All @@ -88,33 +86,40 @@ void main() {
// reflectance equation
vec3 Lo = vec3(0.0);

// calculate per-light radiance
vec3 L = normalize(lightPos - gPosition);
vec3 H = normalize(V + L);
float distance = length(lightPos - gPosition);
float attenuation = 1.0 / (LIGHT_CONSTANT + LIGHT_LINEAR * distance + LIGHT_QUADRATIC * (distance * distance));
vec3 radiance = lightColor * attenuation;

// Cook-Torrance BRDF
float NDF = distributionGGX(N, H, roughness);
float G = geometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

// kS is equal to Fresnel
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= vec3(1.0 - metallic);

vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;

// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);
Lo += (kD * albedo / M_PI + specular) * radiance * NdotL;
for (int i = 0; i < numLights; i++) {
// calculate per-light radiance
vec3 L = normalize(pointLights[i].position - vPosition);
vec3 H = normalize(V + L);
float distance = length(pointLights[i].position - vPosition);
float constant = pointLights[i].attenuation.r;
float linear = pointLights[i].attenuation.g;
float quadratic = pointLights[i].attenuation.b;
float attenuation = 1.0 / (constant + linear * distance + quadratic * (distance * distance));
vec3 radiance = pointLights[i].color * attenuation;

// Cook-Torrance BRDF
float NDF = distributionGGX(N, H, roughness);
float G = geometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);

vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001;
vec3 specular = numerator / denominator;

// kS is equal to Fresnel
vec3 kS = F;
vec3 kD = vec3(1.0) - kS;
kD *= vec3(1.0 - metallic);

// add to outgoing radiance Lo
float NdotL = max(dot(N, L), 0.0);

Lo += (kD * albedo / M_PI + specular) * radiance * NdotL;
}

// ambient
vec3 ambient = vec3(ambientStrength) * albedo * ao;

vec3 color = ambient + Lo;

// Reinhard HDR tonemapping
Expand All @@ -124,12 +129,5 @@ void main() {
float gamma = 2.2;
color = pow(color, vec3(1.0 / gamma));

float mixVal = 1.0;
if (showWireframe) {
float d = min(gEdgeDistance.x, gEdgeDistance.y);
d = min(d, gEdgeDistance.z);
mixVal = smoothstep(lineWidth - 1, lineWidth + 1, d);
}

FragColor = mix(vec4(lineColor, 1.0), vec4(color, 1.0), mixVal);
FragColor = vec4(color, 1.0);
}
72 changes: 0 additions & 72 deletions assets/shaders/pbr.geom.glsl

This file was deleted.

29 changes: 20 additions & 9 deletions game/src/layer/imguilayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@ void ImGuiLayer::onImGuiRender() {

static auto hasVsync = Application::hasVerticalSync();
auto isFullscreen = Application::get().isFullscreen();
auto isWireframeActive = Maze::get().getMazeLayer()->isWireframeActive();

auto metallic = Maze::get().getMazeLayer()->isMetallic();
auto ambientStrength = Maze::get().getMazeLayer()->getAmbientStrength();
auto ambientOcclusion = Maze::get().getMazeLayer()->getAmbientOcclusion();
auto roughness = Maze::get().getMazeLayer()->getRoughness();
auto numLights = Maze::get().getMazeLayer()->getNumLights();
auto attentuation = Maze::get().getMazeLayer()->getAttenuationIndex();

ImGui::SetNextWindowPos({ width - 376.F, 0.F });
ImGui::SetNextWindowSize({ 376.F, 467.F });
ImGui::SetNextWindowSize({ 376.F, 516.F });

constexpr auto windowFlags =
ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings |
Expand Down Expand Up @@ -111,15 +112,25 @@ void ImGuiLayer::onImGuiRender() {
Application::get().toggleFullscreen();
}

ImGui::TableNextRow();
ImGui::TableNextColumn();
ImGui::Text("Show Wireframe");
ImGui::TableNextColumn();
if (ImGui::Checkbox("##wireframe", &isWireframeActive)) {
Maze::get().getMazeLayer()->setWireframeActive(isWireframeActive);
ImGui::EndTable();
ImGui::Separator();

if (ImGui::SliderInt("Lights", &numLights, 1, 6)) {
Maze::get().getMazeLayer()->setNumLights(numLights);
}

ImGui::EndTable();
ImGui::Separator();

if (ImGui::SliderInt("Attentuation", &attentuation, 0, 10)) {
Maze::get().getMazeLayer()->setAttenuationIndex(attentuation);
}

auto attenuation =
Maze::get().getMazeLayer()->getAttenuationValuesFromIndex(
attentuation);
ImGui::Text("Distance: %3.f [%1.1f, %1.3f, %1.4f]", attenuation.x,
attenuation.y, attenuation.z, attenuation.w);

ImGui::Separator();

ImGui::Text("PBR:");
Expand Down
Loading
Loading