Skip to content

Commit fbf8dcf

Browse files
committed
drivers/glrend: hopefully, finally fix these f***ing lighting states
1 parent ebe100c commit fbf8dcf

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
lines changed

drivers/glrend/brender.common.glsl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#define BRT_QUADRATIC 0u /* Quadratic attenuation, i.e. standard 1/clq ... */
2323
#define BRT_RADII 1u /* Radial attenuation, i.e. linear falloff. */
2424

25+
#define COLOUR_SOURCE_GEOMETRY 0u
26+
#define COLOUR_SOURCE_SURFACE 1u
27+
2528
#define DEBUG_DISABLE_LIGHTS 0
2629
#define DEBUG_DISABLE_LIGHT_AMBIENT 0
2730
#define DEBUG_DISABLE_LIGHT_DIRECTIONAL 0
@@ -66,8 +69,9 @@ layout(std140, binding=1) uniform br_model_state
6669
float ks; /* Specular mod (doesn't seem to be used by Croc) */
6770
float kd; /* Diffuse mod */
6871
float power;
69-
uint unlit; /* Is this surface unlit? */
70-
bool use_vertex_colour;
72+
bool lighting; /* BRT_LIGHTING_B */
73+
bool prelighting; /* BRT_PRELIGHTING_B */
74+
int colour_source;
7175
int uv_source;
7276
bool disable_colour_key;
7377
uint texture_mode;
@@ -223,13 +227,7 @@ void lightingColourSpot(in vec3 p, in vec3 n, in uint i, inout vec3 outA, inout
223227
*/
224228
void accumulateLights(in vec3 position, in vec3 normal, inout vec3 ambient, inout vec3 diffuse, inout vec3 specular)
225229
{
226-
#if DEBUG_DISABLE_LIGHTS
227-
diffuse += vec3(1);
228-
return;
229-
#endif
230-
231-
if(unlit != 0u) {
232-
diffuse += vec3(1);
230+
if(!lighting) {
233231
return;
234232
}
235233

drivers/glrend/brender.frag.glsl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,22 @@ void main()
153153
accumulateLights(position.xyz, normal, lightA, lightD, lightS);
154154
#endif
155155

156-
vec4 vertexColour = use_vertex_colour ? colour : vec4(1);
157-
vec4 fragColour = ((vec4(lightA + lightD, 1.0)) * (surface_colour * texColour * vertexColour)) + vec4(lightS, 0.0);
156+
vec4 surfaceColour = colour_source == COLOUR_SOURCE_GEOMETRY ? colour : surface_colour;
157+
158+
/*
159+
* If no lighting at all, fullbright baby.
160+
*/
161+
lightD += (!prelighting && !lighting) ? vec3(1) : vec3(0);
162+
163+
#if DEBUG_DISABLE_LIGHTS
164+
lightA = vec3(0);
165+
lightD = vec3(1);
166+
lightS = vec3(0);
167+
#endif
168+
169+
vec4 fragColour = vec4(0);
170+
fragColour += prelighting ? vec4(colour.rgb, 1) : vec4(0);
171+
fragColour += ((vec4(lightA + lightD, 1.0)) * (surfaceColour * texColour)) + vec4(lightS, 0.0);
158172

159173
if(enable_fog) {
160174
fragColour = applyFog(fragColour, viewDistance);

drivers/glrend/shader_main.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,9 @@ typedef struct br_gl_main_data_model {
8484
alignas(4) float ks;
8585
alignas(4) float kd;
8686
alignas(4) float power;
87-
alignas(4) uint32_t unlit;
88-
alignas(4) uint32_t use_vertex_colour;
87+
alignas(4) uint32_t lighting;
88+
alignas(4) uint32_t prelighting;
89+
alignas(4) uint32_t colour_source;
8990
alignas(4) uint32_t uv_source;
9091
alignas(4) uint32_t disable_colour_key;
9192
alignas(4) uint32_t texture_mode;

drivers/glrend/v1model.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static void apply_blend_mode(state_stack *self, const GladGLContext *gl)
212212
}
213213
}
214214

215-
static void apply_stored_properties(const GladGLContext *gl, br_renderer *renderer, state_stack *state, uint32_t states, br_boolean *unlit,
215+
static void apply_stored_properties(const GladGLContext *gl, br_renderer *renderer, state_stack *state, uint32_t states,
216216
br_gl_main_data_model *model, GLuint tex_default)
217217
{
218218
br_boolean depth_test = BR_FALSE;
@@ -251,19 +251,22 @@ static void apply_stored_properties(const GladGLContext *gl, br_renderer *render
251251
}
252252
}
253253

254-
*unlit = BR_FALSE;
254+
model->lighting = BR_FALSE;
255+
model->prelighting = BR_FALSE;
256+
255257
if(states & MASK_STATE_SURFACE) {
256258
if(state->surface.colour_source == BRT_SURFACE) {
257259
br_uint_32 colour = state->surface.colour;
258260
float r = BR_RED(colour) / 255.0f;
259261
float g = BR_GRN(colour) / 255.0f;
260262
float b = BR_BLU(colour) / 255.0f;
261263
BrVector4Set(&model->surface_colour, r, g, b, state->surface.opacity);
262-
model->use_vertex_colour = 0;
264+
265+
model->colour_source = 1;
263266
} else {
264267
ASSERT(state->surface.colour_source == BRT_GEOMETRY);
265268
BrVector4Set(&model->surface_colour, 1.0f, 1.0f, 1.0f, state->surface.opacity);
266-
model->use_vertex_colour = 1;
269+
model->colour_source = 0;
267270
}
268271

269272
model->ka = state->surface.ka;
@@ -301,7 +304,9 @@ static void apply_stored_properties(const GladGLContext *gl, br_renderer *render
301304
BrMatrix4Copy23(&model->map_transform, &state->surface.map_transform);
302305

303306
depth_test = !state->surface.force_front && !state->surface.force_back;
304-
*unlit = !state->surface.lighting || state->surface.prelighting;
307+
308+
model->lighting = state->surface.lighting;
309+
model->prelighting = state->surface.prelighting;
305310
}
306311

307312
{
@@ -390,10 +395,7 @@ static br_boolean apply_state(br_renderer *renderer, const GladGLContext *gl)
390395
/* NB: Flag is never set */
391396
// int model_lit = self->model->flags & V11MODF_LIT;
392397

393-
unlit = BR_TRUE;
394-
apply_stored_properties(gl, renderer, renderer->state.current, MASK_STATE_STORED | MASK_STATE_OUTPUT, &unlit, &model, ctx->tex_white);
395-
396-
model.unlit = (br_uint_32)unlit;
398+
apply_stored_properties(gl, renderer, renderer->state.current, MASK_STATE_STORED | MASK_STATE_OUTPUT, &model, ctx->tex_white);
397399

398400
return BufferRingGLPush(&renderer->model_ring, &model, sizeof(model));
399401
}

0 commit comments

Comments
 (0)