Simplified MatrixMultiply() function

This commit is contained in:
raysan5
2016-01-06 17:22:24 +01:00
parent 1ce010c7d4
commit 7f2e67e924
3 changed files with 114 additions and 82 deletions

View File

@@ -1500,28 +1500,34 @@ void rlglDrawModel(Model model, Vector3 position, float rotationAngle, Vector3 r
#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
glUseProgram(model.shader.id);
// Apply transformation provided in model.transform matrix
// TODO: review if at this point the modelview matrix just contains view matrix values
Matrix viewworld = modelview; // Store view matrix before applying model transformations
Matrix modelviewworld = MatrixMultiply(model.transform, modelview); // World-space transformation
// Apply transformations provided in function
// At this point the modelview matrix just contains the view matrix (camera)
// That's because Begin3dMode() sets it an no model-drawing function modifies it, all use rlPushMatrix() and rlPopMatrix()
Matrix matView = modelview; // View matrix (camera)
Matrix matProjection = projection; // Projection matrix (perspective)
// Calculate transformation matrix from function parameters
// Get transform matrix (rotation -> scale -> translation)
Matrix rotation = MatrixRotate(rotationAngle*DEG2RAD, rotationAxis);
Matrix matRotation = MatrixRotate(rotationAngle*DEG2RAD, rotationAxis);
Matrix matScale = MatrixScale(scale.x, scale.y, scale.z);
Matrix translation = MatrixTranslate(position.x, position.y, position.z);
Matrix matTranslation = MatrixTranslate(position.x, position.y, position.z);
Matrix matTransform = MatrixMultiply(MatrixMultiply(matRotation, matScale), matTranslation);
// Combine model internal transformation matrix (model.transform) with matrix generated by function parameters (matTransform)
Matrix matModel = MatrixMultiply(model.transform, matTransform); // Transform to world-space coordinates
// Calculate model-view matrix combining matModel and matView
Matrix matModelView = MatrixMultiply(matModel, matView); // Transform to camera-space coordinates
Matrix transform = MatrixMultiply(MatrixMultiply(rotation, matScale), translation); // Object-space transformation matrix
modelviewworld = MatrixMultiply(transform, modelview); // World-space transformation
// Calculate model-view-projection matrix (MVP)
//Matrix matMVP = MatrixMultiply(matModelView, matProjection); // Transform to screen-space coordinates
// Projection: Screen-space transformation
// NOTE: Drawing in OpenGL 3.3+, transform is passed to shader
glUniformMatrix4fv(model.shader.projectionLoc, 1, false, GetMatrixVector(projection));
glUniformMatrix4fv(model.shader.modelLoc, 1, false, GetMatrixVector(transform));
glUniformMatrix4fv(model.shader.viewLoc, 1, false, GetMatrixVector(viewworld));
glUniformMatrix4fv(model.shader.modelviewLoc, 1, false, GetMatrixVector(modelviewworld));
// NOTE: Drawing in OpenGL 3.3+, matrices are passed to shader
// TODO: Reduce number of matrices passed to shaders, use only matMVP
glUniformMatrix4fv(model.shader.modelLoc, 1, false, GetMatrixVector(matModel));
glUniformMatrix4fv(model.shader.viewLoc, 1, false, GetMatrixVector(matView));
glUniformMatrix4fv(model.shader.projectionLoc, 1, false, GetMatrixVector(matProjection));
glUniformMatrix4fv(model.shader.modelviewLoc, 1, false, GetMatrixVector(matModelView));
// Apply color tinting to model
// NOTE: Just update one uniform on fragment shader