From 849d0a78f21ccfcff608b258b99b046f00c3fb81 Mon Sep 17 00:00:00 2001 From: alistair Date: Tue, 30 Nov 2021 02:42:51 +1000 Subject: [PATCH] loading --- CMakeLists.txt | 2 + data/levels/init.json | 5 +- data/shaders/blend.frag | 62 +++++++++++++ data/shaders/instanced.vert | 49 +++++++++++ data/shaders/normaldip.frag | 7 ++ data/shaders/normaldip.geom | 28 ++++++ data/shaders/screentexture.frag | 21 +++++ data/shaders/screentexture.vert | 11 +++ data/shaders/sky.frag | 12 +++ data/shaders/sky.vert | 18 ++++ data/shaders/test.frag | 151 ++++++++++++++++++++++++++++++++ data/shaders/test.vert | 42 +++++++++ src/mesh.cpp | 5 +- 13 files changed, 410 insertions(+), 3 deletions(-) create mode 100644 data/shaders/blend.frag create mode 100644 data/shaders/instanced.vert create mode 100644 data/shaders/normaldip.frag create mode 100644 data/shaders/normaldip.geom create mode 100644 data/shaders/screentexture.frag create mode 100644 data/shaders/screentexture.vert create mode 100644 data/shaders/sky.frag create mode 100644 data/shaders/sky.vert create mode 100644 data/shaders/test.frag create mode 100644 data/shaders/test.vert diff --git a/CMakeLists.txt b/CMakeLists.txt index 47ef088..b00734d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,8 @@ set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fs set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address") set (CMAKE_BINARY_DIR .) +include_directories(lib/nlohmann_json/include) + add_subdirectory(lib/nlohmann_json) add_subdirectory(lib/fmt) diff --git a/data/levels/init.json b/data/levels/init.json index cd0c0d1..9d45d99 100644 --- a/data/levels/init.json +++ b/data/levels/init.json @@ -4,6 +4,9 @@ "backpack": { "object": "backpack.obj", "textures": ["ao.jpg", "diffuse.jpg", "roughness.jpg", "normal.jpg", "specular.jpg"] + }, + "unnamed_6": { + "object": "unnamed_6.obj" } }, "shaders": [ @@ -36,7 +39,7 @@ ], "entities": [ { - "draw": {"model": "backpack"}, + "draw": {"model": "unnamed_6"}, "physics": { "position": [1,4,1], "orientation": [0,2,0] diff --git a/data/shaders/blend.frag b/data/shaders/blend.frag new file mode 100644 index 0000000..24982ff --- /dev/null +++ b/data/shaders/blend.frag @@ -0,0 +1,62 @@ +#version 330 core + +out vec4 LFragment; + +in vec2 theTexCoord; +in vec3 Normal; +in vec3 FragPos; + +//uniform vec3 objectColour; +//uniform vec3 lightColour; +uniform int lighting_emit; + +//uniform vec3 lightPosition; +uniform vec3 cameraPosition; +uniform sampler2D texture_diffuse1; + +struct Material { + sampler2D texture_diffuse1; + sampler2D texture_diffuse2; + sampler2D texture_diffuse3; + sampler2D texture_specular1; + sampler2D texture_specular2; + sampler2D texture_specular3; + vec3 ambient; + vec3 diffuse; + vec3 specular; + float shininess; +}; + +#define DIRECTIONAL_LIGHT 0 +#define POINT_LIGHT 1 +#define SPOT_LIGHT 2 + +struct Light { + int type; + vec3 direction; + vec3 position; + float angle_cutoff; + float outer_angle_cutoff; + vec3 ambient; + vec3 diffuse; + vec3 specular; + bool attenuate; + vec3 attenuation; +}; + +#define MAX_LIGHTS 10 + +uniform Light lights[MAX_LIGHTS]; +uniform int num_lights; + +uniform Material material; +// uniform Light light; + +void main() +{ + + LFragment = vec4(texture(material.texture_diffuse1, theTexCoord)); + if (LFragment.a < 0.1) + discard; +} + diff --git a/data/shaders/instanced.vert b/data/shaders/instanced.vert new file mode 100644 index 0000000..d8b07be --- /dev/null +++ b/data/shaders/instanced.vert @@ -0,0 +1,49 @@ +#version 330 core + +layout (location = 0) in vec3 LVertexPos2D; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; +layout (location = 3) in vec3 offset; +layout (location = 4) in vec3 aTangent; +layout (location = 5) in mat4 modelInst; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec3 Normal; +out vec3 FragPos; +out vec3 theTexCoord; +out mat3 TBN; + +void main() +{ + vec4 pos = (vec4( LVertexPos2D, 1.0) + vec4(offset, 0)); + + vec4 ppos = projection * view * model * pos; + +// ps1 jiggle + vec2 resolution = vec2(100,75); + vec4 snapped = ppos; + snapped.xyz = ppos.xyz / ppos.w; + snapped.xy = floor(resolution * snapped.xy) / resolution; + snapped.xyz *= ppos.w; + +// use gl_Position = ppos; to disable jiggle + gl_Position = snapped; + + theTexCoord = vec3(aTexCoords, 1.0); + Normal = mat3(transpose(inverse(model))) * aNormal; + FragPos = vec3((model) * pos); + + // calculating TBN + vec3 T = normalize(vec3(model * vec4(aTangent, 0.0))); + vec3 N = normalize(vec3(model * vec4(aNormal, 0.0))); + + T = normalize(T - dot(T, N) * N); + vec3 B = cross(N,T); + + TBN = mat3(T, B, N); + + +} diff --git a/data/shaders/normaldip.frag b/data/shaders/normaldip.frag new file mode 100644 index 0000000..c7cec41 --- /dev/null +++ b/data/shaders/normaldip.frag @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0, 1.0, 0.0, 1.0); +} diff --git a/data/shaders/normaldip.geom b/data/shaders/normaldip.geom new file mode 100644 index 0000000..974f13b --- /dev/null +++ b/data/shaders/normaldip.geom @@ -0,0 +1,28 @@ + +#version 330 core +layout (triangles) in; +layout (line_strip, max_vertices = 6) out; + +in vec3 Normal[]; +in vec3 FragPos[]; + +const float MAGNITUDE = 0.4; + +uniform mat4 projection; + +void GenerateLine(int index) +{ + gl_Position = projection * FragPos[index]; + EmitVertex(); + gl_Position = projection * (FragPos[index] + + (Normal[index], 0.0) * MAGNITUDE); + EmitVertex(); + EndPrimitive(); +} + +void main() +{ + GenerateLine(0); // first vertex normal + GenerateLine(1); // second vertex normal + GenerateLine(2); // third vertex normal +} diff --git a/data/shaders/screentexture.frag b/data/shaders/screentexture.frag new file mode 100644 index 0000000..ca9e47b --- /dev/null +++ b/data/shaders/screentexture.frag @@ -0,0 +1,21 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D screenTexture; + +const float gamma = 2.2; +uniform float exposure = 0.8; + +void main() +{ + vec3 hdrcol = texture(screenTexture, TexCoords).rgb; + vec3 mapped = vec3(1.0) - exp((-hdrcol) * exposure); + //vec3 mapped = hdrcol / (hdrcol + vec3(1.0)); + // gamma correction + mapped = pow(mapped, vec3(1.0 / gamma)); + + FragColor = vec4(mapped, 1.0); + +} diff --git a/data/shaders/screentexture.vert b/data/shaders/screentexture.vert new file mode 100644 index 0000000..ce58e36 --- /dev/null +++ b/data/shaders/screentexture.vert @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec2 aPos; +layout (location = 1) in vec2 aTexCoords; + +out vec2 TexCoords; + +void main() +{ + gl_Position = vec4(aPos.x, aPos.y, 0.0, 1.0); + TexCoords = aTexCoords; +} diff --git a/data/shaders/sky.frag b/data/shaders/sky.frag new file mode 100644 index 0000000..4df2b5d --- /dev/null +++ b/data/shaders/sky.frag @@ -0,0 +1,12 @@ +#version 330 core +out vec4 FragColor; + +in vec3 TexCoords; + +uniform samplerCube skybox; + +void main() +{ +// FragColor = vec4(0.0,0.0,0.0,1.1); + FragColor = texture(skybox, TexCoords); +} diff --git a/data/shaders/sky.vert b/data/shaders/sky.vert new file mode 100644 index 0000000..ff3d182 --- /dev/null +++ b/data/shaders/sky.vert @@ -0,0 +1,18 @@ + +#version 330 core +layout (location = 0) in vec3 LVertexPos2D; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; +layout (location = 4) in vec2 aTangent; + +out vec3 TexCoords; + +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + TexCoords = LVertexPos2D; + vec4 pos = projection * view * vec4( LVertexPos2D, 1.0); + gl_Position = pos.xyww; +} diff --git a/data/shaders/test.frag b/data/shaders/test.frag new file mode 100644 index 0000000..aa29b5c --- /dev/null +++ b/data/shaders/test.frag @@ -0,0 +1,151 @@ +#version 330 core + +out vec4 LFragment; + +in vec3 Normal; +in vec3 FragPos; +in mat3 TBN; +in vec3 theTexCoord; + +uniform vec3 cameraPosition; +uniform samplerCube skybox; + + +float gamma = 2.2; + +struct Material { + sampler2D texture_diffuse1; + sampler2D texture_diffuse2; + sampler2D texture_diffuse3; + float shininess; +}; + +#define DIRECTIONAL_LIGHT 0 +#define POINT_LIGHT 1 +#define SPOT_LIGHT 2 + + +struct Light { + int type; + vec3 direction; + vec3 position; + float angle_cutoff; + float outer_angle_cutoff; + vec3 ambient; + vec3 diffuse; + vec3 specular; + bool attenuate; + vec3 attenuation; +}; + +#define MAX_LIGHTS 10 + +uniform Light lights[MAX_LIGHTS]; +uniform int num_lights; + +uniform Material material; + +vec4 +reflection(vec3 norm) +{ + vec3 reflectiveness = material.specular * material.shininess / 50; + vec3 I = normalize(FragPos - cameraPosition); + vec3 R = reflect(I, norm); + return texture(skybox, R) * (material.shininess / 90); +} + +float +spotlight(int i, vec3 lightDir) +{ + + float theta = dot(lightDir, normalize(-lights[i].direction)); + float epsilon = lights[i].angle_cutoff - lights[i].outer_angle_cutoff; + float intensity = clamp((theta - lights[i].outer_angle_cutoff)/epsilon, 0.0, 1.0); + + return intensity; +} + +float +attenuation(int i) +{ + float dist = length(lights[i].position - FragPos); + // for no gamma correction + //float attenuation = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * + //dist + lights[i].attenuation.y * (dist * dist)); + + // for gamma correction + float attenuation = 1.0 / (lights[i].attenuation.x + lights[i].attenuation.y * + dist + lights[i].attenuation.y * (dist)); + return attenuation; +} + +vec3 +specular(int i, vec3 lightDir, vec3 norm) +{ + float spec = max(dot(normalize(cameraPosition - FragPos), reflect(-lightDir, norm)), 0.0); + spec = pow(spec, material.shininess); + vec3 specular = lights[i].specular * (spec * + vec3(texture(material.texture_specular1, vec2(theTexCoord)))); + return specular; +} + +vec3 +diffuse(int i, vec3 lightDir, vec3 norm) +{ + float diff_val = max(dot(norm, lightDir), 0.0); + vec3 diffuse = lights[i].diffuse * (diff_val * material.diffuse); + return diffuse; +} + + +void +main() +{ + vec3 endResult = vec3(0); + // normal + vec3 norm; + norm = texture(material.texture_normal1, vec2(theTexCoord)).rgb; + norm = norm * 2.0 - 1.0; + norm = normalize(TBN * norm); + + for (int i = 0; i < num_lights; i++) { + + vec3 ambient = lights[i].ambient * material.ambient; + + + // diffuse + vec3 lightDir = vec3(0); + if (lights[i].type == POINT_LIGHT || lights[i].type == SPOT_LIGHT) { + lightDir = normalize(lights[i].position - FragPos); + } else if (lights[i].type == DIRECTIONAL_LIGHT) { + lightDir = normalize(-lights[i].direction); + } + + vec3 diffuse = diffuse(i, lightDir, norm); + + vec3 specular = specular(i, lightDir, norm); + + vec3 result = ambient; + float intensity = 1.0; + + if (lights[i].type == SPOT_LIGHT) { + intensity = spotlight(i, lightDir); + } + + result = ambient + (diffuse + specular) * intensity; + + if (lights[i].attenuate) { + result *= attenuation(i); + } + + endResult += result; + } + + vec4 parta = (vec4(endResult, 1.0)) * texture(material.texture_diffuse1, vec2(theTexCoord)); + LFragment = parta; + // gamma correction +// LFragment.rgb = pow(parta.rgb, vec3(1.0/gamma)); +// LFragment = vec4(norm, 1); + +} + diff --git a/data/shaders/test.vert b/data/shaders/test.vert new file mode 100644 index 0000000..1a04dbc --- /dev/null +++ b/data/shaders/test.vert @@ -0,0 +1,42 @@ +#version 330 core + +layout (location = 0) in vec3 aVertexPos; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; +layout (location = 4) in vec3 aTangent; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; +uniform bool skyboxON; + +out vec3 Normal; +out vec3 FragPos; +out vec3 theTexCoord; +out mat3 TBN; + +void main() +{ + gl_Position = projection * view * model * vec4( aVertexPos, 1.0); + + + if (skyboxON) { + theTexCoord = aVertexPos; + FragPos = aVertexPos; // vec3(model * vec4(aVertexPos, 1.0)); + return; + } + + // calculating TBN + vec3 T = normalize(vec3(model * vec4(aTangent, 0.0))); + vec3 N = normalize(vec3(model * vec4(aNormal, 0.0))); + + T = normalize(T - dot(T, N) * N); + vec3 B = cross(N,T); + + TBN = mat3(T, B, N); + + // calculating normal + theTexCoord = vec3(aTexCoords, 1.0); + Normal = mat3(transpose(inverse(model))) * aNormal; + FragPos = vec3(model * vec4(aVertexPos, 1.0)); +} diff --git a/src/mesh.cpp b/src/mesh.cpp index 1d5ca31..cde2558 100644 --- a/src/mesh.cpp +++ b/src/mesh.cpp @@ -2,6 +2,7 @@ #include "stb_image.h" #include "glerror.h" #include +#include Mesh::Mesh(std::vector vertices, std::vector indices, @@ -217,8 +218,8 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); stbi_image_free(data); }