From f8af4c44d17b94ab814f1784060388afd7a24e03 Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Sat, 26 Jan 2019 14:54:29 +0500 Subject: Removed previous implementation of Shader --- cwd/assets/altcraft/shaders/entity.json | 10 +++ cwd/assets/altcraft/shaders/face.json | 10 +++ cwd/assets/altcraft/shaders/fbo.json | 7 ++ cwd/assets/altcraft/shaders/frag/entity.fs | 9 +++ cwd/assets/altcraft/shaders/frag/face.fs | 48 ++++++++++++ cwd/assets/altcraft/shaders/frag/fbo.fs | 11 +++ cwd/assets/altcraft/shaders/frag/sky.fs | 51 +++++++++++++ cwd/assets/altcraft/shaders/sky.json | 15 ++++ cwd/assets/altcraft/shaders/vert/entity.vs | 13 ++++ cwd/assets/altcraft/shaders/vert/face.vs | 41 ++++++++++ cwd/assets/altcraft/shaders/vert/fbo.vs | 11 +++ cwd/assets/altcraft/shaders/vert/sky.vs | 17 +++++ cwd/shaders/entity.fs | 9 --- cwd/shaders/entity.vs | 13 ---- cwd/shaders/face.fs | 49 ------------ cwd/shaders/face.vs | 41 ---------- cwd/shaders/fbo.fs | 11 --- cwd/shaders/fbo.vs | 11 --- cwd/shaders/sky.fs | 51 ------------- cwd/shaders/sky.vs | 17 ----- src/AssetManager.cpp | 3 +- src/AssetManager.hpp | 2 +- src/Framebuffer.cpp | 16 ++-- src/RendererEntity.cpp | 6 +- src/RendererEntity.hpp | 5 +- src/RendererWorld.cpp | 57 ++++++-------- src/RendererWorld.hpp | 2 - src/Shader.cpp | 115 ++--------------------------- src/Shader.hpp | 35 ++++----- 29 files changed, 300 insertions(+), 386 deletions(-) create mode 100644 cwd/assets/altcraft/shaders/entity.json create mode 100644 cwd/assets/altcraft/shaders/face.json create mode 100644 cwd/assets/altcraft/shaders/fbo.json create mode 100644 cwd/assets/altcraft/shaders/frag/entity.fs create mode 100644 cwd/assets/altcraft/shaders/frag/face.fs create mode 100644 cwd/assets/altcraft/shaders/frag/fbo.fs create mode 100644 cwd/assets/altcraft/shaders/frag/sky.fs create mode 100644 cwd/assets/altcraft/shaders/sky.json create mode 100644 cwd/assets/altcraft/shaders/vert/entity.vs create mode 100644 cwd/assets/altcraft/shaders/vert/face.vs create mode 100644 cwd/assets/altcraft/shaders/vert/fbo.vs create mode 100644 cwd/assets/altcraft/shaders/vert/sky.vs delete mode 100644 cwd/shaders/entity.fs delete mode 100644 cwd/shaders/entity.vs delete mode 100644 cwd/shaders/face.fs delete mode 100644 cwd/shaders/face.vs delete mode 100644 cwd/shaders/fbo.fs delete mode 100644 cwd/shaders/fbo.vs delete mode 100644 cwd/shaders/sky.fs delete mode 100644 cwd/shaders/sky.vs diff --git a/cwd/assets/altcraft/shaders/entity.json b/cwd/assets/altcraft/shaders/entity.json new file mode 100644 index 0000000..0b65c72 --- /dev/null +++ b/cwd/assets/altcraft/shaders/entity.json @@ -0,0 +1,10 @@ +{ + "vert": "/altcraft/shaders/vert/entity", + "frag": "/altcraft/shaders/frag/entity", + "uniforms": [ + "color", + "view", + "projection", + "model" + ] +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/face.json b/cwd/assets/altcraft/shaders/face.json new file mode 100644 index 0000000..1454dcf --- /dev/null +++ b/cwd/assets/altcraft/shaders/face.json @@ -0,0 +1,10 @@ +{ + "vert": "/altcraft/shaders/vert/face", + "frag": "/altcraft/shaders/frag/face", + "uniforms": [ + "projView", + "textureAtlas", + "DayTime", + "MinLightLevel" + ] +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/fbo.json b/cwd/assets/altcraft/shaders/fbo.json new file mode 100644 index 0000000..d9f8734 --- /dev/null +++ b/cwd/assets/altcraft/shaders/fbo.json @@ -0,0 +1,7 @@ +{ + "vert": "/altcraft/shaders/vert/fbo", + "frag": "/altcraft/shaders/frag/fbo", + "uniforms": [ + "inputTexture" + ] +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/frag/entity.fs b/cwd/assets/altcraft/shaders/frag/entity.fs new file mode 100644 index 0000000..f0bb42f --- /dev/null +++ b/cwd/assets/altcraft/shaders/frag/entity.fs @@ -0,0 +1,9 @@ +#version 330 core + +uniform vec3 color; +in vec2 uvPos; + +void main(){ + //if (uvPos.x < 0.9 && uvPos.x > 0.1 && uvPos.y < 0.9 && uvPos.y > 0.1) discard; + gl_FragColor = vec4(color,1); +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/frag/face.fs b/cwd/assets/altcraft/shaders/frag/face.fs new file mode 100644 index 0000000..98b389a --- /dev/null +++ b/cwd/assets/altcraft/shaders/frag/face.fs @@ -0,0 +1,48 @@ +#version 330 core + +in VS_OUT { + vec2 UvPosition; + vec3 Texture; + vec3 Color; + vec2 Light; +} fs_in; + +uniform sampler2DArray textureAtlas; +uniform float DayTime; +uniform float MinLightLevel; + +vec3 rgb2hsv(vec3 c) +{ + vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); + vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); + vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); + + float d = q.x - min(q.w, q.y); + float e = 1.0e-10; + return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); +} + +vec3 hsv2rgb(vec3 c) +{ + vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); + vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); + return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); +} + +void main() { + vec4 color = texture(textureAtlas,fs_in.Texture); + if (color.a < 0.3) + discard; + + vec3 hsvColor = rgb2hsv(color.xyz); + hsvColor+=fs_in.Color; + color = vec4(hsv2rgb(hsvColor),1); + + float light = fs_in.Light.x / 15.0; + float skyLight = (fs_in.Light.y / 15.0) * DayTime; + + float faceLight = clamp(light + skyLight,MinLightLevel,1.0); + + color = vec4(color.rgb * faceLight, color.a); + gl_FragColor = color; +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/frag/fbo.fs b/cwd/assets/altcraft/shaders/frag/fbo.fs new file mode 100644 index 0000000..df624b3 --- /dev/null +++ b/cwd/assets/altcraft/shaders/frag/fbo.fs @@ -0,0 +1,11 @@ +#version 330 core + +out vec4 FragColor; +in vec2 TexCoords; + +uniform sampler2D inputTexture; + +void main() +{ + FragColor = texture(inputTexture, TexCoords); +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/frag/sky.fs b/cwd/assets/altcraft/shaders/frag/sky.fs new file mode 100644 index 0000000..36d65a5 --- /dev/null +++ b/cwd/assets/altcraft/shaders/frag/sky.fs @@ -0,0 +1,51 @@ +#version 330 core + +in vec2 uvPos; +in vec3 pos; + +uniform sampler2DArray textureAtlas; +uniform float DayTime; +uniform vec4 sunTexture; +uniform float sunTextureLayer; +uniform vec4 moonTexture; +uniform float moonTextureLayer; + +const vec4 DaySkyColor = vec4(0.49,0.66,1, 1); + +const vec3 SunPos = vec3(0,0.1,0.5); + +const vec3 MoonPos = vec3(0,0.1,-0.5); + +vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { + float x = TextureAtlasCoords.x; + float y = TextureAtlasCoords.y; + float w = TextureAtlasCoords.z; + float h = TextureAtlasCoords.w; + vec2 A = vec2(x, 1 - y - h); + vec2 B = vec2(x + w, 1 - y); + vec2 transformed = A + UvCoords * (B - A); + return vec3(transformed.x, transformed.y, Layer); +} + +vec4 Sun() { + vec3 sunDelta = (pos - SunPos)*3.0f; + float distanceToSun = length(sunDelta); + vec4 sunColor = texture(textureAtlas,TransformTextureCoord(sunTexture,(vec2(sunDelta.xy)+0.5f),sunTextureLayer)); + vec4 sun = mix(vec4(0,0,0,1),sunColor,clamp(1-distanceToSun*2.0f,0,1)); + return sun; +} + +vec4 Moon() { + vec3 moonDelta = (pos - MoonPos)*4.5f; + float distanceToMoon = length(moonDelta); + vec4 moonColor = texture(textureAtlas,TransformTextureCoord(moonTexture,(vec2(moonDelta.xy)+0.5f),moonTextureLayer)); + vec4 moon = mix(vec4(0,0,0,1),moonColor,clamp(1-distanceToMoon*2.0f,0,1)); + return moon; +} + +void main() { + vec4 starColor = vec4(0.0f, 0.04f, 0.06f, 1.0f); + gl_FragColor = mix(starColor, DaySkyColor, DayTime); + gl_FragColor += Sun(); + gl_FragColor += Moon(); +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/sky.json b/cwd/assets/altcraft/shaders/sky.json new file mode 100644 index 0000000..6e53db6 --- /dev/null +++ b/cwd/assets/altcraft/shaders/sky.json @@ -0,0 +1,15 @@ +{ + "vert": "/altcraft/shaders/vert/sky", + "frag": "/altcraft/shaders/frag/sky", + "uniforms": [ + "view", + "projection", + "model", + "textureAtlas", + "DayTime", + "sunTexture", + "sunTextureLayer", + "moonTexture", + "moonTextureLayer" + ] +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/vert/entity.vs b/cwd/assets/altcraft/shaders/vert/entity.vs new file mode 100644 index 0000000..baa25e5 --- /dev/null +++ b/cwd/assets/altcraft/shaders/vert/entity.vs @@ -0,0 +1,13 @@ +#version 330 core + +uniform mat4 view; +uniform mat4 projection; +uniform mat4 model; +layout (location = 0) in vec3 position; +layout (location = 1) in vec2 uvPosition; +out vec2 uvPos; + +void main(){ + uvPos = uvPosition; + gl_Position = projection*view*model*vec4(position,1); +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/vert/face.vs b/cwd/assets/altcraft/shaders/vert/face.vs new file mode 100644 index 0000000..9c40846 --- /dev/null +++ b/cwd/assets/altcraft/shaders/vert/face.vs @@ -0,0 +1,41 @@ +#version 330 core +layout (location = 0) in vec3 position; +layout (location = 2) in vec2 UvCoordinates; +layout (location = 7) in vec4 Texture; +layout (location = 8) in mat4 model; +layout (location = 12) in vec3 color; +layout (location = 13) in vec2 light; +layout (location = 14) in float TextureLayer; + +out VS_OUT { + vec2 UvPosition; + vec3 Texture; + vec3 Color; + vec2 Light; +} vs_out; + +//uniform mat4 view; +//uniform mat4 projection; +uniform mat4 projView; + +vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { + float x = TextureAtlasCoords.x; + float y = TextureAtlasCoords.y; + float w = TextureAtlasCoords.z; + float h = TextureAtlasCoords.w; + vec2 A = vec2(x, 1 - y - h); + vec2 B = vec2(x + w, 1 - y); + vec2 transformed = A + UvCoords * (B - A); + return vec3(transformed.x, transformed.y, Layer); +} + +void main() +{ + vec4 sourcePosition = vec4(position,1.0f); + gl_Position = projView * model * sourcePosition; + + vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); + vs_out.Texture = TransformTextureCoord(Texture,UvCoordinates,TextureLayer); + vs_out.Color = color; + vs_out.Light = light; +} diff --git a/cwd/assets/altcraft/shaders/vert/fbo.vs b/cwd/assets/altcraft/shaders/vert/fbo.vs new file mode 100644 index 0000000..e1e8966 --- /dev/null +++ b/cwd/assets/altcraft/shaders/vert/fbo.vs @@ -0,0 +1,11 @@ +#version 330 core +layout (location = 0) in vec2 Pos; +layout (location = 1) in vec2 TextureCoords; + +out vec2 TexCoords; + +void main() +{ + gl_Position = vec4(Pos.x, Pos.y, 0.0, 1.0); + TexCoords = TextureCoords; +} \ No newline at end of file diff --git a/cwd/assets/altcraft/shaders/vert/sky.vs b/cwd/assets/altcraft/shaders/vert/sky.vs new file mode 100644 index 0000000..983e1f3 --- /dev/null +++ b/cwd/assets/altcraft/shaders/vert/sky.vs @@ -0,0 +1,17 @@ +#version 330 core + +uniform mat4 view; +uniform mat4 projection; +uniform mat4 model; + +layout (location = 0) in vec3 position; +layout (location = 1) in vec2 uvPosition; + +out vec2 uvPos; +out vec3 pos; + +void main(){ + uvPos = uvPosition; + pos = position; + gl_Position = projection*view*model*vec4(position,1); +} \ No newline at end of file diff --git a/cwd/shaders/entity.fs b/cwd/shaders/entity.fs deleted file mode 100644 index f0bb42f..0000000 --- a/cwd/shaders/entity.fs +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core - -uniform vec3 color; -in vec2 uvPos; - -void main(){ - //if (uvPos.x < 0.9 && uvPos.x > 0.1 && uvPos.y < 0.9 && uvPos.y > 0.1) discard; - gl_FragColor = vec4(color,1); -} \ No newline at end of file diff --git a/cwd/shaders/entity.vs b/cwd/shaders/entity.vs deleted file mode 100644 index baa25e5..0000000 --- a/cwd/shaders/entity.vs +++ /dev/null @@ -1,13 +0,0 @@ -#version 330 core - -uniform mat4 view; -uniform mat4 projection; -uniform mat4 model; -layout (location = 0) in vec3 position; -layout (location = 1) in vec2 uvPosition; -out vec2 uvPos; - -void main(){ - uvPos = uvPosition; - gl_Position = projection*view*model*vec4(position,1); -} \ No newline at end of file diff --git a/cwd/shaders/face.fs b/cwd/shaders/face.fs deleted file mode 100644 index 4872f06..0000000 --- a/cwd/shaders/face.fs +++ /dev/null @@ -1,49 +0,0 @@ -#version 330 core - -in VS_OUT { - vec2 UvPosition; - vec3 Texture; - vec3 Color; - vec2 Light; -} fs_in; - -uniform sampler2DArray textureAtlas; -uniform vec2 windowSize; -uniform float DayTime; -uniform float MinLightLevel; - -vec3 rgb2hsv(vec3 c) -{ - vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); - vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); - vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); - - float d = q.x - min(q.w, q.y); - float e = 1.0e-10; - return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); -} - -vec3 hsv2rgb(vec3 c) -{ - vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); - vec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www); - return c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); -} - -void main() { - vec4 color = texture(textureAtlas,fs_in.Texture); - if (color.a < 0.3) - discard; - - vec3 hsvColor = rgb2hsv(color.xyz); - hsvColor+=fs_in.Color; - color = vec4(hsv2rgb(hsvColor),1); - - float light = fs_in.Light.x / 15.0; - float skyLight = (fs_in.Light.y / 15.0) * DayTime; - - float faceLight = clamp(light + skyLight,MinLightLevel,1.0); - - color = vec4(color.rgb * faceLight, color.a); - gl_FragColor = color; -} \ No newline at end of file diff --git a/cwd/shaders/face.vs b/cwd/shaders/face.vs deleted file mode 100644 index 9c40846..0000000 --- a/cwd/shaders/face.vs +++ /dev/null @@ -1,41 +0,0 @@ -#version 330 core -layout (location = 0) in vec3 position; -layout (location = 2) in vec2 UvCoordinates; -layout (location = 7) in vec4 Texture; -layout (location = 8) in mat4 model; -layout (location = 12) in vec3 color; -layout (location = 13) in vec2 light; -layout (location = 14) in float TextureLayer; - -out VS_OUT { - vec2 UvPosition; - vec3 Texture; - vec3 Color; - vec2 Light; -} vs_out; - -//uniform mat4 view; -//uniform mat4 projection; -uniform mat4 projView; - -vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { - float x = TextureAtlasCoords.x; - float y = TextureAtlasCoords.y; - float w = TextureAtlasCoords.z; - float h = TextureAtlasCoords.w; - vec2 A = vec2(x, 1 - y - h); - vec2 B = vec2(x + w, 1 - y); - vec2 transformed = A + UvCoords * (B - A); - return vec3(transformed.x, transformed.y, Layer); -} - -void main() -{ - vec4 sourcePosition = vec4(position,1.0f); - gl_Position = projView * model * sourcePosition; - - vs_out.UvPosition = vec2(UvCoordinates.x,UvCoordinates.y); - vs_out.Texture = TransformTextureCoord(Texture,UvCoordinates,TextureLayer); - vs_out.Color = color; - vs_out.Light = light; -} diff --git a/cwd/shaders/fbo.fs b/cwd/shaders/fbo.fs deleted file mode 100644 index df624b3..0000000 --- a/cwd/shaders/fbo.fs +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core - -out vec4 FragColor; -in vec2 TexCoords; - -uniform sampler2D inputTexture; - -void main() -{ - FragColor = texture(inputTexture, TexCoords); -} \ No newline at end of file diff --git a/cwd/shaders/fbo.vs b/cwd/shaders/fbo.vs deleted file mode 100644 index e1e8966..0000000 --- a/cwd/shaders/fbo.vs +++ /dev/null @@ -1,11 +0,0 @@ -#version 330 core -layout (location = 0) in vec2 Pos; -layout (location = 1) in vec2 TextureCoords; - -out vec2 TexCoords; - -void main() -{ - gl_Position = vec4(Pos.x, Pos.y, 0.0, 1.0); - TexCoords = TextureCoords; -} \ No newline at end of file diff --git a/cwd/shaders/sky.fs b/cwd/shaders/sky.fs deleted file mode 100644 index 36d65a5..0000000 --- a/cwd/shaders/sky.fs +++ /dev/null @@ -1,51 +0,0 @@ -#version 330 core - -in vec2 uvPos; -in vec3 pos; - -uniform sampler2DArray textureAtlas; -uniform float DayTime; -uniform vec4 sunTexture; -uniform float sunTextureLayer; -uniform vec4 moonTexture; -uniform float moonTextureLayer; - -const vec4 DaySkyColor = vec4(0.49,0.66,1, 1); - -const vec3 SunPos = vec3(0,0.1,0.5); - -const vec3 MoonPos = vec3(0,0.1,-0.5); - -vec3 TransformTextureCoord(vec4 TextureAtlasCoords, vec2 UvCoords, float Layer) { - float x = TextureAtlasCoords.x; - float y = TextureAtlasCoords.y; - float w = TextureAtlasCoords.z; - float h = TextureAtlasCoords.w; - vec2 A = vec2(x, 1 - y - h); - vec2 B = vec2(x + w, 1 - y); - vec2 transformed = A + UvCoords * (B - A); - return vec3(transformed.x, transformed.y, Layer); -} - -vec4 Sun() { - vec3 sunDelta = (pos - SunPos)*3.0f; - float distanceToSun = length(sunDelta); - vec4 sunColor = texture(textureAtlas,TransformTextureCoord(sunTexture,(vec2(sunDelta.xy)+0.5f),sunTextureLayer)); - vec4 sun = mix(vec4(0,0,0,1),sunColor,clamp(1-distanceToSun*2.0f,0,1)); - return sun; -} - -vec4 Moon() { - vec3 moonDelta = (pos - MoonPos)*4.5f; - float distanceToMoon = length(moonDelta); - vec4 moonColor = texture(textureAtlas,TransformTextureCoord(moonTexture,(vec2(moonDelta.xy)+0.5f),moonTextureLayer)); - vec4 moon = mix(vec4(0,0,0,1),moonColor,clamp(1-distanceToMoon*2.0f,0,1)); - return moon; -} - -void main() { - vec4 starColor = vec4(0.0f, 0.04f, 0.06f, 1.0f); - gl_FragColor = mix(starColor, DaySkyColor, DayTime); - gl_FragColor += Sun(); - gl_FragColor += Moon(); -} \ No newline at end of file diff --git a/cwd/shaders/sky.vs b/cwd/shaders/sky.vs deleted file mode 100644 index 983e1f3..0000000 --- a/cwd/shaders/sky.vs +++ /dev/null @@ -1,17 +0,0 @@ -#version 330 core - -uniform mat4 view; -uniform mat4 projection; -uniform mat4 model; - -layout (location = 0) in vec3 position; -layout (location = 1) in vec2 uvPosition; - -out vec2 uvPos; -out vec3 pos; - -void main(){ - uvPos = uvPosition; - pos = position; - gl_Position = projection*view*model*vec4(position,1); -} \ No newline at end of file diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 5f2a160..3765f02 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -365,8 +365,9 @@ void ParseAssetShader(AssetTreeNode &node) { node.asset = std::make_unique(); AssetShader *asset = dynamic_cast(node.asset.get()); - asset->shader = std::make_unique(vertSource, fragSource, uniforms); + asset->shader = std::make_unique(vertSource, fragSource, uniforms); } catch (...) { + glCheckError(); return; } } diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index 3b4ad2f..852378f 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -162,7 +162,7 @@ struct AssetTexture : Asset { }; struct AssetShader : Asset { - std::unique_ptr shader; + std::unique_ptr shader; }; namespace AssetManager { diff --git a/src/Framebuffer.cpp b/src/Framebuffer.cpp index 7127a57..fb7ebc7 100644 --- a/src/Framebuffer.cpp +++ b/src/Framebuffer.cpp @@ -2,12 +2,13 @@ #include "Shader.hpp" #include #include "Utility.hpp" +#include "AssetManager.hpp" -GLuint quadVao, quadVbo; -Shader *quadShader = nullptr; +const GLuint magic = 316784; +GLuint quadVao = magic, quadVbo = magic; Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDepthStencilBuffer) : width(width), height(height) { - if (quadShader == nullptr) { + if (quadVao == magic) { float quadVertices[] = { // positions // texCoords -1.0f, 1.0f, 0.0f, 1.0f, @@ -28,9 +29,8 @@ Framebuffer::Framebuffer(unsigned int width, unsigned int height, bool createDep glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)(2 * sizeof(float))); - quadShader = new Shader("./shaders/fbo.vs", "./shaders/fbo.fs"); - quadShader->Use(); - glUniform1i(glGetUniformLocation(quadShader->Program, "inputTexture"), 1); + AssetManager::GetAsset("/altcraft/shaders/fbo")->shader->Activate(); + AssetManager::GetAsset("/altcraft/shaders/fbo")->shader->SetUniform("inputTexture", 1); glActiveTexture(GL_TEXTURE1); glCheckError(); } @@ -76,9 +76,9 @@ void Framebuffer::Activate() { void Framebuffer::RenderTo(Framebuffer &target) { glBindFramebuffer(GL_FRAMEBUFFER, target.fbo); - glViewport(0, 0, target.width, target.height); + glViewport(0, 0, target.width, target.height); + AssetManager::GetAsset("/altcraft/shaders/fbo")->shader->Activate(); glBindVertexArray(quadVao); - glUseProgram(quadShader->Program); glBindTexture(GL_TEXTURE_2D, texColor); glDrawArrays(GL_TRIANGLES, 0, 6); } diff --git a/src/RendererEntity.cpp b/src/RendererEntity.cpp index d465a07..a1c9566 100644 --- a/src/RendererEntity.cpp +++ b/src/RendererEntity.cpp @@ -6,6 +6,7 @@ #include "Entity.hpp" #include "World.hpp" #include "Renderer.hpp" +#include "AssetManager.hpp" const GLfloat vertices[] = { -0.5f, 0.5f, 0.5f, @@ -129,8 +130,9 @@ void RendererEntity::Render(RenderState & renderState) { model = glm::translate(model, glm::vec3(0, entity.height / 2.0, 0)); model = glm::scale(model, glm::vec3(entity.width, entity.height, entity.width)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform3f(colorLoc, entity.renderColor.x, entity.renderColor.y, entity.renderColor.z); + Shader *entityShader = AssetManager::GetAsset("/altcraft/shaders/entity")->shader.get(); + entityShader->SetUniform("model", model); + entityShader->SetUniform("color", entity.renderColor); glCheckError(); glDrawArrays(GL_LINES, 0, 24); diff --git a/src/RendererEntity.hpp b/src/RendererEntity.hpp index 51d4163..e2e8bf1 100644 --- a/src/RendererEntity.hpp +++ b/src/RendererEntity.hpp @@ -12,10 +12,7 @@ public: RendererEntity(World *ptr, unsigned int id); ~RendererEntity(); - void Render(RenderState& renderState); - - GLint modelLoc = 0; - GLint colorLoc = 0; + void Render(RenderState& renderState); static GLuint GetVao(); }; diff --git a/src/RendererWorld.cpp b/src/RendererWorld.cpp index 7f47a1e..75859e9 100644 --- a/src/RendererWorld.cpp +++ b/src/RendererWorld.cpp @@ -245,7 +245,8 @@ RendererWorld::RendererWorld(GameState* ptr) { listener->RegisterHandler("SetMinLightLevel", [this](const Event& eventData) { auto value = eventData.get(); - glUniform1f(glGetUniformLocation(blockShader->Program, "MinLightLevel"), value); + AssetManager::GetAsset("/altcraft/shaders/face")->shader->Activate(); + AssetManager::GetAsset("/altcraft/shaders/face")->shader->SetUniform("MinLightLevel", value); }); for (int i = 0; i < numOfWorkers; i++) @@ -263,15 +264,12 @@ RendererWorld::~RendererWorld() { isRunning = false; for (int i = 0; i < numOfWorkers; i++) workers[i].join(); - delete blockShader; - delete entityShader; DebugInfo::renderSections = 0; DebugInfo::readyRenderer = 0; } void RendererWorld::Render(RenderState & renderState) { //Common - GLint projectionLoc, viewLoc, modelLoc, pvLoc, windowSizeLoc, colorLoc; glm::mat4 projection = glm::perspective( glm::radians(70.0f), (float) renderState.WindowWidth / (float) renderState.WindowHeight, 0.1f, 10000000.0f @@ -281,20 +279,14 @@ void RendererWorld::Render(RenderState & renderState) { //Render Entities glLineWidth(3.0); - renderState.SetActiveShader(entityShader->Program); + Shader *entityShader = AssetManager::GetAsset("/altcraft/shaders/entity")->shader.get(); + entityShader->Activate(); + entityShader->SetUniform("projection", projection); + entityShader->SetUniform("view", view); glCheckError(); - projectionLoc = glGetUniformLocation(entityShader->Program, "projection"); - viewLoc = glGetUniformLocation(entityShader->Program, "view"); - glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection)); - glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view)); - glCheckError(); - modelLoc = glGetUniformLocation(entityShader->Program, "model"); - colorLoc = glGetUniformLocation(entityShader->Program, "color"); renderState.SetActiveVao(RendererEntity::GetVao()); for (auto& it : entities) { - it.modelLoc = modelLoc; - it.colorLoc = colorLoc; it.Render(renderState); } @@ -307,8 +299,8 @@ void RendererWorld::Render(RenderState & renderState) { model = glm::translate(model, selectedBlock.glm()); model = glm::translate(model,glm::vec3(0.5f,0.5f,0.5f)); model = glm::scale(model,glm::vec3(1.01f,1.01f,1.01f)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); - glUniform3f(colorLoc, 0.0, 0.0, 0.0); + entityShader->SetUniform("model", model); + entityShader->SetUniform("color", glm::vec3(0, 0, 0)); glCheckError(); glDrawArrays(GL_LINES, 0, 24); } @@ -323,11 +315,11 @@ void RendererWorld::Render(RenderState & renderState) { glm::mat4 model; model = glm::translate(model, hit.glm()); model = glm::scale(model,glm::vec3(0.3f,0.3f,0.3f)); - glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model)); + entityShader->SetUniform("model", model); if (selectedBlock == Vector()) - glUniform3f(colorLoc,0.7,0.0,0.0); + entityShader->SetUniform("color", glm::vec3(0.7f, 0, 0)); else - glUniform3f(colorLoc, 0.0, 0.0, 0.7); + entityShader->SetUniform("color", glm::vec3(0, 0, 0.7f)); glCheckError(); glDrawArrays(GL_LINE_STRIP, 0, 36); } @@ -338,7 +330,7 @@ void RendererWorld::Render(RenderState & renderState) { //Render sky renderState.TimeOfDay = gs->TimeOfDay; - NewShader *skyShader = AssetManager::GetAsset("/altcraft/shaders/sky")->shader.get(); + Shader *skyShader = AssetManager::GetAsset("/altcraft/shaders/sky")->shader.get(); skyShader->Activate(); skyShader->SetUniform("projection", projection); skyShader->SetUniform("view", view); @@ -385,15 +377,10 @@ void RendererWorld::Render(RenderState & renderState) { glCheckError(); //Render sections - renderState.SetActiveShader(blockShader->Program); - projectionLoc = glGetUniformLocation(blockShader->Program, "projection"); - viewLoc = glGetUniformLocation(blockShader->Program, "view"); - windowSizeLoc = glGetUniformLocation(blockShader->Program, "windowSize"); - pvLoc = glGetUniformLocation(blockShader->Program, "projView"); - - glUniform1f(glGetUniformLocation(blockShader->Program, "DayTime"), mixLevel); - glUniformMatrix4fv(pvLoc, 1, GL_FALSE, glm::value_ptr(projView)); - glUniform2f(windowSizeLoc, renderState.WindowWidth, renderState.WindowHeight); + Shader *blockShader = AssetManager::GetAsset("/altcraft/shaders/face")->shader.get(); + blockShader->Activate(); + blockShader->SetUniform("DayTime", mixLevel); + blockShader->SetUniform("projView", projView); glCheckError(); Frustum frustum(projView); @@ -419,19 +406,17 @@ void RendererWorld::Render(RenderState & renderState) { } void RendererWorld::PrepareRender() { - blockShader = new Shader("./shaders/face.vs", "./shaders/face.fs"); - blockShader->Use(); - glUniform1i(glGetUniformLocation(blockShader->Program, "textureAtlas"), 0); - glUniform1f(glGetUniformLocation(blockShader->Program, "MinLightLevel"), 0.2f); - - entityShader = new Shader("./shaders/entity.vs", "./shaders/entity.fs"); + Shader *blockShader = AssetManager::GetAsset("/altcraft/shaders/face")->shader.get(); + blockShader->Activate(); + blockShader->SetUniform("textureAtlas", 0); + blockShader->SetUniform("MinLightLevel", 0.2f); TextureCoord sunTexture = AssetManager::GetTexture("/minecraft/textures/environment/sun"); TextureCoord moonTexture = AssetManager::GetTexture("/minecraft/textures/environment/moon_phases"); moonTexture.w /= 4.0f; //First phase will be fine for now moonTexture.h /= 2.0f; - NewShader *sky = AssetManager::GetAsset("/altcraft/shaders/sky")->shader.get(); + Shader *sky = AssetManager::GetAsset("/altcraft/shaders/sky")->shader.get(); sky->Activate(); sky->SetUniform("textureAtlas", 0); sky->SetUniform("sunTexture", glm::vec4(sunTexture.x, sunTexture.y, sunTexture.w, sunTexture.h)); diff --git a/src/RendererWorld.hpp b/src/RendererWorld.hpp index d8b74e6..dbdd852 100644 --- a/src/RendererWorld.hpp +++ b/src/RendererWorld.hpp @@ -43,10 +43,8 @@ class RendererWorld { //Blocks std::vector renderList; std::map sections; - Shader *blockShader; void UpdateAllSections(VectorF playerPos); //Entities - Shader *entityShader; std::vector entities; //Sky Texture *skyTexture; diff --git a/src/Shader.cpp b/src/Shader.cpp index 9447131..08866e1 100644 --- a/src/Shader.cpp +++ b/src/Shader.cpp @@ -5,114 +5,8 @@ #include -Shader::Shader(const GLchar *vertexPath, const GLchar *fragmentPath, const GLchar *geometryPath) { - vertex = vertexPath; - fragment = fragmentPath; - - std::string vertexCode; - std::string fragmentCode; - std::string geometryCode; - std::ifstream vShaderFile; - std::ifstream fShaderFile; - std::ifstream gShaderFile; - - vShaderFile.exceptions(std::ifstream::failbit); - fShaderFile.exceptions(std::ifstream::failbit); - gShaderFile.exceptions(std::ifstream::failbit); - try { - vShaderFile.open(vertexPath); - fShaderFile.open(fragmentPath); - if (geometryPath != nullptr) - gShaderFile.open(geometryPath); - std::stringstream vShaderStream, fShaderStream, gShaderStream; - - vShaderStream << vShaderFile.rdbuf(); - fShaderStream << fShaderFile.rdbuf(); - if (geometryPath != nullptr) - gShaderStream << gShaderFile.rdbuf(); - - vShaderFile.close(); - fShaderFile.close(); - if (geometryPath != nullptr) - gShaderFile.close(); - - vertexCode = vShaderStream.str(); - fragmentCode = fShaderStream.str(); - if (geometryPath != nullptr) - geometryCode = gShaderStream.str(); - } - catch (std::ifstream::failure e) { - LOG(ERROR) << "Shader source not readed"; - } - const GLchar *vShaderCode = vertexCode.c_str(); - const GLchar *fShaderCode = fragmentCode.c_str(); - const GLchar *gShaderCode = geometryCode.c_str(); - - GLuint vertex, fragment, geometry; - GLint success; - GLchar infoLog[512]; - vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); - glCompileShader(vertex); - - glGetShaderiv(vertex, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(vertex, 512, NULL, infoLog); - LOG(ERROR) << "Vertex shader compilation failed: " << infoLog; - }; - - fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); - glCompileShader(fragment); - - glGetShaderiv(fragment, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(fragment, 512, NULL, infoLog); - LOG(ERROR) << "Fragment shader compilation failed: " << infoLog; - }; - - if (geometryPath != nullptr) { - geometry = glCreateShader(GL_GEOMETRY_SHADER); - glShaderSource(geometry, 1, &gShaderCode, NULL); - glCompileShader(geometry); - - glGetShaderiv(geometry, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(geometry, 512, NULL, infoLog); - LOG(ERROR) << "Geometry shader compilation failed: " << infoLog; - }; - } - - this->Program = glCreateProgram(); - glAttachShader(this->Program, vertex); - glAttachShader(this->Program, fragment); - if (geometryPath != nullptr) - glAttachShader(this->Program, geometry); - glLinkProgram(this->Program); - glGetProgramiv(this->Program, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(this->Program, 512, NULL, infoLog); - LOG(FATAL) << "Shader program not linked: " << infoLog; - } - - glDeleteShader(vertex); - glDeleteShader(fragment); -} - -void Shader::Use() { - glUseProgram(this->Program); -} - -void Shader::Reload() { - const GLchar *vertexPath = vertex; - const GLchar *fragmentPath = fragment; - this->~Shader(); - new(this) Shader(vertexPath, fragmentPath); - LOG(INFO) << "Shader is realoded!"; -} - -GLuint NewShader::GetUniformLocation(const std::string &name) { +GLuint Shader::GetUniformLocation(const std::string &name) { auto it = uniforms.find(name); if (it == uniforms.end()) { LOG(ERROR) << "Accessed not existing uniform " << name; @@ -121,7 +15,7 @@ GLuint NewShader::GetUniformLocation(const std::string &name) { return it->second; } -NewShader::NewShader(const std::string &vertSource, const std::string &fragSource, const std::vector &uniformsNames) +Shader::Shader(const std::string &vertSource, const std::string &fragSource, const std::vector &uniformsNames) { bool vertFailed = false, fragFailed = false, linkFailed = false, uniformsFailed = false; const GLchar *vertSourcePtr = vertSource.c_str(); @@ -173,6 +67,7 @@ NewShader::NewShader(const std::string &vertSource, const std::string &fragSourc if (linkFailed) throw std::runtime_error("Shader not linked"); + glUseProgram(program); for (auto &it : uniformsNames) { GLuint location = glGetUniformLocation(program, it.c_str()); @@ -186,13 +81,13 @@ NewShader::NewShader(const std::string &vertSource, const std::string &fragSourc } } -NewShader::~NewShader() +Shader::~Shader() { if (program) glDeleteProgram(program); } -void NewShader::Activate() +void Shader::Activate() { glUseProgram(program); } diff --git a/src/Shader.hpp b/src/Shader.hpp index b551602..d70d394 100644 --- a/src/Shader.hpp +++ b/src/Shader.hpp @@ -7,34 +7,21 @@ #include #include -class Shader -{ -private: - const GLchar *vertex; - const GLchar *fragment; -public: - GLuint Program; - Shader(const GLchar* vertexPath, const GLchar* fragmentPath, const GLchar* geometryPath = nullptr); - void Use(); - - void Reload(); -}; - -class NewShader { +class Shader { std::map uniforms; GLuint program = 0; GLuint GetUniformLocation(const std::string &name); public: - NewShader(const NewShader &) = delete; - NewShader(NewShader &&other) = delete; - NewShader &operator=(const NewShader &) = delete; - NewShader &operator=(NewShader &&other) = delete; + Shader(const Shader &) = delete; + Shader(Shader &&other) = delete; + Shader &operator=(const Shader &) = delete; + Shader &operator=(Shader &&other) = delete; - NewShader(const std::string &vertSource, const std::string &fragSource, const std::vector &uniformsNames); + Shader(const std::string &vertSource, const std::string &fragSource, const std::vector &uniformsNames); - ~NewShader(); + ~Shader(); void Activate(); @@ -46,6 +33,14 @@ public: glUniform1f(GetUniformLocation(name), val); } + inline void SetUniform(const std::string &name, glm::vec2 val) { + glUniform2f(GetUniformLocation(name), val.x, val.y); + } + + inline void SetUniform(const std::string &name, glm::vec3 val) { + glUniform3f(GetUniformLocation(name), val.x, val.y, val.z); + } + inline void SetUniform(const std::string &name, glm::vec4 val) { glUniform4f(GetUniformLocation(name), val.x, val.y, val.z, val.w); } -- cgit v1.2.3