[vulkan] Sort of implement ambient lighting

It works, but as it uses the camera position rather than the lit
entity's position, it is incorrect. I have some interim ideas to handle
it, though.
This commit is contained in:
Bill Currie 2021-01-28 19:17:40 +09:00
parent 206c631811
commit 7ee02f3965
2 changed files with 43 additions and 11 deletions

View file

@ -37,6 +37,22 @@ layout (location = 2) in vec3 normal;
layout (location = 0) out vec4 frag_color;
vec3
calc_light (LightData light)
{
if (light.type == 0) {
vec3 dist = light.position - position;
float dd = dot (dist, dist);
float mag = max (0.0, dot (dist, normal));
return light.color * mag * light.dist / dd;
} else if (light.type == 1) {
} else if (light.type == 2) {
float mag = max (0.0, -dot (light.direction, normal));
// position is ambient light
return light.color * dot (light.direction, normal) + light.position;
}
}
void
main (void)
{
@ -49,10 +65,7 @@ main (void)
if (MaxLights > 0) {
for (i = 0; i < light_count; i++) {
vec3 dist = lights[i].position - position;
float dd = dot (dist, dist);
float mag = max (0.0, dot (dist, normal));
light += lights[i].color * mag * lights[i].dist / dd;
light += calc_light (lights[i]);
}
}
c *= vec4 (light, 1);

View file

@ -77,7 +77,7 @@ get_view (qfv_tex_t *tex, qfv_tex_t *default_tex)
}
void
Vulkan_DrawAlias (struct entity_s *ent, struct vulkan_ctx_s *ctx)
Vulkan_DrawAlias (entity_t *ent, struct vulkan_ctx_s *ctx)
{
qfv_device_t *device = ctx->device;
qfv_devfuncs_t *dfunc = device->funcs;
@ -138,6 +138,22 @@ Vulkan_DrawAlias (struct entity_s *ent, struct vulkan_ctx_s *ctx)
dfunc->vkCmdDrawIndexed (aframe->cmd, 3 * hdr->mdl.numtris, 1, 0, 0, 0);
}
static void
calc_lighting (qfv_light_t *light, entity_t *ent)
{
vec3_t ambient_color;
//FIXME should be ent->position
float l = R_LightPoint (r_origin) / 128.0;
//XXX l = max (light, max (ent->model->min_light, ent->min_light));
light->type = 2;
VectorSet (1, 1, 1, ambient_color); //FIXME
// position doubles as ambient light
VectorScale (ambient_color, l, light->position);
VectorSet (-1, 0, 0, light->direction); //FIXME
VectorCopy (light->position, light->color);
}
void
Vulkan_AliasBegin (vulkan_ctx_t *ctx)
{
@ -153,16 +169,19 @@ Vulkan_AliasBegin (vulkan_ctx_t *ctx)
VkCommandBuffer cmd = aframe->cmd;
DARRAY_APPEND (cframe->subCommand, cmd);
R_FindNearLights (r_origin, ALIAS_LIGHTS, lights);
aframe->lights->light_count = 0;
for (int i = 0; i < ALIAS_LIGHTS; i++) {
//FIXME ambient needs to be per entity
aframe->lights->light_count = 1;
calc_lighting (&aframe->lights->lights[0], 0);
R_FindNearLights (r_origin, ALIAS_LIGHTS - 1, lights);
for (int i = 0; i < ALIAS_LIGHTS - 1; i++) {
if (!lights[i]) {
break;
}
aframe->lights->light_count++;
VectorCopy (lights[i]->color, aframe->lights->lights[i].color);
VectorCopy (lights[i]->origin, aframe->lights->lights[i].position);
aframe->lights->lights[i].dist = lights[i]->radius;
VectorCopy (lights[i]->color, aframe->lights->lights[i + 1].color);
VectorCopy (lights[i]->origin, aframe->lights->lights[i + 1].position);
aframe->lights->lights[i + 1].dist = lights[i]->radius;
aframe->lights->lights[i + 1].type = 0;
}
//FIXME need per frame matrices