Be more intentional about doubles vs. floats;

This commit is contained in:
bjorn 2019-01-24 16:38:50 -08:00
parent bec98004ff
commit bda248b86e
5 changed files with 95 additions and 96 deletions

View File

@ -1165,7 +1165,7 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
for (int i = 0; i < n; i++) {
if (hasCenterPoint) {
memcpy(vertices, ((float[]) { 0, 0, 0, 0, 0, 1, .5, .5 }), 8 * sizeof(float));
memcpy(vertices, ((float[]) { 0.f, 0.f, 0.f, 0.f, 0.f, 1.f, .5f, .5f }), 8 * sizeof(float));
vertices += 8;
}
@ -1173,9 +1173,9 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
float angleShift = (r2 - r1) / (float) segments;
for (int i = 0; i <= segments; i++) {
float x = cos(theta) * .5;
float y = sin(theta) * .5;
memcpy(vertices, ((float[]) { x, y, 0, 0, 0, 1, x + .5, 1 - (y + .5) }), 8 * sizeof(float));
float x = cosf(theta) * .5f;
float y = sinf(theta) * .5f;
memcpy(vertices, ((float[]) { x, y, 0.f, 0.f, 0.f, 1.f, x + .5f, 1.f - (y + .5f) }), 8 * sizeof(float));
vertices += 8;
theta += angleShift;
}
@ -1201,11 +1201,11 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
// Ring
for (int j = 0; j <= segments; j++) {
float theta = j * (2 * M_PI) / segments;
float X = cos(theta);
float Y = sin(theta);
float X = cosf(theta);
float Y = sinf(theta);
memcpy(vertices, (float[16]) {
r1 * X, r1 * Y, -.5, X, Y, 0, 0, 0,
r2 * X, r2 * Y, .5, X, Y, 0, 0, 0
r1 * X, r1 * Y, -.5f, X, Y, 0.f, 0.f, 0.f,
r2 * X, r2 * Y, .5f, X, Y, 0.f, 0.f, 0.f
}, 16 * sizeof(float));
vertices += 16;
}
@ -1213,11 +1213,11 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
// Top
int top = (segments + 1) * 2 + I;
if (capped && r1 != 0) {
memcpy(vertices, (float[8]) { 0, 0, -.5, 0.f, 0.f, -1.f, 0, 0 }, 8 * sizeof(float));
memcpy(vertices, (float[8]) { 0.f, 0.f, -.5f, 0.f, 0.f, -1.f, 0.f, 0.f }, 8 * sizeof(float));
vertices += 8;
for (int j = 0; j <= segments; j++) {
int k = j * 2 * 8;
memcpy(vertices, (float[8]) { v[k + 0], v[k + 1], v[k + 2], 0.f, 0.f, -1.f, 0, 0 }, 8 * sizeof(float));
memcpy(vertices, (float[8]) { v[k + 0], v[k + 1], v[k + 2], 0.f, 0.f, -1.f, 0.f, 0.f }, 8 * sizeof(float));
vertices += 8;
}
}
@ -1225,11 +1225,11 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
// Bottom
int bot = (segments + 1) * 2 + (1 + segments + 1) * (capped && r1 != 0) + I;
if (capped && r2 != 0) {
memcpy(vertices, (float[8]) { 0, 0, .5, 0.f, 0.f, 1.f, 0, 0 }, 8 * sizeof(float));
memcpy(vertices, (float[8]) { 0.f, 0.f, .5f, 0.f, 0.f, 1.f, 0.f, 0.f }, 8 * sizeof(float));
vertices += 8;
for (int j = 0; j <= segments; j++) {
int k = j * 2 * 8 + 8;
memcpy(vertices, (float[8]) { v[k + 0], v[k + 1], v[k + 2], 0.f, 0.f, 1.f, 0, 0 }, 8 * sizeof(float));
memcpy(vertices, (float[8]) { v[k + 0], v[k + 1], v[k + 2], 0.f, 0.f, 1.f, 0.f, 0.f }, 8 * sizeof(float));
vertices += 8;
}
}
@ -1261,14 +1261,14 @@ static void writeGeometry(Batch* batch, float* vertices, uint16_t* indices, uint
for (int i = 0; i < n; i++) {
for (int j = 0; j <= segments; j++) {
float v = j / (float) segments;
float sinV = sin(v * M_PI);
float cosV = cos(v * M_PI);
float sinV = sinf(v * (float) M_PI);
float cosV = cosf(v * (float) M_PI);
for (int k = 0; k <= segments; k++) {
float u = k / (float) segments;
float x = sin(u * 2 * M_PI) * sinV;
float x = sinf(u * 2.f * (float) M_PI) * sinV;
float y = cosV;
float z = -cos(u * 2 * M_PI) * sinV;
memcpy(vertices, ((float[8]) { x, y, z, x, y, z, u, 1 - v }), 8 * sizeof(float));
float z = -cosf(u * 2.f * (float) M_PI) * sinV;
memcpy(vertices, ((float[8]) { x, y, z, x, y, z, u, 1.f - v }), 8 * sizeof(float));
vertices += 8;
}
}

View File

@ -83,22 +83,22 @@ void lovrMaterialSetTexture(Material* material, MaterialTexture textureType, Tex
void lovrMaterialGetTransform(Material* material, float* ox, float* oy, float* sx, float* sy, float* angle) {
*ox = material->transform[6];
*oy = material->transform[7];
*sx = sqrt(material->transform[0] * material->transform[0] + material->transform[1] * material->transform[1]);
*sy = sqrt(material->transform[3] * material->transform[3] + material->transform[4] * material->transform[4]);
*angle = atan2(-material->transform[3], material->transform[0]);
*sx = sqrtf(material->transform[0] * material->transform[0] + material->transform[1] * material->transform[1]);
*sy = sqrtf(material->transform[3] * material->transform[3] + material->transform[4] * material->transform[4]);
*angle = atan2f(-material->transform[3], material->transform[0]);
}
void lovrMaterialSetTransform(Material* material, float ox, float oy, float sx, float sy, float angle) {
lovrGraphicsFlushMaterial(material);
float c = cos(angle);
float s = sin(angle);
float c = cosf(angle);
float s = sinf(angle);
material->transform[0] = c * sx;
material->transform[1] = s * sx;
material->transform[2] = 0;
material->transform[2] = 0.f;
material->transform[3] = -s * sy;
material->transform[4] = c * sy;
material->transform[5] = 0;
material->transform[5] = 0.f;
material->transform[6] = ox;
material->transform[7] = oy;
material->transform[8] = 1;
material->transform[8] = 1.f;
}

View File

@ -46,14 +46,14 @@ vec3 vec3_normalize(vec3 v) {
}
float vec3_length(vec3 v) {
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
return sqrtf(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
float vec3_distance(vec3 v, vec3 u) {
float dx = v[0] - u[0];
float dy = v[1] - u[1];
float dz = v[2] - u[2];
return sqrt(dx * dx + dy * dy + dz * dz);
return sqrtf(dx * dx + dy * dy + dz * dz);
}
float vec3_dot(vec3 v, vec3 u) {
@ -90,13 +90,15 @@ quat quat_set(quat q, float x, float y, float z, float w) {
}
quat quat_fromAngleAxis(quat q, float angle, float ax, float ay, float az) {
float length = sqrt(ax * ax + ay * ay + az * az);
length = length == 0. ? 1. : length;
float s = sin(angle * .5f);
float c = cos(angle * .5f);
q[0] = s * ax / length;
q[1] = s * ay / length;
q[2] = s * az / length;
float length = sqrtf(ax * ax + ay * ay + az * az);
float s = sinf(angle * .5f);
float c = cosf(angle * .5f);
if (length > 0.f) {
s /= length;
}
q[0] = s * ax;
q[1] = s * ay;
q[2] = s * az;
q[3] = c;
return q;
}
@ -105,13 +107,13 @@ quat quat_fromAngleAxis(quat q, float angle, float ax, float ay, float az) {
#define MAX(a, b) (a > b ? a : b)
#endif
quat quat_fromMat4(quat q, mat4 m) {
float x = sqrt(MAX(0, 1 + m[0] - m[5] - m[10])) / 2;
float y = sqrt(MAX(0, 1 - m[0] + m[5] - m[10])) / 2;
float z = sqrt(MAX(0, 1 - m[0] - m[5] + m[10])) / 2;
float w = sqrt(MAX(0, 1 + m[0] + m[5] + m[10])) / 2;
x = (m[9] - m[6]) > 0 ? -x : x;
y = (m[2] - m[8]) > 0 ? -y : y;
z = (m[4] - m[1]) > 0 ? -z : z;
float x = sqrtf(MAX(0.f, 1.f + m[0] - m[5] - m[10])) / 2.f;
float y = sqrtf(MAX(0.f, 1.f - m[0] + m[5] - m[10])) / 2.f;
float z = sqrtf(MAX(0.f, 1.f - m[0] - m[5] + m[10])) / 2.f;
float w = sqrtf(MAX(0.f, 1.f + m[0] + m[5] + m[10])) / 2.f;
x = (m[9] - m[6]) > 0.f ? -x : x;
y = (m[2] - m[8]) > 0.f ? -y : y;
z = (m[4] - m[1]) > 0.f ? -z : z;
q[0] = x;
q[1] = y;
q[2] = z;
@ -129,50 +131,47 @@ quat quat_mul(quat q, quat r) {
}
quat quat_normalize(quat q) {
float len = quat_length(q);
if (len == 0) {
return q;
float length = quat_length(q);
if (length > 0.f) {
q[0] /= length;
q[1] /= length;
q[2] /= length;
q[3] /= length;
}
len = 1 / len;
q[0] *= len;
q[1] *= len;
q[2] *= len;
q[3] *= len;
return q;
}
float quat_length(quat q) {
return sqrt(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
return sqrtf(q[0] * q[0] + q[1] * q[1] + q[2] * q[2] + q[3] * q[3]);
}
quat quat_slerp(quat q, quat r, float t) {
float dot = q[0] * r[0] + q[1] * r[1] + q[2] * r[2] + q[3] * r[3];
if (fabs(dot) >= 1.f) {
if (fabsf(dot) >= 1.f) {
return q;
}
if (dot < 0) {
q[0] *= -1;
q[1] *= -1;
q[2] *= -1;
q[3] *= -1;
dot *= -1;
if (dot < 0.f) {
q[0] *= -1.f;
q[1] *= -1.f;
q[2] *= -1.f;
q[3] *= -1.f;
dot *= -1.f;
}
float halfTheta = acos(dot);
float sinHalfTheta = sqrt(1.f - dot * dot);
float halfTheta = acosf(dot);
float sinHalfTheta = sqrtf(1.f - dot * dot);
if (fabs(sinHalfTheta) < .001) {
q[0] = q[0] * .5 + r[0] * .5;
q[1] = q[1] * .5 + r[1] * .5;
q[2] = q[2] * .5 + r[2] * .5;
q[3] = q[3] * .5 + r[3] * .5;
if (fabsf(sinHalfTheta) < .001f) {
q[0] = q[0] * .5f + r[0] * .5f;
q[1] = q[1] * .5f + r[1] * .5f;
q[2] = q[2] * .5f + r[2] * .5f;
q[3] = q[3] * .5f + r[3] * .5f;
return q;
}
float a = sin((1 - t) * halfTheta) / sinHalfTheta;
float b = sin(t * halfTheta) / sinHalfTheta;
float a = sinf((1.f - t) * halfTheta) / sinHalfTheta;
float b = sinf(t * halfTheta) / sinHalfTheta;
q[0] = q[0] * a + r[0] * b;
q[1] = q[1] * a + r[1] * b;
@ -190,21 +189,21 @@ void quat_rotate(quat q, vec3 v) {
vec3_cross(vec3_init(c, u), v);
float uu = vec3_dot(u, u);
float uv = vec3_dot(u, v);
vec3_scale(u, 2 * uv);
vec3_scale(u, 2.f * uv);
vec3_scale(v, s * s - uu);
vec3_scale(c, 2 * s);
vec3_scale(c, 2.f * s);
vec3_add(v, vec3_add(u, c));
}
void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) {
if (q[3] > 1 || q[3] < -1) {
if (q[3] > 1.f || q[3] < -1.f) {
quat_normalize(q);
}
float qw = q[3];
float s = sqrt(1 - qw * qw);
s = s < .0001 ? 1 : 1 / s;
*angle = 2 * acos(qw);
float s = sqrtf(1.f - qw * qw);
s = s < .0001f ? 1.f : 1.f / s;
*angle = 2.f * acosf(qw);
*x = q[0] * s;
*y = q[1] * s;
*z = q[2] * s;
@ -212,22 +211,22 @@ void quat_getAngleAxis(quat q, float* angle, float* x, float* y, float* z) {
quat quat_between(quat q, vec3 u, vec3 v) {
float dot = vec3_dot(u, v);
if (dot > .99999) {
if (dot > .99999f) {
q[0] = q[1] = q[2] = 0.f;
q[3] = 1.f;
return q;
} else if (dot < -.99999) {
} else if (dot < -.99999f) {
float axis[3];
vec3_cross(vec3_set(axis, 1, 0, 0), u);
if (vec3_length(axis) < .00001) {
vec3_cross(vec3_set(axis, 0, 1, 0), u);
vec3_cross(vec3_set(axis, 1.f, 0.f, 0.f), u);
if (vec3_length(axis) < .00001f) {
vec3_cross(vec3_set(axis, 0.f, 1.f, 0.f), u);
}
vec3_normalize(axis);
quat_fromAngleAxis(q, M_PI, axis[0], axis[1], axis[2]);
return q;
}
vec3_cross(vec3_init(q, u), v);
q[3] = 1 + dot;
q[3] = 1.f + dot;
return quat_normalize(q);
}
@ -529,18 +528,18 @@ mat4 mat4_orthographic(mat4 m, float left, float right, float top, float bottom,
}
mat4 mat4_perspective(mat4 m, float clipNear, float clipFar, float fovy, float aspect) {
float range = tan(fovy * .5f) * clipNear;
float sx = (2.0f * clipNear) / (range * aspect + range * aspect);
float range = tanf(fovy * .5f) * clipNear;
float sx = (2.f * clipNear) / (range * aspect + range * aspect);
float sy = clipNear / range;
float sz = -(clipFar + clipNear) / (clipFar - clipNear);
float pz = (-2.0f * clipFar * clipNear) / (clipFar - clipNear);
float pz = (-2.f * clipFar * clipNear) / (clipFar - clipNear);
memset(m, 0, 16 * sizeof(float));
m[0] = sx;
m[5] = sy;
m[10] = sz;
m[11] = -1.0f;
m[11] = -1.f;
m[14] = pz;
m[15] = 0.0f;
m[15] = 0.f;
return m;
}
@ -556,7 +555,7 @@ mat4 mat4_lookAt(mat4 m, vec3 from, vec3 to, vec3 up) {
z1 = from[1] - to[1];
z2 = from[2] - to[2];
len = 1 / sqrt(z0 * z0 + z1 * z1 + z2 * z2);
len = 1.f / sqrtf(z0 * z0 + z1 * z1 + z2 * z2);
z0 *= len;
z1 *= len;
z2 *= len;
@ -564,7 +563,7 @@ mat4 mat4_lookAt(mat4 m, vec3 from, vec3 to, vec3 up) {
x0 = up[1] * z2 - up[2] * z1;
x1 = up[2] * z0 - up[0] * z2;
x2 = up[0] * z1 - up[1] * z0;
len = sqrt(x0 * x0 + x1 * x1 + x2 * x2);
len = sqrtf(x0 * x0 + x1 * x1 + x2 * x2);
if (!len) {
x0 = 0;
x1 = 0;
@ -580,7 +579,7 @@ mat4 mat4_lookAt(mat4 m, vec3 from, vec3 to, vec3 up) {
y1 = z2 * x0 - z0 * x2;
y2 = z0 * x1 - z1 * x0;
len = sqrt(y0 * y0 + y1 * y1 + y2 * y2);
len = sqrtf(y0 * y0 + y1 * y1 + y2 * y2);
if (!len) {
y0 = 0;
y1 = 0;

View File

@ -33,11 +33,11 @@ RandomGenerator* lovrMathGetRandomGenerator() {
}
void lovrMathOrientationToDirection(float angle, float ax, float ay, float az, vec3 v) {
float sinTheta = sin(angle);
float cosTheta = cos(angle);
v[0] = sinTheta * -ay + (1 - cosTheta) * -az * ax;
v[1] = sinTheta * ax + (1 - cosTheta) * -az * ay;
v[2] = -cosTheta + (1 - cosTheta) * -az * az;
float sinTheta = sinf(angle);
float cosTheta = cosf(angle);
v[0] = sinTheta * -ay + (1.f - cosTheta) * -az * ax;
v[1] = sinTheta * ax + (1.f - cosTheta) * -az * ay;
v[2] = -cosTheta + (1.f - cosTheta) * -az * az;
}
float lovrMathGammaToLinear(float x) {

View File

@ -137,11 +137,11 @@ int lovrWorldCollide(World* world, Shape* a, Shape* b, float friction, float res
return false;
}
if (friction < 0) {
friction = sqrt(colliderA->friction * colliderB->friction);
if (friction < 0.f) {
friction = sqrtf(colliderA->friction * colliderB->friction);
}
if (restitution < 0) {
if (restitution < 0.f) {
restitution = MAX(colliderA->restitution, colliderB->restitution);
}
@ -212,7 +212,7 @@ void lovrWorldRaycast(World* world, float x1, float y1, float z1, float x2, floa
float dx = x2 - x1;
float dy = y2 - y1;
float dz = z2 - z1;
float length = sqrt(dx * dx + dy * dy + dz * dz);
float length = sqrtf(dx * dx + dy * dy + dz * dz);
dGeomID ray = dCreateRay(world->space, length);
dGeomRaySet(ray, x1, y1, z1, dx, dy, dz);
dSpaceCollide2(ray, (dGeomID) world->space, &data, raycastCallback);