Add flag to mjrContext to support legacy depth mapping by default

This commit is contained in:
Levi Burner
2023-09-26 13:34:00 -04:00
parent d32eb6bd12
commit 2e52bf807b
8 changed files with 40 additions and 0 deletions
+7
View File
@@ -39,6 +39,10 @@ typedef enum mjtFramebuffer_ { // OpenGL framebuffer option
mjFB_OFFSCREEN // offscreen buffer
} mjtFramebuffer;
typedef enum mjtDepthMapping_ { // OpenGL depth buffer readout mapping (from znear to zfar)
mjDM_ZEROTOONE = 0, // Legacy default, reverses reversed Z rendering, performance penalty
mjDM_ONETOZERO // Native output of reversed Z rendering, decreases numerical error
} mjtDepthMapping;
typedef enum mjtFontScale_ { // font scale, used at context creation
mjFONTSCALE_50 = 50, // 50% scale, suitable for low-res rendering
@@ -151,6 +155,9 @@ struct mjrContext_ { // custom OpenGL context
// pixel output format
int readPixelFormat; // default color pixel format for mjr_readPixels
// depth output format
int readDepthMapping; // depth mapping, 0 to 1 (default, legacy), or 1 to 0 (reversed, native)
};
typedef struct mjrContext_ mjrContext;
+9
View File
@@ -621,6 +621,15 @@ ENUMS: Mapping[str, EnumDecl] = dict([
('mjFB_OFFSCREEN', 1),
]),
)),
('mjtDepthMapping',
EnumDecl(
name='mjtDepthMapping',
declname='enum mjtDepthMapping_',
values=dict([
('mjDM_ZEROTOONE', 0),
('mjDM_ONETOZERO', 1),
]),
)),
('mjtFontScale',
EnumDecl(
name='mjtFontScale',
+5
View File
@@ -7126,6 +7126,11 @@ STRUCTS: Mapping[str, StructDecl] = dict([
type=ValueType(name='int'),
doc='default color pixel format for mjr_readPixels',
),
StructFieldDecl(
name='readDepthMapping',
type=ValueType(name='int'),
doc='depth mapping, 0 to 1 (default, legacy), or 1 to 0 (reversed, native)', # pylint: disable=line-too-long
),
),
)),
('mjuiState',
+1
View File
@@ -215,6 +215,7 @@ PYBIND11_MODULE(_render, pymodule) {
X(windowDoublebuffer);
X(currentBuffer);
X(readPixelFormat);
X(readDepthMapping);
#undef X
#define X(var) \
+2
View File
@@ -86,6 +86,7 @@ the clause:
_render.mjr_setBuffer(
_enums.mjtFramebuffer.mjFB_OFFSCREEN.value, self._mjr_context
)
self._mjr_context.readDepthMapping = _enums.mjtDepthMapping.mjDM_ONETOZERO
# Default render flags.
self._depth_rendering = False
@@ -196,6 +197,7 @@ the clause:
out_64 = D / (out_64 + C)
# Cast result back to float32 for backwards compatibility
# This has a small accuracy cost
out[:] = out_64.astype(np.float32)
# Reset scene flags.
+3
View File
@@ -1612,6 +1612,9 @@ void mjr_makeContext_offSize(const mjModel* m, mjrContext* con, int fontscale,
// set default color pixel format for mjr_readPixels
con->readPixelFormat = GL_RGB;
// set default depth mapping for mjr_readPixels
con->readDepthMapping = mjDM_ZEROTOONE;
}
+9
View File
@@ -115,6 +115,10 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
if (depth) {
glReadPixels(viewport.left, viewport.bottom, viewport.width, viewport.height,
GL_DEPTH_COMPONENT, GL_FLOAT, depth);
if (con->readDepthMapping == mjDM_ZEROTOONE) {
int N_pixels = viewport.width * viewport.height;
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
}
}
}
@@ -159,6 +163,11 @@ void mjr_readPixels(unsigned char* rgb, float* depth,
if (depth) {
glReadPixels(viewport.left, viewport.bottom, viewport.width, viewport.height,
GL_DEPTH_COMPONENT, GL_FLOAT, depth);
if (con->readDepthMapping == mjDM_ZEROTOONE) {
int N_pixels = viewport.width * viewport.height;
for (int i = 0; i < N_pixels; i++) depth[i] = 1.0 - depth[i]; // Reverse the reversed Z buffer
}
}
// restore currentBuffer
+4
View File
@@ -389,6 +389,10 @@ public enum mjtFramebuffer : int{
mjFB_WINDOW = 0,
mjFB_OFFSCREEN = 1,
}
public enum mjtDepthMapping : int{
mjDM_ZEROTOONE = 0,
mjDM_ONETOZERO = 1,
}
public enum mjtFontScale : int{
mjFONTSCALE_50 = 50,
mjFONTSCALE_100 = 100,