Convert light directional boolean into a type enum.

PiperOrigin-RevId: 761983728
Change-Id: Id5bf93c103e5358d9c5cd9f1176532a978e0b3a8
This commit is contained in:
Google DeepMind
2025-05-22 08:50:23 -07:00
committed by Copybara-Service
parent 373b4c0437
commit 74cc904edc
21 changed files with 130 additions and 49 deletions
+3 -3
View File
@@ -2143,7 +2143,7 @@ void mjv_makeLights(const mjModel* m, const mjData* d, mjvScene* scn) {
// set default properties
memset(thislight, 0, sizeof(mjvLight));
thislight->headlight = 1;
thislight->directional = 1;
thislight->type = mjLIGHT_DIRECTIONAL;
thislight->castshadow = 0;
// compute head position and gaze direction in model space
@@ -2169,12 +2169,12 @@ void mjv_makeLights(const mjModel* m, const mjData* d, mjvScene* scn) {
// copy properties
memset(thislight, 0, sizeof(mjvLight));
thislight->directional = m->light_directional[i];
thislight->type = m->light_type[i];
thislight->castshadow = m->light_castshadow[i];
thislight->bulbradius = m->light_bulbradius[i];
thislight->intensity = m->light_intensity[i];
thislight->range = m->light_range[i];
if (!thislight->directional) {
if (thislight->type == mjLIGHT_SPOT) {
f2f(thislight->attenuation, m->light_attenuation+3*i, 3);
thislight->exponent = m->light_exponent[i];
thislight->cutoff = m->light_cutoff[i];
+14 -6
View File
@@ -672,7 +672,7 @@ static void initLights(mjvScene* scn) {
glLightfv(GL_LIGHT0+i, GL_SPECULAR, scn->lights[i].specular);
// parameters for directional light
if (scn->lights[i].directional) {
if (scn->lights[i].type == mjLIGHT_DIRECTIONAL) {
glLightf(GL_LIGHT0+i, GL_SPOT_EXPONENT, 0);
glLightf(GL_LIGHT0+i, GL_SPOT_CUTOFF, 180);
glLightf(GL_LIGHT0+i, GL_CONSTANT_ATTENUATION, 1);
@@ -681,13 +681,17 @@ static void initLights(mjvScene* scn) {
}
// parameters for spot light
else {
else if (scn->lights[i].type == mjLIGHT_SPOT) {
glLightf(GL_LIGHT0+i, GL_SPOT_EXPONENT, scn->lights[i].exponent);
glLightf(GL_LIGHT0+i, GL_SPOT_CUTOFF, scn->lights[i].cutoff);
glLightf(GL_LIGHT0+i, GL_CONSTANT_ATTENUATION, scn->lights[i].attenuation[0]);
glLightf(GL_LIGHT0+i, GL_LINEAR_ATTENUATION, scn->lights[i].attenuation[1]);
glLightf(GL_LIGHT0+i, GL_QUADRATIC_ATTENUATION, scn->lights[i].attenuation[2]);
}
else {
mju_error("Unsupported light type: %d", scn->lights[i].type);
}
}
// disable all lights (enable selectively in render)
@@ -790,14 +794,16 @@ static void adjustLight(const mjvLight* thislight, int n) {
float temp[4];
// set position and direction according to type
if (thislight->directional) {
if (thislight->type == mjLIGHT_DIRECTIONAL) {
mjr_setf4(temp, -thislight->dir[0], -thislight->dir[1], -thislight->dir[2], 0);
glLightfv(GL_LIGHT0+n, GL_POSITION, temp);
} else {
} else if (thislight->type == mjLIGHT_SPOT) {
mjr_setf4(temp, thislight->dir[0], thislight->dir[1], thislight->dir[2], 0);
glLightfv(GL_LIGHT0+n, GL_SPOT_DIRECTION, temp);
mjr_setf4(temp, thislight->pos[0], thislight->pos[1], thislight->pos[2], 1);
glLightfv(GL_LIGHT0+n, GL_POSITION, temp);
} else {
mju_error("Unsupported light type: %d", thislight->type);
}
}
@@ -1183,13 +1189,15 @@ void mjr_render(mjrRect viewport, mjvScene* scn, const mjrContext* con) {
// reverse Z rendering mapping without shift [znear, zfar] -> [1, -1] (ndc)
glScalef(1.0f, 1.0f, -1.0f);
}
if (thislight->directional) {
if (thislight->type == mjLIGHT_DIRECTIONAL) {
glOrtho(-con->shadowClip, con->shadowClip,
-con->shadowClip, con->shadowClip,
cam.frustum_near, cam.frustum_far);
} else {
} else if (thislight->type == mjLIGHT_SPOT) {
mjr_perspective(mju_min(2*thislight->cutoff*con->shadowScale, 160), 1,
cam.frustum_near, cam.frustum_far);
} else {
mju_error("Unsupported light type: %d", thislight->type);
}
glGetFloatv(GL_PROJECTION_MATRIX, lightProject);
+1 -1
View File
@@ -2669,7 +2669,7 @@ void mjCModel::CopyTree(mjModel* m) {
m->light_bodyid[lid] = pl->body->id;
m->light_mode[lid] = (int)pl->mode;
m->light_targetbodyid[lid] = pl->targetbodyid;
m->light_directional[lid] = (mjtByte)pl->directional;
m->light_type[lid] = pl->type;
m->light_castshadow[lid] = (mjtByte)pl->castshadow;
m->light_active[lid] = (mjtByte)pl->active;
mjuu_copyvec(m->light_pos+3*lid, pl->pos, 3);
+2 -2
View File
@@ -27,7 +27,7 @@
// keyword maps (defined in implementation files)
extern const int joint_sz;
extern const int camlight_sz;
extern const int light_sz;
extern const int lighttype_sz;
extern const int integrator_sz;
extern const int collision_sz;
extern const int cone_sz;
@@ -51,7 +51,7 @@ extern const mjMap TFAuto_map[];
extern const mjMap joint_map[];
extern const mjMap geom_map[];
extern const mjMap camlight_map[];
extern const mjMap light_map[];
extern const mjMap lighttype_map[];
extern const mjMap integrator_map[];
extern const mjMap collision_map[];
extern const mjMap impedance_map[];
+23 -4
View File
@@ -167,8 +167,8 @@ const char* MJCF[nMJCF][mjXATTRNUM] = {
{"camera", "?", "17", "orthographic", "fovy", "ipd", "resolution", "pos", "quat",
"axisangle", "xyaxes", "zaxis", "euler", "mode", "focal", "focalpixel",
"principal", "principalpixel", "sensorsize", "user"},
{"light", "?", "15", "pos", "dir", "bulbradius", "intensity", "range",
"directional", "castshadow", "active", "attenuation", "cutoff", "exponent",
{"light", "?", "16", "pos", "dir", "bulbradius", "intensity", "range",
"directional", "type", "castshadow", "active", "attenuation", "cutoff", "exponent",
"ambient", "diffuse", "specular", "mode"},
{"pair", "?", "7", "condim", "friction", "solref", "solreffriction", "solimp",
"gap", "margin"},
@@ -281,7 +281,7 @@ const char* MJCF[nMJCF][mjXATTRNUM] = {
{"camera", "*", "20", "name", "class", "orthographic", "fovy", "ipd", "resolution", "pos",
"quat", "axisangle", "xyaxes", "zaxis", "euler", "mode", "target",
"focal", "focalpixel", "principal", "principalpixel", "sensorsize", "user"},
{"light", "*", "18", "name", "class", "directional", "castshadow", "active",
{"light", "*", "19", "name", "class", "directional", "type", "castshadow", "active",
"pos", "dir", "bulbradius", "intensity", "range", "attenuation", "cutoff",
"exponent", "ambient", "diffuse", "specular", "mode", "target"},
{"plugin", "*", "2", "plugin", "instance"},
@@ -580,6 +580,17 @@ const mjMap camlight_map[camlight_sz] = {
{"targetbodycom", mjCAMLIGHT_TARGETBODYCOM}
};
// light type
const int lighttype_sz = 4;
const mjMap lighttype_map[lighttype_sz] = {
{"spot", mjLIGHT_SPOT},
{"directional", mjLIGHT_DIRECTIONAL},
{"point", mjLIGHT_POINT},
{"image", mjLIGHT_IMAGE}
};
// texmat role type
const int texrole_sz = mjNTEXROLE - 1;
const mjMap texrole_map[texrole_sz] = {
@@ -1852,6 +1863,7 @@ void mjXReader::OneCamera(XMLElement* elem, mjsCamera* camera) {
// light element parser
void mjXReader::OneLight(XMLElement* elem, mjsLight* light) {
int n;
bool has_directional = false;
string text, name, targetbody;
// read attributes
@@ -1865,7 +1877,14 @@ void mjXReader::OneLight(XMLElement* elem, mjsLight* light) {
light->mode = (mjtCamLight)n;
}
if (MapValue(elem, "directional", &n, bool_map, 2)) {
light->directional = (n == 1);
light->type = (n == 1) ? mjLIGHT_DIRECTIONAL : mjLIGHT_SPOT;
has_directional = true;
}
if (MapValue(elem, "type", &n, lighttype_map, lighttype_sz)) {
if (has_directional) {
throw mjXError(elem, "type and directional cannot both be defined");
}
light->type = (mjtLightType)n;
}
if (MapValue(elem, "castshadow", &n, bool_map, 2)) {
light->castshadow = (n == 1);
+1 -1
View File
@@ -607,7 +607,7 @@ void mjXWriter::OneLight(XMLElement* elem, const mjCLight* light, mjCDef* def,
WriteAttr(elem, "bulbradius", 1, &light->bulbradius, &def->Light().bulbradius);
WriteAttr(elem, "intensity", 1, &light->intensity, &def->Light().intensity);
WriteAttr(elem, "range", 1, &light->range, &def->Light().range);
WriteAttrKey(elem, "directional", bool_map, 2, light->directional, def->Light().directional);
WriteAttrKey(elem, "type", lighttype_map, lighttype_sz, light->type, def->Light().type);
WriteAttrKey(elem, "castshadow", bool_map, 2, light->castshadow, def->Light().castshadow);
WriteAttrKey(elem, "active", bool_map, 2, light->active, def->Light().active);
WriteAttr(elem, "attenuation", 3, light->attenuation, def->Light().attenuation);