aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorraysan5 <raysan5@gmail.com>2017-07-21 17:19:28 +0200
committerraysan5 <raysan5@gmail.com>2017-07-21 17:19:28 +0200
commite52032f64660008fdcf0c8d707ef6aed1e6fc32f (patch)
treecc3826f1a19356490f37a00e7026830e0958cd70 /src
parent9040526f175ac6e19551fa72e2dc25790be9f818 (diff)
downloadraylib-e52032f64660008fdcf0c8d707ef6aed1e6fc32f.tar.gz
raylib-e52032f64660008fdcf0c8d707ef6aed1e6fc32f.zip
Complete review of raymath
Now it should be coherent with OpenGL math standards
Diffstat (limited to 'src')
-rw-r--r--src/core.c7
-rw-r--r--src/models.c30
-rw-r--r--src/raymath.h83
-rw-r--r--src/rlgl.c35
-rw-r--r--src/rlgl.h2
5 files changed, 71 insertions, 86 deletions
diff --git a/src/core.c b/src/core.c
index 32d4666b..5e7cc811 100644
--- a/src/core.c
+++ b/src/core.c
@@ -895,8 +895,8 @@ void Begin3dMode(Camera camera)
rlLoadIdentity(); // Reset current matrix (MODELVIEW)
// Setup Camera view
- Matrix cameraView = MatrixLookAt(camera.position, camera.target, camera.up);
- rlMultMatrixf(MatrixToFloat(cameraView)); // Multiply MODELVIEW matrix by view matrix (camera)
+ Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
+ rlMultMatrixf(MatrixToFloat(matView)); // Multiply MODELVIEW matrix by view matrix (camera)
rlEnableDepthTest(); // Enable DEPTH_TEST for 3D
}
@@ -991,7 +991,7 @@ Ray GetMouseRay(Vector2 mousePosition, Camera camera)
// For example, if you get view matrix, transpose and inverted and you transform it
// to a vector, you will get its 3d world position coordinates (camera.position).
// If you don't transpose, final position will be wrong.
- MatrixTranspose(&matView);
+ //MatrixTranspose(&matView);
//#define USE_RLGL_UNPROJECT
#if defined(USE_RLGL_UNPROJECT) // OPTION 1: Use rlUnproject()
@@ -1037,7 +1037,6 @@ Vector2 GetWorldToScreen(Vector3 position, Camera camera)
// Calculate view matrix from camera look at (and transpose it)
Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
- MatrixTranspose(&matView);
// Convert world position vector to quaternion
Quaternion worldPos = { position.x, position.y, position.z, 1.0f };
diff --git a/src/models.c b/src/models.c
index 7dce9fa6..cfcefc2f 100644
--- a/src/models.c
+++ b/src/models.c
@@ -119,10 +119,10 @@ void DrawCube(Vector3 position, float width, float height, float length, Color c
rlPushMatrix();
- // NOTE: Be careful! Function order matters (rotate -> scale -> translate)
+ // NOTE: Be careful! Function order matters (scale -> rotate -> translate)
+ rlScalef(1.0f, 3.0f, 1.0f);
+ rlRotatef(45, 0, 1, 0);
rlTranslatef(position.x, position.y, position.z);
- //rlScalef(2.0f, 2.0f, 2.0f);
- //rlRotatef(45, 0, 1, 0);
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -199,8 +199,8 @@ void DrawCubeWires(Vector3 position, float width, float height, float length, Co
rlPushMatrix();
- rlTranslatef(position.x, position.y, position.z);
//rlRotatef(45, 0, 1, 0);
+ rlTranslatef(position.x, position.y, position.z);
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -271,10 +271,10 @@ void DrawCubeTexture(Texture2D texture, Vector3 position, float width, float hei
rlEnableTexture(texture.id);
//rlPushMatrix();
- // NOTE: Be careful! Function order matters (scale, translate, rotate)
+ // NOTE: Be careful! Function order matters (scale -> rotate -> translate)
//rlScalef(2.0f, 2.0f, 2.0f);
- //rlTranslatef(2.0f, 0.0f, 0.0f);
//rlRotatef(45, 0, 1, 0);
+ //rlTranslatef(2.0f, 0.0f, 0.0f);
rlBegin(RL_QUADS);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -330,8 +330,8 @@ void DrawSphere(Vector3 centerPos, float radius, Color color)
void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color color)
{
rlPushMatrix();
- rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(radius, radius, radius);
+ rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -369,8 +369,8 @@ void DrawSphereEx(Vector3 centerPos, float radius, int rings, int slices, Color
void DrawSphereWires(Vector3 centerPos, float radius, int rings, int slices, Color color)
{
rlPushMatrix();
- rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(radius, radius, radius);
+ rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlBegin(RL_LINES);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -496,8 +496,8 @@ void DrawPlane(Vector3 centerPos, Vector2 size, Color color)
{
// NOTE: Plane is always created on XZ ground
rlPushMatrix();
- rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlScalef(size.x, 1.0f, size.y);
+ rlTranslatef(centerPos.x, centerPos.y, centerPos.z);
rlBegin(RL_TRIANGLES);
rlColor4ub(color.r, color.g, color.b, color.a);
@@ -567,9 +567,8 @@ void DrawGizmo(Vector3 position)
float length = 1.0f;
rlPushMatrix();
- rlTranslatef(position.x, position.y, position.z);
- //rlRotatef(rotation, 0, 1, 0);
rlScalef(length, length, length);
+ rlTranslatef(position.x, position.y, position.z);
rlBegin(RL_LINES);
rlColor3f(1.0f, 0.0f, 0.0f); rlVertex3f(0.0f, 0.0f, 0.0f);
@@ -1347,11 +1346,10 @@ void DrawBillboardRec(Camera camera, Texture2D texture, Rectangle sourceRec, Vec
// NOTE: Billboard size will maintain sourceRec aspect ratio, size will represent billboard width
Vector2 sizeRatio = { size, size*(float)sourceRec.height/sourceRec.width };
- Matrix viewMatrix = MatrixLookAt(camera.position, camera.target, camera.up);
- MatrixTranspose(&viewMatrix);
+ Matrix matView = MatrixLookAt(camera.position, camera.target, camera.up);
- Vector3 right = { viewMatrix.m0, viewMatrix.m4, viewMatrix.m8 };
- //Vector3 up = { viewMatrix.m1, viewMatrix.m5, viewMatrix.m9 };
+ Vector3 right = { matView.m0, matView.m4, matView.m8 };
+ //Vector3 up = { matView.m1, matView.m5, matView.m9 };
// NOTE: Billboard locked on axis-Y
Vector3 up = { 0.0f, 1.0f, 0.0f };
@@ -1660,7 +1658,7 @@ RayHitInfo GetCollisionRayGround(Ray ray, float groundHeight)
}
// Calculate mesh bounding box limits
-// NOTE: minVertex and maxVertex should be transformed by model transform matrix (position, scale, rotate)
+// NOTE: minVertex and maxVertex should be transformed by model transform matrix
BoundingBox CalculateBoundingBox(Mesh mesh)
{
// Get min and max vertex to construct bounds (AABB)
diff --git a/src/raymath.h b/src/raymath.h
index ec824408..a4f3f895 100644
--- a/src/raymath.h
+++ b/src/raymath.h
@@ -706,10 +706,10 @@ RMDEF Matrix MatrixSubstract(Matrix left, Matrix right)
// Returns translation matrix
RMDEF Matrix MatrixTranslate(float x, float y, float z)
{
- Matrix result = { 1.0f, 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f, 0.0f,
- 0.0f, 0.0f, 1.0f, 0.0f,
- x, y, z, 1.0f };
+ Matrix result = { 1.0f, 0.0f, 0.0f, x,
+ 0.0f, 1.0f, 0.0f, y,
+ 0.0f, 0.0f, 1.0f, z,
+ 0.0f, 0.0f, 0.0f, 1.0f };
return result;
}
@@ -834,22 +834,22 @@ RMDEF Matrix MatrixMultiply(Matrix left, Matrix right)
{
Matrix result;
- result.m0 = right.m0*left.m0 + right.m1*left.m4 + right.m2*left.m8 + right.m3*left.m12;
- result.m1 = right.m0*left.m1 + right.m1*left.m5 + right.m2*left.m9 + right.m3*left.m13;
- result.m2 = right.m0*left.m2 + right.m1*left.m6 + right.m2*left.m10 + right.m3*left.m14;
- result.m3 = right.m0*left.m3 + right.m1*left.m7 + right.m2*left.m11 + right.m3*left.m15;
- result.m4 = right.m4*left.m0 + right.m5*left.m4 + right.m6*left.m8 + right.m7*left.m12;
- result.m5 = right.m4*left.m1 + right.m5*left.m5 + right.m6*left.m9 + right.m7*left.m13;
- result.m6 = right.m4*left.m2 + right.m5*left.m6 + right.m6*left.m10 + right.m7*left.m14;
- result.m7 = right.m4*left.m3 + right.m5*left.m7 + right.m6*left.m11 + right.m7*left.m15;
- result.m8 = right.m8*left.m0 + right.m9*left.m4 + right.m10*left.m8 + right.m11*left.m12;
- result.m9 = right.m8*left.m1 + right.m9*left.m5 + right.m10*left.m9 + right.m11*left.m13;
- result.m10 = right.m8*left.m2 + right.m9*left.m6 + right.m10*left.m10 + right.m11*left.m14;
- result.m11 = right.m8*left.m3 + right.m9*left.m7 + right.m10*left.m11 + right.m11*left.m15;
- result.m12 = right.m12*left.m0 + right.m13*left.m4 + right.m14*left.m8 + right.m15*left.m12;
- result.m13 = right.m12*left.m1 + right.m13*left.m5 + right.m14*left.m9 + right.m15*left.m13;
- result.m14 = right.m12*left.m2 + right.m13*left.m6 + right.m14*left.m10 + right.m15*left.m14;
- result.m15 = right.m12*left.m3 + right.m13*left.m7 + right.m14*left.m11 + right.m15*left.m15;
+ result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12;
+ result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13;
+ result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14;
+ result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15;
+ result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12;
+ result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13;
+ result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14;
+ result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15;
+ result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12;
+ result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13;
+ result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14;
+ result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15;
+ result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12;
+ result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13;
+ result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14;
+ result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15;
return result;
}
@@ -936,50 +936,49 @@ RMDEF Matrix MatrixLookAt(Vector3 eye, Vector3 target, Vector3 up)
VectorNormalize(&x);
Vector3 y = VectorCrossProduct(z, x);
VectorNormalize(&y);
-
+
result.m0 = x.x;
result.m1 = x.y;
result.m2 = x.z;
- result.m3 = -((x.x*eye.x) + (x.y*eye.y) + (x.z*eye.z));
+ result.m3 = 0.0f;
result.m4 = y.x;
result.m5 = y.y;
result.m6 = y.z;
- result.m7 = -((y.x*eye.x) + (y.y*eye.y) + (y.z*eye.z));
+ result.m7 = 0.0f;
result.m8 = z.x;
result.m9 = z.y;
result.m10 = z.z;
- result.m11 = -((z.x*eye.x) + (z.y*eye.y) + (z.z*eye.z));
- result.m12 = 0.0f;
- result.m13 = 0.0f;
- result.m14 = 0.0f;
+ result.m11 = 0.0f;
+ result.m12 = eye.x;
+ result.m13 = eye.y;
+ result.m14 = eye.z;
result.m15 = 1.0f;
+ MatrixInvert(&result);
+
return result;
}
// Returns float array of matrix data
-// NOTE: Returned vector is a transposed version of the Matrix struct,
-// it should be this way because, despite raymath use OpenGL column-major convention,
-// Matrix struct memory alignment and variables naming are not coherent
RMDEF float *MatrixToFloat(Matrix mat)
{
static float buffer[16];
buffer[0] = mat.m0;
- buffer[1] = mat.m4;
- buffer[2] = mat.m8;
- buffer[3] = mat.m12;
- buffer[4] = mat.m1;
+ buffer[1] = mat.m1;
+ buffer[2] = mat.m2;
+ buffer[3] = mat.m3;
+ buffer[4] = mat.m4;
buffer[5] = mat.m5;
- buffer[6] = mat.m9;
- buffer[7] = mat.m13;
- buffer[8] = mat.m2;
- buffer[9] = mat.m6;
+ buffer[6] = mat.m6;
+ buffer[7] = mat.m7;
+ buffer[8] = mat.m8;
+ buffer[9] = mat.m9;
buffer[10] = mat.m10;
- buffer[11] = mat.m14;
- buffer[12] = mat.m3;
- buffer[13] = mat.m7;
- buffer[14] = mat.m11;
+ buffer[11] = mat.m11;
+ buffer[12] = mat.m12;
+ buffer[13] = mat.m13;
+ buffer[14] = mat.m14;
buffer[15] = mat.m15;
return buffer;
diff --git a/src/rlgl.c b/src/rlgl.c
index 54b30b90..93612896 100644
--- a/src/rlgl.c
+++ b/src/rlgl.c
@@ -395,7 +395,7 @@ void rlLoadIdentity(void) { glLoadIdentity(); }
void rlTranslatef(float x, float y, float z) { glTranslatef(x, y, z); }
void rlRotatef(float angleDeg, float x, float y, float z) { glRotatef(angleDeg, x, y, z); }
void rlScalef(float x, float y, float z) { glScalef(x, y, z); }
-void rlMultMatrixf(float *mat) { glMultMatrixf(mat); }
+void rlMultMatrixf(float *matf) { glMultMatrixf(matf); }
#elif defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)
@@ -445,7 +445,6 @@ void rlLoadIdentity(void)
void rlTranslatef(float x, float y, float z)
{
Matrix matTranslation = MatrixTranslate(x, y, z);
- MatrixTranspose(&matTranslation);
*currentMatrix = MatrixMultiply(*currentMatrix, matTranslation);
}
@@ -458,7 +457,6 @@ void rlRotatef(float angleDeg, float x, float y, float z)
Vector3 axis = (Vector3){ x, y, z };
VectorNormalize(&axis);
matRotation = MatrixRotate(axis, angleDeg*DEG2RAD);
- MatrixTranspose(&matRotation);
*currentMatrix = MatrixMultiply(*currentMatrix, matRotation);
}
@@ -467,28 +465,26 @@ void rlRotatef(float angleDeg, float x, float y, float z)
void rlScalef(float x, float y, float z)
{
Matrix matScale = MatrixScale(x, y, z);
- MatrixTranspose(&matScale);
*currentMatrix = MatrixMultiply(*currentMatrix, matScale);
}
// Multiply the current matrix by another matrix
-void rlMultMatrixf(float *mat)
+void rlMultMatrixf(float *matf)
{
// Matrix creation from array
- Matrix mat2 = { m[0], m[1], m[2], m[3],
- m[4], m[5], m[6], m[7],
- m[8], m[9], m[10], m[11],
- m[12], m[13], m[14], m[15] };
+ Matrix mat = { matf[0], matf[4], matf[8], matf[12],
+ matf[1], matf[5], matf[9], matf[13],
+ matf[2], matf[6], matf[10], matf[14],
+ matf[3], matf[7], matf[11], matf[15] };
- *currentMatrix = MatrixMultiply(*currentMatrix, mat2);
+ *currentMatrix = MatrixMultiply(*currentMatrix, mat);
}
// Multiply the current matrix by a perspective matrix generated by parameters
void rlFrustum(double left, double right, double bottom, double top, double near, double far)
{
Matrix matPerps = MatrixFrustum(left, right, bottom, top, near, far);
- MatrixTranspose(&matPerps);
*currentMatrix = MatrixMultiply(*currentMatrix, matPerps);
}
@@ -497,7 +493,6 @@ void rlFrustum(double left, double right, double bottom, double top, double near
void rlOrtho(double left, double right, double bottom, double top, double near, double far)
{
Matrix matOrtho = MatrixOrtho(left, right, bottom, top, near, far);
- MatrixTranspose(&matOrtho);
*currentMatrix = MatrixMultiply(*currentMatrix, matOrtho);
}
@@ -2545,7 +2540,7 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size)
// Create projection (transposed) and different views for each face
Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0);
- MatrixTranspose(&fboProjection);
+ //MatrixTranspose(&fboProjection);
Matrix fboViews[6] = {
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
@@ -2617,7 +2612,7 @@ Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size)
// Create projection (transposed) and different views for each face
Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0);
- MatrixTranspose(&fboProjection);
+ //MatrixTranspose(&fboProjection);
Matrix fboViews[6] = {
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
@@ -2693,7 +2688,7 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size)
// Create projection (transposed) and different views for each face
Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0);
- MatrixTranspose(&fboProjection);
+ //MatrixTranspose(&fboProjection);
Matrix fboViews[6] = {
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
@@ -2931,7 +2926,6 @@ void ToggleVrMode(void)
// Reset viewport and default projection-modelview matrices
rlViewport(0, 0, screenWidth, screenHeight);
projection = MatrixOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f);
- MatrixTranspose(&projection);
modelview = MatrixIdentity();
}
else vrStereoRender = true;
@@ -3034,7 +3028,6 @@ void EndVrDrawing(void)
// Reset viewport and default projection-modelview matrices
rlViewport(0, 0, screenWidth, screenHeight);
projection = MatrixOrtho(0, screenWidth, screenHeight, 0, 0.0f, 1.0f);
- MatrixTranspose(&projection);
modelview = MatrixIdentity();
rlDisableDepthTest();
@@ -3977,18 +3970,14 @@ static void SetStereoConfig(VrDeviceInfo hmd)
// Fovy is normally computed with: 2*atan2(hmd.vScreenSize, 2*hmd.eyeToScreenDistance)*RAD2DEG
// ...but with lens distortion it is increased (see Oculus SDK Documentation)
//float fovy = 2.0f*atan2(hmd.vScreenSize*0.5f*distortionScale, hmd.eyeToScreenDistance)*RAD2DEG; // Really need distortionScale?
- float fovy = 2.0f*(float)atan2(hmd.vScreenSize*0.5f, hmd.eyeToScreenDistance)*RAD2DEG;
+ float fovy = 2.0f*(float)atan2(hmd.vScreenSize*0.5f, hmd.eyeToScreenDistance);
// Compute camera projection matrices
float projOffset = 4.0f*lensShift; // Scaled to projection space coordinates [-1..1]
- Matrix proj = MatrixPerspective(fovy*DEG2RAD, aspect, 0.01, 1000.0);
+ Matrix proj = MatrixPerspective(fovy, aspect, 0.01, 1000.0);
vrConfig.eyesProjection[0] = MatrixMultiply(proj, MatrixTranslate(projOffset, 0.0f, 0.0f));
vrConfig.eyesProjection[1] = MatrixMultiply(proj, MatrixTranslate(-projOffset, 0.0f, 0.0f));
- // NOTE: Projection matrices must be transposed due to raymath convention
- MatrixTranspose(&vrConfig.eyesProjection[0]);
- MatrixTranspose(&vrConfig.eyesProjection[1]);
-
// Compute camera transformation matrices
// NOTE: Camera movement might seem more natural if we model the head.
// Our axis of rotation is the base of our head, so we might want to add
diff --git a/src/rlgl.h b/src/rlgl.h
index 84ece706..b9ea0f43 100644
--- a/src/rlgl.h
+++ b/src/rlgl.h
@@ -355,7 +355,7 @@ void rlLoadIdentity(void); // Reset current matrix to ident
void rlTranslatef(float x, float y, float z); // Multiply the current matrix by a translation matrix
void rlRotatef(float angleDeg, float x, float y, float z); // Multiply the current matrix by a rotation matrix
void rlScalef(float x, float y, float z); // Multiply the current matrix by a scaling matrix
-void rlMultMatrixf(float *mat); // Multiply the current matrix by another matrix
+void rlMultMatrixf(float *matf); // Multiply the current matrix by another matrix
void rlFrustum(double left, double right, double bottom, double top, double near, double far);
void rlOrtho(double left, double right, double bottom, double top, double near, double far);
void rlViewport(int x, int y, int width, int height); // Set the viewport area