Skip to content

Commit

Permalink
Light Type is now and enum
Browse files Browse the repository at this point in the history
  • Loading branch information
DBauer15 committed May 4, 2024
1 parent 17650db commit e103138
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Stage uses a single type to represent all lights in the scene. The `Light` conta
* An optional `map_texid`
* A `type`

The type defines what type of light it is and the other fields are interpreted accordingly. For example if `type` is `STAGE_DISTANT_LIGHT`, the difference between its `from` and `to` defines the light direction.
The type defines what type of light it is and the other fields are interpreted accordingly. For example if `type` is `LightType::DistantLight`, the difference between its `from` and `to` defines the light direction.

Some lights use textures, like environment maps. The texture is referenced by `map_texid` which indexes into the list of `Image` in the `Scene`.

Expand Down
16 changes: 9 additions & 7 deletions src/backstage/light.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ namespace backstage {

/* Uber-light definition that can be interpreted as any supported light type */

#define DISTANT_LIGHT 0
#define INFINITE_LIGHT 1
#define POINT_LIGHT 2
#define SPHERE_LIGHT 3
#define DISK_LIGHT 4
enum LightType {
DistantLight = 0,
InfiniteLight,
PointLight,
SphereLight,
DiskLight,
};

struct Light {
stage_vec3f L;
stage_vec3f from;
stage_vec3f to;
float radius;
int32_t map_texid;
uint32_t type;
LightType type;

static Light defaultLight() {
Light light;
Expand All @@ -30,7 +32,7 @@ struct Light {
light.to = stage_vec3f(0.f, -1.f, 0.f);
light.map_texid = -1;
light.radius = 0.f;
light.type = DISTANT_LIGHT;
light.type = LightType::DistantLight;

return light;
}
Expand Down
12 changes: 6 additions & 6 deletions src/backstage/scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ OBJScene::loadObj() {

// OBJ does not support lights, so we add a single infinite area light
Light light = Light::defaultLight();
light.type = INFINITE_LIGHT;
light.type = LightType::InfiniteLight;
m_lights.push_back(light);
}

Expand Down Expand Up @@ -445,7 +445,7 @@ PBRTScene::loadPBRT() {
m_textures.push_back(std::move(sky_texture));

Light light = Light::defaultLight();
light.type = INFINITE_LIGHT;
light.type = LightType::InfiniteLight;
light.map_texid = m_textures.size() - 1;
m_lights.push_back(light);
}
Expand Down Expand Up @@ -533,7 +533,7 @@ PBRTScene::loadPBRTObjectsRecursive(std::shared_ptr<pbrt::Object> current,
Light light = Light::defaultLight();
if (infinite_light) {
light.L = make_vec3(&infinite_light->L.x);
light.type = INFINITE_LIGHT;
light.type = LightType::InfiniteLight;

if (!infinite_light->mapName.empty()) {
std::filesystem::path filename = getAbsolutePath(infinite_light->mapName);
Expand All @@ -547,15 +547,15 @@ PBRTScene::loadPBRTObjectsRecursive(std::shared_ptr<pbrt::Object> current,

if (distant_light) {
light.L = make_vec3(&distant_light->L.x);
light.type = DISTANT_LIGHT;
light.type = LightType::DistantLight;
light.from = make_vec3(&distant_light->from.x);
light.to = make_vec3(&distant_light->to.x);
LOG("Parsed distant light source");
}

if (point_light) {
light.L = make_vec3(&point_light->I.x);
light.type = POINT_LIGHT;
light.type = LightType::PointLight;
light.from = make_vec3(&point_light->from.x);
LOG("Parsed point light source");
}
Expand All @@ -582,7 +582,7 @@ PBRTScene::loadPBRTObjectsRecursive(std::shared_ptr<pbrt::Object> current,

if (area_light_rgb || area_light_bb) {
Light light = Light::defaultLight();
light.type = sphere_shape ? SPHERE_LIGHT : DISK_LIGHT;
light.type = sphere_shape ? LightType::SphereLight : LightType::DiskLight;
light.radius = sphere_shape ? sphere_shape->radius : disk_shape->radius;
pbrt::vec3f from = sphere_shape ? sphere_shape->transform.p : disk_shape->transform.p;
light.from = make_vec3(&from.x);
Expand Down
4 changes: 2 additions & 2 deletions src/stage_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ stage_light_get_map_texid(stage_light_t light) {
return light->map_texid;
}

uint32_t
stage_light_type_t
stage_light_get_type(stage_light_t light) {
return light->type;
return stage_light_type_t(light->type);
}

/* Material API */
Expand Down
16 changes: 9 additions & 7 deletions src/stage_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ typedef union {
} stage_mat4f_t;

/* POD Declarations */
#define STAGE_DISTANT_LIGHT 0
#define STAGE_INFINITE_LIGHT 1
#define STAGE_POINT_LIGHT 2
#define STAGE_SPHERE_LIGHT 3
#define STAGE_DISK_LIGHT 4

typedef struct stage_camera* stage_camera_t;
typedef struct stage_image* stage_image_t;
typedef struct stage_image* stage_image_list_t;
Expand All @@ -64,6 +58,14 @@ typedef enum {
VertexLayout_Block_V = 0x020,
} stage_vertex_layout_t;

typedef enum {
DistantLight = 0,
InfiniteLight,
PointLight,
SphereLight,
DiskLight,
} stage_light_type_t;

/* API Functions */
typedef unsigned int stage_error_t;
#define STAGE_NO_ERROR 0x0000
Expand Down Expand Up @@ -135,7 +137,7 @@ stage_light_get_radius(stage_light_t light);
int32_t
stage_light_get_map_texid(stage_light_t light);

uint32_t
stage_light_type_t
stage_light_get_type(stage_light_t light);

/* Material API */
Expand Down

0 comments on commit e103138

Please sign in to comment.