Added model loading

This commit is contained in:
Robert 2021-01-30 04:02:15 +01:00
parent 707687b682
commit 8c62929e3c
24 changed files with 199852 additions and 23 deletions

View file

@ -29,6 +29,6 @@ add_custom_command(TARGET model POST_BUILD
if(WIN32)
add_custom_command(TARGET model POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:openglu> $<TARGET_FILE:glfw> $<TARGET_FILE_DIR:model>
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:openglu> $<TARGET_FILE:glfw> $<TARGET_FILE:assimp> $<TARGET_FILE_DIR:model>
)
endif()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

View file

@ -0,0 +1,16 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Scene_-_Root
Ns 225.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.0 0.0 0.0
Ni 1.450000
d 1.000000
illum 2
map_Kd diffuse.jpg
map_Bump normal.png
map_Ks specular.jpg

File diff suppressed because it is too large Load diff

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 MiB

View file

@ -0,0 +1,3 @@
Model by Berk Gedik, from: https://sketchfab.com/3d-models/survival-guitar-backpack-low-poly-799f8c4511f84fab8c3f12887f7e6b36
Modified material assignment (Joey de Vries) for easier load in OpenGL model loading chapter, and renamed albedo to diffuse and metallic to specular to match non-PBR lighting setup.

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.4 MiB

View file

@ -6,9 +6,6 @@
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/string_cast.hpp>
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
bool firstMouse = true;
bool escaped = false;
double lastX = 0.0f;
@ -60,13 +57,13 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.Forward(0.1f);
camera.SetPosition(camera.GetPosition() + 0.1f * glm::normalize(glm::vec3(camera.GetFront().x, 0.0f, camera.GetFront().z)));
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.Sideways(-0.1f);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.Forward(-0.1f);
camera.SetPosition(camera.GetPosition() - 0.1f * glm::normalize(glm::vec3(camera.GetFront().x, 0.0f, camera.GetFront().z)));
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.Sideways(0.1f);
@ -91,7 +88,7 @@ int main(int argc, char** argv)
int windowWidth = (int)(16.f / 9.f * windowHeight);
// Create Window
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "First Person Movement Test", NULL, NULL);
GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "Model loading test", NULL, NULL);
if (window == nullptr)
{
std::cerr << "Failed to create GLFW window" << std::endl;
@ -127,18 +124,38 @@ int main(int argc, char** argv)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
oglu::Color bgColor = oglu::Color::Black;
lightSource.SetPosition(1.0f, 1.0f, -1.0f);
oglu::Model model("assets/backpack.obj");
oglu::SpotLight flashlight;
flashlight.linear = 0.022f;
flashlight.quadratic = 0.0019f;
flashlight.angle = 18.0f;
flashlight.outerAngle = 25.0f;
while (!glfwWindowShouldClose(window))
{
processInput(window);
oglu::ClearScreen(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT, bgColor);
flashlight.SetPosition(camera.GetPosition());
flashlight.direction = camera.GetFront();
shader->Use();
shader->SetUniformMatrix4fv("view", 1, GL_FALSE, glm::value_ptr(camera.GetMatrix()));
shader->SetUniformMatrix4fv("projection", 1, GL_FALSE, glm::value_ptr(camera.GetProjection()));
shader->SetUniform3fv("flashlight.position", 1, flashlight.GetPositionPointer());
shader->SetUniform3fv("flashlight.direction", 1, glm::value_ptr(flashlight.direction));
shader->SetUniform("flashlight.angle", glm::cos(glm::radians(flashlight.angle)));
shader->SetUniform("flashlight.outerAngle", glm::cos(glm::radians(flashlight.outerAngle)));
shader->SetUniform("flashlight.constant", flashlight.constant);
shader->SetUniform("flashlight.linear", flashlight.linear);
shader->SetUniform("flashlight.quadratic", flashlight.quadratic);
model.Render(shader);
glfwSwapBuffers(window);
glfwPollEvents();

View file

@ -1,8 +1,46 @@
#version 330 core
struct Material
{
sampler2D tex_diffuse1;
sampler2D tex_specular1;
};
vec4 out FragColor;
struct SpotLight
{
vec3 position, direction;
float angle, outerAngle;
float constant, linear, quadratic;
};
in vec3 oNormal;
in vec2 oUV;
in vec3 oFragPos;
out vec4 FragColor;
uniform Material material;
uniform SpotLight flashlight;
void main()
{
FragColor = vec4(0.0);
vec3 lightDir = normalize(flashlight.position - oFragPos);
float theta = dot(lightDir, normalize(-flashlight.direction));
float epsilon = flashlight.angle - flashlight.outerAngle;
float intensity = clamp((theta - flashlight.outerAngle) / epsilon, 0.0, 1.0);
float diff = max(dot(oNormal, lightDir), 0.0);
vec3 reflectDir = reflect(-lightDir, oNormal);
float spec = pow(max(dot(lightDir, reflectDir), 0.0), 32.0);
float dist = length(flashlight.position - oFragPos);
float attenuation = 1.0 / (flashlight.constant + flashlight.linear * dist + flashlight.quadratic * dist * dist);
vec3 diffuse = vec3(1.0) * diff * vec3(texture(material.tex_diffuse1, oUV));
vec3 specular = vec3(1.0) * spec * vec3(texture(material.tex_specular1, oUV));
vec3 ambient = vec3(0.1) * vec3(texture(material.tex_diffuse1, oUV));
FragColor = vec4(ambient + (diffuse + specular) * intensity * attenuation, 1.0);
// FragColor = vec4(vec3(texture(material.tex_diffuse1, oUV)), 1.0);
}

View file

@ -1,6 +1,21 @@
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aNormal;
layout (location = 2) in vec2 aUV;
out vec3 oNormal;
out vec2 oUV;
out vec3 oFragPos;
uniform mat4 view;
uniform mat4 projection;
void main()
{
gl_Position = vec4(0.0);
oNormal = aNormal;
oUV = aUV;
gl_Position = projection * view * vec4(aPos, 1.0);
oFragPos = aPos;
}