diff --git a/Makefile b/Makefile index d38cbffa..7e0426ab 100755 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ endif UNAME_P != uname -m #CC specifies which compiler we're using -CC = gcc -std=c99 +CC = clang -std=c99 ifeq ($(DEBUG), 1) DEFFALGS += -DDEBUG @@ -87,7 +87,7 @@ ifeq ($(UNAME), Windows_NT) CLIBS = glew32 EXT = .exe else - LINKER_FLAGS = -static-libgcc + LINKER_FLAGS = ELIBS = editor engine CLIBS = glfw SDL2 SDL2_mixer m EXT = @@ -96,7 +96,8 @@ endif ELIBS != $(call prefix, $(ELIBS), -l) CLIBS != $(call prefix, $(CLIBS), -l) -LELIBS = -Wl,-Bstatic $(ELIBS) -Wl,-Bdynamic $(CLIBS) +#LELIBS = -Wl,-Bstatic $(ELIBS) -Wl,-Bdynamic $(CLIBS) +LELIBS = $(ELIBS) $(CLIBS) objects = $(bsobjects) $(eobjects) $(pinobjects) DEPENDS = $(objects:.o=.d) diff --git a/fonts/notosans.ttf b/fonts/notosans.ttf new file mode 100755 index 00000000..a1b8994e Binary files /dev/null and b/fonts/notosans.ttf differ diff --git a/shaders/albedofrag.glsl b/shaders/albedofrag.glsl new file mode 100755 index 00000000..8edcb0d2 --- /dev/null +++ b/shaders/albedofrag.glsl @@ -0,0 +1,12 @@ +#version 330 core +out vec4 FragColor; + +in vec3 FragPos; +in vec2 TexCoords; + +uniform sampler2D texture_diffuse1; + +void main() +{ + FragColor = texture(texture_diffuse1, TexCoords); +} \ No newline at end of file diff --git a/shaders/animspritefrag.glsl b/shaders/animspritefrag.glsl new file mode 100755 index 00000000..d985be1d --- /dev/null +++ b/shaders/animspritefrag.glsl @@ -0,0 +1,14 @@ +#version 330 core +in vec2 TexCoords; +out vec4 color; + +uniform sampler2DArray image; +uniform float frame; +uniform vec3 spriteColor; + +void main() +{ + color = vec4(spriteColor, 1.f) * texture(image, vec3(TexCoords,frame)); + if (color.a < 0.1) + discard; +} \ No newline at end of file diff --git a/shaders/animspritevert.glsl b/shaders/animspritevert.glsl new file mode 100755 index 00000000..d260dec9 --- /dev/null +++ b/shaders/animspritevert.glsl @@ -0,0 +1,17 @@ +#version 330 core +layout (location = 0) in vec2 vertex; // + +out vec2 TexCoords; + +layout (std140) uniform Projection +{ + mat4 projection; +}; + +uniform mat4 model; + +void main() +{ + TexCoords = vertex.xy; + gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); +} \ No newline at end of file diff --git a/shaders/billboardfrag.glsl b/shaders/billboardfrag.glsl new file mode 100755 index 00000000..6d42cd36 --- /dev/null +++ b/shaders/billboardfrag.glsl @@ -0,0 +1,3 @@ +void main(){ + color = texture( myTextureSampler, UV ); +} \ No newline at end of file diff --git a/shaders/billboardvertex.glsl b/shaders/billboardvertex.glsl new file mode 100755 index 00000000..210e71c3 --- /dev/null +++ b/shaders/billboardvertex.glsl @@ -0,0 +1,11 @@ +void main(){ + + // Output position of the vertex, in clip space + // map [0..800][0..600] to [-1..1][-1..1] + vec2 vertexPosition_homoneneousspace = vertexPosition_screenspace - vec2(400,300); // [0..800][0..600] -> [-400..400][-300..300] + vertexPosition_homoneneousspace /= vec2(400,300); + gl_Position = vec4(vertexPosition_homoneneousspace,0,1); + + // UV of the vertex. No special space for this one. + UV = vertexUV; +} \ No newline at end of file diff --git a/shaders/btdebugfrag.glsl b/shaders/btdebugfrag.glsl new file mode 100755 index 00000000..a38cbd2a --- /dev/null +++ b/shaders/btdebugfrag.glsl @@ -0,0 +1,9 @@ +#version 330 core +layout (location = 0) out vec4 color; + +in vec3 vcolor; + +void main(void) +{ + color = vec4(vcolor, 1.f); +} \ No newline at end of file diff --git a/shaders/btdebugvert.glsl b/shaders/btdebugvert.glsl new file mode 100755 index 00000000..b3ff7881 --- /dev/null +++ b/shaders/btdebugvert.glsl @@ -0,0 +1,17 @@ +#version 330 core +layout (location = 0) in vec3 position; +layout (location = 1) in vec3 color; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +out vec3 vcolor; + +void main() +{ + gl_Position = projection * view * vec4(position, 1.0f); + + vcolor = color; +} \ No newline at end of file diff --git a/shaders/circlefrag.glsl b/shaders/circlefrag.glsl new file mode 100755 index 00000000..39d38bbe --- /dev/null +++ b/shaders/circlefrag.glsl @@ -0,0 +1,24 @@ +#version 330 +in vec2 coords; + +out vec4 color; + +uniform float radius; +uniform int thickness; + +void main() +{ + // int tt = thickness + 1; + float R1 = 1.f; + float R2 = 1.f - (thickness / radius); + float dist = sqrt(dot(coords, coords)); + if (dist <= R2 || dist >= R1) + discard; +/* + float smoother = 0.01f - (radius * 0.00003f); + float sm = smoothstep(R1, R1-smoother, dist); + float sm2 = smoothstep(R2, R2+smoother, dist); + float alpha = sm*sm2; +*/ + color = vec4(0.4f, 0.5f, 0.6f, 1.f); +} \ No newline at end of file diff --git a/shaders/circlevert.glsl b/shaders/circlevert.glsl new file mode 100755 index 00000000..ffba2ab7 --- /dev/null +++ b/shaders/circlevert.glsl @@ -0,0 +1,15 @@ +#version 330 core +layout (location = 0) in vec4 vertex; +out vec2 coords; + +layout (std140) uniform Projection +{ + mat4 projection; +}; + +void main() +{ + coords = vertex.zw; + + gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); +} \ No newline at end of file diff --git a/shaders/debugcolorfrag.glsl b/shaders/debugcolorfrag.glsl new file mode 100755 index 00000000..61d87b0c --- /dev/null +++ b/shaders/debugcolorfrag.glsl @@ -0,0 +1,9 @@ +#version 330 core +out vec4 FragColor; + +uniform vec3 PickingColor; + +void main() +{ + FragColor = vec4(PickingColor, 1.0); // orthographic +} \ No newline at end of file diff --git a/shaders/debugdepthfrag.glsl b/shaders/debugdepthfrag.glsl new file mode 100755 index 00000000..340ff561 --- /dev/null +++ b/shaders/debugdepthfrag.glsl @@ -0,0 +1,22 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D depthMap; +uniform float near_plane; +uniform float far_plane; + +// required when using a perspective projection matrix +float LinearizeDepth(float depth) +{ + float z = depth * 2.0 - 1.0; // Back to NDC + return (2.0 * near_plane * far_plane) / (far_plane + near_plane - z * (far_plane - near_plane)); +} + +void main() +{ + float depthValue = texture(depthMap, TexCoords).r; + //FragColor = vec4(vec3(LinearizeDepth(depthValue) / far_plane), 1.0); // perspective + FragColor = texture(depthMap, TexCoords); // orthographic +} \ No newline at end of file diff --git a/shaders/fragment.glsl b/shaders/fragment.glsl new file mode 100755 index 00000000..ea29f4f3 --- /dev/null +++ b/shaders/fragment.glsl @@ -0,0 +1,99 @@ +#version 330 core + +out vec4 FragColor; + +struct Light { + vec3 direction; + vec3 color; +}; + +struct PLight { + vec3 position; + vec3 color; + float constant; + float linear; + float quadratic; +}; + +struct SLight { + vec3 position; + vec3 direction; + float cutOff; + float outerCutOff; +}; + +uniform vec3 objectColor; +uniform vec3 lightColor; +uniform vec3 lightPos; +uniform vec3 viewPos; + +uniform sampler2D diffuseTex; +uniform sampler2D specTex; +uniform float shininess; +uniform Light light; +uniform PLight plight; +uniform SLight slight; + +in vec3 Normal; +in vec3 FragPos; + +in vec2 TexCoords; + +float near = 0.1; +float far = 100.0; + +float LinearizeDepth(float depth) +{ + float z = depth * 2.0 - 1.0; // back to NDC + return (2.0 * near * far) / (far + near - z * (far - near)); +} + +// void main() +// { +// float depth = LinearizeDepth(gl_FragCoord.z) / far; // divide by far for demonstration +// FragColor = vec4(vec3(depth), 1.0); +// } +void main() +{ + //vec3 dirDir = normalize(-Directional.direction); + + float ambientStrength = 1.f; + vec3 ambient = ambientStrength * vec3(texture(diffuseTex, TexCoords)); + + // vec3 norm = normalize(Normal); + + // vec3 diffuse; + + // Directional + //vec3 lightDir = normalize(-light.direction); + + + + // Point + // float distance = length(plight.position - FragPos); + // float attenuation = 1.f / (plight.constant + plight.linear * distance + plight.quadratic * (distance * distance)); + // vec3 lightDir = normalize(lightPos - FragPos); + + // float diff = max(dot(norm, lightDir), 0.f); + //diffuse = diff * lightColor * vec3(texture(diffuseTex, TexCoords)) * attenuation; + + // Spot + // lightDir = normalize(slight.position - FragPos); + // float theta = dot(lightDir, normalize(-slight.direction)); + // float epsilon = slight.cutOff - slight.outerCutOff; + // float intensity = clamp((theta - slight.outerCutOff) / epsilon, 0.f, 1.f); + // diffuse = lightColor * diff * vec3(texture(diffuseTex, TexCoords)) * intensity; + + // float specularStrength = 0.5f; + // vec3 viewDir = normalize(viewPos - FragPos); + // vec3 reflectDir = reflect(-lightDir, norm); + // float spec = pow(max(dot(viewDir, reflectDir), 0.f), 32); + // vec3 specular = specularStrength * spec * lightColor * texture(specTex, TexCoords).rgb; + + // if(texture(specTex, TexCoords).r < 0.1f) + // discard; + + //vec3 result = (ambient + diffuse + specular) * objectColor; + + FragColor = vec4(ambient, 1.f); +} diff --git a/shaders/gizmofrag.glsl b/shaders/gizmofrag.glsl new file mode 100755 index 00000000..6728e4d6 --- /dev/null +++ b/shaders/gizmofrag.glsl @@ -0,0 +1,11 @@ +#version 330 core + +layout (location = 0) out vec4 color; + +in vec3 vcolor; +in vec3 FragPos; + +void main(void) +{ + color = vec4(vcolor, 1.f); +} \ No newline at end of file diff --git a/shaders/gizmovert.glsl b/shaders/gizmovert.glsl new file mode 100755 index 00000000..944dd332 --- /dev/null +++ b/shaders/gizmovert.glsl @@ -0,0 +1,21 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +out vec3 vcolor; +uniform mat4 model; + +void main() +{ + vec3 m = aPos; + + vcolor.r = m.r / 1.f; + vcolor.g = m.g / 1.f; + vcolor.b = m.b / 1.f; + + gl_Position = projection * view * model * (vec4(m, 1.f)); +} \ No newline at end of file diff --git a/shaders/gridfrag.glsl b/shaders/gridfrag.glsl new file mode 100755 index 00000000..c5a4ae3e --- /dev/null +++ b/shaders/gridfrag.glsl @@ -0,0 +1,19 @@ +#version 330 +out vec4 color; + +in vec2 apos; +vec2 bpos; +uniform int thickness; +uniform int span; + +void main(void) +{ + float t = thickness / 2.f; + bpos.x = mod(apos.x, span); + bpos.y = mod(apos.y, span); + + if (!(bpos.x <= t || bpos.x >= (span - t) || bpos.y <= t || bpos.y >= (span - t))) + discard; + + color = vec4(0.4f, 0.7f, 0.2f, 0.3f); +} \ No newline at end of file diff --git a/shaders/gridvert.glsl b/shaders/gridvert.glsl new file mode 100755 index 00000000..92ec666c --- /dev/null +++ b/shaders/gridvert.glsl @@ -0,0 +1,16 @@ +#version 330 +layout (location = 0) in vec2 pos; +out vec2 apos; + +layout (std140) uniform Projection { + mat4 projection; +}; + +void main() +{ + mat4 iproj = inverse(projection); + vec4 ipos = iproj * vec4(pos, 0.f, 1.f); + apos = ipos.xy; + + gl_Position = vec4(pos, 0.f, 1.f); +} \ No newline at end of file diff --git a/shaders/lightfrag.glsl b/shaders/lightfrag.glsl new file mode 100755 index 00000000..040b7282 --- /dev/null +++ b/shaders/lightfrag.glsl @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(1.0f); // set alle 4 vector values to 1.0 +} \ No newline at end of file diff --git a/shaders/lightvert.glsl b/shaders/lightvert.glsl new file mode 100755 index 00000000..752ce21f --- /dev/null +++ b/shaders/lightvert.glsl @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/shaders/linefrag.glsl b/shaders/linefrag.glsl new file mode 100755 index 00000000..db2d5459 --- /dev/null +++ b/shaders/linefrag.glsl @@ -0,0 +1,7 @@ +#version 330 +out vec4 color; + +void main() +{ + color = vec4(0.2f, 0.1f, 0.8f, 1.f); +} \ No newline at end of file diff --git a/shaders/linevert.glsl b/shaders/linevert.glsl new file mode 100755 index 00000000..3053b405 --- /dev/null +++ b/shaders/linevert.glsl @@ -0,0 +1,11 @@ +#version 330 +layout (location = 0) in vec2 pos; + +layout (std140) uniform Projection { + mat4 projection; +}; + +void main() +{ + gl_Position = projection * vec4(pos, 0.f, 1.f); +} \ No newline at end of file diff --git a/shaders/modelfrag.glsl b/shaders/modelfrag.glsl new file mode 100755 index 00000000..d76c51c1 --- /dev/null +++ b/shaders/modelfrag.glsl @@ -0,0 +1,185 @@ +#version 330 core +out vec4 FragColor; + +struct Light { + vec3 direction; + vec3 color; + float strength; +}; + +struct PLight { + vec3 position; + float constant; + float linear; + float quadratic; + vec3 color; + float strength; +}; + +struct SpotLight { + vec3 position; + vec3 direction; + vec3 color; + float strength; + float cutoff; + float outerCutoff; + float distance; + float constant; + float linear; + float quadratic; +}; + +in vec3 FragPos; +in vec2 TexCoords; +in vec4 FragPosLightSpace; +in mat3 TBN; + +//in vec3 TangentLightPos; +in vec3 TangentViewPos; +in vec3 TangentFragPos; + +uniform sampler2D texture_diffuse1; +uniform sampler2D texture_normal1; +uniform sampler2D shadowMap; + +uniform Light dirLight; +uniform vec3 viewPos; + +vec3 norm; +vec3 viewDir; + +#define NR_POINT_LIGHTS 1 +uniform PLight pointLights[NR_POINT_LIGHTS]; + +uniform SpotLight spotLight; + +float ShadowCalculation(vec4 fragPosLightSpace, vec3 normal, vec3 lightDir) { + // perform perspective divide + vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w; + // transform to [0,1] range + projCoords = projCoords * 0.5 + 0.5; + // get closest depth value from light's perspective (using [0,1] range fragPosLight as coords) + float closestDepth = texture(shadowMap, projCoords.xy).r; + // get depth of current fragment from light's perspective + float currentDepth = projCoords.z; + // calculate bias (based on depth map resolution and slope) + float bias = max(0.05 * (1.0 - dot(normal, lightDir)), 0.005); + // check whether current frag pos is in shadow + // float shadow = currentDepth - bias > closest Depth ? 1.0 : 0.0; + // PCF + float shadow = 0.0; + vec2 texelSize = 1.0 / textureSize(shadowMap, 0); + for(int x = -1; x <= 1; ++x) + { + for(int y = -1; y <= 1; ++y) + { + float pcfDepth = texture(shadowMap, projCoords.xy + vec2(x, y) * texelSize).r; + shadow += currentDepth - bias > pcfDepth ? 1.0 : 0.0; + } + } + shadow /= 9.0; + + // keep the shadow at 0.0 when outside the far_plane region of the light's frustum. + if(projCoords.z > 1.0) + shadow = 0.0; + + return shadow; +} + +vec3 CalcDirLight(Light light) { + vec3 lightDir = TBN * normalize(-light.direction); + float lightStrength = 3.f; + vec3 lightColor = vec3(1.f); + + // diffuse shading + float diff = max(dot(norm, lightDir), 0.f); + + // specular shading + float specularStrength = 0.5f; + vec3 reflectDir = reflect(-lightDir, norm); + vec3 halfwayDir = normalize(lightDir + viewDir); // Use this instead of reflectDir? + float spec = pow(max(dot(viewDir, halfwayDir), 0.0), 32.f) * specularStrength; // 32.f is "shininess" + + float shadow = ShadowCalculation(FragPosLightSpace, norm, lightDir); + + // combine results + return light.strength * (diff + spec) * (1.f-shadow) * light.color; +} + +vec3 CalcPointLight(PLight light) { + vec3 tbnPos = TBN * light.position; + vec3 lightDir = normalize(tbnPos - TangentFragPos); + + // Diffuse + float diff = max(dot(norm, lightDir), 0.f); + + // specular + vec3 reflectDir = reflect(-lightDir, norm); + vec3 halfwayDir = normalize(lightDir + viewDir); + float spec = pow(max(dot(viewDir, halfwayDir), 0.f), 32.f); + + // Attenuation + float distance = length(light.position - TangentFragPos); + float attenuation = 1.f / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + + return light.color * attenuation * light.strength * (diff + spec); +} + +vec3 CalcSpotLight(SpotLight light) { + vec3 tbnPos = TBN * light.position; + vec3 tbnDir = TBN * light.direction; + vec3 lightDir = normalize(tbnPos - TangentFragPos); + + float diff = max(dot(norm, lightDir), 0.f); + + vec3 reflectDir = reflect(-lightDir, norm); + vec3 halfwayDir = normalize(lightDir + viewDir); + float spec = pow(max(dot(viewDir, halfwayDir), 0.f), 32.f); + + float theta = dot(lightDir, normalize(-tbnDir)); + float epsilon = light.cutoff - light.outerCutoff; + float intensity = clamp((theta - light.outerCutoff) / epsilon, 0.f, 1.f); + + float distance = length(light.position - FragPos); + float attenuation = 1.f / (light.constant + light.linear * distance + light.quadratic * (distance * distance)); + + return light.color * light.strength * (diff + spec) * intensity * attenuation; + +} + +void main() +{ + vec4 model = texture(texture_diffuse1, TexCoords); + + if (model.a <= 0.1f) + discard; + + vec3 albedo = model.rgb; + + norm = texture(texture_normal1, TexCoords).rgb; + norm = normalize(norm * 2.f - 1.f); + //vec3 norm = normalize(TangentViewPos); + + viewDir = normalize(TangentViewPos - TangentFragPos); + + // Ambient doesn't change + float ambientStrength = 0.3f; + vec3 ambientColor = vec3(1.f); + vec3 ambient = ambientStrength * ambientColor; + + // Per light calculation + + // Directional + vec3 result = CalcDirLight(dirLight); + result += ambient; + + // Point + for (int i = 0; i < NR_POINT_LIGHTS; i++) + result += CalcPointLight(pointLights[i]); + + result += CalcSpotLight(spotLight); + + + FragColor = vec4(result * model.rgb, 1.f); +} + diff --git a/shaders/modelvert.glsl b/shaders/modelvert.glsl new file mode 100755 index 00000000..31a82e9e --- /dev/null +++ b/shaders/modelvert.glsl @@ -0,0 +1,68 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; +layout (location = 3) in vec3 aTangent; +layout (location = 4) in vec3 aBitangent; +layout (location = 5) in ivec4 boneIds; +layout (location = 6) in vec4 weights; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +//out vec3 Normal; +out vec3 FragPos; +out vec2 TexCoords; + +//out vec3 TangentLightPos; +out vec3 TangentViewPos; +out vec3 TangentFragPos; + +out vec4 FragPosLightSpace; +out mat3 TBN; + +uniform mat4 model; +uniform mat4 lightSpaceMatrix; +uniform vec3 viewPos; + +const int MAX_BONES = 100; +const int MAX_BONE_INFLUENCE = 4; +uniform mat4 finalBonesMatrices[MAX_BONES]; + +void main() +{ + vec4 totalPosition = vec4(0.f); + for (int i = 0; i < MAX_BONE_INFLUENCE; i++) { + if (boneIds[i] == -1) + continue; + + if (boneIds[i] >= 4) { + totalPosition = vec4(aPos, 1.f); + break; + } + + vec4 localPosition = finalBonesMatrices[boneIds[i]] * vec4(aPos, 1.f); + totalPosition += localPosition * weights[i]; + vec3 localNormal = mat3(finalBonesMatrices[boneIds[i]]) * aNormal; + } + + + FragPos = vec3(model * vec4(aPos, 1.f)); + TexCoords = aTexCoords; + + mat3 normalMatrix = transpose(inverse(mat3(model))); + vec3 T = normalize(normalMatrix * aTangent); + vec3 N = normalize(normalMatrix * aNormal); + T = normalize(T - dot(T, N) * N); + vec3 B = cross(N, T); + + TBN = transpose(mat3(T, B, N)); + // TangentLightPos = TBN * lightPos; + TangentViewPos = TBN * viewPos; + TangentFragPos = TBN * FragPos; + + FragPosLightSpace = lightSpaceMatrix * vec4(FragPos, 1.f); + gl_Position = projection * view * model * totalPosition; +} \ No newline at end of file diff --git a/shaders/outline.glsl b/shaders/outline.glsl new file mode 100755 index 00000000..3e4a38dd --- /dev/null +++ b/shaders/outline.glsl @@ -0,0 +1,7 @@ +#version 330 core +out vec4 FragColor; + +void main() +{ + FragColor = vec4(0.92f, 0.86f, 0.68f, 1.0f); +} \ No newline at end of file diff --git a/shaders/outlinevert.glsl b/shaders/outlinevert.glsl new file mode 100755 index 00000000..7876006a --- /dev/null +++ b/shaders/outlinevert.glsl @@ -0,0 +1,15 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +uniform mat4 model; + +void main() +{ + gl_Position = projection * view * model * vec4(aPos + (aNormal * 2.f), 1.f); +} \ No newline at end of file diff --git a/shaders/postfrag.glsl b/shaders/postfrag.glsl new file mode 100755 index 00000000..8d67fc87 --- /dev/null +++ b/shaders/postfrag.glsl @@ -0,0 +1,58 @@ +#version 330 core +out vec4 FragColor; + +in vec2 TexCoords; + +uniform sampler2D screenTexture; + +const float offset = 1.0 / 300.0; + +void main() +{ + FragColor = texture(screenTexture, TexCoords); + //Invert color + //FragColor = vec4(vec3(1.0 - texture(screenTexture, TexCoords)), 1.0); + + //Grayscale + //FragColor = texture(screenTexture, TexCoords); + //float average = 0.2126 * FragColor.r + 0.7152 * FragColor.g + 0.0722 * FragColor.b; + //FragColor = vec4(average, average, average, 1.0); + + //Blur + // vec2 offsets[9] = vec2[]( + // vec2(-offset, offset), // top-left + // vec2( 0.0f, offset), // top-center + // vec2( offset, offset), // top-right + // vec2(-offset, 0.0f), // center-left + // vec2( 0.0f, 0.0f), // center-center + // vec2( offset, 0.0f), // center-right + // vec2(-offset, -offset), // bottom-left + // vec2( 0.0f, -offset), // bottom-center + // vec2( offset, -offset) // bottom-right + // ); + + // Sharpen kernel + // float kernel[9] = float[]( + // -1, -1, -1, + // -1, 9, -1, + // -1, -1, -1 + // ); + + // Blur kernel + // float kernel[9] = float[]( + // 1.0 / 16, 2.0 / 16, 1.0 / 16, + // 2.0 / 16, 4.0 / 16, 2.0 / 16, + // 1.0 / 16, 2.0 / 16, 1.0 / 16 + // ); + + // vec3 sampleTex[9]; + // for(int i = 0; i < 9; i++) + // { + // sampleTex[i] = vec3(texture(screenTexture, TexCoords.st + offsets[i])); + // } + // vec3 col = vec3(0.0); + // for(int i = 0; i < 9; i++) + // col += sampleTex[i] * kernel[i]; + + // FragColor = vec4(col, 1.0); +} \ No newline at end of file diff --git a/shaders/postvert.glsl b/shaders/postvert.glsl new file mode 100755 index 00000000..3632ce12 --- /dev/null +++ b/shaders/postvert.glsl @@ -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; +} \ No newline at end of file diff --git a/shaders/shadowfrag.glsl b/shaders/shadowfrag.glsl new file mode 100755 index 00000000..46b038fa --- /dev/null +++ b/shaders/shadowfrag.glsl @@ -0,0 +1,6 @@ +#version 330 core + +void main() +{ + // gl_FragDepth = gl_FragCoord.z; +} \ No newline at end of file diff --git a/shaders/shadowvert.glsl b/shaders/shadowvert.glsl new file mode 100755 index 00000000..2501662d --- /dev/null +++ b/shaders/shadowvert.glsl @@ -0,0 +1,10 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +uniform mat4 lightSpaceMatrix; +uniform mat4 model; + +void main() +{ + gl_Position = lightSpaceMatrix * model * vec4(aPos, 1.0); +} \ No newline at end of file diff --git a/shaders/simplevert.glsl b/shaders/simplevert.glsl new file mode 100755 index 00000000..e96124b1 --- /dev/null +++ b/shaders/simplevert.glsl @@ -0,0 +1,19 @@ +#version 330 core +layout (location = 0) in vec3 aPos; +layout (location = 2) in vec2 aTexCoords; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +out vec2 TexCoords; + +uniform mat4 model; + +void main() +{ + TexCoords = aTexCoords; + + gl_Position = projection * view * model * vec4(aPos, 1.f); +} \ No newline at end of file diff --git a/shaders/skyfrag.glsl b/shaders/skyfrag.glsl new file mode 100755 index 00000000..5d0e53e6 --- /dev/null +++ b/shaders/skyfrag.glsl @@ -0,0 +1,11 @@ +#version 330 core +out vec4 FragColor; + +in vec3 TexCoords; + +uniform samplerCube skybox; + +void main() +{ + FragColor = texture(skybox, TexCoords); +} \ No newline at end of file diff --git a/shaders/skyvert.glsl b/shaders/skyvert.glsl new file mode 100755 index 00000000..b9b11afe --- /dev/null +++ b/shaders/skyvert.glsl @@ -0,0 +1,18 @@ +#version 330 core +layout (location = 0) in vec3 aPos; + +out vec3 TexCoords; + +layout (std140) uniform Matrices { + mat4 projection; + mat4 view; +}; + +uniform mat4 skyview; + +void main() +{ + TexCoords = aPos; + vec4 pos = projection * skyview * vec4(aPos, 1.0); + gl_Position = pos.xyww; +} \ No newline at end of file diff --git a/shaders/spritefrag.glsl b/shaders/spritefrag.glsl new file mode 100755 index 00000000..8215e646 --- /dev/null +++ b/shaders/spritefrag.glsl @@ -0,0 +1,14 @@ +#version 330 core +in vec2 texcoords; +out vec4 color; + +uniform sampler2D image; +uniform vec3 spriteColor; + +void main() +{ + color = vec4(spriteColor, 1.f) * texture(image, texcoords); + + if (color.a <= 0.1f) + discard; +} \ No newline at end of file diff --git a/shaders/spritevert.glsl b/shaders/spritevert.glsl new file mode 100755 index 00000000..ad9714bc --- /dev/null +++ b/shaders/spritevert.glsl @@ -0,0 +1,16 @@ +#version 330 core +layout (location = 0) in vec2 vertex; +out vec2 texcoords; + +layout (std140) uniform Projection +{ + mat4 projection; +}; + +uniform mat4 model; + +void main() +{ + texcoords = vertex.xy; + gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); +} \ No newline at end of file diff --git a/shaders/textfrag.glsl b/shaders/textfrag.glsl new file mode 100755 index 00000000..bbd0c170 --- /dev/null +++ b/shaders/textfrag.glsl @@ -0,0 +1,12 @@ +#version 330 core +in vec2 TexCoords; +out vec4 color; + +uniform sampler2D text; +uniform vec3 textColor; + +void main() +{ + vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r); + color = vec4(textColor, 1.0) * sampled; +} \ No newline at end of file diff --git a/shaders/textvert.glsl b/shaders/textvert.glsl new file mode 100755 index 00000000..94b6118d --- /dev/null +++ b/shaders/textvert.glsl @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec4 vertex; // +out vec2 TexCoords; + +uniform mat4 projection; + +void main() +{ + gl_Position = projection * vec4(vertex.xy, 0.0, 1.0); + TexCoords = vertex.zw; +} \ No newline at end of file diff --git a/shaders/vertex.glsl b/shaders/vertex.glsl new file mode 100755 index 00000000..ec03afac --- /dev/null +++ b/shaders/vertex.glsl @@ -0,0 +1,22 @@ +#version 330 core + +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec3 aNormal; +layout (location = 2) in vec2 aTexCoords; + +uniform mat4 model; +uniform mat4 view; +uniform mat4 projection; + +out vec3 Normal; +out vec3 FragPos; +out vec2 TexCoords; + +void main() +{ + FragPos = vec3(model * vec4(aPos, 1.f)); + Normal = mat3(transpose(inverse(model))) * aNormal; + TexCoords = aTexCoords; + + gl_Position = projection * view * model * vec4(aPos, 1.0f); // see how we directly give a vec3 to vec4's constructor +} \ No newline at end of file diff --git a/shaders/videofrag.glsl b/shaders/videofrag.glsl new file mode 100755 index 00000000..e8cd8822 --- /dev/null +++ b/shaders/videofrag.glsl @@ -0,0 +1,22 @@ +#version 330 core +in vec2 TexCoords; +out vec4 color; + +uniform sampler2D texture_y; +uniform sampler2D texture_cb; +uniform sampler2D texture_cr; +uniform vec3 spriteColor; + +mat4 rec601 = mat4( + 1.16438, 0.00000, 1.59603, -0.87079, + 1.16438, -0.39176, -0.81297, 0.52959, + 1.16438, 2.01723, 0.00000, -1.08139, + 0, 0, 0, 1 +); + +void main() { + float y = texture2D(texture_y, TexCoords).r; + float cb = texture2D(texture_cb, TexCoords).r; + float cr = texture2D(texture_cr, TexCoords).r; + color = vec4(y, cb, cr, 1.f) * rec601 * vec4(spriteColor, 1.f); +} \ No newline at end of file diff --git a/shaders/videovert.glsl b/shaders/videovert.glsl new file mode 100755 index 00000000..e01972c9 --- /dev/null +++ b/shaders/videovert.glsl @@ -0,0 +1,12 @@ +#version 330 core +layout (location = 0) in vec4 vertex; + +out vec2 TexCoords; + +uniform mat4 model; +uniform mat4 projection; + +void main() { + TexCoords = vertex.zw; + gl_Position = projection * model * vec4(vertex.xy, 0.0, 1.0); +} \ No newline at end of file diff --git a/source/editor/Nuklear/nuklear.h b/source/editor/Nuklear/nuklear.h index 73e79baa..aef8f7fe 100644 --- a/source/editor/Nuklear/nuklear.h +++ b/source/editor/Nuklear/nuklear.h @@ -6079,7 +6079,6 @@ NK_LIB void nk_property(struct nk_context *ctx, const char *name, struct nk_prop #define STB_RECT_PACK_IMPLEMENTATION #define STB_TRUETYPE_IMPLEMENTATION - /* Allow consumer to define own STBTT_malloc/STBTT_free, and use the font atlas' allocator otherwise */ #ifndef STBTT_malloc static void* @@ -15094,6 +15093,7 @@ STBTT_DEF unsigned char *stbtt_GetGlyphBitmapSubpixel(const stbtt_fontinfo *info gbm.stride = gbm.w; stbtt_Rasterize(&gbm, 0.35f, vertices, num_verts, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1, info->userdata); + } } STBTT_free(vertices, info->userdata); return gbm.pixels; diff --git a/source/editor/editor.c b/source/editor/editor.c index 1e011a1c..c801d93c 100755 --- a/source/editor/editor.c +++ b/source/editor/editor.c @@ -416,7 +416,7 @@ static void edit_mouse_cb(GLFWwindow *w, int button, int action, int mods) } } -struct nk_context *nkctx; +struct nk_context *ctx; struct nk_glfw nkglfw = {0}; void editor_init(struct mSDLWindow *window) @@ -433,8 +433,13 @@ void editor_init(struct mSDLWindow *window) fclose(feditor); } - nkctx = nk_glfw3_init(&nkglfw, window->window, NK_GLFW3_INSTALL_CALLBACKS); - //set_style(nkctx, THEME_WHITE); + ctx = nk_glfw3_init(&nkglfw, window->window, NK_GLFW3_INSTALL_CALLBACKS); + + + struct nk_font_atlas *atlas; + nk_glfw3_font_stash_begin(&nkglfw, &atlas); + struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "fonts/notosans.tff", 14, 0); + nk_glfw3_font_stash_end(&nkglfw); glfwSetKeyCallback(window->window, edit_input_cb); glfwSetMouseButtonCallback(window->window, edit_mouse_cb); @@ -442,18 +447,18 @@ void editor_init(struct mSDLWindow *window) void editor_input() { - //io = &ImGui::GetIO(); } int editor_wantkeyboard() { -/* - if (io == NULL) return 0; - return io->WantCaptureKeyboard; - */ return 0; } +const int nuk_std = NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| + NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE; + +const struct nk_rect nk_rect_std = {250, 250, 250, 250}; + void editor_project_gui() { /* Grid, etc */ @@ -471,18 +476,56 @@ void editor_project_gui() */ } + static char text[3][64]; + static int text_len[3]; + static const char *items[] = {"Item 0","item 1","item 2"}; + static int selected_item = 0; + static int check = 1; + int i; -/* - if (ImGui::BeginMainMenuBar()) { - ImGui::Text("Current level: %s", - current_level[0] == - '\0' ? "Level not saved!" : current_level); + if (nk_begin(ctx, "Grid Demo", nk_rect(600, 350, 275, 250), + NK_WINDOW_TITLE|NK_WINDOW_BORDER|NK_WINDOW_MOVABLE| + NK_WINDOW_NO_SCROLLBAR)) + { - if (ImGui::BeginMenu("Windows")) { - ImGui::MenuItem("Resources", "F2", &editor.showAssetMenu); - ImGui::MenuItem("Hierarchy", "F4", &editor.showHierarchy); - ImGui::MenuItem("Lighting", "F5", &editor.showLighting); + nk_layout_row_dynamic(ctx, 30, 2); + nk_label(ctx, "Floating point:", NK_TEXT_RIGHT); + nk_edit_string(ctx, NK_EDIT_FIELD, text[0], &text_len[0], 64, nk_filter_float); + nk_label(ctx, "Hexadecimal:", NK_TEXT_RIGHT); + nk_edit_string(ctx, NK_EDIT_FIELD, text[1], &text_len[1], 64, nk_filter_hex); + nk_label(ctx, "Binary:", NK_TEXT_RIGHT); + nk_edit_string(ctx, NK_EDIT_FIELD, text[2], &text_len[2], 64, nk_filter_binary); + nk_label(ctx, "Checkbox:", NK_TEXT_RIGHT); + nk_checkbox_label(ctx, "Check me", &check); + nk_label(ctx, "Combobox:", NK_TEXT_RIGHT); + if (nk_combo_begin_label(ctx, items[selected_item], nk_vec2(nk_widget_width(ctx), 200))) { + nk_layout_row_dynamic(ctx, 25, 1); + for (i = 0; i < 3; ++i) + if (nk_combo_item_label(ctx, items[i], NK_TEXT_LEFT)) + selected_item = i; + nk_combo_end(ctx); + } + } +nk_end(ctx); + +if (nk_begin(ctx, "Menu Demo", nk_rect(600, 350, 275, 250), nuk_std)) { + nk_menubar_begin(ctx); + + nk_layout_row_dynamic(ctx, 30, 4); + + char bbbuf[256]; + snprintf(bbbuf, 256, "Current level: %s", current_level[0] == '\0' ? "Level not saved!" : current_level); + nk_label(ctx, bbbuf, NK_TEXT_LEFT); + + if (nk_menu_begin_text(ctx, "Windows", 7, NK_TEXT_LEFT, nk_vec2(100, 30))) { + nk_layout_row_dynamic(ctx, 30, 1); + + if (nk_button_label(ctx, "Resources")) editor.showAssetMenu = !editor.showAssetMenu; + if (nk_button_label(ctx, "Hierarchy")) editor.showHierarchy = !editor.showHierarchy; + + /* + ImGui::MenuItem("Lighting", "F5", &editor.showLighting); ImGui::MenuItem("Game Settings", "F6", &editor.showGameSettings); ImGui::MenuItem("View", "F7", &editor.showViewmode); @@ -490,68 +533,67 @@ void editor_project_gui() ImGui::MenuItem("Export", "F9", &editor.showExport); ImGui::MenuItem("Level", "F10", &editor.showLevel); ImGui::MenuItem("REPL", "`", &editor.showREPL); - ImGui::EndMenu(); - } - if (ImGui::BeginMenu("Levels")) { - if (ImGui::Button("New")) { - new_level(); - current_level[0] = '\0'; - } + */ + } - if (ImGui::Button("Save")) { - save_level(current_level); - get_levels(); - } + if (nk_menu_begin_text(ctx, "Levels", 100, 0, nk_vec2(100, 50))) { - if (ImGui::Button("Save as")) { - save_level(levelname); + if (nk_button_label(ctx, "New")) { + new_level(); + current_level[0] = '\0'; + } + + if (nk_button_label(ctx, "Save")) { + save_level(current_level); + get_levels(); + } + + if (nk_button_label(ctx, "Save as")) { + save_level(levelname); strcpy(current_level, levelname); levelname[0] = '\0'; get_levels(); - } - - ImGui::SameLine(); - ImGui::InputText("", levelname, MAXNAME); - - vec_walk(levels, (void (*)(void *)) &editor_level_btn); - - ImGui::EndMenu(); } - ImGui::EndMainMenuBar(); - } + // ImGui::InputText("", levelname, MAXNAME); - if (editor.showExport) { - ImGui::Begin("Export and Bake", &editor.showExport); + vec_walk(levels, (void (*)(void *)) &editor_level_btn); - if (ImGui::Button("Bake")) { + } + + nk_menubar_end(ctx); +} +nk_end(ctx); + + + if (editor.showExport && nk_begin(ctx, "Export and Bake", nk_rect_std, nuk_std)) { + + if (nk_button_label(ctx, "Bake")) { } - if (ImGui::Button("Build")) { + if (nk_button_label(ctx, "Build")) { } - ImGui::End(); + nk_end(ctx); } // Shadow map vars - if (editor.showLighting) { - ImGui::Begin("Lighting options", &editor.showLighting); - if (ImGui::CollapsingHeader("Directional shadow map")) { - ImGui::SliderFloat("Near plane", &near_plane, -200.f, 200.f, - NULL, 1.f); - ImGui::SliderFloat("Far plane", &far_plane, -200.f, 200.f, - NULL, 1.f); - ImGui::SliderFloat("Shadow Lookahead", &shadowLookahead, 0.f, - 100.f, NULL, 1.f); - ImGui::SliderFloat("Plane size", &plane_size, 0.f, 100.f, NULL, - 1.f); + if (editor.showLighting && nk_begin(ctx, "Lighting options", nk_rect_std, nuk_std)) { + nk_layout_row_dynamic(ctx, 25, 1); + if (nk_combo_begin_text(ctx, "Directional shadow map", 50, nk_vec2(nk_widget_width(ctx), 400))) { + nk_slider_float(ctx, -200.f, &near_plane, 200.f, 1.f); + nk_slider_float(ctx, -200.f, &far_plane, 200.f, 1.f); + nk_slider_float(ctx, 0.f, &shadowLookahead, 100.f, 1.f); + nk_slider_float(ctx, 0.f, &plane_size, 100.f, 1.f); } - ImGui::End(); + nk_end(ctx); } + /* + if (editor.showGameSettings) { ImGui::Begin("Game settings", &editor.showGameSettings); @@ -775,13 +817,50 @@ void editor_render() { nk_glfw3_new_frame(&nkglfw); + struct nk_colorf bg; + bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f; + if (cur_project) editor_project_gui(); else editor_proj_select_gui(); + editor_project_gui(); + + if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250), + NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| + NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) + { + enum {EASY, HARD}; + static int op = EASY; + static int property = 20; + nk_layout_row_static(ctx, 30, 80, 1); + if (nk_button_label(ctx, "button")) + fprintf(stdout, "button pressed\n"); + + nk_layout_row_dynamic(ctx, 30, 2); + if (nk_option_label(ctx, "easy", op == EASY)) op = EASY; + if (nk_option_label(ctx, "hard", op == HARD)) op = HARD; + + nk_layout_row_dynamic(ctx, 25, 1); + nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1); + + nk_layout_row_dynamic(ctx, 20, 1); + nk_label(ctx, "background:", NK_TEXT_LEFT); + nk_layout_row_dynamic(ctx, 25, 1); + if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) { + nk_layout_row_dynamic(ctx, 120, 1); + bg = nk_color_picker(ctx, bg, NK_RGBA); + nk_layout_row_dynamic(ctx, 25, 1); + bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f); + bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f); + bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f); + bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f); + nk_combo_end(ctx); + } + } +nk_end(ctx); - nk_end(nkctx); nk_glfw3_render(&nkglfw, NK_ANTI_ALIASING_ON, MAX_VERTEX_BUFFER, MAX_ELEMENT_BUFFER); } @@ -812,12 +891,12 @@ int is_allowed_extension(const char *ext) void editor_level_btn(char *level) { -/* - if (ImGui::Button(level)) { + + if (nk_button_label(ctx, level)) { load_level(level); strcpy(current_level, level); } - */ + } void editor_selectasset(struct fileasset *asset) diff --git a/source/engine/openglrender.c b/source/engine/openglrender.c index 47d22cef..678436c0 100755 --- a/source/engine/openglrender.c +++ b/source/engine/openglrender.c @@ -105,9 +105,9 @@ void openglInit() debugdraw_init(); - stdFont = MakeFont("notosans.ttf", 300); + //stdFont = MakeFont("notosans.ttf", 300); - text_settype(stdFont); + //text_settype(stdFont); //glEnable(GL_STENCIL_TEST); glClearColor(editorClearColor[0], editorClearColor[1],