REVIEWED: DrawMeshInstanced() matrix computations
Simplified some parts and reviewed for a correct computation of matrices, considering stereo render view/projection per eye transformations
This commit is contained in:
parent
434a3a276d
commit
c772de702b
3 changed files with 45 additions and 49 deletions
|
@ -6,7 +6,7 @@ in vec2 vertexTexCoord;
|
||||||
in vec3 vertexNormal;
|
in vec3 vertexNormal;
|
||||||
in vec4 vertexColor;
|
in vec4 vertexColor;
|
||||||
|
|
||||||
layout (location = 12) in mat4 instance;
|
in mat4 instanceTransform;
|
||||||
|
|
||||||
// Input uniform values
|
// Input uniform values
|
||||||
uniform mat4 mvp;
|
uniform mat4 mvp;
|
||||||
|
@ -22,14 +22,14 @@ out vec3 fragNormal;
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Send vertex attributes to fragment shader
|
// Send vertex attributes to fragment shader
|
||||||
fragPosition = vec3(instance * vec4(vertexPosition, 1.0));
|
fragPosition = vec3(vec4(vertexPosition, 1.0));
|
||||||
fragTexCoord = vertexTexCoord;
|
fragTexCoord = vertexTexCoord;
|
||||||
fragColor = vertexColor;
|
fragColor = vertexColor;
|
||||||
|
|
||||||
mat3 normalMatrix = transpose(inverse(mat3(instance)));
|
mat3 normalMatrix = transpose(inverse(mat3(instanceTransform)));
|
||||||
fragNormal = normalize(normalMatrix*vertexNormal);
|
fragNormal = normalize(normalMatrix*vertexNormal);
|
||||||
|
|
||||||
mat4 mvpi = mvp * instance;
|
mat4 mvpi = mvp*instanceTransform;
|
||||||
|
|
||||||
// Calculate final vertex position
|
// Calculate final vertex position
|
||||||
gl_Position = mvpi*vec4(vertexPosition, 1.0);
|
gl_Position = mvpi*vec4(vertexPosition, 1.0);
|
||||||
|
|
|
@ -89,7 +89,7 @@ int main(void)
|
||||||
// Get some shader loactions
|
// Get some shader loactions
|
||||||
shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
|
shader.locs[SHADER_LOC_MATRIX_MVP] = GetShaderLocation(shader, "mvp");
|
||||||
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
|
||||||
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instance");
|
shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocationAttrib(shader, "instanceTransform");
|
||||||
|
|
||||||
// Ambient light level
|
// Ambient light level
|
||||||
int ambientLoc = GetShaderLocation(shader, "ambient");
|
int ambientLoc = GetShaderLocation(shader, "ambient");
|
||||||
|
@ -173,6 +173,7 @@ int main(void)
|
||||||
ClearBackground(RAYWHITE);
|
ClearBackground(RAYWHITE);
|
||||||
|
|
||||||
BeginMode3D(camera);
|
BeginMode3D(camera);
|
||||||
|
//DrawMesh(cube, material, MatrixIdentity());
|
||||||
DrawMeshInstanced(cube, material, transforms, instances);
|
DrawMeshInstanced(cube, material, transforms, instances);
|
||||||
EndMode3D();
|
EndMode3D();
|
||||||
|
|
||||||
|
|
73
src/models.c
73
src/models.c
|
@ -964,6 +964,13 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
|
||||||
|
// Check instancing
|
||||||
|
bool instancing = false;
|
||||||
|
if (instances < 1) return;
|
||||||
|
else if (instances > 1) instancing = true;
|
||||||
|
float16 *instanceTransforms = NULL;
|
||||||
|
unsigned int instancesVboId = 0;
|
||||||
|
|
||||||
// Bind shader program
|
// Bind shader program
|
||||||
rlEnableShader(material.shader.id);
|
rlEnableShader(material.shader.id);
|
||||||
|
|
||||||
|
@ -982,7 +989,7 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
rlSetUniform(material.shader.locs[SHADER_LOC_COLOR_DIFFUSE], values, SHADER_UNIFORM_VEC4, 1);
|
rlSetUniform(material.shader.locs[SHADER_LOC_COLOR_DIFFUSE], values, SHADER_UNIFORM_VEC4, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Upload to shader material.colSpecular (if available)
|
// Upload to shader material.colSpecular (if location available)
|
||||||
if (material.shader.locs[SHADER_LOC_COLOR_SPECULAR] != -1)
|
if (material.shader.locs[SHADER_LOC_COLOR_SPECULAR] != -1)
|
||||||
{
|
{
|
||||||
float values[4] = {
|
float values[4] = {
|
||||||
|
@ -995,28 +1002,26 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
rlSetUniform(material.shader.locs[SHADER_LOC_COLOR_SPECULAR], values, SHADER_UNIFORM_VEC4, 1);
|
rlSetUniform(material.shader.locs[SHADER_LOC_COLOR_SPECULAR], values, SHADER_UNIFORM_VEC4, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (material.shader.locs[SHADER_LOC_MATRIX_VIEW] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_VIEW], rlGetMatrixModelview());
|
// Get a copy of current matrices to work with,
|
||||||
if (material.shader.locs[SHADER_LOC_MATRIX_PROJECTION] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_PROJECTION], rlGetMatrixProjection());
|
// just in case stereo render is required and we need to modify them
|
||||||
|
// NOTE: At this point the modelview matrix just contains the view matrix (camera)
|
||||||
|
// That's because BeginMode3D() sets it and there is no model-drawing function
|
||||||
|
// that modifies it, all use rlPushMatrix() and rlPopMatrix()
|
||||||
|
Matrix matView = rlGetMatrixModelview();
|
||||||
|
Matrix matModelView = matView;
|
||||||
|
Matrix matProjection = rlGetMatrixProjection();
|
||||||
|
|
||||||
bool instancing = false;
|
// Upload view and projection matrices (if locations available)
|
||||||
if (instances < 1) return;
|
if (material.shader.locs[SHADER_LOC_MATRIX_VIEW] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_VIEW], matView);
|
||||||
else if (instances > 1) instancing = true;
|
if (material.shader.locs[SHADER_LOC_MATRIX_PROJECTION] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_PROJECTION], matProjection);
|
||||||
|
|
||||||
float16 *instanceTransforms = NULL;
|
|
||||||
unsigned int instancesVboId = 0;
|
|
||||||
|
|
||||||
Matrix matView = MatrixIdentity();
|
|
||||||
Matrix matModelView = MatrixIdentity();
|
|
||||||
Matrix matProjection = MatrixIdentity();
|
|
||||||
|
|
||||||
// TODO: Review how matrices multiplications are computed!
|
|
||||||
|
|
||||||
if (instancing)
|
if (instancing)
|
||||||
{
|
{
|
||||||
// Create instances buffer
|
// Create instances buffer
|
||||||
instanceTransforms = RL_MALLOC(instances*sizeof(float16));
|
instanceTransforms = RL_MALLOC(instances*sizeof(float16));
|
||||||
|
|
||||||
for (int i = 0; i < instances; i++) instanceTransforms[i] = MatrixToFloatV(MatrixMultiply(transforms[i], rlGetMatrixModelview()));
|
// Fill buffer with instances transformations as float16 arrays
|
||||||
|
for (int i = 0; i < instances; i++) instanceTransforms[i] = MatrixToFloatV(transforms[i]);
|
||||||
|
|
||||||
// Enable mesh VAO to attach new buffer
|
// Enable mesh VAO to attach new buffer
|
||||||
rlEnableVertexArray(mesh.vaoId);
|
rlEnableVertexArray(mesh.vaoId);
|
||||||
|
@ -1027,30 +1032,26 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
// no faster, since we're transferring all the transform matrices anyway
|
// no faster, since we're transferring all the transform matrices anyway
|
||||||
instancesVboId = rlLoadVertexBuffer(instanceTransforms, instances*sizeof(float16), false);
|
instancesVboId = rlLoadVertexBuffer(instanceTransforms, instances*sizeof(float16), false);
|
||||||
|
|
||||||
// Instances are send to attribute location: SHADER_LOC_MATRIX_MODEL
|
// Instances transformation matrices are send to shader attribute location: SHADER_LOC_MATRIX_MODEL
|
||||||
unsigned int instanceLoc = material.shader.locs[SHADER_LOC_MATRIX_MODEL];
|
|
||||||
|
|
||||||
for (unsigned int i = 0; i < 4; i++)
|
for (unsigned int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
rlEnableVertexAttribute(instanceLoc + i);
|
rlEnableVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i);
|
||||||
rlSetVertexAttribute(instanceLoc + i, 4, RL_FLOAT, 0, sizeof(Matrix), (void *)(i*sizeof(Vector4)));
|
rlSetVertexAttribute(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 4, RL_FLOAT, 0, sizeof(Matrix), (void *)(i*sizeof(Vector4)));
|
||||||
|
rlSetVertexAttributeDivisor(material.shader.locs[SHADER_LOC_MATRIX_MODEL] + i, 1);
|
||||||
rlSetVertexAttributeDivisor(instanceLoc + i, 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rlDisableVertexBuffer();
|
rlDisableVertexBuffer();
|
||||||
rlDisableVertexArray();
|
rlDisableVertexArray();
|
||||||
|
|
||||||
|
// Accumulate internal matrix transform (push/pop) and view matrix
|
||||||
|
// NOTE: In this case, model instance transformation must be computed in the shader
|
||||||
|
matModelView = MatrixMultiply(rlGetMatrixTransform(), matView);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Calculate and send to shader model matrix
|
// Model transformation matrix is send to shader uniform location: SHADER_LOC_MATRIX_MODEL
|
||||||
if (material.shader.locs[SHADER_LOC_MATRIX_MODEL] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_MODEL], transforms[0]);
|
if (material.shader.locs[SHADER_LOC_MATRIX_MODEL] != -1) rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_MODEL], transforms[0]);
|
||||||
|
|
||||||
// At this point the modelview matrix just contains the view matrix (camera)
|
|
||||||
// That's because BeginMode3D() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix()
|
|
||||||
matView = rlGetMatrixModelview(); // View matrix (camera)
|
|
||||||
matProjection = rlGetMatrixProjection(); // Projection matrix (perspective)
|
|
||||||
|
|
||||||
// Accumulate several transformations:
|
// Accumulate several transformations:
|
||||||
// matView: rlgl internal modelview matrix (actually, just view matrix)
|
// matView: rlgl internal modelview matrix (actually, just view matrix)
|
||||||
// rlGetMatrixTransform(): rlgl internal transform matrix due to push/pop matrix stack
|
// rlGetMatrixTransform(): rlgl internal transform matrix due to push/pop matrix stack
|
||||||
|
@ -1146,22 +1147,16 @@ void DrawMeshInstanced(Mesh mesh, Material material, Matrix *transforms, int ins
|
||||||
|
|
||||||
for (int eye = 0; eye < eyesCount; eye++)
|
for (int eye = 0; eye < eyesCount; eye++)
|
||||||
{
|
{
|
||||||
if (eyesCount == 1) rlSetMatrixModelview(matModelView);
|
// Calculate model-view-projection matrix (MVP)
|
||||||
|
Matrix matMVP = MatrixIdentity();
|
||||||
|
if (eyesCount == 1) matMVP = MatrixMultiply(matModelView, matProjection);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Setup current eye viewport (half screen width)
|
// Setup current eye viewport (half screen width)
|
||||||
rlViewport(eye*rlGetFramebufferWidth()/2, 0, rlGetFramebufferWidth()/2, rlGetFramebufferHeight());
|
rlViewport(eye*rlGetFramebufferWidth()/2, 0, rlGetFramebufferWidth()/2, rlGetFramebufferHeight());
|
||||||
|
matMVP = MatrixMultiply(MatrixMultiply(matModelView, rlGetMatrixViewOffsetStereo(eye)), rlGetMatrixProjectionStereo(eye));
|
||||||
// Set current eye view offset to modelview matrix
|
|
||||||
rlSetMatrixModelview(MatrixMultiply(matModelView, rlGetMatrixViewOffsetStereo(eye)));
|
|
||||||
|
|
||||||
// Set current eye projection matrix
|
|
||||||
rlSetMatrixProjection(rlGetMatrixProjectionStereo(eye));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate model-view-projection matrix (MVP)
|
|
||||||
Matrix matMVP = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection());
|
|
||||||
|
|
||||||
// Send combined model-view-projection matrix to shader
|
// Send combined model-view-projection matrix to shader
|
||||||
rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_MVP], matMVP);
|
rlSetUniformMatrix(material.shader.locs[SHADER_LOC_MATRIX_MVP], matMVP);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue