1818 " uniform mat4 u_WorldViewProj;\n " \
1919 " void main() {\n " \
2020 " v_texcoord = vec2(a_position_tu.w, 1.0-a_normal_tv.w);\n " \
21+ " v_normal = a_normal_tv.xyz;\n " \
2122 " gl_Position = u_WorldViewProj * vec4(a_position_tu.xyz, 1.0);\n " \
2223 " }\n "
2324
2425#define MODEL_FRAGMENT_SHADER \
2526 " uniform sampler2D s_texture;\n " \
27+ " uniform vec3 u_lightDir;\n " \
28+ " uniform vec4 u_ambientColor;\n " \
29+ " uniform vec4 u_lightColor;\n " \
2630 " void main() {\n " \
27- " fragColor = texture2D(s_texture, v_texcoord.xy);\n " \
31+ " vec4 lighting;\n " \
32+ " vec4 color = texture2D(s_texture, v_texcoord.xy);\n " \
33+ " lighting = vec4(color.rgb * u_ambientColor.rgb * u_ambientColor.a, color.a);\n " \
34+ " lighting.rgb += u_lightColor.rgb * u_lightColor.a * color.rgb * saturate(1.0 - dot(v_normal, u_lightDir));\n " \
35+ " fragColor = lighting;\n " \
2836 " }\n "
2937
3038const char * model_shader =
3139 " varying vec2 v_texcoord;\n "
40+ " varying vec3 v_normal;\n "
3241 " varying vec4 v_color;\n "
33- " varying vec4 v_page_clut;\n "
34- " varying float v_z;\n "
3542 " #ifdef VERTEX\n "
3643 MODEL_VERTEX_SHADER
3744 " #else\n "
@@ -142,9 +149,61 @@ void SDLPollEvent()
142149
143150CRenderModel* g_renderModels[MAX_MODELS];
144151TextureID g_hwTexturePages[128 ][32 ];
145- ShaderID g_modelShader = - 1 ;
152+
146153extern bool g_originalTransparencyKey;
147154
155+ // -----------------------------------------------------------------
156+
157+ struct WorldRenderProperties
158+ {
159+ Vector4D ambientColor;
160+ Vector4D lightColor;
161+ Vector3D lightDir;
162+
163+ } g_worldRenderProperties;
164+
165+ void SetupLightingProperties ()
166+ {
167+ g_worldRenderProperties.ambientColor = ColorRGBA (0 .95f , 0 .9f , 1 .0f , 0 .35f );
168+ g_worldRenderProperties.lightColor = ColorRGBA (1 .0f , 1 .0f , 1 .0f , 0 .8f );
169+ g_worldRenderProperties.lightDir = normalize (Vector3D (-1 , -1 , -1 ));
170+ }
171+
172+ // -----------------------------------------------------------------
173+
174+
175+ struct ModelShaderInfo
176+ {
177+ ShaderID shader{ 0 };
178+
179+ int ambientColorConstantId{ -1 };
180+ int lightColorConstantId{ -1 };
181+
182+ int lightDirConstantId{ -1 };
183+ } g_modelShader;
184+
185+ void InitModelShader ()
186+ {
187+ // create shader
188+ g_modelShader.shader = GR_CompileShader (model_shader);
189+
190+ g_modelShader.ambientColorConstantId = GR_GetShaderConstantIndex (g_modelShader.shader , " u_ambientColor" );
191+ g_modelShader.lightColorConstantId = GR_GetShaderConstantIndex (g_modelShader.shader , " u_lightColor" );
192+
193+ g_modelShader.lightDirConstantId = GR_GetShaderConstantIndex (g_modelShader.shader , " u_lightDir" );
194+ }
195+
196+ void SetupModelShader ()
197+ {
198+ GR_SetShader (g_modelShader.shader );
199+ GR_SetShaderConstatntVector3D (g_modelShader.lightDirConstantId , g_worldRenderProperties.lightDir );
200+
201+ GR_SetShaderConstatntVector4D (g_modelShader.ambientColorConstantId , g_worldRenderProperties.ambientColor );
202+ GR_SetShaderConstatntVector4D (g_modelShader.lightColorConstantId , g_worldRenderProperties.lightColor );
203+ }
204+
205+ // -----------------------------------------------------------------
206+
148207// Creates hardware texture
149208void InitHWTexturePage (int nPage)
150209{
@@ -217,6 +276,18 @@ void InitHWTexturePage(int nPage)
217276 free (color_data);
218277}
219278
279+ TextureID GetHWTexture (int tpage, int pal)
280+ {
281+ extern TextureID g_whiteTexture;
282+
283+ if (tpage < 0 )
284+ return g_whiteTexture;
285+
286+ return g_hwTexturePages[tpage][pal];
287+ }
288+
289+ // -----------------------------------------------------------------
290+
220291extern int g_windowWidth;
221292extern int g_windowHeight;
222293
@@ -235,6 +306,8 @@ void RenderView()
235306
236307 view.translate (-cameraPos);
237308
309+ SetupLightingProperties ();
310+
238311 GR_SetMatrix (MATRIX_VIEW, view);
239312 GR_SetMatrix (MATRIX_PROJECTION, proj);
240313
@@ -259,8 +332,7 @@ int ViewerMain(const char* filename)
259332 return -1 ;
260333 }
261334
262- // create shader
263- g_modelShader = GR_CompileShader (model_shader);
335+ InitModelShader ();
264336
265337 // Load level file
266338 LoadLevelFile (filename);
0 commit comments