Add basic lit shading
This commit is contained in:
parent
752ea5ad4a
commit
8ccbd3926e
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -25,3 +25,6 @@
|
|||||||
[submodule "include/glad"]
|
[submodule "include/glad"]
|
||||||
path = include/glad
|
path = include/glad
|
||||||
url = https://github.com/Dav1dde/glad.git
|
url = https://github.com/Dav1dde/glad.git
|
||||||
|
[submodule "include/imGuIZMO.quat"]
|
||||||
|
path = include/imGuIZMO.quat
|
||||||
|
url = https://github.com/BrutPitt/imGuIZMO.quat
|
||||||
|
@ -30,31 +30,31 @@ if(CMAKE_BINARY_DIR MATCHES " ")
|
|||||||
WARNING "Spaces in the build dir can cause errors, thou art been warned\n")
|
WARNING "Spaces in the build dir can cause errors, thou art been warned\n")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(include/glm)
|
add_subdirectory(extern/glm)
|
||||||
add_subdirectory(include/glfw)
|
add_subdirectory(extern/glfw)
|
||||||
add_subdirectory(include/SOIL2)
|
add_subdirectory(extern/SOIL2)
|
||||||
add_subdirectory(include/assimp)
|
add_subdirectory(extern/assimp)
|
||||||
add_subdirectory(include/CLog)
|
add_subdirectory(extern/CLog)
|
||||||
|
|
||||||
# Glad is configured using cmake (unlike glew)
|
# Glad is configured using cmake (unlike glew)
|
||||||
# https://github.com/Dav1dde/glad/wiki/C#cmake
|
# 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)
|
add_subdirectory("${GLAD_SOURCES_DIR}/cmake" glad_cmake)
|
||||||
|
|
||||||
glad_add_library(glad_gl_core_33 REPRODUCIBLE API gl:core=3.3)
|
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(
|
add_library(
|
||||||
imgui STATIC
|
imgui STATIC
|
||||||
include/imgui/imgui.cpp
|
extern/imgui/imgui.cpp
|
||||||
include/imgui/imgui_demo.cpp
|
extern/imgui/imgui_demo.cpp
|
||||||
include/imgui/imgui_draw.cpp
|
extern/imgui/imgui_draw.cpp
|
||||||
include/imgui/imgui_tables.cpp
|
extern/imgui/imgui_tables.cpp
|
||||||
include/imgui/imgui_widgets.cpp
|
extern/imgui/imgui_widgets.cpp
|
||||||
include/imgui/backends/imgui_impl_glfw.cpp # Zingaloid backend import
|
extern/imgui/backends/imgui_impl_glfw.cpp # Zingaloid backend import
|
||||||
include/imgui/backends/imgui_impl_opengl3.cpp)
|
extern/imgui/backends/imgui_impl_opengl3.cpp)
|
||||||
target_link_libraries(imgui glfw)
|
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
|
target_link_libraries(fred $<$<PLATFORM_ID:Linux>:-lm> glad_gl_core_33 glm glfw
|
||||||
soil2 assimp imgui clog)
|
soil2 assimp imgui clog)
|
||||||
|
2
TODO.md
2
TODO.md
@ -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.
|
Target soil2 has PUBLIC_HEADER files but no PUBLIC_HEADER DESTINATION.
|
||||||
This warning is for project developers. Use -Wno-dev to suppress it.
|
This warning is for project developers. Use -Wno-dev to suppress it.
|
||||||
- [x] Overhaul asset debug screen
|
- [x] Overhaul asset debug screen
|
||||||
- [ ] Simple Lighting
|
- [x] Simple Lighting
|
||||||
- [ ] Lightmapped Lighting
|
- [ ] Lightmapped Lighting
|
||||||
- [ ] Get ziggy with it
|
- [ ] Get ziggy with it
|
||||||
- [x] Implement Cameras
|
- [x] Implement Cameras
|
||||||
|
0
include/CLog → extern/CLog
vendored
0
include/CLog → extern/CLog
vendored
0
include/SOIL2 → extern/SOIL2
vendored
0
include/SOIL2 → extern/SOIL2
vendored
0
include/assimp → extern/assimp
vendored
0
include/assimp → extern/assimp
vendored
0
include/glad → extern/glad
vendored
0
include/glad → extern/glad
vendored
0
include/glfw → extern/glfw
vendored
0
include/glfw → extern/glfw
vendored
0
include/glm → extern/glm
vendored
0
include/glm → extern/glm
vendored
1
extern/imGuIZMO.quat
vendored
Submodule
1
extern/imGuIZMO.quat
vendored
Submodule
@ -0,0 +1 @@
|
|||||||
|
Subproject commit 6c038a90fdadae580b357fbaf26f83cafeb83a6a
|
0
include/imgui → extern/imgui
vendored
0
include/imgui → extern/imgui
vendored
12
models/suzanne.mtl
Normal file
12
models/suzanne.mtl
Normal 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
2068
models/suzanne.obj
Normal file
File diff suppressed because it is too large
Load Diff
@ -7,10 +7,10 @@ layout(location = 1) in vec2 vertexUV;
|
|||||||
out vec2 UV;
|
out vec2 UV;
|
||||||
|
|
||||||
// Model view projection from the CPU
|
// Model view projection from the CPU
|
||||||
uniform mat4 MVP;
|
uniform mat4 mvp;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
gl_Position = MVP * vec4(vertexPosition_modelspace, 1);
|
gl_Position = mvp * vec4(vertexPosition_modelspace, 1);
|
||||||
//gl_Position = vec4(vertexPosition_modelspace, 1);
|
//gl_Position = vec4(vertexPosition_modelspace, 1);
|
||||||
|
|
||||||
vec2 UV_FLIPPED;
|
vec2 UV_FLIPPED;
|
34
shaders/basic_lit.frag
Normal file
34
shaders/basic_lit.frag
Normal 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
32
shaders/basic_lit.vert
Normal 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;
|
||||||
|
}
|
@ -1,4 +1,3 @@
|
|||||||
#include <cstddef>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -66,6 +65,10 @@ static bool loadModel(const char *path, std::vector<unsigned short> &indices,
|
|||||||
}
|
}
|
||||||
|
|
||||||
normals.reserve(mesh->mNumVertices);
|
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++) {
|
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
||||||
indices.push_back(mesh->mFaces[i].mIndices[0]);
|
indices.push_back(mesh->mFaces[i].mIndices[0]);
|
||||||
indices.push_back(mesh->mFaces[i].mIndices[1]);
|
indices.push_back(mesh->mFaces[i].mIndices[1]);
|
||||||
@ -165,29 +168,47 @@ public:
|
|||||||
GLuint *elementBuffer;
|
GLuint *elementBuffer;
|
||||||
|
|
||||||
GLuint matrixID;
|
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::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::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);
|
glm::vec3 scaling = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||||
|
|
||||||
GLuint *modelTexture;
|
GLuint *albedoTexture;
|
||||||
|
GLuint *specularTexture;
|
||||||
|
|
||||||
GLuint *shaderProgram;
|
GLuint *shaderProgram;
|
||||||
|
|
||||||
Asset(Model &model, Texture &texture, Shader &shader) {
|
Asset(Model &model, Texture &albedoTextureI, Texture &specularTextureI, Shader &shader) {
|
||||||
indices = &model.indices;
|
indices = &model.indices;
|
||||||
vertexBuffer = &model.vertexBuffer;
|
vertexBuffer = &model.vertexBuffer;
|
||||||
uvBuffer = &model.uvBuffer;
|
uvBuffer = &model.uvBuffer;
|
||||||
normalBuffer = &model.normalBuffer;
|
normalBuffer = &model.normalBuffer;
|
||||||
elementBuffer = &model.elementBuffer;
|
elementBuffer = &model.elementBuffer;
|
||||||
|
|
||||||
modelTexture = &texture.texture;
|
albedoTexture = &albedoTextureI.texture;
|
||||||
|
specularTexture = &specularTextureI.texture;
|
||||||
|
|
||||||
shaderProgram = &shader.shaderProgram;
|
shaderProgram = &shader.shaderProgram;
|
||||||
|
|
||||||
matrixID = glGetUniformLocation(*shaderProgram, "MVP");
|
matrixID = glGetUniformLocation(*shaderProgram, "mvp");
|
||||||
textureID = glGetUniformLocation(*shaderProgram, "textureSampler");
|
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();
|
ImGui::End();
|
||||||
|
|
||||||
glUniformMatrix4fv(currentAsset->matrixID, 1, GL_FALSE, &mvp[0][0]);
|
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);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, *currentAsset->modelTexture);
|
glBindTexture(GL_TEXTURE_2D, *currentAsset->albedoTexture);
|
||||||
// Set sampler texture
|
// 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
|
// DRAWING HAPPENS HERE
|
||||||
// Vertex Data
|
// Vertex Data
|
||||||
@ -452,10 +486,14 @@ void renderCallback() {
|
|||||||
int main() {
|
int main() {
|
||||||
fred::initWindow();
|
fred::initWindow();
|
||||||
fred::Model coneModel("../models/model.obj");
|
fred::Model coneModel("../models/model.obj");
|
||||||
|
fred::Model suzanneMod("../models/suzanne.obj");
|
||||||
fred::Texture buffBlackGuy("../textures/results/texture_BMP_DXT5_3.DDS");
|
fred::Texture buffBlackGuy("../textures/results/texture_BMP_DXT5_3.DDS");
|
||||||
fred::Shader basicShader("../shaders/shader.vert", "../shaders/shader.frag");
|
fred::Texture suzanneTexAlb("../textures/results/suzanne_albedo_DXT5.DDS");
|
||||||
fred::Asset cone(coneModel, buffBlackGuy, basicShader);
|
fred::Texture suzanneTexSpec("../textures/results/suzanne_specular_DXT5.DDS");
|
||||||
fred::Asset coneTwo(coneModel, buffBlackGuy, basicShader);
|
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));
|
fred::Camera mainCamera(glm::vec3(4, 3, 3));
|
||||||
mainCamera.lookAt(glm::vec3(0, 0, 0));
|
mainCamera.lookAt(glm::vec3(0, 0, 0));
|
||||||
@ -465,7 +503,7 @@ int main() {
|
|||||||
scene.addCamera(mainCamera);
|
scene.addCamera(mainCamera);
|
||||||
|
|
||||||
scene.addAsset(cone);
|
scene.addAsset(cone);
|
||||||
scene.addAsset(coneTwo);
|
scene.addAsset(suzanne);
|
||||||
|
|
||||||
scene.setRenderCallback(&renderCallback);
|
scene.setRenderCallback(&renderCallback);
|
||||||
|
|
||||||
@ -474,9 +512,9 @@ int main() {
|
|||||||
while (fred::shouldExit()) {
|
while (fred::shouldExit()) {
|
||||||
fred::render(scene);
|
fred::render(scene);
|
||||||
cone.position.x += 0.01 * fred::getDeltaTime();
|
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();
|
eulerAngles.x += glm::radians(1.0f) * fred::getDeltaTime();
|
||||||
coneTwo.rotation = glm::quat(eulerAngles);
|
suzanne.rotation = glm::quat(eulerAngles);
|
||||||
}
|
}
|
||||||
|
|
||||||
fred::destroy();
|
fred::destroy();
|
BIN
textures/results/suzanne_albedo_DXT1.DDS
Normal file
BIN
textures/results/suzanne_albedo_DXT1.DDS
Normal file
Binary file not shown.
BIN
textures/results/suzanne_albedo_DXT3.DDS
Normal file
BIN
textures/results/suzanne_albedo_DXT3.DDS
Normal file
Binary file not shown.
BIN
textures/results/suzanne_albedo_DXT5.DDS
Normal file
BIN
textures/results/suzanne_albedo_DXT5.DDS
Normal file
Binary file not shown.
BIN
textures/results/suzanne_specular_DXT1.DDS
Normal file
BIN
textures/results/suzanne_specular_DXT1.DDS
Normal file
Binary file not shown.
BIN
textures/results/suzanne_specular_DXT3.DDS
Normal file
BIN
textures/results/suzanne_specular_DXT3.DDS
Normal file
Binary file not shown.
BIN
textures/results/suzanne_specular_DXT5.DDS
Normal file
BIN
textures/results/suzanne_specular_DXT5.DDS
Normal file
Binary file not shown.
BIN
textures/suzanne_albedo.png
Normal file
BIN
textures/suzanne_albedo.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 248 KiB |
BIN
textures/suzanne_specular.png
Normal file
BIN
textures/suzanne_specular.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 106 KiB |
Loading…
Reference in New Issue
Block a user