Browse Source

loading

master
alistair 2 years ago
parent
commit
849d0a78f2
  1. 2
      CMakeLists.txt
  2. 5
      data/levels/init.json
  3. 62
      data/shaders/blend.frag
  4. 49
      data/shaders/instanced.vert
  5. 7
      data/shaders/normaldip.frag
  6. 28
      data/shaders/normaldip.geom
  7. 21
      data/shaders/screentexture.frag
  8. 11
      data/shaders/screentexture.vert
  9. 12
      data/shaders/sky.frag
  10. 18
      data/shaders/sky.vert
  11. 151
      data/shaders/test.frag
  12. 42
      data/shaders/test.vert
  13. 5
      src/mesh.cpp

2
CMakeLists.txt

@ -13,6 +13,8 @@ set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fs @@ -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)

5
data/levels/init.json

@ -4,6 +4,9 @@ @@ -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 @@ @@ -36,7 +39,7 @@
],
"entities": [
{
"draw": {"model": "backpack"},
"draw": {"model": "unnamed_6"},
"physics": {
"position": [1,4,1],
"orientation": [0,2,0]

62
data/shaders/blend.frag

@ -0,0 +1,62 @@ @@ -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;
}

49
data/shaders/instanced.vert

@ -0,0 +1,49 @@ @@ -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);
}

7
data/shaders/normaldip.frag

@ -0,0 +1,7 @@ @@ -0,0 +1,7 @@
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0, 1.0, 0.0, 1.0);
}

28
data/shaders/normaldip.geom

@ -0,0 +1,28 @@ @@ -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
}

21
data/shaders/screentexture.frag

@ -0,0 +1,21 @@ @@ -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);
}

11
data/shaders/screentexture.vert

@ -0,0 +1,11 @@ @@ -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;
}

12
data/shaders/sky.frag

@ -0,0 +1,12 @@ @@ -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);
}

18
data/shaders/sky.vert

@ -0,0 +1,18 @@ @@ -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;
}

151
data/shaders/test.frag

@ -0,0 +1,151 @@ @@ -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);
}

42
data/shaders/test.vert

@ -0,0 +1,42 @@ @@ -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));
}

5
src/mesh.cpp

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
#include "stb_image.h"
#include "glerror.h"
#include <assimp/postprocess.h>
#include <fmt/core.h>
Mesh::Mesh(std::vector<struct vertex> vertices, std::vector<unsigned int> indices,
@ -217,8 +218,8 @@ unsigned int Model::texture_from_file(const char *fname, std::string directory) @@ -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);
}

Loading…
Cancel
Save