Skip to content

Commit d27a7dc

Browse files
committed
- added lighting support
1 parent 2da60d4 commit d27a7dc

3 files changed

Lines changed: 84 additions & 11 deletions

File tree

DriverLevelTool/exporter/export_models.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
extern bool g_extract_dmodels;
1313
extern std::string g_levname_moddir;
1414
extern std::string g_levname_texdir;
15-
extern DkList<std::string> g_model_names;
15+
extern DkList<std::string> g_model_names;
1616

1717
//-------------------------------------------------------------
1818
// writes Wavefront OBJ into stream

DriverLevelTool/viewer/rendermodel.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,16 +321,17 @@ void CRenderModel::GenerateBuffers()
321321

322322
void CRenderModel::Draw()
323323
{
324-
extern TextureID g_hwTexturePages[128][32];
325-
extern ShaderID g_modelShader;
324+
extern TextureID GetHWTexture(int tpage, int pal);
325+
extern void SetupModelShader();
326326

327+
SetupModelShader();
327328
GR_SetVAO(m_vao);
328329

329330
for(int i = 0; i < m_batches.numElem(); i++)
330331
{
331332
modelBatch_t& batch = m_batches[i];
332-
GR_SetShader(g_modelShader);
333-
GR_SetTexture(g_hwTexturePages[batch.tpage][0]);
333+
334+
GR_SetTexture(GetHWTexture(batch.tpage, 0));
334335
GR_DrawIndexed(PRIM_TRIANGLES, batch.startIndex, batch.numIndices);
335336
}
336337
}

DriverLevelTool/viewer/viewer.cpp

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@
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

3038
const 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

143150
CRenderModel* g_renderModels[MAX_MODELS];
144151
TextureID g_hwTexturePages[128][32];
145-
ShaderID g_modelShader = -1;
152+
146153
extern 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
149208
void 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+
220291
extern int g_windowWidth;
221292
extern 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

Comments
 (0)