Add basic lit shading

This commit is contained in:
illegitimate-egg 2024-10-13 12:17:29 +01:00
parent 752ea5ad4a
commit 8ccbd3926e
28 changed files with 2221 additions and 33 deletions

3
.gitmodules vendored
View File

@ -25,3 +25,6 @@
[submodule "include/glad"]
path = include/glad
url = https://github.com/Dav1dde/glad.git
[submodule "include/imGuIZMO.quat"]
path = include/imGuIZMO.quat
url = https://github.com/BrutPitt/imGuIZMO.quat

View File

@ -30,31 +30,31 @@ if(CMAKE_BINARY_DIR MATCHES " ")
WARNING "Spaces in the build dir can cause errors, thou art been warned\n")
endif()
add_subdirectory(include/glm)
add_subdirectory(include/glfw)
add_subdirectory(include/SOIL2)
add_subdirectory(include/assimp)
add_subdirectory(include/CLog)
add_subdirectory(extern/glm)
add_subdirectory(extern/glfw)
add_subdirectory(extern/SOIL2)
add_subdirectory(extern/assimp)
add_subdirectory(extern/CLog)
# Glad is configured using cmake (unlike glew)
# https://github.com/Dav1dde/glad/wiki/C#cmake
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/include/glad")
set(GLAD_SOURCES_DIR "${PROJECT_SOURCE_DIR}/extern/glad")
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
glad_add_library(glad_gl_core_33 REPRODUCIBLE API gl:core=3.3)
include_directories(include/imgui) # ImGui doesn't have a CMakeLists of its own
include_directories(extern/imgui) # ImGui doesn't have a CMakeLists of its own
add_library(
imgui STATIC
include/imgui/imgui.cpp
include/imgui/imgui_demo.cpp
include/imgui/imgui_draw.cpp
include/imgui/imgui_tables.cpp
include/imgui/imgui_widgets.cpp
include/imgui/backends/imgui_impl_glfw.cpp # Zingaloid backend import
include/imgui/backends/imgui_impl_opengl3.cpp)
extern/imgui/imgui.cpp
extern/imgui/imgui_demo.cpp
extern/imgui/imgui_draw.cpp
extern/imgui/imgui_tables.cpp
extern/imgui/imgui_widgets.cpp
extern/imgui/backends/imgui_impl_glfw.cpp # Zingaloid backend import
extern/imgui/backends/imgui_impl_opengl3.cpp)
target_link_libraries(imgui glfw)
add_executable(fred engine.cpp shader.c)
add_executable(fred src/engine.cpp src/shader.c)
target_link_libraries(fred $<$<PLATFORM_ID:Linux>:-lm> glad_gl_core_33 glm glfw
soil2 assimp imgui clog)

View File

@ -16,7 +16,7 @@ CMake Warning (dev) at include/SOIL2/CMakeLists.txt:67 (install):
Target soil2 has PUBLIC_HEADER files but no PUBLIC_HEADER DESTINATION.
This warning is for project developers. Use -Wno-dev to suppress it.
- [x] Overhaul asset debug screen
- [ ] Simple Lighting
- [x] Simple Lighting
- [ ] Lightmapped Lighting
- [ ] Get ziggy with it
- [x] Implement Cameras

View File

View File

View File

View File

View File

View File

1
extern/imGuIZMO.quat vendored Submodule

@ -0,0 +1 @@
Subproject commit 6c038a90fdadae580b357fbaf26f83cafeb83a6a

View File

12
models/suzanne.mtl Normal file
View File

@ -0,0 +1,12 @@
# Blender 3.6.16 MTL File: 'None'
# www.blender.org
newmtl Material.001
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 1
map_Kd /home/illegitimate-egg/fred/textures/suzanne_albedo.png
map_Ks /home/illegitimate-egg/fred/textures/suzanne_specular.png

2068
models/suzanne.obj Normal file

File diff suppressed because it is too large Load Diff

View File

@ -7,10 +7,10 @@ layout(location = 1) in vec2 vertexUV;
out vec2 UV;
// Model view projection from the CPU
uniform mat4 MVP;
uniform mat4 mvp;
void main() {
gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
gl_Position = mvp * vec4(vertexPosition_modelspace, 1);
//gl_Position = vec4(vertexPosition_modelspace, 1);
vec2 UV_FLIPPED;

34
shaders/basic_lit.frag Normal file
View File

@ -0,0 +1,34 @@
#version 330 core
in vec2 UV;
in vec3 position_worldspace;
in vec3 normal_cameraspace;
in vec3 eyeDirection_cameraspace;
in vec3 lightDirection_cameraspace;
out vec3 color;
uniform sampler2D albedoSampler;
uniform sampler2D specularSampler;
uniform mat4 mv; // mv of mvp (Model, View, Projection)
uniform vec3 lightPosition_worldspace;
uniform vec3 lightColor;
uniform float lightPower;
void main() {
vec3 materialDiffuseColor = texture(albedoSampler, UV).rgb;
vec3 materialAmbientColor = vec3(0.1, 0.1, 0.1) * materialDiffuseColor;
vec3 materialSpecularColor = texture(specularSampler, UV).rgb;
float distance = length(lightPosition_worldspace - position_worldspace);
vec3 n = normalize(normal_cameraspace);
vec3 l = normalize(lightDirection_cameraspace);
float cosTheta = clamp(dot(n, vec3(1.0)), 0, 1);
vec3 E = normalize(eyeDirection_cameraspace);
vec3 R = reflect(vec3(-1.0), n);
float cosAlpha = clamp(dot(E, R), 0, 1);
color = materialAmbientColor + materialDiffuseColor * lightColor * lightPower * cosTheta / (distance*distance) + materialSpecularColor * lightColor * lightPower * pow(cosAlpha, 5) / (distance * distance);
}

32
shaders/basic_lit.vert Normal file
View File

@ -0,0 +1,32 @@
#version 330 core
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNormal_modelspace;
out vec2 UV;
out vec3 position_worldspace;
out vec3 normal_cameraspace;
out vec3 eyeDirection_cameraspace;
out vec3 lightDirection_cameraspace;
uniform mat4 mvp;
uniform mat4 v;
uniform mat4 m;
uniform vec3 lightPosition_worldspace;
void main() {
gl_Position = mvp * vec4(vertexPosition_modelspace, 1);
position_worldspace = (m * vec4(vertexPosition_modelspace, 1)).xyz;
vec3 vertexPosition_cameraspace = (v * m * vec4(vertexPosition_modelspace, 1)).xyz;
eyeDirection_cameraspace = vec3(0, 0, 0) - vertexPosition_cameraspace;
vec3 lightPosition_cameraspace = (v * vec4(lightPosition_worldspace, 1)).xyz;
lightDirection_cameraspace = lightPosition_cameraspace + eyeDirection_cameraspace;
normal_cameraspace = (v * m * vec4(vertexNormal_modelspace, 0)).xyz;
UV = vertexUV;
}

View File

@ -1,4 +1,3 @@
#include <cstddef>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -66,6 +65,10 @@ static bool loadModel(const char *path, std::vector<unsigned short> &indices,
}
normals.reserve(mesh->mNumVertices);
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
aiVector3D normal = mesh->mNormals[i];
normals.push_back(glm::vec3(normal.x, normal.y, normal.z));
}
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
indices.push_back(mesh->mFaces[i].mIndices[0]);
indices.push_back(mesh->mFaces[i].mIndices[1]);
@ -165,29 +168,47 @@ public:
GLuint *elementBuffer;
GLuint matrixID;
GLuint textureID;
GLuint viewMatrixID;
GLuint modelMatrixID;
GLuint albedoTextureID;
GLuint specularTextureID;
GLuint lightID;
GLuint lightColor;
GLuint lightPower;
glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::quat rotation = glm::quat(1.0f, 0.0f, 0.0f, 0.0f); // https://en.wikipedia.org/wiki/Quaternion
glm::vec3 scaling = glm::vec3(1.0f, 1.0f, 1.0f);
GLuint *modelTexture;
GLuint *albedoTexture;
GLuint *specularTexture;
GLuint *shaderProgram;
Asset(Model &model, Texture &texture, Shader &shader) {
Asset(Model &model, Texture &albedoTextureI, Texture &specularTextureI, Shader &shader) {
indices = &model.indices;
vertexBuffer = &model.vertexBuffer;
uvBuffer = &model.uvBuffer;
normalBuffer = &model.normalBuffer;
elementBuffer = &model.elementBuffer;
modelTexture = &texture.texture;
albedoTexture = &albedoTextureI.texture;
specularTexture = &specularTextureI.texture;
shaderProgram = &shader.shaderProgram;
matrixID = glGetUniformLocation(*shaderProgram, "MVP");
textureID = glGetUniformLocation(*shaderProgram, "textureSampler");
matrixID = glGetUniformLocation(*shaderProgram, "mvp");
viewMatrixID = glGetUniformLocation(*shaderProgram, "v");
modelMatrixID = glGetUniformLocation(*shaderProgram, "m");
albedoTextureID = glGetUniformLocation(*shaderProgram, "albedoSampler");
specularTextureID = glGetUniformLocation(*shaderProgram, "specularSampler");
lightID = glGetUniformLocation(*shaderProgram, "lightPosition_worldspace");
lightColor = glGetUniformLocation(*shaderProgram, "lightColor");
lightPower = glGetUniformLocation(*shaderProgram, "lightPower");
}
};
@ -393,11 +414,24 @@ void render(Scene scene) {
ImGui::End();
glUniformMatrix4fv(currentAsset->matrixID, 1, GL_FALSE, &mvp[0][0]);
glUniformMatrix4fv(currentAsset->modelMatrixID, 1, GL_FALSE, &modelMatrix[0][0]);
glUniformMatrix4fv(currentAsset->viewMatrixID, 1, GL_FALSE, &viewMatrix[0][0]);
glm::vec3 lightPos = glm::vec3(4, 4, 4);
glm::vec3 lightColor = glm::vec3(1, 1, 1);
float lightPower = 50;
glUniform3f(currentAsset->lightID, lightPos.x, lightPos.y, lightPos.z);
glUniform3f(currentAsset->lightColor, lightColor.x, lightColor.y, lightColor.z);
glUniform1f(currentAsset->lightPower, lightPower);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, *currentAsset->modelTexture);
glBindTexture(GL_TEXTURE_2D, *currentAsset->albedoTexture);
// Set sampler texture
glUniform1i(currentAsset->textureID, 0);
glUniform1i(currentAsset->albedoTextureID, 0);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, *currentAsset->specularTexture);
glUniform1i(currentAsset->specularTextureID, 1);
// DRAWING HAPPENS HERE
// Vertex Data
@ -452,10 +486,14 @@ void renderCallback() {
int main() {
fred::initWindow();
fred::Model coneModel("../models/model.obj");
fred::Model suzanneMod("../models/suzanne.obj");
fred::Texture buffBlackGuy("../textures/results/texture_BMP_DXT5_3.DDS");
fred::Shader basicShader("../shaders/shader.vert", "../shaders/shader.frag");
fred::Asset cone(coneModel, buffBlackGuy, basicShader);
fred::Asset coneTwo(coneModel, buffBlackGuy, basicShader);
fred::Texture suzanneTexAlb("../textures/results/suzanne_albedo_DXT5.DDS");
fred::Texture suzanneTexSpec("../textures/results/suzanne_specular_DXT5.DDS");
fred::Shader basicShader("../shaders/basic.vert", "../shaders/basic.frag");
fred::Shader basicLitShader("../shaders/basic_lit.vert", "../shaders/basic_lit.frag");
fred::Asset cone(coneModel, buffBlackGuy, buffBlackGuy, basicShader);
fred::Asset suzanne(suzanneMod, suzanneTexAlb, suzanneTexSpec, basicLitShader);
fred::Camera mainCamera(glm::vec3(4, 3, 3));
mainCamera.lookAt(glm::vec3(0, 0, 0));
@ -465,7 +503,7 @@ int main() {
scene.addCamera(mainCamera);
scene.addAsset(cone);
scene.addAsset(coneTwo);
scene.addAsset(suzanne);
scene.setRenderCallback(&renderCallback);
@ -474,9 +512,9 @@ int main() {
while (fred::shouldExit()) {
fred::render(scene);
cone.position.x += 0.01 * fred::getDeltaTime();
glm::vec3 eulerAngles = glm::eulerAngles(coneTwo.rotation);
glm::vec3 eulerAngles = glm::eulerAngles(suzanne.rotation);
eulerAngles.x += glm::radians(1.0f) * fred::getDeltaTime();
coneTwo.rotation = glm::quat(eulerAngles);
suzanne.rotation = glm::quat(eulerAngles);
}
fred::destroy();

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
textures/suzanne_albedo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 248 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 106 KiB