@@ -1,16 +1,16 @@
in vec2 v_TextureCoord;
in vec4 v_Color;
out vec4 out_FragColor;

void main() {
vec4 TexColor;
if (en_TexturingEnabled == true) {
TexColor = texture2D( en_TexSampler, v_TextureCoord.st ) * v_Color;
} else {
TexColor = v_Color;
}
if (en_AlphaTestEnabled == true) {
if (TexColor.a<=en_AlphaTestValue) discard;
}
out_FragColor = TexColor;
}
in vec2 v_TextureCoord;
in vec4 v_Color;
out vec4 out_FragColor;

void main() {
vec4 TexColor;
if (en_TexturingEnabled == true) {
TexColor = texture2D( en_TexSampler, v_TextureCoord.st ) * v_Color;
} else {
TexColor = v_Color;
}
if (en_AlphaTestEnabled == true) {
if (TexColor.a<=en_AlphaTestValue) discard;
}
out_FragColor = TexColor;
}
@@ -1,36 +1,26 @@
#ifdef ENIGMA_GRAPHICS_OPENGLES3
#version 300 es
precision mediump float;
#elif defined ENIGMA_GRAPHICS_OPENGLES2
#version 200 es
precision mediump float;
#else
#version 330 core
#endif

#define MATRIX_VIEW 0
#define MATRIX_PROJECTION 1
#define MATRIX_WORLD 2
#define MATRIX_WORLD_VIEW 3
#define MATRIX_WORLD_VIEW_PROJECTION 4
#define MATRICES_MAX 5

uniform mat4 transform_matrix[MATRICES_MAX];
#define gm_Matrices transform_matrix
#define modelMatrix transform_matrix[MATRIX_WORLD]
#define modelViewMatrix transform_matrix[MATRIX_WORLD_VIEW]
#define projectionMatrix transform_matrix[MATRIX_PROJECTION]
#define viewMatrix transform_matrix[MATRIX_VIEW]
#define modelViewProjectionMatrix transform_matrix[MATRIX_WORLD_VIEW_PROJECTION]

uniform mat3 normalMatrix;

uniform sampler2D en_TexSampler;
#define gm_BaseTexture en_TexSampler
uniform bool en_TexturingEnabled;
uniform bool en_ColorEnabled;
uniform bool en_AlphaTestEnabled;

uniform float en_AlphaTestValue;
uniform vec4 en_BoundColor;
#line 0
#define MATRIX_VIEW 0
#define MATRIX_PROJECTION 1
#define MATRIX_WORLD 2
#define MATRIX_WORLD_VIEW 3
#define MATRIX_WORLD_VIEW_PROJECTION 4
#define MATRICES_MAX 5

uniform mat4 transform_matrix[MATRICES_MAX];
#define gm_Matrices transform_matrix
#define modelMatrix transform_matrix[MATRIX_WORLD]
#define modelViewMatrix transform_matrix[MATRIX_WORLD_VIEW]
#define projectionMatrix transform_matrix[MATRIX_PROJECTION]
#define viewMatrix transform_matrix[MATRIX_VIEW]
#define modelViewProjectionMatrix transform_matrix[MATRIX_WORLD_VIEW_PROJECTION]

uniform mat3 normalMatrix;

uniform sampler2D en_TexSampler;
#define gm_BaseTexture en_TexSampler
uniform bool en_TexturingEnabled;
uniform bool en_ColorEnabled;
uniform bool en_AlphaTestEnabled;

uniform float en_AlphaTestValue;
uniform vec4 en_BoundColor;
#line 0
@@ -0,0 +1 @@
#version 330 core
@@ -1,89 +1,89 @@
in vec3 in_Position; // (x,y,z)
in vec3 in_Normal; // (x,y,z)
in vec4 in_Color; // (r,g,b,a)
in vec2 in_TextureCoord; // (u,v)

out vec2 v_TextureCoord;
out vec4 v_Color;
uniform int en_ActiveLights;
uniform bool en_ColorEnabled;

uniform bool en_LightingEnabled;
uniform bool en_VS_FogEnabled;
uniform float en_FogStart;
uniform float en_RcpFogRange;

uniform vec4 en_BoundColor;

#define MAX_LIGHTS 8

uniform vec4 en_AmbientColor; // rgba=color

struct LightInfo {
vec4 Position; // Light position in eye coords
vec4 La; // Ambient light intensity
vec4 Ld; // Diffuse light intensity
vec4 Ls; // Specular light intensity
float cA, lA, qA; // Attenuation for point lights
};
uniform LightInfo Light[MAX_LIGHTS];

struct MaterialInfo {
vec4 Ka; // Ambient reflectivity
vec4 Kd; // Diffuse reflectivity
vec4 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
};
uniform MaterialInfo Material;

void getEyeSpace( inout vec3 norm, inout vec4 position )
{
norm = normalize( normalMatrix * in_Normal );
position = modelViewMatrix * vec4(in_Position, 1.0);
}

vec4 phongModel( in vec3 norm, in vec4 position )
{
vec4 total_light = vec4(0.0);
vec3 v = normalize(-position.xyz);
float attenuation;
for (int index = 0; index < en_ActiveLights; ++index){
vec3 L;
if (Light[index].Position.w == 0.0){ //Directional light
L = normalize(Light[index].Position.xyz);
attenuation = 1.0;
}else{ //Point light
vec3 positionToLightSource = vec3(Light[index].Position.xyz - position.xyz);
float distance = length(positionToLightSource);
L = normalize(positionToLightSource);
attenuation = 1.0 / (Light[index].cA + Light[index].lA * distance + Light[index].qA * distance * distance);
}
vec3 r = reflect( -L, norm );
total_light += Light[index].La * Material.Ka;
float LdotN = max( dot(norm, L), 0.0 );
vec4 diffuse = vec4(attenuation * vec3(Light[index].Ld) * vec3(Material.Kd) * LdotN,1.0);
vec4 spec = vec4(0.0);
if( LdotN > 0.0 )
spec = clamp(vec4(attenuation * vec3(Light[index].Ls) * vec3(Material.Ks) * pow( max( dot(r,v), 0.0 ), Material.Shininess ),1.0),0.0,1.0);
total_light += diffuse + spec;
}
return total_light;
}

void main() {
vec4 iColor = en_BoundColor;
if (en_ColorEnabled == true){
iColor = in_Color;
}
if (en_LightingEnabled == true){
vec3 eyeNorm;
vec4 eyePosition;
getEyeSpace(eyeNorm, eyePosition);
v_Color = (en_AmbientColor + phongModel( eyeNorm, eyePosition )) * iColor;
}else{
v_Color = iColor;
}
gl_Position = modelViewProjectionMatrix * vec4( in_Position.xyz, 1.0);

v_TextureCoord = in_TextureCoord;
}
in vec3 in_Position; // (x,y,z)
in vec3 in_Normal; // (x,y,z)
in vec4 in_Color; // (r,g,b,a)
in vec2 in_TextureCoord; // (u,v)

out vec2 v_TextureCoord;
out vec4 v_Color;
uniform int en_ActiveLights;
uniform bool en_ColorEnabled;

uniform bool en_LightingEnabled;
uniform bool en_VS_FogEnabled;
uniform float en_FogStart;
uniform float en_RcpFogRange;

uniform vec4 en_BoundColor;

#define MAX_LIGHTS 8

uniform vec4 en_AmbientColor; // rgba=color

struct LightInfo {
vec4 Position; // Light position in eye coords
vec4 La; // Ambient light intensity
vec4 Ld; // Diffuse light intensity
vec4 Ls; // Specular light intensity
float cA, lA, qA; // Attenuation for point lights
};
uniform LightInfo Light[MAX_LIGHTS];

struct MaterialInfo {
vec4 Ka; // Ambient reflectivity
vec4 Kd; // Diffuse reflectivity
vec4 Ks; // Specular reflectivity
float Shininess; // Specular shininess factor
};
uniform MaterialInfo Material;

void getEyeSpace( inout vec3 norm, inout vec4 position )
{
norm = normalize( normalMatrix * in_Normal );
position = modelViewMatrix * vec4(in_Position, 1.0);
}

vec4 phongModel( in vec3 norm, in vec4 position )
{
vec4 total_light = vec4(0.0);
vec3 v = normalize(-position.xyz);
float attenuation;
for (int index = 0; index < en_ActiveLights; ++index){
vec3 L;
if (Light[index].Position.w == 0.0){ //Directional light
L = normalize(Light[index].Position.xyz);
attenuation = 1.0;
}else{ //Point light
vec3 positionToLightSource = vec3(Light[index].Position.xyz - position.xyz);
float distance = length(positionToLightSource);
L = normalize(positionToLightSource);
attenuation = 1.0 / (Light[index].cA + Light[index].lA * distance + Light[index].qA * distance * distance);
}
vec3 r = reflect( -L, norm );
total_light += Light[index].La * Material.Ka;
float LdotN = max( dot(norm, L), 0.0 );
vec4 diffuse = vec4(attenuation * vec3(Light[index].Ld) * vec3(Material.Kd) * LdotN,1.0);
vec4 spec = vec4(0.0);
if( LdotN > 0.0 )
spec = clamp(vec4(attenuation * vec3(Light[index].Ls) * vec3(Material.Ks) * pow( max( dot(r,v), 0.0 ), Material.Shininess ),1.0),0.0,1.0);
total_light += diffuse + spec;
}
return total_light;
}

void main() {
vec4 iColor = en_BoundColor;
if (en_ColorEnabled == true){
iColor = in_Color;
}
if (en_LightingEnabled == true){
vec3 eyeNorm;
vec4 eyePosition;
getEyeSpace(eyeNorm, eyePosition);
v_Color = (en_AmbientColor + phongModel( eyeNorm, eyePosition )) * iColor;
}else{
v_Color = iColor;
}
gl_Position = modelViewProjectionMatrix * vec4( in_Position.xyz, 1.0);

v_TextureCoord = in_TextureCoord;
}
@@ -1,28 +1,18 @@
#ifdef ENIGMA_GRAPHICS_OPENGLES3
#version 300 es
precision mediump float;
#elif defined ENIGMA_GRAPHICS_OPENGLES2
#version 200 es
precision mediump float;
#else
#version 330 core
#endif

#define MATRIX_VIEW 0
#define MATRIX_PROJECTION 1
#define MATRIX_WORLD 2
#define MATRIX_WORLD_VIEW 3
#define MATRIX_WORLD_VIEW_PROJECTION 4
#define MATRICES_MAX 5

uniform mat4 transform_matrix[MATRICES_MAX];
#define gm_Matrices transform_matrix
#define modelMatrix transform_matrix[MATRIX_WORLD]
#define modelViewMatrix transform_matrix[MATRIX_WORLD_VIEW]
#define projectionMatrix transform_matrix[MATRIX_PROJECTION]
#define viewMatrix transform_matrix[MATRIX_VIEW]
#define modelViewProjectionMatrix transform_matrix[MATRIX_WORLD_VIEW_PROJECTION]
#define in_Colour in_Color

uniform mat3 normalMatrix;
#line 0
#define MATRIX_VIEW 0
#define MATRIX_PROJECTION 1
#define MATRIX_WORLD 2
#define MATRIX_WORLD_VIEW 3
#define MATRIX_WORLD_VIEW_PROJECTION 4
#define MATRICES_MAX 5

uniform mat4 transform_matrix[MATRICES_MAX];
#define gm_Matrices transform_matrix
#define modelMatrix transform_matrix[MATRIX_WORLD]
#define modelViewMatrix transform_matrix[MATRIX_WORLD_VIEW]
#define projectionMatrix transform_matrix[MATRIX_PROJECTION]
#define viewMatrix transform_matrix[MATRIX_VIEW]
#define modelViewProjectionMatrix transform_matrix[MATRIX_WORLD_VIEW_PROJECTION]
#define in_Colour in_Color

uniform mat3 normalMatrix;
#line 0
@@ -3,6 +3,7 @@ override CXXFLAGS += -IGraphics_Systems/OpenGLES2/glad/ -DENIGMA_GRAPHICS_OPENGL
SOURCES += $(wildcard Graphics_Systems/OpenGLES2/*.cpp) $(wildcard Graphics_Systems/OpenGL2/*.cpp) Graphics_Systems/OpenGLES2/glad/glad.c
SOURCES := $(filter-out Graphics_Systems/OpenGL2/GL2version.cpp, $(SOURCES))

SHADER_VERSION := Graphics_Systems/OpenGLES2/shaders/version.glsl
VERTEX_SHADER_PREFIX := Graphics_Systems/OpenGL2/vertex_shader_prefix.glsl
VERTEX_SHADER := Graphics_Systems/OpenGL2/vertex_shader.glsl
FRAGMENT_SHADER_PREFIX := Graphics_Systems/OpenGL2/fragment_shader_prefix.glsl
@@ -0,0 +1,2 @@
#version 100 ES
precision mediump float;
@@ -3,6 +3,7 @@ override CXXFLAGS += -IGraphics_Systems/OpenGLES3/glad/ -DENIGMA_GRAPHICS_OPENGL
SOURCES += $(wildcard Graphics_Systems/OpenGLES3/*.cpp) $(wildcard Graphics_Systems/OpenGL3/*.cpp) Graphics_Systems/OpenGLES3/glad/glad.c
SOURCES := $(filter-out Graphics_Systems/OpenGL3/GL3version.cpp, $(SOURCES))

SHADER_VERSION := Graphics_Systems/OpenGLES3/shaders/version.glsl
VERTEX_SHADER_PREFIX := Graphics_Systems/OpenGL3/vertex_shader_prefix.glsl
VERTEX_SHADER := Graphics_Systems/OpenGL3/vertex_shader.glsl
FRAGMENT_SHADER_PREFIX := Graphics_Systems/OpenGL3/fragment_shader_prefix.glsl
@@ -0,0 +1,2 @@
#version 300 es
precision mediump float;
@@ -99,7 +99,7 @@ clean:
cleandep:
$(RM) $(CLEANDEPS)

SHADER_CPP := /$(subst //,/,$(subst :,,$(CODEGEN)/shader.cpp))
SHADER_CPP := $(subst //,/,/$(subst :,,$(CODEGEN)/$(GRAPHICS)_shader.cpp))

SOURCES := $(wildcard *.cpp) $(wildcard Platforms/General/*.cpp) $(SHADER_CPP)
include $(addsuffix /Makefile,$(SYSTEMS) $(EXTENSIONS))
@@ -129,8 +129,9 @@ compile_game: cleandep $(OBJECTS) $(RCFILES) $(RESOURCEBINARY) $(DEPENDENCIES)
$(CXX) $(LDFLAGS) -o "$(OUTPUTNAME)" $(OBJECTS) $(RESOURCEBINARY) $(LDLIBS)
@echo Built to "$(OUTPUTNAME)"

$(SHADER_CPP): $(VERTEX_SHADER_PREFIX) $(VERTEX_SHADER) $(FRAGMENT_SHADER_PREFIX) $(FRAGMENT_SHADER)
./shader_build.sh $(SHADER_CPP) $(VERTEX_SHADER_PREFIX) $(VERTEX_SHADER) $(FRAGMENT_SHADER_PREFIX) $(FRAGMENT_SHADER)
#We want to update shader cpp when there are any changes to generator script or any of the files we concat
$(SHADER_CPP): ./shader_build.sh $(SHADER_VERSION) $(VERTEX_SHADER_PREFIX) $(VERTEX_SHADER) $(FRAGMENT_SHADER_PREFIX) $(FRAGMENT_SHADER)
./shader_build.sh $(SHADER_CPP) $(SHADER_VERSION) $(VERTEX_SHADER_PREFIX) $(VERTEX_SHADER) $(FRAGMENT_SHADER_PREFIX) $(FRAGMENT_SHADER)

# GCC will figure out dependencies and write out makefile rules in %.d when they change
# -MMD outputs dependencies to %.d as a side effect of compilation, ignoring system headers
@@ -1,31 +1,30 @@
#!/bin/bash

output_location="$1"
vertex_prefix="$2"
vertex_body="$3"
fragment_prefix="$4"
fragment_body="$5"
shader_version="$2"
vertex_prefix="$3"
vertex_body="$4"
fragment_prefix="$5"
fragment_body="$6"

function write_shader_source() {
file_name="$1"
variable_name="$2"
preserve_empty_lines="$3"
echo "const char* ${variable_name}=" >> $output_location
while read p; do
# in the shader prefix we don't care to preserve empty lines
# because it always ends with a line directive so that line
# numbers in the body match the ones the user sees in the IDE
if [[ ! -z ${p::-1} ]] || [ $preserve_empty_lines = true ]; then
echo -n '"' >> "$output_location"
echo -n "${p::-1}" >> "$output_location"
echo '\n"' >> "$output_location"
fi
done < "$file_name"
echo "\"\\n\";" >> "$output_location"
deref() { echo "${!1}"; }

function write_glsl_cpp() {
in="$1"
out="$2"
while read -r p; do
echo -n '"' >> "$out"
echo -n "$p" >> "$out"
echo '\n"' >> "$out"
done < "$in"
}

echo "/// Combined shader codes" > "$output_location"
write_shader_source "$vertex_prefix" "vertex_prefix" false
write_shader_source "$vertex_body" "vertex_body" true
write_shader_source "$fragment_prefix" "fragment_prefix" false
write_shader_source "$fragment_body" "fragment_body" true
echo "namespace enigma {" > "$output_location"

for var in shader_version vertex_prefix vertex_body fragment_prefix fragment_body; do
echo "const char* ${var}=" >> "$output_location"
write_glsl_cpp $(deref $var) "$output_location"
echo ";" >> "$output_location"
done

echo "} //namespace enigma" >> "$output_location"