Skip to content

Commit b74f0fa

Browse files
committed
Added GLSL 3.3 shaders
1 parent 2cbfc1b commit b74f0fa

File tree

3 files changed

+219
-0
lines changed

3 files changed

+219
-0
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ install(DIRECTORY icons DESTINATION ${COLOBOT_INSTALL_DATA_DIR})
2020
install(DIRECTORY models DESTINATION ${COLOBOT_INSTALL_DATA_DIR})
2121
install(DIRECTORY sounds DESTINATION ${COLOBOT_INSTALL_DATA_DIR})
2222
install(DIRECTORY textures DESTINATION ${COLOBOT_INSTALL_DATA_DIR})
23+
install(DIRECTORY shaders DESTINATION ${COLOBOT_INSTALL_DATA_DIR})
2324
install(DIRECTORY DESTINATION ${COLOBOT_INSTALL_DATA_DIR}/mods) # Empty directory
2425

2526
set(DATA_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

shaders/fragment_shader_33.glsl

+155
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* This file is part of the Colobot: Gold Edition source code
3+
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4+
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see http://gnu.org/licenses
18+
*/
19+
20+
// FRAGMENT SHADER
21+
#version 330
22+
23+
struct LightParams
24+
{
25+
bool Enabled;
26+
vec4 Position;
27+
vec4 Ambient;
28+
vec4 Diffuse;
29+
vec4 Specular;
30+
float Shininess;
31+
vec3 Attenuation;
32+
};
33+
34+
uniform sampler2D uni_PrimaryTexture;
35+
uniform sampler2D uni_SecondaryTexture;
36+
uniform sampler2DShadow uni_ShadowTexture;
37+
38+
uniform bool uni_PrimaryTextureEnabled;
39+
uniform bool uni_SecondaryTextureEnabled;
40+
uniform bool uni_ShadowTextureEnabled;
41+
42+
uniform bool uni_FogEnabled;
43+
uniform vec2 uni_FogRange;
44+
uniform vec4 uni_FogColor;
45+
46+
uniform bool uni_AlphaTestEnabled;
47+
uniform float uni_AlphaReference;
48+
49+
uniform vec4 uni_AmbientColor;
50+
uniform vec4 uni_DiffuseColor;
51+
uniform vec4 uni_SpecularColor;
52+
53+
uniform bool uni_LightingEnabled;
54+
uniform LightParams uni_Light[8];
55+
56+
uniform bool uni_SmoothShading;
57+
58+
in VertexData
59+
{
60+
vec3 NormalSmooth;
61+
flat vec3 NormalFlat;
62+
vec4 Color;
63+
vec2 TexCoord0;
64+
vec2 TexCoord1;
65+
vec4 ShadowCoord;
66+
vec4 Position;
67+
float Distance;
68+
} data;
69+
70+
out vec4 out_FragColor;
71+
72+
void main()
73+
{
74+
vec4 color = data.Color;
75+
76+
if (uni_LightingEnabled)
77+
{
78+
vec4 ambient = vec4(0.0f);
79+
vec4 diffuse = vec4(0.0f);
80+
vec4 specular = vec4(0.0f);
81+
82+
for(int i=0; i<8; i++)
83+
{
84+
if(uni_Light[i].Enabled)
85+
{
86+
ambient += uni_Light[i].Ambient;
87+
88+
vec3 normal = (uni_SmoothShading ? data.NormalSmooth : data.NormalFlat);
89+
normal = (gl_FrontFacing ? normal : -normal);
90+
91+
vec3 lightDirection = vec3(0.0f);
92+
float atten;
93+
94+
// Directional light
95+
if(uni_Light[i].Position[3] == 0.0f)
96+
{
97+
lightDirection = uni_Light[i].Position.xyz;
98+
atten = 1.0f;
99+
}
100+
// Point light
101+
else
102+
{
103+
vec3 lightDirection = normalize(uni_Light[i].Position.xyz - data.Position.xyz);
104+
float dist = distance(uni_Light[i].Position.xyz, data.Position.xyz);
105+
106+
atten = 1.0f / (uni_Light[i].Attenuation.x
107+
+ uni_Light[i].Attenuation.y * dist
108+
+ uni_Light[i].Attenuation.z * dist * dist);
109+
}
110+
111+
vec3 reflectDirection = -reflect(lightDirection, normal);
112+
113+
diffuse += atten * clamp(dot(normal, lightDirection), 0.0f, 1.0f) * uni_Light[i].Diffuse;
114+
specular += atten * clamp(pow(dot(normal, lightDirection + reflectDirection), 10.0f), 0.0f, 1.0f) * uni_Light[i].Specular;
115+
}
116+
}
117+
118+
vec4 result = uni_AmbientColor * ambient
119+
+ uni_DiffuseColor * diffuse
120+
+ uni_SpecularColor * specular;
121+
122+
color.rgb = min(vec3(1.0f), result.rgb);
123+
color.a = 1.0f; //min(1.0f, 1.0f);
124+
}
125+
126+
if (uni_PrimaryTextureEnabled)
127+
{
128+
color = color * texture(uni_PrimaryTexture, data.TexCoord0);
129+
}
130+
131+
if (uni_SecondaryTextureEnabled)
132+
{
133+
color = color * texture(uni_SecondaryTexture, data.TexCoord1);
134+
}
135+
136+
if (uni_ShadowTextureEnabled)
137+
{
138+
color = color * (0.35f + 0.65f * texture(uni_ShadowTexture, data.ShadowCoord.xyz));
139+
}
140+
141+
if (uni_FogEnabled)
142+
{
143+
float interpolate = (data.Distance - uni_FogRange.x) / (uni_FogRange.y - uni_FogRange.x);
144+
145+
color = mix(color, uni_FogColor, clamp(interpolate, 0.0f, 1.0f));
146+
}
147+
148+
if (uni_AlphaTestEnabled)
149+
{
150+
if(color.a < uni_AlphaReference)
151+
discard;
152+
}
153+
154+
out_FragColor = color;
155+
}

shaders/vertex_shader_33.glsl

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of the Colobot: Gold Edition source code
3+
* Copyright (C) 2001-2014, Daniel Roux, EPSITEC SA & TerranovaTeam
4+
* http://epsiteс.ch; http://colobot.info; http://github.com/colobot
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14+
* See the GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see http://gnu.org/licenses
18+
*/
19+
20+
// VERTEX SHADER
21+
#version 330
22+
23+
uniform mat4 uni_ProjectionMatrix;
24+
uniform mat4 uni_ViewMatrix;
25+
uniform mat4 uni_ModelMatrix;
26+
uniform mat4 uni_ShadowMatrix;
27+
uniform mat4 uni_NormalMatrix;
28+
29+
layout(location = 0) in vec4 in_VertexCoord;
30+
layout(location = 1) in vec3 in_Normal;
31+
layout(location = 2) in vec4 in_Color;
32+
layout(location = 3) in vec2 in_TexCoord0;
33+
layout(location = 4) in vec2 in_TexCoord1;
34+
35+
out VertexData
36+
{
37+
vec3 NormalSmooth;
38+
flat vec3 NormalFlat;
39+
vec4 Color;
40+
vec2 TexCoord0;
41+
vec2 TexCoord1;
42+
vec4 ShadowCoord;
43+
vec4 Position;
44+
float Distance;
45+
} data;
46+
47+
void main()
48+
{
49+
vec4 position = uni_ModelMatrix * in_VertexCoord;
50+
vec4 eyeSpace = uni_ViewMatrix * position;
51+
gl_Position = uni_ProjectionMatrix * eyeSpace;
52+
53+
vec3 normal = normalize((uni_NormalMatrix * vec4(in_Normal, 0.0f)).xyz);
54+
55+
data.Color = in_Color;
56+
data.NormalSmooth = normal;
57+
data.NormalFlat = normal;
58+
data.TexCoord0 = in_TexCoord0;
59+
data.TexCoord1 = in_TexCoord1;
60+
data.ShadowCoord = uni_ShadowMatrix * position;
61+
data.Position = position;
62+
data.Distance = abs(eyeSpace.z);
63+
}

0 commit comments

Comments
 (0)