mirror of https://github.com/bjornbytes/lovr.git
Simplify rotations;
This commit is contained in:
parent
92d5620f48
commit
83fb958ac0
|
@ -294,8 +294,8 @@ void lovrGraphicsTranslate(float x, float y, float z) {
|
|||
mat4_translate(state.transforms[state.transform], x, y, z);
|
||||
}
|
||||
|
||||
void lovrGraphicsRotate(float w, float x, float y, float z) {
|
||||
mat4_rotate(state.transforms[state.transform], w, x, y, z);
|
||||
void lovrGraphicsRotate(float angle, float ax, float ay, float az) {
|
||||
mat4_rotate(state.transforms[state.transform], angle, ax, ay, az);
|
||||
}
|
||||
|
||||
void lovrGraphicsScale(float x, float y, float z) {
|
||||
|
@ -304,28 +304,11 @@ void lovrGraphicsScale(float x, float y, float z) {
|
|||
|
||||
void lovrGraphicsTransform(float tx, float ty, float tz, float sx, float sy, float sz, float angle, float ax, float ay, float az) {
|
||||
|
||||
// Normalize rotation vector
|
||||
float len = sqrtf(ax * ax + ay * ay + az * az);
|
||||
if (len != 1 && len != 0) {
|
||||
len = 1 / len;
|
||||
ax *= len;
|
||||
ay *= len;
|
||||
az *= len;
|
||||
}
|
||||
|
||||
// Convert angle-axis to quaternion
|
||||
float cos2 = cos(angle / 2.f);
|
||||
float sin2 = sin(angle / 2.f);
|
||||
float qw = cos2;
|
||||
float qx = sin2 * ax;
|
||||
float qy = sin2 * ay;
|
||||
float qz = sin2 * az;
|
||||
|
||||
// M *= T * S * R
|
||||
float transform[16];
|
||||
mat4_setTranslation(transform, tx, ty, tz);
|
||||
mat4_scale(transform, sx, sy, sz);
|
||||
mat4_rotate(transform, qw, qx, qy, qz);
|
||||
mat4_rotate(transform, angle, ax, ay, az);
|
||||
lovrGraphicsMatrixTransform(transform);
|
||||
}
|
||||
|
||||
|
@ -552,17 +535,10 @@ void lovrGraphicsSkybox(Skybox* skybox, float angle, float ax, float ay, float a
|
|||
lovrRetain(&lastShader->ref);
|
||||
lovrGraphicsSetShader(state.skyboxShader);
|
||||
|
||||
float cos2 = cos(angle / 2);
|
||||
float sin2 = sin(angle / 2);
|
||||
float rw = cos2;
|
||||
float rx = sin2 * ax;
|
||||
float ry = sin2 * ay;
|
||||
float rz = sin2 * az;
|
||||
|
||||
lovrGraphicsPrepare();
|
||||
lovrGraphicsPush();
|
||||
lovrGraphicsOrigin();
|
||||
lovrGraphicsRotate(rw, rx, ry, rz);
|
||||
lovrGraphicsRotate(angle, ax, ay, az);
|
||||
|
||||
float cube[] = {
|
||||
// Front
|
||||
|
|
|
@ -108,7 +108,7 @@ int lovrGraphicsPush();
|
|||
int lovrGraphicsPop();
|
||||
void lovrGraphicsOrigin();
|
||||
void lovrGraphicsTranslate(float x, float y, float z);
|
||||
void lovrGraphicsRotate(float w, float x, float y, float z);
|
||||
void lovrGraphicsRotate(float angle, float ax, float ay, float az);
|
||||
void lovrGraphicsScale(float x, float y, float z);
|
||||
void lovrGraphicsTransform(float tx, float ty, float tz, float sx, float sy, float sz, float angle, float ax, float ay, float az);
|
||||
void lovrGraphicsMatrixTransform(mat4 transform);
|
||||
|
|
|
@ -426,13 +426,7 @@ int l_lovrGraphicsRotate(lua_State* L) {
|
|||
float axisX = luaL_checknumber(L, 2);
|
||||
float axisY = luaL_checknumber(L, 3);
|
||||
float axisZ = luaL_checknumber(L, 4);
|
||||
float cos2 = cos(angle / 2);
|
||||
float sin2 = sin(angle / 2);
|
||||
float w = cos2;
|
||||
float x = sin2 * axisX;
|
||||
float y = sin2 * axisY;
|
||||
float z = sin2 * axisZ;
|
||||
lovrGraphicsRotate(w, x, y, z);
|
||||
lovrGraphicsRotate(angle, axisX, axisY, axisZ);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
24
src/matrix.c
24
src/matrix.c
|
@ -80,7 +80,25 @@ mat4 mat4_setTranslation(mat4 matrix, float x, float y, float z) {
|
|||
return matrix;
|
||||
}
|
||||
|
||||
mat4 mat4_setRotation(mat4 matrix, float w, float x, float y, float z) {
|
||||
mat4 mat4_setRotation(mat4 matrix, float angle, float ax, float ay, float az) {
|
||||
|
||||
// Normalize rotation vector
|
||||
float len = sqrtf(ax * ax + ay * ay + az * az);
|
||||
if (len != 1 && len != 0) {
|
||||
len = 1 / len;
|
||||
ax *= len;
|
||||
ay *= len;
|
||||
az *= len;
|
||||
}
|
||||
|
||||
// Convert angle-axis to quaternion
|
||||
float cos2 = cos(angle / 2.f);
|
||||
float sin2 = sin(angle / 2.f);
|
||||
float w = cos2;
|
||||
float x = sin2 * ax;
|
||||
float y = sin2 * ay;
|
||||
float z = sin2 * az;
|
||||
|
||||
mat4_setIdentity(matrix);
|
||||
matrix[0] = 1 - 2 * y * y - 2 * z * z;
|
||||
matrix[1] = 2 * x * y + 2 * w * z;
|
||||
|
@ -141,9 +159,9 @@ mat4 mat4_translate(mat4 matrix, float x, float y, float z) {
|
|||
return mat4_multiply(matrix, translation);
|
||||
}
|
||||
|
||||
mat4 mat4_rotate(mat4 matrix, float w, float x, float y, float z) {
|
||||
mat4 mat4_rotate(mat4 matrix, float angle, float ax, float ay, float az) {
|
||||
float rotation[16];
|
||||
mat4_setRotation(rotation, w, x, y, z);
|
||||
mat4_setRotation(rotation, angle, ax, ay, az);
|
||||
return mat4_multiply(matrix, rotation);
|
||||
}
|
||||
|
||||
|
|
|
@ -13,12 +13,12 @@ mat4 mat4_fromMat34(mat4 matrix, float (*source)[4]);
|
|||
mat4 mat4_fromMat44(mat4 matrix, float (*source)[4]);
|
||||
mat4 mat4_setIdentity(mat4 matrix);
|
||||
mat4 mat4_setTranslation(mat4 matrix, float x, float y, float z);
|
||||
mat4 mat4_setRotation(mat4 matrix, float w, float x, float y, float z);
|
||||
mat4 mat4_setRotation(mat4 matrix, float angle, float ax, float ay, float az);
|
||||
mat4 mat4_setScale(mat4 matrix, float x, float y, float z);
|
||||
mat4 mat4_setProjection(mat4 matrix, float near, float far, float fov, float aspect);
|
||||
void mat4_getRotation(mat4 matrix, float* w, float* x, float* y, float* z);
|
||||
mat4 mat4_translate(mat4 matrix, float x, float y, float z);
|
||||
mat4 mat4_rotate(mat4 matrix, float w, float x, float y, float z);
|
||||
mat4 mat4_rotate(mat4 matrix, float angle, float ax, float ay, float az);
|
||||
mat4 mat4_scale(mat4 matrix, float x, float y, float z);
|
||||
mat4 mat4_multiply(mat4 a, mat4 b);
|
||||
void mat4_multiplyVector(mat4 m, float* v);
|
||||
|
|
Loading…
Reference in New Issue