From b82ed7fd56dded371b63ded9c1fb2c48abe21dc8 Mon Sep 17 00:00:00 2001 From: bjorn Date: Sat, 21 Oct 2017 13:14:35 -0700 Subject: [PATCH] Put shaders in their own file; --- CMakeLists.txt | 1 + src/graphics/shader.c | 83 +----------------------------------------- src/graphics/shaders.c | 83 ++++++++++++++++++++++++++++++++++++++++++ src/graphics/shaders.h | 12 ++++++ 4 files changed, 97 insertions(+), 82 deletions(-) create mode 100644 src/graphics/shaders.c create mode 100644 src/graphics/shaders.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 599b818e..cf320fb7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -243,6 +243,7 @@ set(LOVR_SRC src/graphics/mesh.c src/graphics/model.c src/graphics/shader.c + src/graphics/shaders.c src/graphics/texture.c src/headset/headset.c src/lib/glad/glad.c diff --git a/src/graphics/shader.c b/src/graphics/shader.c index 5d6ca68e..a6f605af 100644 --- a/src/graphics/shader.c +++ b/src/graphics/shader.c @@ -1,91 +1,10 @@ #include "graphics/shader.h" +#include "graphics/shaders.h" #include "graphics/graphics.h" #include "math/mat4.h" #include #include -static const char* lovrShaderVertexPrefix = "" -#ifdef EMSCRIPTEN -"#version 300 es \n" -"precision mediump float; \n" -#else -"#version 150 \n" -#endif -"in vec3 lovrPosition; \n" -"in vec3 lovrNormal; \n" -"in vec2 lovrTexCoord; \n" -"out vec2 texCoord; \n" -"uniform mat4 lovrModel; \n" -"uniform mat4 lovrView; \n" -"uniform mat4 lovrProjection; \n" -"uniform mat4 lovrTransform; \n" -"uniform mat3 lovrNormalMatrix; \n"; - -static const char* lovrShaderFragmentPrefix = "" -#ifdef EMSCRIPTEN -"#version 300 es \n" -"precision mediump float; \n" -#else -"#version 150 \n" -"in vec4 gl_FragCoord; \n" -#endif -"in vec2 texCoord; \n" -"out vec4 lovrFragColor; \n" -"uniform vec4 lovrColor; \n" -"uniform sampler2D lovrTexture; \n"; - -static const char* lovrShaderVertexSuffix = "" -"void main() { \n" -" texCoord = lovrTexCoord; \n" -" gl_Position = position(lovrProjection, lovrTransform, vec4(lovrPosition, 1.0)); \n" -"}"; - -static const char* lovrShaderFragmentSuffix = "" -"void main() { \n" -" lovrFragColor = color(lovrColor, lovrTexture, texCoord); \n" -"}"; - -static const char* lovrDefaultVertexShader = "" -"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" -" return projection * transform * vertex; \n" -"}"; - -static const char* lovrDefaultFragmentShader = "" -"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" -" return graphicsColor * texture(image, uv); \n" -"}"; - -static const char* lovrSkyboxVertexShader = "" -"out vec3 texturePosition; \n" -"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" -" texturePosition = vertex.xyz; \n" -" return projection * transform * vertex; \n" -"}"; - -static const char* lovrSkyboxFragmentShader = "" -"in vec3 texturePosition; \n" -"uniform samplerCube cube; \n" -"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" -" return graphicsColor * texture(cube, texturePosition); \n" -"}"; - -static const char* lovrFontFragmentShader = "" -"float median(float r, float g, float b) { \n" -" return max(min(r, g), min(max(r, g), b)); \n" -"} \n" -"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" -" vec3 col = texture(image, uv).rgb; \n" -" float sdf = median(col.r, col.g, col.b); \n" -" float w = fwidth(sdf); \n" -" float alpha = smoothstep(.5 - w, .5 + w, sdf); \n" -" return vec4(graphicsColor.rgb, graphicsColor.a * alpha); \n" -"}"; - -static const char* lovrNoopVertexShader = "" -"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" -" return vertex; \n" -"}"; - static GLuint compileShader(GLenum type, const char* source) { GLuint shader = glCreateShader(type); diff --git a/src/graphics/shaders.c b/src/graphics/shaders.c new file mode 100644 index 00000000..a2e06488 --- /dev/null +++ b/src/graphics/shaders.c @@ -0,0 +1,83 @@ +#include "graphics/shaders.h" + +const char* lovrShaderVertexPrefix = "" +#ifdef EMSCRIPTEN +"#version 300 es \n" +"precision mediump float; \n" +#else +"#version 150 \n" +#endif +"in vec3 lovrPosition; \n" +"in vec3 lovrNormal; \n" +"in vec2 lovrTexCoord; \n" +"out vec2 texCoord; \n" +"uniform mat4 lovrModel; \n" +"uniform mat4 lovrView; \n" +"uniform mat4 lovrProjection; \n" +"uniform mat4 lovrTransform; \n" +"uniform mat3 lovrNormalMatrix; \n"; + +const char* lovrShaderFragmentPrefix = "" +#ifdef EMSCRIPTEN +"#version 300 es \n" +"precision mediump float; \n" +#else +"#version 150 \n" +"in vec4 gl_FragCoord; \n" +#endif +"in vec2 texCoord; \n" +"out vec4 lovrFragColor; \n" +"uniform vec4 lovrColor; \n" +"uniform sampler2D lovrTexture; \n"; + +const char* lovrShaderVertexSuffix = "" +"void main() { \n" +" texCoord = lovrTexCoord; \n" +" gl_Position = position(lovrProjection, lovrTransform, vec4(lovrPosition, 1.0)); \n" +"}"; + +const char* lovrShaderFragmentSuffix = "" +"void main() { \n" +" lovrFragColor = color(lovrColor, lovrTexture, texCoord); \n" +"}"; + +const char* lovrDefaultVertexShader = "" +"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" +" return projection * transform * vertex; \n" +"}"; + +const char* lovrDefaultFragmentShader = "" +"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" +" return graphicsColor * texture(image, uv); \n" +"}"; + +const char* lovrSkyboxVertexShader = "" +"out vec3 texturePosition; \n" +"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" +" texturePosition = vertex.xyz; \n" +" return projection * transform * vertex; \n" +"}"; + +const char* lovrSkyboxFragmentShader = "" +"in vec3 texturePosition; \n" +"uniform samplerCube cube; \n" +"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" +" return graphicsColor * texture(cube, texturePosition); \n" +"}"; + +const char* lovrFontFragmentShader = "" +"float median(float r, float g, float b) { \n" +" return max(min(r, g), min(max(r, g), b)); \n" +"} \n" +"vec4 color(vec4 graphicsColor, sampler2D image, vec2 uv) { \n" +" vec3 col = texture(image, uv).rgb; \n" +" float sdf = median(col.r, col.g, col.b); \n" +" float w = fwidth(sdf); \n" +" float alpha = smoothstep(.5 - w, .5 + w, sdf); \n" +" return vec4(graphicsColor.rgb, graphicsColor.a * alpha); \n" +"}"; + +const char* lovrNoopVertexShader = "" +"vec4 position(mat4 projection, mat4 transform, vec4 vertex) { \n" +" return vertex; \n" +"}"; diff --git a/src/graphics/shaders.h b/src/graphics/shaders.h new file mode 100644 index 00000000..105f7a17 --- /dev/null +++ b/src/graphics/shaders.h @@ -0,0 +1,12 @@ +#pragma once + +extern const char* lovrShaderVertexPrefix; +extern const char* lovrShaderVertexSuffix; +extern const char* lovrShaderFragmentPrefix; +extern const char* lovrShaderFragmentSuffix; +extern const char* lovrDefaultVertexShader; +extern const char* lovrDefaultFragmentShader; +extern const char* lovrSkyboxVertexShader; +extern const char* lovrSkyboxFragmentShader; +extern const char* lovrFontFragmentShader; +extern const char* lovrNoopVertexShader;