Browse Source

work on quat and matrix math - deleted multiple copies of raymath.h causing issues (#1359)

Co-authored-by: codifies <nospam@antispam.com>
pull/1379/head
chriscamacho 4 years ago
committed by GitHub
parent
commit
d140dc81c0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 191 additions and 4493 deletions
  1. +5
    -3
      examples/Makefile
  2. +131
    -0
      examples/core/core_quat_conversion.c
  3. +0
    -1466
      examples/core/raymath.h
  4. +0
    -1466
      examples/models/raymath.h
  5. +0
    -1466
      examples/shaders/raymath.h
  6. +55
    -92
      src/raymath.h

+ 5
- 3
examples/Makefile View File

@ -267,7 +267,8 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP)
ifeq ($(PLATFORM_OS),LINUX) ifeq ($(PLATFORM_OS),LINUX)
# Reset everything. # Reset everything.
# Precedence: immediately local, installed version, raysan5 provided libs -I$(RAYLIB_H_INSTALL_PATH) -I$(RAYLIB_PATH)/release/include # Precedence: immediately local, installed version, raysan5 provided libs -I$(RAYLIB_H_INSTALL_PATH) -I$(RAYLIB_PATH)/release/include
INCLUDE_PATHS = -I$(RAYLIB_H_INSTALL_PATH) -isystem. -isystem$(RAYLIB_PATH)/src -isystem$(RAYLIB_PATH)/release/include -isystem$(RAYLIB_PATH)/src/external
#INCLUDE_PATHS = -I$(RAYLIB_H_INSTALL_PATH) -isystem. -isystem$(RAYLIB_PATH)/src -isystem$(RAYLIB_PATH)/release/include -isystem$(RAYLIB_PATH)/src/external
INCLUDE_PATHS = -I$(RAYLIB_H_INSTALL_PATH) -I. -I$(RAYLIB_PATH)/src -I$(RAYLIB_PATH)/release/include -I$(RAYLIB_PATH)/src/external
endif endif
endif endif
@ -290,7 +291,7 @@ ifeq ($(PLATFORM),PLATFORM_DESKTOP)
ifeq ($(PLATFORM_OS),LINUX) ifeq ($(PLATFORM_OS),LINUX)
# Reset everything. # Reset everything.
# Precedence: immediately local, installed version, raysan5 provided libs # Precedence: immediately local, installed version, raysan5 provided libs
LDFLAGS = -L. -L$(RAYLIB_INSTALL_PATH) -L$(RAYLIB_RELEASE_PATH)
LDFLAGS = -L. -L$(RAYLIB_INSTALL_PATH) -L$(RAYLIB_RELEASE_PATH) -L$(RAYLIB_PATH)
endif endif
endif endif
@ -378,7 +379,8 @@ CORE = \
core/core_scissor_test \ core/core_scissor_test \
core/core_storage_values \ core/core_storage_values \
core/core_vr_simulator \ core/core_vr_simulator \
core/core_loading_thread
core/core_loading_thread \
core/core_quat_conversion
SHAPES = \ SHAPES = \
shapes/shapes_basic_shapes \ shapes/shapes_basic_shapes \

+ 131
- 0
examples/core/core_quat_conversion.c View File

@ -0,0 +1,131 @@
/*******************************************************************************************
*
* raylib [core] example - quat conversions
*
* Welcome to raylib!
*
* generally you should really stick to eulers OR quats...
* This tests that various conversions are equivilant.
*
* You can find all basic examples on [C:\raylib\raylib\examples] directory and
* raylib official webpage: [www.raylib.com]
*
* Enjoy using raylib. :)
*
* This example has been created using raylib 1.0 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Copyright (c) 2013-2020 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include "raylib.h"
#include "raymath.h"
#ifndef PI2
#define PI2 PI*2
#endif
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [core] example - quat conversions");
Camera3D camera = { 0 };
camera.position = (Vector3){ 0.0f, 10.0f, 10.0f }; // Camera position
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
camera.fovy = 45.0f; // Camera field-of-view Y
camera.type = CAMERA_PERSPECTIVE; // Camera mode type
Mesh msh = GenMeshCylinder(.2, 1, 32);
Model mod = LoadModelFromMesh(msh);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
Quaternion q1;
Matrix m1,m2,m3,m4;
Vector3 v1,v2;
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
if (!IsKeyDown(KEY_SPACE)) {
v1.x += 0.01;
v1.y += 0.03;
v1.z += 0.05;
}
if (v1.x > PI2) v1.x-=PI2;
if (v1.y > PI2) v1.y-=PI2;
if (v1.z > PI2) v1.z-=PI2;
q1 = QuaternionFromEuler(v1.x, v1.y, v1.z);
m1 = MatrixRotateZYX(v1);
m2 = QuaternionToMatrix(q1);
q1 = QuaternionFromMatrix(m1);
m3 = QuaternionToMatrix(q1);
v2 = QuaternionToEuler(q1);
v2.x*=DEG2RAD; v2.y*=DEG2RAD; v2.z*=DEG2RAD;
m4 = MatrixRotateZYX(v2);
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
mod.transform = m1;
DrawModel(mod, (Vector3){-1,0,0},1.0,RED);
mod.transform = m2;
DrawModel(mod, (Vector3){1,0,0},1.0,RED);
mod.transform = m3;
DrawModel(mod, (Vector3){0,0,0},1.0,RED);
mod.transform = m4;
DrawModel(mod, (Vector3){0,0,-1},1.0,RED);
DrawGrid(10, 1.0f);
EndMode3D();
if (v2.x<0) v2.x+=PI2;
if (v2.y<0) v2.y+=PI2;
if (v2.z<0) v2.z+=PI2;
Color cx,cy,cz;
cx=cy=cz=BLACK;
if (v1.x == v2.x) cx = GREEN;
if (v1.y == v2.y) cy = GREEN;
if (v1.z == v2.z) cz = GREEN;
DrawText(TextFormat("%2.3f",v1.x),20,20,20,cx);
DrawText(TextFormat("%2.3f",v1.y),20,40,20,cy);
DrawText(TextFormat("%2.3f",v1.z),20,60,20,cz);
DrawText(TextFormat("%2.3f",v2.x),200,20,20,cx);
DrawText(TextFormat("%2.3f",v2.y),200,40,20,cy);
DrawText(TextFormat("%2.3f",v2.z),200,60,20,cz);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}

+ 0
- 1466
examples/core/raymath.h
File diff suppressed because it is too large
View File


+ 0
- 1466
examples/models/raymath.h
File diff suppressed because it is too large
View File


+ 0
- 1466
examples/shaders/raymath.h
File diff suppressed because it is too large
View File


+ 55
- 92
src/raymath.h View File

@ -78,6 +78,8 @@
#define PI 3.14159265358979323846 #define PI 3.14159265358979323846
#endif #endif
#ifndef DEG2RAD #ifndef DEG2RAD
#define DEG2RAD (PI/180.0f) #define DEG2RAD (PI/180.0f)
#endif #endif
@ -926,6 +928,8 @@ RMDEF Matrix MatrixRotateZ(float angle)
return result; return result;
} }
// Returns scaling matrix // Returns scaling matrix
RMDEF Matrix MatrixScale(float x, float y, float z) RMDEF Matrix MatrixScale(float x, float y, float z)
{ {
@ -963,6 +967,17 @@ RMDEF Matrix MatrixMultiply(Matrix left, Matrix right)
return result; return result;
} }
// TODO suboptimal should be able to create this matrix in one go
// this is an aditional 3 matrix multiplies!
RMDEF Matrix MatrixRotateZYX(Vector3 v)
{
Matrix result = MatrixRotateZ(v.z);
result = MatrixMultiply(result, MatrixRotateY(v.y));
result = MatrixMultiply(result, MatrixRotateX(v.x));
return result;
}
// Returns perspective projection matrix // Returns perspective projection matrix
RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far) RMDEF Matrix MatrixFrustum(double left, double right, double bottom, double top, double near, double far)
{ {
@ -1297,105 +1312,53 @@ RMDEF Quaternion QuaternionFromVector3ToVector3(Vector3 from, Vector3 to)
} }
// Returns a quaternion for a given rotation matrix // Returns a quaternion for a given rotation matrix
RMDEF Quaternion QuaternionFromMatrix(Matrix mat)
{
Quaternion result = { 0 };
float trace = MatrixTrace(mat);
if (trace > 0.0f)
{
float s = sqrtf(trace + 1)*2.0f;
float invS = 1.0f/s;
result.w = s*0.25f;
result.x = (mat.m6 - mat.m9)*invS;
result.y = (mat.m8 - mat.m2)*invS;
result.z = (mat.m1 - mat.m4)*invS;
}
else
{
float m00 = mat.m0, m11 = mat.m5, m22 = mat.m10;
if (m00 > m11 && m00 > m22)
{
float s = (float)sqrt(1.0f + m00 - m11 - m22)*2.0f;
float invS = 1.0f/s;
result.w = (mat.m6 - mat.m9)*invS;
result.x = s*0.25f;
result.y = (mat.m4 + mat.m1)*invS;
result.z = (mat.m8 + mat.m2)*invS;
}
else if (m11 > m22)
{
float s = sqrtf(1.0f + m11 - m00 - m22)*2.0f;
float invS = 1.0f/s;
result.w = (mat.m8 - mat.m2)*invS;
result.x = (mat.m4 + mat.m1)*invS;
result.y = s*0.25f;
result.z = (mat.m9 + mat.m6)*invS;
}
else
{
float s = sqrtf(1.0f + m22 - m00 - m11)*2.0f;
float invS = 1.0f/s;
result.w = (mat.m1 - mat.m4)*invS;
result.x = (mat.m8 + mat.m2)*invS;
result.y = (mat.m9 + mat.m6)*invS;
result.z = s*0.25f;
}
}
return result;
RMDEF Quaternion QuaternionFromMatrix(Matrix m)
{
Quaternion q;
if ( m.m0 > m.m5 && m.m0 > m.m10 ) {
float s = sqrt( 1.0 + m.m0 - m.m5 - m.m10 ) * 2;
q.x = 0.25 * s;
q.y = (m.m4 + m.m1 ) / s;
q.z = (m.m2 + m.m8 ) / s;
q.w = (m.m9 - m.m6 ) / s;
} else if ( m.m5 > m.m10 ) {
float s = sqrt( 1.0 + m.m5 - m.m0 - m.m10 ) * 2;
q.x = (m.m4 + m.m1 ) / s;
q.y = 0.25 * s;
q.z = (m.m9 + m.m6 ) / s;
q.w = (m.m2 - m.m8 ) / s;
} else {
float s = sqrt( 1.0 + m.m10 - m.m0 - m.m5 ) * 2;
q.x = (m.m2 + m.m8 ) / s;
q.y = (m.m9 + m.m6 ) / s;
q.z = 0.25 * s;
q.w = (m.m4 - m.m1 ) / s;
}
return q;
} }
// Returns a matrix for a given quaternion // Returns a matrix for a given quaternion
RMDEF Matrix QuaternionToMatrix(Quaternion q) RMDEF Matrix QuaternionToMatrix(Quaternion q)
{ {
Matrix result = { 0 };
float x = q.x, y = q.y, z = q.z, w = q.w;
float x2 = x + x;
float y2 = y + y;
float z2 = z + z;
float length = QuaternionLength(q);
float lengthSquared = length*length;
float xx = x*x2/lengthSquared;
float xy = x*y2/lengthSquared;
float xz = x*z2/lengthSquared;
float yy = y*y2/lengthSquared;
float yz = y*z2/lengthSquared;
float zz = z*z2/lengthSquared;
float wx = w*x2/lengthSquared;
float wy = w*y2/lengthSquared;
float wz = w*z2/lengthSquared;
Matrix m = MatrixIdentity();
float a2=2*(q.x*q.x), b2=2*(q.y*q.y), c2=2*(q.z*q.z); //, d2=2*(q.w*q.w);
float ab=2*(q.x*q.y), ac=2*(q.x*q.z), bc=2*(q.y*q.z);
float ad=2*(q.x*q.w), bd=2*(q.y*q.w), cd=2*(q.z*q.w);
result.m0 = 1.0f - (yy + zz);
result.m1 = xy - wz;
result.m2 = xz + wy;
result.m3 = 0.0f;
result.m4 = xy + wz;
result.m5 = 1.0f - (xx + zz);
result.m6 = yz - wx;
result.m7 = 0.0f;
result.m8 = xz - wy;
result.m9 = yz + wx;
result.m10 = 1.0f - (xx + yy);
result.m11 = 0.0f;
result.m12 = 0.0f;
result.m13 = 0.0f;
result.m14 = 0.0f;
result.m15 = 1.0f;
m.m0 = 1 - b2 - c2;
m.m1 = ab - cd;
m.m2 = ac + bd;
m.m4 = ab + cd;
m.m5 = 1 - a2 - c2;
m.m6 = bc - ad;
m.m8 = ac - bd;
m.m9 = bc + ad;
m.m10 = 1 - a2 - b2;
return result;
return m;
} }
// Returns rotation quaternion for an angle and axis // Returns rotation quaternion for an angle and axis

Loading…
Cancel
Save