prosperon/source/engine/3d/light.c

202 lines
5.6 KiB
C
Raw Permalink Normal View History

2022-01-21 11:26:22 -06:00
#include "light.h"
2021-11-30 21:29:18 -06:00
#include <stdbool.h>
/*
void Light::serialize(FILE * file)
{
GameObject::serialize(file);
SerializeFloat(file, &strength);
SerializeVec3(file, (float *) &color);
SerializeBool(file, &dynamic);
}
void Light::deserialize(FILE * file)
{
GameObject::deserialize(file);
DeserializeFloat(file, &strength);
DeserializeVec3(file, (float *) &color);
DeserializeBool(file, &dynamic);
}
static const mfloat_t dlight_init_rot[3] = { 80.f, 120.f, 165.f };
struct mDirectionalLight *dLight = NULL;
struct mDirectionalLight *MakeDLight()
{
if (dLight != NULL) {
2023-05-12 13:22:05 -05:00
dLight =
(struct mDirectionalLight *)
malloc(sizeof(struct mDirectionalLight));
quat_from_euler(dLight->light.obj.transform.rotation,
dlight_init_rot);
2021-11-30 21:29:18 -06:00
2023-05-12 13:22:05 -05:00
return dLight;
2021-11-30 21:29:18 -06:00
}
return dLight;
}
void dlight_prepshader(struct mDirectionalLight *light,
2023-05-12 13:22:05 -05:00
struct shader *shader)
2021-11-30 21:29:18 -06:00
{
mfloat_t fwd[3] = { 0.f };
trans_forward(fwd, &light->light.obj.transform);
shader_setvec3(shader, "dirLight.direction", fwd);
shader_setvec3(shader, "dirLight.color", light->light.color);
shader_setfloat(shader, "dirLight.strength", light->light.strength);
}
static struct mPointLight *pointLights[4];
static int numLights = 0;
struct mPointLight *MakePointlight()
{
if (numLights < 4) {
2023-05-12 13:22:05 -05:00
struct mPointLight *light =
(struct mPointLight *) malloc(sizeof(struct mPointLight));
pointLights[numLights++] = light;
light->light.strength = 0.2f;
light->constant = 1.f;
light->linear = 0.9f;
light->quadratic = 0.032f;
return light;
2021-11-30 21:29:18 -06:00
}
return NULL;
}
static void prepstring(char *buffer, char *prepend, const char *append)
{
snprintf(buffer, 100, "%s%s", prepend, append);
}
2022-11-19 17:13:57 -06:00
void pointlights_prepshader(struct shader *shader)
2021-11-30 21:29:18 -06:00
{
for (int i = 0; i < numLights; i++)
2023-05-12 13:22:05 -05:00
pointlight_prepshader(pointLights[i], shader, i);
2021-11-30 21:29:18 -06:00
}
void pointlight_prepshader(struct mPointLight *light,
2023-05-12 13:22:05 -05:00
struct shader *shader, int num)
2021-11-30 21:29:18 -06:00
{
shader_use(shader);
char prepend[100] = { '\0' };
snprintf(prepend, 100, "%s%d%s", "pointLights[", num, "].");
char str[100] = { '\0' };
prepstring(str, prepend, "position");
shader_setvec3(shader, str, light->light.obj.transform.position);
prepstring(str, prepend, "constant");
shader_setfloat(shader, str, light->constant);
prepstring(str, prepend, "linear");
shader_setfloat(shader, str, light->linear);
prepstring(str, prepend, "quadratic");
shader_setfloat(shader, str, light->quadratic);
prepstring(str, prepend, "strength");
shader_setfloat(shader, str, light->light.strength);
prepstring(str, prepend, "color");
shader_setvec3(shader, str, light->light.color);
}
static struct mSpotLight *spotLights[4];
static int numSpots = 0;
struct mSpotLight *MakeSpotlight()
{
if (numSpots < 4) {
2023-05-12 13:22:05 -05:00
struct mSpotLight *light =
(struct mSpotLight *) malloc(sizeof(struct mSpotLight));
spotLights[numSpots++] = light;
return light;
2021-11-30 21:29:18 -06:00
}
return NULL;
}
2022-11-19 17:13:57 -06:00
void spotlights_prepshader(struct shader *shader)
2021-11-30 21:29:18 -06:00
{
for (int i = 0; i < numSpots; i++)
2023-05-12 13:22:05 -05:00
spotlight_prepshader(spotLights[i], shader, i);
2021-11-30 21:29:18 -06:00
}
2022-11-19 17:13:57 -06:00
void spotlight_prepshader(struct mSpotLight *light, struct shader *shader,
2023-05-12 13:22:05 -05:00
int num)
2021-11-30 21:29:18 -06:00
{
mfloat_t fwd[3] = { 0.f };
trans_forward(fwd, &light->light.obj.transform);
shader_use(shader);
shader_setvec3(shader, "spotLight.position",
2023-05-12 13:22:05 -05:00
light->light.obj.transform.position);
2021-11-30 21:29:18 -06:00
shader_setvec3(shader, "spotLight.direction", fwd);
shader_setvec3(shader, "spotLight.color", light->light.color);
shader_setfloat(shader, "spotLight.strength", light->light.strength);
shader_setfloat(shader, "spotLight.cutoff", light->cutoff);
shader_setfloat(shader, "spotLight.distance", light->distance);
shader_setfloat(shader, "spotLight.outerCutoff", light->outerCutoff);
shader_setfloat(shader, "spotLight.linear", light->linear);
shader_setfloat(shader, "spotLight.quadratic", light->quadratic);
shader_setfloat(shader, "spotLight.constant", light->constant);
}
2022-02-06 10:14:57 -06:00
*/
2022-08-12 14:03:56 -05:00
/*
void light_gui(struct mLight *light)
{
object_gui(&light->obj);
if (nk_tree_push(ctx, NK_TREE_NODE, "Light", NK_MINIMIZED)) {
2023-05-12 13:22:05 -05:00
nk_property_float(ctx, "Strength", 0.f, &light->strength, 1.f, 0.01f, 0.001f);
// ImGui::ColorEdit3("Color", &light->color[0]);
nk_checkbox_label(ctx, "Dynamic", (bool *) &light->dynamic);
nk_tree_pop(ctx);
2022-08-12 14:03:56 -05:00
}
}
void pointlight_gui(struct mPointLight *light)
{
light_gui(&light->light);
if (nk_tree_push(ctx, NK_TREE_NODE, "Point Light", NK_MINIMIZED)) {
2023-05-12 13:22:05 -05:00
nk_property_float(ctx, "Constant", 0.f, &light->constant, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Linear", 0.f, &light->linear, 0.3f, 0.01f, 0.001f);
nk_property_float(ctx, "Quadratic", 0.f, &light->quadratic, 0.3f, 0.01f, 0.001f);
nk_tree_pop(ctx);
2022-08-12 14:03:56 -05:00
}
}
void spotlight_gui(struct mSpotLight *spot)
{
light_gui(&spot->light);
if (nk_tree_push(ctx, NK_TREE_NODE, "Spotlight", NK_MINIMIZED)) {
2023-05-12 13:22:05 -05:00
nk_property_float(ctx, "Linear", 0.f, &spot->linear, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Quadratic", 0.f, &spot->quadratic, 1.f, 0.01f, 0.001f);
nk_property_float(ctx, "Distance", 0.f, &spot->distance, 200.f, 1.f, 0.1f, 200.f);
nk_property_float(ctx, "Cutoff Degrees", 0.f, &spot->cutoff, 0.7f, 0.01f, 0.001f);
nk_property_float(ctx, "Outer Cutoff Degrees", 0.f, &spot->outerCutoff, 0.7f, 0.01f, 0.001f);
nk_tree_pop(ctx);
2022-08-12 14:03:56 -05:00
}
}
*/