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
+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);