-
Notifications
You must be signed in to change notification settings - Fork 95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature] Sped Up Height Map Renderer #699
Merged
+177
−82
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,18 +8,19 @@ | |
import com.badlogic.gdx.graphics.g3d.Renderable; | ||
import com.badlogic.gdx.graphics.g3d.RenderableProvider; | ||
import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader; | ||
import com.badlogic.gdx.graphics.glutils.ShaderProgram; | ||
import com.badlogic.gdx.math.Vector2; | ||
import com.badlogic.gdx.utils.Array; | ||
import com.badlogic.gdx.utils.Pool; | ||
import net.mgsx.gltf.scene3d.attributes.PBRColorAttribute; | ||
import org.bytedeco.javacpp.BytePointer; | ||
import org.bytedeco.javacpp.FloatPointer; | ||
import org.bytedeco.opencv.global.opencv_core; | ||
import org.bytedeco.opencv.opencv_core.Mat; | ||
import org.lwjgl.opengl.GL41; | ||
import us.ihmc.sensorProcessing.heightMap.HeightMapTools; | ||
import us.ihmc.euclid.transform.RigidBodyTransform; | ||
import us.ihmc.log.LogTools; | ||
import us.ihmc.rdx.shader.RDXShader; | ||
import us.ihmc.rdx.shader.RDXUniform; | ||
|
||
import java.nio.FloatBuffer; | ||
|
||
/** | ||
* Renders a height map as a point cloud. The height map is stored as a 16-bit grayscale image. | ||
|
@@ -33,31 +34,16 @@ public class RDXHeightMapRenderer implements RenderableProvider | |
private Renderable renderable; | ||
|
||
public static final int FLOATS_PER_CELL = 8; | ||
private final VertexAttributes vertexAttributes = new VertexAttributes(new VertexAttribute(VertexAttributes.Usage.Position, | ||
3, | ||
ShaderProgram.POSITION_ATTRIBUTE), | ||
new VertexAttribute(VertexAttributes.Usage.ColorUnpacked, | ||
4, | ||
ShaderProgram.COLOR_ATTRIBUTE), | ||
new VertexAttribute(VertexAttributes.Usage.Generic, | ||
1, | ||
GL41.GL_FLOAT, | ||
false, | ||
"a_size")); | ||
private final RDXUniform screenWidthUniform = RDXUniform.createGlobalUniform("u_screenWidth", | ||
(shader, inputID, renderable, combinedAttributes) -> shader.set(inputID, | ||
shader.camera.viewportWidth)); | ||
private final RDXUniform multiColorUniform = RDXUniform.createGlobalUniform("u_multiColor", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
int multiColor = 0; | ||
shader.set(inputID, multiColor); | ||
}); | ||
|
||
private float[] intermediateVertexBuffer; | ||
private final VertexAttributes vertexAttributes = new VertexAttributes(new VertexAttribute(VertexAttributes.Usage.Generic, 1, "a_height")); | ||
|
||
private int totalCells; | ||
// Uniforms | ||
private int centerIndex; | ||
private final Vector2 gridCenter = new Vector2(); | ||
private float cellSize; | ||
private float heightScalingFactor; | ||
private float heightOffset; | ||
|
||
public void create(int numberOfCells) | ||
public void create(int maxCells) | ||
{ | ||
GL41.glEnable(GL41.GL_VERTEX_PROGRAM_POINT_SIZE); | ||
|
||
|
@@ -66,78 +52,103 @@ public void create(int numberOfCells) | |
renderable.meshPart.offset = 0; | ||
renderable.material = new Material(PBRColorAttribute.createBaseColorFactor(Color.WHITE)); | ||
|
||
totalCells = numberOfCells; | ||
if (renderable.meshPart.mesh != null) | ||
renderable.meshPart.mesh.dispose(); | ||
boolean isStatic = false; | ||
int maxIndices = 0; | ||
renderable.meshPart.mesh = new Mesh(isStatic, totalCells, maxIndices, vertexAttributes); | ||
renderable.meshPart.mesh = new Mesh(isStatic, maxCells, maxIndices, vertexAttributes); | ||
|
||
RDXShader shader = new RDXShader(getClass()); | ||
shader.create(); | ||
shader.getBaseShader().register(DefaultShader.Inputs.viewTrans, DefaultShader.Setters.viewTrans); | ||
shader.getBaseShader().register(DefaultShader.Inputs.projTrans, DefaultShader.Setters.projTrans); | ||
shader.registerUniform(screenWidthUniform); | ||
shader.registerUniform(multiColorUniform); | ||
registerUniforms(shader); | ||
shader.init(renderable); | ||
renderable.shader = shader.getBaseShader(); | ||
|
||
LogTools.info("Vertex Buffer Size: {}", totalCells * FLOATS_PER_CELL); | ||
intermediateVertexBuffer = new float[totalCells * FLOATS_PER_CELL]; | ||
LogTools.info("Vertex Buffer Size: {}", maxCells * FLOATS_PER_CELL); | ||
} | ||
|
||
@SuppressWarnings("CodeBlock2Expr") | ||
private void registerUniforms(RDXShader rdxShader) | ||
{ | ||
rdxShader.getBaseShader().register(DefaultShader.Inputs.viewTrans, DefaultShader.Setters.viewTrans); | ||
rdxShader.getBaseShader().register(DefaultShader.Inputs.projTrans, DefaultShader.Setters.projTrans); | ||
|
||
RDXUniform screenWidthUniform = RDXUniform.createGlobalUniform("u_screenWidth", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, shader.camera.viewportWidth); | ||
}); | ||
rdxShader.registerUniform(screenWidthUniform); | ||
|
||
RDXUniform centerIndexUniform = RDXUniform.createGlobalUniform("u_centerIndex", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, centerIndex); | ||
}); | ||
rdxShader.registerUniform(centerIndexUniform); | ||
|
||
RDXUniform gridCenterUniform = RDXUniform.createGlobalUniform("u_gridCenter", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, gridCenter); | ||
}); | ||
rdxShader.registerUniform(gridCenterUniform); | ||
|
||
RDXUniform cellSizeUniform = RDXUniform.createGlobalUniform("u_cellSize", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, cellSize); | ||
}); | ||
rdxShader.registerUniform(cellSizeUniform); | ||
|
||
RDXUniform heightScalingFactorUniform = RDXUniform.createGlobalUniform("u_heightScalingFactor", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, heightScalingFactor); | ||
}); | ||
rdxShader.registerUniform(heightScalingFactorUniform); | ||
|
||
RDXUniform heightOffsetUniform = RDXUniform.createGlobalUniform("u_heightOffset", (shader, inputID, renderable, combinedAttributes) -> | ||
{ | ||
shader.set(inputID, heightOffset); | ||
}); | ||
rdxShader.registerUniform(heightOffsetUniform); | ||
} | ||
|
||
public void update(RigidBodyTransform zUpFrameToWorld, | ||
BytePointer heightMapPointer, | ||
public void update(Mat heightMapImage, | ||
float heightOffset, | ||
float gridCenterX, | ||
float gridCenterY, | ||
int centerIndex, | ||
float cellSizeXYInMeters, | ||
float heightScalingFactor) | ||
{ | ||
zUpFrameToWorld.getTranslation().setZ(0); | ||
// Update uniforms | ||
this.heightOffset = heightOffset; | ||
this.gridCenter.set(gridCenterX, gridCenterY); | ||
this.centerIndex = centerIndex; | ||
this.cellSize = cellSizeXYInMeters; | ||
this.heightScalingFactor = heightScalingFactor; | ||
|
||
int cellsPerAxis = 2 * centerIndex + 1; | ||
FloatBuffer verticesBuffer = renderable.meshPart.mesh.getVerticesBuffer(true); | ||
|
||
for (int xIndex = 0; xIndex < cellsPerAxis; xIndex++) | ||
// Ensure correct length and initialize buffer | ||
int cellsPerAxis = 2 * centerIndex + 1; | ||
int totalCells = cellsPerAxis * cellsPerAxis; | ||
if (renderable.meshPart.size != totalCells) | ||
{ | ||
for (int yIndex = 0; yIndex < cellsPerAxis; yIndex++) | ||
{ | ||
double xPosition = HeightMapTools.indexToCoordinate(xIndex, gridCenterX, cellSizeXYInMeters, centerIndex); | ||
double yPosition = HeightMapTools.indexToCoordinate(yIndex, gridCenterY, cellSizeXYInMeters, centerIndex); | ||
|
||
/* look at the header docs for decoding the height map values as below */ | ||
int heightIndex = xIndex * cellsPerAxis + yIndex; | ||
int vertexIndex = heightIndex * FLOATS_PER_CELL; | ||
int height = heightMapPointer.getShort(heightIndex * 2L) & 0xFFFF; | ||
float zPosition = ((float) height / heightScalingFactor); | ||
zPosition -= heightOffset; | ||
|
||
intermediateVertexBuffer[vertexIndex] = (float) xPosition; | ||
intermediateVertexBuffer[vertexIndex + 1] = (float) yPosition; | ||
intermediateVertexBuffer[vertexIndex + 2] = zPosition; | ||
|
||
// Color (0.0 to 1.0) | ||
double[] redGreenBlue = HeightMapTools.getRedGreenBlue(zPosition); | ||
Color color = new Color((float) redGreenBlue[0], (float) redGreenBlue[1], (float) redGreenBlue[2], 1.0f); | ||
intermediateVertexBuffer[vertexIndex + 3] = color.r; | ||
intermediateVertexBuffer[vertexIndex + 4] = color.g; | ||
intermediateVertexBuffer[vertexIndex + 5] = color.b; | ||
intermediateVertexBuffer[vertexIndex + 6] = color.a; | ||
|
||
// Size | ||
intermediateVertexBuffer[vertexIndex + 7] = 0.02f; | ||
} | ||
renderable.meshPart.size = totalCells; | ||
verticesBuffer.limit(totalCells); | ||
} | ||
|
||
renderable.meshPart.size = totalCells; | ||
renderable.meshPart.mesh.setVertices(intermediateVertexBuffer, 0, totalCells * FLOATS_PER_CELL); | ||
FloatPointer verticesPointer = new FloatPointer(verticesBuffer); | ||
Mat verticesMat = new Mat(cellsPerAxis, cellsPerAxis, opencv_core.CV_32FC1, verticesPointer); | ||
heightMapImage.convertTo(verticesMat, opencv_core.CV_32FC1); | ||
|
||
verticesMat.close(); | ||
verticesPointer.close(); | ||
} | ||
|
||
@Override | ||
public void getRenderables(Array<Renderable> renderables, Pool<Renderable> pool) | ||
{ | ||
renderables.add(renderable); | ||
if (renderable != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we create the renderable as a field |
||
renderables.add(renderable); | ||
} | ||
|
||
public void dispose() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 80 additions & 7 deletions
87
...gh-level-behaviors/src/libgdx/resources/us/ihmc/rdx/ui/graphics/RDXHeightMapRenderer.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you find this LogTools useful? I'd say we can remove it.