revert lines glsl shader, and add points glsl shader for instanced rendering of point cloud.

This commit is contained in:
Erwin Coumans
2022-04-23 13:56:40 -07:00
parent d3b4c27db4
commit 31b9be82a1
7 changed files with 118 additions and 18 deletions

View File

@@ -98,7 +98,10 @@ struct caster2
#include "Shaders/segmentationMaskInstancingPS.h"
#include "Shaders/linesPS.h"
#include "Shaders/pointsPS.h"
#include "Shaders/linesVS.h"
#include "Shaders/pointsVS.h"
#include "GLRenderToTexture.h"
#include "stb_image/stb_image_write.h"
@@ -287,26 +290,43 @@ static GLuint triangleVertexBufferObject = 0;
static GLuint triangleVertexArrayObject = 0;
static GLuint triangleIndexVbo = 0;
static GLuint linesShader; // The line renderer
static GLuint linesShader; // The line renderer
static GLuint pointsShader; // The point renderer
static GLuint useShadowMapInstancingShader; // The shadow instancing renderer
static GLuint createShadowMapInstancingShader; // The shadow instancing renderer
static GLuint projectiveTextureInstancingShader; // The projective texture instancing renderer
static GLuint segmentationMaskInstancingShader; // The segmentation mask instancing renderer
static GLuint instancingShader; // The instancing renderer
static GLuint instancingShaderPointSprite; // The point sprite instancing renderer
//static bool done = false;
static GLint points_ModelViewMatrix = 0;
static GLint points_ProjectionMatrix = 0;
static GLint points_position = 0;
static GLint points_colourIn = 0;
static GLint points_colour = 0;
GLuint pointsVertexBufferObject = 0;
GLuint pointsVertexArrayObject = 0;
GLuint pointsIndexVbo = 0;
static GLint lines_ModelViewMatrix = 0;
static GLint lines_ProjectionMatrix = 0;
static GLint lines_position = 0;
static GLint lines_colour = 0;
static GLint lines_colourIn = 0;
GLuint lineVertexBufferObject = 0;
GLuint lineVertexArrayObject = 0;
GLuint lineIndexVbo = 0;
GLuint linesVertexBufferObject = 0;
GLuint linesVertexArrayObject = 0;
GLuint linesIndexVbo = 0;
@@ -1243,12 +1263,38 @@ void GLInstancingRenderer::InitShaders()
glBindVertexArray(0);
}
{
pointsShader = gltLoadShaderPair(pointsVertexShader, pointsFragmentShader);
points_ModelViewMatrix = glGetUniformLocation(pointsShader, "ModelViewMatrix");
points_ProjectionMatrix = glGetUniformLocation(pointsShader, "ProjectionMatrix");
points_colour = glGetUniformLocation(pointsShader, "colour");
points_colourIn = glGetAttribLocation(pointsShader, "colourIn");
points_position = glGetAttribLocation(pointsShader, "position");
glLinkProgram(pointsShader);
glUseProgram(pointsShader);
int max_viewports=-1;
glGetIntegerv(GL_MAX_VIEWPORTS, &max_viewports);
{
glGenVertexArrays(1, &pointsVertexArrayObject);
glBindVertexArray(pointsVertexArrayObject);
glGenBuffers(1, &pointsVertexBufferObject);
glGenBuffers(1, &pointsIndexVbo);
int sz = MAX_POINTS_IN_BATCH * sizeof(b3Vector3);
glBindVertexArray(pointsVertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, pointsVertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sz, 0, GL_DYNAMIC_DRAW);
glBindVertexArray(0);
}
}
linesShader = gltLoadShaderPair(linesVertexShader, linesFragmentShader);
lines_ModelViewMatrix = glGetUniformLocation(linesShader, "ModelViewMatrix");
lines_ProjectionMatrix = glGetUniformLocation(linesShader, "ProjectionMatrix");
lines_colour = glGetUniformLocation(linesShader, "colour");
lines_colourIn = glGetAttribLocation(linesShader, "colourIn");
lines_position = glGetAttribLocation(linesShader, "position");
glLinkProgram(linesShader);
glUseProgram(linesShader);
@@ -1267,6 +1313,9 @@ void GLInstancingRenderer::InitShaders()
glBindVertexArray(0);
}
{
glGenVertexArrays(1, &lineVertexArrayObject);
glBindVertexArray(lineVertexArrayObject);
@@ -1891,19 +1940,20 @@ void GLInstancingRenderer::drawPoint(const float* positions, const float color[4
{
drawPoints(positions, color, 1, 3 * sizeof(float), pointDrawSize);
}
void GLInstancingRenderer::drawPoints(const float* positions, const float* colors, int numPoints, int pointStrideInBytes, float pointDrawSize)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);
b3Assert(glGetError() == GL_NO_ERROR);
glUseProgram(linesShader);
glUniformMatrix4fv(lines_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(lines_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniform4f(lines_colour, 0, 0, 0, -1);
glUseProgram(pointsShader);
glUniformMatrix4fv(points_ProjectionMatrix, 1, false, &m_data->m_projectionMatrix[0]);
glUniformMatrix4fv(points_ModelViewMatrix, 1, false, &m_data->m_viewMatrix[0]);
glUniform4f(points_colour, 0, 0, 0, -1);
glPointSize(pointDrawSize);
glBindVertexArray(lineVertexArrayObject);
glBindVertexArray(pointsVertexArrayObject);
int maxPointsInBatch = MAX_POINTS_IN_BATCH;
int remainingPoints = numPoints;
@@ -1913,15 +1963,15 @@ void GLInstancingRenderer::drawPoints(const float* positions, const float* color
int curPointsInBatch = b3Min(maxPointsInBatch, remainingPoints);
if (curPointsInBatch)
{
glBindBuffer(GL_ARRAY_BUFFER, lineVertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, pointsVertexBufferObject);
glBufferSubData(GL_ARRAY_BUFFER, 0, curPointsInBatch * pointStrideInBytes, positions + offsetNumPoints * 3);
glEnableVertexAttribArray(lines_position);
glVertexAttribPointer(lines_position, 3, GL_FLOAT, GL_FALSE, pointStrideInBytes, 0);
glEnableVertexAttribArray(points_position);
glVertexAttribPointer(points_position, 3, GL_FLOAT, GL_FALSE, pointStrideInBytes, 0);
glBindBuffer(GL_ARRAY_BUFFER, lineVertexArrayObject);
glBindBuffer(GL_ARRAY_BUFFER, pointsVertexArrayObject);
glBufferSubData(GL_ARRAY_BUFFER, 0, curPointsInBatch * 4 * sizeof(float), colors + offsetNumPoints * 4);
glEnableVertexAttribArray(lines_colourIn);
glVertexAttribPointer(lines_colourIn, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glEnableVertexAttribArray(points_colourIn);
glVertexAttribPointer(points_colourIn, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glDrawArrays(GL_POINTS, 0, curPointsInBatch);
remainingPoints -= curPointsInBatch;
@@ -1932,7 +1982,7 @@ void GLInstancingRenderer::drawPoints(const float* positions, const float* color
break;
}
}
glBindVertexArray(0);
glPointSize(1);
glUseProgram(0);

View File

@@ -11,7 +11,7 @@ out vec4 colourV;
void main (void)
{
colourV = (colour[3] == -1) ? colourIn : colour;
colourV = colour;
gl_Position = ProjectionMatrix * ModelViewMatrix * position;
}

View File

@@ -5,11 +5,10 @@ static const char* linesVertexShader= \
"uniform mat4 ProjectionMatrix;\n"
"uniform vec4 colour;\n"
"in vec4 position;\n"
"in vec4 colourIn;\n"
"out vec4 colourV;\n"
"void main (void)\n"
"{\n"
" colourV = (colour[3] == -1) ? colourIn : colour;\n"
" colourV = colour;\n"
" gl_Position = ProjectionMatrix * ModelViewMatrix * position;\n"
" \n"
"}\n"

View File

@@ -0,0 +1,10 @@
#version 150
in vec4 colourV;
out vec4 fragColour;
void main(void)
{
fragColour = colourV;
}

View File

@@ -0,0 +1,10 @@
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* pointsFragmentShader= \
"#version 150\n"
"in vec4 colourV;\n"
"out vec4 fragColour;\n"
"void main(void)\n"
"{\n"
" fragColour = colourV;\n"
"}\n"
;

View File

@@ -0,0 +1,15 @@
#version 150
uniform mat4 ModelViewMatrix;
uniform mat4 ProjectionMatrix;
uniform vec4 colour;
in vec4 position;
in vec4 colourIn;
out vec4 colourV;
void main (void)
{
colourV = (colour[3] == -1) ? colourIn : colour;
gl_Position = ProjectionMatrix * ModelViewMatrix * position;
}

View File

@@ -0,0 +1,16 @@
//this file is autogenerated using stringify.bat (premake --stringify) in the build folder of this project
static const char* pointsVertexShader= \
"#version 150 \n"
"uniform mat4 ModelViewMatrix;\n"
"uniform mat4 ProjectionMatrix;\n"
"uniform vec4 colour;\n"
"in vec4 position;\n"
"in vec4 colourIn;\n"
"out vec4 colourV;\n"
"void main (void)\n"
"{\n"
" colourV = (colour[3] == -1) ? colourIn : colour;\n"
" gl_Position = ProjectionMatrix * ModelViewMatrix * position;\n"
" \n"
"}\n"
;