Basic physics with boxes

This commit is contained in:
grayson committed 2021-10-25 00:40:44 -05:00
1 parent 1d97a6f543
commit b4c704f5ec
259 files changed
+48478 -91

No files matched your search

@@ -0,0 +1,16 @@
#version 330 core
in vec3 texCoord;
uniform samplerCube cm;
out vec4 color;
void main() {
vec3 tc;
tc = vec3(-texCoord.x, texCoord.y, texCoord.z);
if (texCoord.y > 0.999999 || texCoord.y < -0.999999) {
tc = vec3(texCoord.x, texCoord.y, texCoord.z);
}
color = texture(cm, tc);
}
@@ -0,0 +1,15 @@
#version 330 core
layout(location = 0) in vec3 pos;
uniform mat4 view;
uniform mat4 proj;
out vec3 texCoord;
void main() {
texCoord = pos;
vec4 vertexPos = proj * view * vec4(pos, 1.0);
gl_Position = vertexPos.xyww;
}
@@ -0,0 +1,9 @@
#version 330 core
uniform float opacity;
out vec4 color;
void main() {
color = vec4(vec3(0.0), opacity);
}
@@ -0,0 +1,7 @@
#version 330 core
layout(location = 0) in vec2 pos;
void main() {
gl_Position = vec4(pos, 0.0, 1.0);
}
@@ -0,0 +1,30 @@
#version 330 core
in vec3 fragPos;
in vec2 texCoord;
in vec3 normal;
in mat3 tbn;
flat in int matNum;
uniform bool hasDiffuseTex[32];
uniform sampler2D diffuseTexture[32];
uniform vec3 diffuseColor[32];
uniform bool hasSpecTex[32];
uniform sampler2D specularTexture[32];
uniform float specPower[32];
uniform bool hasNormTex[32];
uniform sampler2D normalTexture[32];
layout(location = 0) out vec3 pos;
layout(location = 1) out vec3 normals;
layout(location = 2) out vec3 diffuse;
layout(location = 3) out vec3 specular;
void main() {
pos = fragPos;
normals = hasNormTex[matNum] ? normalize(tbn * (texture(normalTexture[matNum], texCoord).xyz * 2.0 - 1.0)) : normal;
diffuse = hasDiffuseTex[matNum] ? texture(diffuseTexture[matNum], texCoord).rgb : diffuseColor[matNum];
specular.r = specPower[matNum];
specular.g = hasSpecTex[matNum] ? texture(specularTexture[matNum], texCoord).r : 0.2;
specular.b = 1.0;
}
@@ -0,0 +1,32 @@
#version 330 core
layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 texCoords;
layout(location = 2) in vec3 normals;
layout(location = 3) in vec3 tangent;
layout(location = 4) in int matN;
uniform mat4 model[60];
uniform mat4 view;
uniform mat4 proj;
out vec3 fragPos;
out vec2 texCoord;
out vec3 normal;
out mat3 tbn;
flat out int matNum;
void main() {
vec4 world = model[gl_InstanceID] * vec4(pos, 1.0);
fragPos = world.xyz;
texCoord = texCoords;
mat3 normalMat = transpose(inverse(mat3(model[gl_InstanceID])));
vec3 t = normalize(normalMat * tangent);
vec3 n = normalize(normalMat * normals);
normal = n;
vec3 b = cross(n, t);
tbn = mat3(t, b, n);
matNum = matN;
gl_Position = proj * view * world;
}
@@ -0,0 +1,133 @@
#version 330 core
in vec2 texCoord;
in vec4 fragPosLightSpace[16];
struct Camera {
vec3 pos;
};
struct Light {
//Directional = 1, Point = 2
int type;
vec3 pos;
vec3 color;
float strength;
sampler2D directionalDepthTex;
samplerCube pointDepthTex;
};
uniform Camera camera;
uniform Light light[12];
uniform int numberOfLights;
uniform bool isContinued;
uniform float farPlane;
uniform sampler2D gBPos;
uniform sampler2D gBNorm;
uniform sampler2D gBDiff;
uniform sampler2D gBSpec;
out vec4 outColor;
float calcShadowDirectional(int lightNum, vec4 fragPosLight, vec3 no, vec3 lDir) {
vec3 fp = fragPosLight.xyz / fragPosLight.w;
fp = fp * 0.5 + 0.5;
float current = fp.z;
float bias = max(0.05 * (1.0 - dot(no, lDir)), 0.005);
float shad = 0.0f;
vec2 texSize = 1.0 / textureSize(light[lightNum].directionalDepthTex, 0);
for (int x = -1; x <= 1; x++) {
for (int y = -1; y <= 1; y++) {
float pcfDepth = texture(light[lightNum].directionalDepthTex, fp.xy + vec2(x, y) * texSize).r;
shad += current - bias > pcfDepth ? 1.0 : 0.0;
}
}
shad /= 9.0;
if (current > 1.0)
shad = 0.0;
return shad;
}
vec3 offsets[20] = vec3[](
vec3(1, 1, 1), vec3(1, -1, 1), vec3(-1, -1, 1), vec3(-1, 1, 1),
vec3(1, 1, -1), vec3(1, -1, -1), vec3(-1, -1, -1), vec3(-1, 1, -1),
vec3(1, 1, 0), vec3(1, -1, 0), vec3(-1, -1, 0), vec3(-1, 1, 0),
vec3(1, 0, 1), vec3(-1, 0, 1), vec3(1, 0, -1), vec3(-1, 0, -1),
vec3(0, 1, 1), vec3(0, -1, 1), vec3(0, -1, -1), vec3(0, 1, -1)
);
float calcShadowPoint(int lightNum, vec3 no, vec3 lDir, vec3 fp) {
vec3 fragLight = fp - light[lightNum].pos;
float current = length(fragLight);
float bias = 0.15;
float closest = 0.0f;
float shad = 0.0f;
int samples = 20;
float viewDist = length(camera.pos - fp);
float disk = (1.0 + (viewDist / farPlane)) / 50.0f;
for (int i = 0; i < samples; i++) {
closest = texture(light[lightNum].pointDepthTex, fragLight + offsets[i] * disk).r;
closest *= farPlane;
if (current - bias > closest)
shad += 1.0f;
}
shad /= float(samples);
return shad;
}
void main() {
vec3 specTemp = texture(gBSpec, texCoord).rgb;
if (specTemp.b != 1.0)
discard;
vec3 color = vec3(0.0);
vec3 fragPos = texture(gBPos, texCoord).xyz;
vec3 norm = texture(gBNorm, texCoord).xyz;
vec3 matDiff = texture(gBDiff, texCoord).rgb;
float specPower = specTemp.r;
float matSpec = specTemp.g;
float ambientStrength = 0.1f;
vec3 ambient = ambientStrength * matDiff;
if (!isContinued)
color += ambient;
for (int i = 0; i < numberOfLights; i++) {
if (light[i].type == 1) {
vec3 lightDir = normalize(light[i].pos - fragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * diff * matDiff * (light[i].strength / 2.0f);
vec3 viewDir = normalize(camera.pos - fragPos);
vec3 reflectDir = reflect(-lightDir, norm);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), specPower);
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.0f);
float shadow = calcShadowDirectional(i, fragPosLightSpace[i], norm, lightDir);
color += (diffuse + specular) * (1.0 - shadow);
continue;
}
if (light[i].type == 2) {
vec3 lightDir = normalize(light[i].pos - fragPos);
float diff = max(dot(norm, lightDir), 0.0);
vec3 diffuse = light[i].color * diff * matDiff * (light[i].strength / 2.5f);
vec3 viewDir = normalize(camera.pos - fragPos);
vec3 halfway = normalize(lightDir + viewDir);
float spec = pow(max(dot(norm, halfway), 0.0), specPower);
vec3 specular = light[i].color * spec * matSpec * (light[i].strength / 2.5f);
float length = length(light[i].pos - fragPos);
float att = clamp(10.0 / length, 0.0, 1.0) * light[i].strength;
float shadow = calcShadowPoint(i, norm, lightDir, fragPos);
color += ((diffuse + specular) * (1.0 - shadow) * att);
continue;
}
}
//TODO: Move this to a post-processing pass (what about the UI textures?)
float gamma = 2.2;
color.rgb = pow(color.rgb, vec3(1.0 / gamma));
outColor = vec4(color, 1.0);
}
@@ -0,0 +1,20 @@
#version 330 core
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 texCoords;
uniform mat4 lightSpaceMat[16];
uniform int numMats;
uniform sampler2D gBPos;
out vec2 texCoord;
out vec4 fragPosLightSpace[16];
void main() {
texCoord = texCoords;
vec3 fragPos = texture(gBPos, texCoord).xyz;
for (int i = 0; i < numMats; i++) {
fragPosLightSpace[i] = lightSpaceMat[i] * vec4(fragPos, 1.0);
}
gl_Position = vec4(pos, 0.0, 1.0);
}
@@ -0,0 +1,5 @@
#version 330 core
void main() {
}
@@ -0,0 +1,12 @@
#version 330 core
layout(location = 0) in vec3 pos;
uniform mat4 lightSpace;
uniform mat4 model[60];
out vec3 texCoord;
void main() {
gl_Position = lightSpace * model[gl_InstanceID] * vec4(pos, 1.0);
}
@@ -0,0 +1,12 @@
#version 330 core
in vec4 fragPos;
uniform vec3 lightPos;
uniform float farPlane;
void main() {
float dist = length(fragPos.xyz - lightPos);
dist /= farPlane;
gl_FragDepth = dist;
}
@@ -0,0 +1,20 @@
#version 330 core
layout(triangles) in;
layout(triangle_strip, max_vertices = 18) out;
uniform mat4 lightSpaceMat[6];
out vec4 fragPos;
void main() {
for (int face = 0; face < 6; face++) {
gl_Layer = face;
for (int i = 0; i < 3; i++) {
fragPos = gl_in[i].gl_Position;
gl_Position = lightSpaceMat[face] * fragPos;
EmitVertex();
}
EndPrimitive();
}
}
@@ -0,0 +1,9 @@
#version 330 core
layout(location = 0) in vec3 pos;
uniform mat4 model[60];
void main() {
gl_Position = model[gl_InstanceID] * vec4(pos, 1.0);
}
@@ -0,0 +1,14 @@
#version 330 core
in vec2 texCoord;
uniform sampler2D text;
uniform vec3 textColor;
uniform float opacity;
out vec4 color;
void main() {
vec4 temp = vec4(1.0, 1.0, 1.0, texture(text, texCoord).r);
color = vec4(textColor.xyz, opacity) * temp;
}
@@ -0,0 +1,13 @@
#version 330 core
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 texCoords;
uniform mat4 proj;
out vec2 texCoord;
void main() {
gl_Position = proj * vec4(pos, 0.0, 1.0);
texCoord = texCoords;
}
@@ -0,0 +1,17 @@
#version 330 core
in vec2 texCoord;
uniform sampler2D tex;
uniform float opacity;
out vec4 outColor;
void main() {
vec4 texColor = texture(tex, texCoord);
vec4 color = texColor;
//TODO: Move this to a post-processing pass too
float gamma = 2.2;
color.rgb = pow(color.rgb, vec3(1.0 / gamma));
outColor = vec4(color.rgb, texColor.a * opacity);
}
@@ -0,0 +1,13 @@
#version 330 core
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 texCoords;
uniform mat4 proj;
out vec2 texCoord;
void main() {
gl_Position = proj * vec4(pos, 0.0, 1.0);
texCoord = texCoords;
}