mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-12-22 01:26:17 +01:00
Add S3L_F shorthand
This commit is contained in:
parent
5bf6c79140
commit
f474f3530e
9 changed files with 229 additions and 228 deletions
|
@ -115,7 +115,7 @@ The basic philosophy is:
|
|||
also provided.
|
||||
- Fixed point arithmetics is used as a principle, but there is no abstraction above it, everything is simply
|
||||
an integer (`S3L_Unit` type). The space is considered to be a uniform dense grid of discrete points, and what would normally be
|
||||
a 1.0 float value is an int value equal to `S3L_FRACTIONS_PER_UNIT` units. Numbers are normalized by this
|
||||
a 1.0 float value is an int value equal to `S3L_FRACTIONS_PER_UNIT` units (aka `S3L_F`). Numbers are normalized by this
|
||||
constant, so e.g. the sin function returns a value from `-S3L_FRACTIONS_PER_UNIT` to `S3L_FRACTIONS_PER_UNIT`. You have to
|
||||
pass numbers of this format to the library functions, but of course you may chooe to use floats in other places of your program.
|
||||
|
||||
|
|
|
@ -62,7 +62,7 @@ void clearScreenBlue()
|
|||
|
||||
for (uint16_t y = 0; y < S3L_RESOLUTION_Y; ++y)
|
||||
{
|
||||
S3L_Unit t = S3L_min(S3L_FRACTIONS_PER_UNIT,((y * S3L_FRACTIONS_PER_UNIT) / S3L_RESOLUTION_Y) * 4);
|
||||
S3L_Unit t = S3L_min(S3L_F,((y * S3L_F) / S3L_RESOLUTION_Y) * 4);
|
||||
|
||||
uint32_t r = S3L_interpolateByUnit(200,242,t);
|
||||
uint32_t g = S3L_interpolateByUnit(102,255,t);
|
||||
|
@ -125,8 +125,8 @@ void draw()
|
|||
|
||||
static inline uint8_t collision(S3L_Vec4 worldPosition)
|
||||
{
|
||||
worldPosition.x /= S3L_FRACTIONS_PER_UNIT;
|
||||
worldPosition.z /= -S3L_FRACTIONS_PER_UNIT;
|
||||
worldPosition.x /= S3L_F;
|
||||
worldPosition.z /= -S3L_F;
|
||||
|
||||
uint16_t index = worldPosition.z * 8 + worldPosition.x;
|
||||
|
||||
|
@ -176,8 +176,8 @@ int main(void)
|
|||
|
||||
S3L_vec4Init(&carDirection);
|
||||
|
||||
scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT / 2;
|
||||
scene.camera.transform.rotation.x = -S3L_FRACTIONS_PER_UNIT / 16;
|
||||
scene.camera.transform.translation.y = S3L_F / 2;
|
||||
scene.camera.transform.rotation.x = -S3L_F / 16;
|
||||
|
||||
int16_t velocity = 0;
|
||||
|
||||
|
@ -241,7 +241,7 @@ int main(void)
|
|||
else
|
||||
models[1].transform.rotation.z = (models[1].transform.rotation.z * 3) / 4;
|
||||
|
||||
S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);
|
||||
S3L_rotationToDirections(models[1].transform.rotation,S3L_F,&carDirection,0,0);
|
||||
|
||||
S3L_Vec4 previousCarPos = models[1].transform.translation;
|
||||
|
||||
|
@ -254,8 +254,8 @@ int main(void)
|
|||
else
|
||||
friction = 1;
|
||||
|
||||
models[1].transform.translation.x += (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.z += (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.x += (carDirection.x * step) / S3L_F;
|
||||
models[1].transform.translation.z += (carDirection.z * step) / S3L_F;
|
||||
|
||||
uint8_t coll = collision(models[1].transform.translation);
|
||||
|
||||
|
@ -269,14 +269,14 @@ int main(void)
|
|||
else if (coll == 2)
|
||||
{
|
||||
// teleport the car
|
||||
models[1].transform.translation.x += 5 * S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.z += 2 * S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.x += 5 * S3L_F;
|
||||
models[1].transform.translation.z += 2 * S3L_F;
|
||||
}
|
||||
else
|
||||
{
|
||||
// teleport the car
|
||||
models[1].transform.translation.x -= 5 * S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.z -= 2 * S3L_FRACTIONS_PER_UNIT;
|
||||
models[1].transform.translation.x -= 5 * S3L_F;
|
||||
models[1].transform.translation.z -= 2 * S3L_F;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -286,13 +286,13 @@ int main(void)
|
|||
velocity = S3L_min(0,velocity + stepFriction * friction);
|
||||
|
||||
S3L_Unit cameraDistance =
|
||||
S3L_interpolate(S3L_FRACTIONS_PER_UNIT / 2,(3 * S3L_FRACTIONS_PER_UNIT) / 4,S3L_abs(velocity),MAX_VELOCITY);
|
||||
S3L_interpolate(S3L_F / 2,(3 * S3L_F) / 4,S3L_abs(velocity),MAX_VELOCITY);
|
||||
|
||||
scene.camera.transform.translation.x =
|
||||
scene.models[1].transform.translation.x - (carDirection.x * cameraDistance) / S3L_FRACTIONS_PER_UNIT;
|
||||
scene.models[1].transform.translation.x - (carDirection.x * cameraDistance) / S3L_F;
|
||||
|
||||
scene.camera.transform.translation.z =
|
||||
scene.models[1].transform.translation.z - (carDirection.z * cameraDistance) / S3L_FRACTIONS_PER_UNIT;
|
||||
scene.models[1].transform.translation.z - (carDirection.z * cameraDistance) / S3L_F;
|
||||
|
||||
scene.camera.transform.rotation.y = models[1].transform.rotation.y;
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
|
||||
#include "../small3dlib.h" // now include the library
|
||||
|
||||
#define U S3L_FRACTIONS_PER_UNIT // this is the library unit, like e.g. 1 meter
|
||||
#define U S3L_F // this is the library unit, like e.g. 1 meter
|
||||
|
||||
S3L_Unit triangleVertices[] = { // x, y, z
|
||||
U, 0, 0, // vertex 1
|
||||
|
@ -50,8 +50,8 @@ int main()
|
|||
S3L_sceneInit(&triangleModel,1,&scene);
|
||||
|
||||
// shift the camera a little bit so that we can see the triangle
|
||||
scene.camera.transform.translation.z = -2 * S3L_FRACTIONS_PER_UNIT;
|
||||
scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT / 2;
|
||||
scene.camera.transform.translation.z = -2 * S3L_F;
|
||||
scene.camera.transform.translation.y = S3L_F / 2;
|
||||
|
||||
S3L_newFrame(); // has to be called before each frame
|
||||
S3L_drawScene(scene); /* this starts the scene rendering, the library
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#define S3L_PERSPECTIVE_CORRECTION 0
|
||||
#endif
|
||||
|
||||
#define S3L_NEAR (S3L_FRACTIONS_PER_UNIT / 5)
|
||||
#define S3L_NEAR (S3L_F / 5)
|
||||
|
||||
#define S3L_Z_BUFFER 1
|
||||
|
||||
|
@ -82,9 +82,9 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
S3L_vec3Normalize(&normal);
|
||||
|
||||
S3L_Unit shading =
|
||||
(S3L_vec3Dot(normal,toLight) + S3L_FRACTIONS_PER_UNIT) / 2;
|
||||
(S3L_vec3Dot(normal,toLight) + S3L_F) / 2;
|
||||
|
||||
shading = S3L_interpolate(shading,0,p->depth,32 * S3L_FRACTIONS_PER_UNIT);
|
||||
shading = S3L_interpolate(shading,0,p->depth,32 * S3L_F);
|
||||
|
||||
int index = (p->y * S3L_RESOLUTION_X + p->x) * 3;
|
||||
|
||||
|
@ -105,9 +105,9 @@ int main()
|
|||
|
||||
S3L_sceneInit(&alligatorModel,1,&scene);
|
||||
|
||||
scene.camera.transform.translation.z = -8 * S3L_FRACTIONS_PER_UNIT;
|
||||
scene.camera.transform.translation.x = 9 * S3L_FRACTIONS_PER_UNIT;
|
||||
scene.camera.transform.translation.y = 6 * S3L_FRACTIONS_PER_UNIT;
|
||||
scene.camera.transform.translation.z = -8 * S3L_F;
|
||||
scene.camera.transform.translation.x = 9 * S3L_F;
|
||||
scene.camera.transform.translation.y = 6 * S3L_F;
|
||||
|
||||
S3L_lookAt(scene.models[0].transform.translation,&(scene.camera.transform));
|
||||
|
||||
|
|
|
@ -239,8 +239,8 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
specularIntensity = 0.7;
|
||||
specularPower = 10.0;
|
||||
|
||||
u = position.x / ((float) S3L_FRACTIONS_PER_UNIT * 2);
|
||||
v = position.z / ((float) S3L_FRACTIONS_PER_UNIT * 2);
|
||||
u = position.x / ((float) S3L_F * 2);
|
||||
v = position.z / ((float) S3L_F * 2);
|
||||
|
||||
uint8_t textureNormal[3];
|
||||
uint8_t textureNormal2[3];
|
||||
|
@ -248,7 +248,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
sampleTexture(sandNormalTexture,SANDNORMAL_TEXTURE_WIDTH,SANDNORMAL_TEXTURE_HEIGHT,u,v,textureNormal);
|
||||
sampleTexture(grassNormalTexture,GRASSNORMAL_TEXTURE_WIDTH,GRASSNORMAL_TEXTURE_HEIGHT,u / 2,v / 2,textureNormal2);
|
||||
|
||||
blend = S3L_clamp(position.y * 4 - S3L_FRACTIONS_PER_UNIT,0,S3L_FRACTIONS_PER_UNIT);
|
||||
blend = S3L_clamp(position.y * 4 - S3L_F,0,S3L_F);
|
||||
|
||||
textureNormal[0] = S3L_interpolateByUnit(textureNormal[0],textureNormal2[0],blend);
|
||||
textureNormal[1] = S3L_interpolateByUnit(textureNormal[1],textureNormal2[1],blend);
|
||||
|
@ -263,16 +263,16 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
specularIntensity = 0.2;
|
||||
specularPower = 20.0;
|
||||
|
||||
u = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT);
|
||||
v = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric) / ((float) S3L_FRACTIONS_PER_UNIT);
|
||||
u = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric) / ((float) S3L_F);
|
||||
v = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric) / ((float) S3L_F);
|
||||
}
|
||||
|
||||
S3L_vec3Normalize(&normal);
|
||||
S3L_reflect(toLightDirection,normal,&reflected);
|
||||
|
||||
float diffuse = 0.5 - (S3L_vec3Dot(toLightDirection,normal) / ((float) S3L_FRACTIONS_PER_UNIT)) * 0.5;
|
||||
float specular = 0.5 + (S3L_vec3Dot(reflected,toCameraDirection) / ((float) S3L_FRACTIONS_PER_UNIT)) * 0.5;
|
||||
float fog = (p->depth / ((float) S3L_FRACTIONS_PER_UNIT * 20));
|
||||
float diffuse = 0.5 - (S3L_vec3Dot(toLightDirection,normal) / ((float) S3L_F)) * 0.5;
|
||||
float specular = 0.5 + (S3L_vec3Dot(reflected,toCameraDirection) / ((float) S3L_F)) * 0.5;
|
||||
float fog = (p->depth / ((float) S3L_F * 20));
|
||||
|
||||
if (fog > 1.0)
|
||||
fog = 1.0;
|
||||
|
@ -285,7 +285,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
{
|
||||
S3L_Unit waterDepth = (p->previousZ - p->depth) / 2;
|
||||
|
||||
float transparency = waterDepth / ((float) (S3L_FRACTIONS_PER_UNIT / 3));
|
||||
float transparency = waterDepth / ((float) (S3L_F / 3));
|
||||
|
||||
transparency = transparency > 1.0 ? 1.0 : transparency;
|
||||
|
||||
|
@ -298,7 +298,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
previousColor[1] = frameBuffer[index + 1];
|
||||
previousColor[2] = frameBuffer[index + 2];
|
||||
|
||||
float fresnel = 0.5 + (S3L_vec3Dot(toCameraDirection,normal) / ((float) S3L_FRACTIONS_PER_UNIT)) * 0.5;
|
||||
float fresnel = 0.5 + (S3L_vec3Dot(toCameraDirection,normal) / ((float) S3L_F)) * 0.5;
|
||||
|
||||
color[0] = interpolate(150,0,fresnel);
|
||||
color[1] = interpolate(230,10,fresnel);
|
||||
|
@ -347,9 +347,9 @@ void createGeometry()
|
|||
for (int y = 0; y < GRID_H; ++y)
|
||||
for (int x = 0; x < GRID_W; ++x)
|
||||
{
|
||||
terrainVertices[i] = (x - GRID_W / 2) * S3L_FRACTIONS_PER_UNIT;
|
||||
terrainVertices[i + 1] = (heightMap[i / 3] - 1) * S3L_FRACTIONS_PER_UNIT / 4;
|
||||
terrainVertices[i + 2] = (y - GRID_H / 2) * S3L_FRACTIONS_PER_UNIT;
|
||||
terrainVertices[i] = (x - GRID_W / 2) * S3L_F;
|
||||
terrainVertices[i + 1] = (heightMap[i / 3] - 1) * S3L_F / 4;
|
||||
terrainVertices[i + 2] = (y - GRID_H / 2) * S3L_F;
|
||||
|
||||
waterVertices[i] = terrainVertices[i];
|
||||
waterVertices[i + 1] = 0;
|
||||
|
@ -385,7 +385,7 @@ void createGeometry()
|
|||
void animateWater()
|
||||
{
|
||||
for (int i = 1; i < GRID_W * GRID_H * 3; i += 3)
|
||||
waterVertices[i] = S3L_FRACTIONS_PER_UNIT / 4 + sin(frame * 0.2) * S3L_FRACTIONS_PER_UNIT / 4;
|
||||
waterVertices[i] = S3L_F / 4 + sin(frame * 0.2) * S3L_F / 4;
|
||||
|
||||
S3L_computeModelNormals(models[WATER_MODEL_INDEX],waterNormals,0);
|
||||
}
|
||||
|
@ -426,11 +426,11 @@ int main()
|
|||
models[1] = treeModel;
|
||||
models[2] = treeModel;
|
||||
|
||||
S3L_Unit scale = S3L_FRACTIONS_PER_UNIT / 4;
|
||||
S3L_Unit scale = S3L_F / 4;
|
||||
|
||||
S3L_transform3DSet(0,1.2 * S3L_FRACTIONS_PER_UNIT,-1.5 * S3L_FRACTIONS_PER_UNIT,0,0,0,scale,scale,scale,&(models[0].transform));
|
||||
S3L_transform3DSet(0.95 * S3L_FRACTIONS_PER_UNIT,1.3 * S3L_FRACTIONS_PER_UNIT,0,0,0,0,scale,scale * 1.3,scale,&(models[1].transform));
|
||||
S3L_transform3DSet(-2 * S3L_FRACTIONS_PER_UNIT,0.8 * S3L_FRACTIONS_PER_UNIT,1.5 * S3L_FRACTIONS_PER_UNIT,0,0,0,scale,scale,scale,&(models[2].transform));
|
||||
S3L_transform3DSet(0,1.2 * S3L_F,-1.5 * S3L_F,0,0,0,scale,scale,scale,&(models[0].transform));
|
||||
S3L_transform3DSet(0.95 * S3L_F,1.3 * S3L_F,0,0,0,0,scale,scale * 1.3,scale,&(models[1].transform));
|
||||
S3L_transform3DSet(-2 * S3L_F,0.8 * S3L_F,1.5 * S3L_F,0,0,0,scale,scale,scale,&(models[2].transform));
|
||||
|
||||
S3L_model3DInit(
|
||||
terrainVertices,
|
||||
|
@ -458,16 +458,16 @@ int main()
|
|||
S3L_transform3DInit(&transform0);
|
||||
S3L_transform3DInit(&transform1);
|
||||
|
||||
transform0.translation.x = -2 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform0.translation.y = 5 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform0.translation.z = -14 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform0.translation.x = -2 * S3L_F;
|
||||
transform0.translation.y = 5 * S3L_F;
|
||||
transform0.translation.z = -14 * S3L_F;
|
||||
|
||||
transform0.rotation.x = -S3L_FRACTIONS_PER_UNIT / 12;
|
||||
transform1.rotation.y = S3L_FRACTIONS_PER_UNIT / 8;
|
||||
transform0.rotation.x = -S3L_F / 12;
|
||||
transform1.rotation.y = S3L_F / 8;
|
||||
|
||||
transform1.translation.x = 5 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform1.translation.y = 6 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform1.translation.z = 3 * S3L_FRACTIONS_PER_UNIT;
|
||||
transform1.translation.x = 5 * S3L_F;
|
||||
transform1.translation.y = 6 * S3L_F;
|
||||
transform1.translation.z = 3 * S3L_F;
|
||||
|
||||
transform1.rotation.x = transform0.rotation.x;
|
||||
transform1.rotation.y = transform0.rotation.y;
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
#define S3L_PERSPECTIVE_CORRECTION 0
|
||||
#endif
|
||||
|
||||
#define S3L_NEAR (S3L_FRACTIONS_PER_UNIT / 5)
|
||||
#define S3L_NEAR (S3L_F / 5)
|
||||
|
||||
#define S3L_USE_WIDER_TYPES 0
|
||||
#define S3L_FLAT 0
|
||||
|
@ -137,7 +137,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
#else
|
||||
16
|
||||
#endif
|
||||
) / S3L_FRACTIONS_PER_UNIT;
|
||||
) / S3L_F;
|
||||
|
||||
r = S3L_clamp(((S3L_Unit) r) - fog,0,255);
|
||||
g = S3L_clamp(((S3L_Unit) g) - fog,0,255);
|
||||
|
@ -193,10 +193,10 @@ int main(void)
|
|||
{
|
||||
sdlInit();
|
||||
|
||||
teleportPoint.x = 6 * S3L_FRACTIONS_PER_UNIT;
|
||||
teleportPoint.y = -3 * S3L_FRACTIONS_PER_UNIT;
|
||||
teleportPoint.z = 3 * S3L_FRACTIONS_PER_UNIT / 2;
|
||||
teleportPoint.w = S3L_FRACTIONS_PER_UNIT;
|
||||
teleportPoint.x = 6 * S3L_F;
|
||||
teleportPoint.y = -3 * S3L_F;
|
||||
teleportPoint.z = 3 * S3L_F / 2;
|
||||
teleportPoint.w = S3L_F;
|
||||
|
||||
nextT = clock();
|
||||
|
||||
|
|
|
@ -93,7 +93,7 @@ void animate(double time)
|
|||
{
|
||||
time = (1.0 + sin(time * 8)) / 2;
|
||||
|
||||
S3L_Unit t = time * S3L_FRACTIONS_PER_UNIT;
|
||||
S3L_Unit t = time * S3L_F;
|
||||
|
||||
for (S3L_Index i = 0; i < CAT1_VERTEX_COUNT * 3; i += 3)
|
||||
{
|
||||
|
@ -244,14 +244,14 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
{
|
||||
int16_t l = S3L_interpolateBarycentric(l0,l1,l2,p->barycentric);
|
||||
|
||||
r = S3L_clamp((((int16_t) r) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
|
||||
g = S3L_clamp((((int16_t) g) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
|
||||
b = S3L_clamp((((int16_t) b) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
|
||||
r = S3L_clamp((((int16_t) r) * l) / S3L_F,0,255);
|
||||
g = S3L_clamp((((int16_t) g) * l) / S3L_F,0,255);
|
||||
b = S3L_clamp((((int16_t) b) * l) / S3L_F,0,255);
|
||||
}
|
||||
|
||||
if (fog)
|
||||
{
|
||||
int16_t f = ((p->depth - S3L_NEAR) * 255) / (S3L_FRACTIONS_PER_UNIT * 64);
|
||||
int16_t f = ((p->depth - S3L_NEAR) * 255) / (S3L_F * 64);
|
||||
|
||||
f *= 2;
|
||||
|
||||
|
@ -348,7 +348,7 @@ int main(void)
|
|||
cat1ModelInit();
|
||||
cat2ModelInit();
|
||||
|
||||
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
|
||||
scene.camera.transform.translation.z = -S3L_F * 8;
|
||||
|
||||
catModel = cat1Model;
|
||||
catModel.vertices = catVertices;
|
||||
|
@ -433,17 +433,17 @@ int main(void)
|
|||
{
|
||||
if (state[SDL_SCANCODE_LEFT])
|
||||
scene.camera.focalLength =
|
||||
S3L_min(S3L_FRACTIONS_PER_UNIT * 5,scene.camera.focalLength + fovStep);
|
||||
S3L_min(S3L_F * 5,scene.camera.focalLength + fovStep);
|
||||
else if (state[SDL_SCANCODE_RIGHT])
|
||||
scene.camera.focalLength =
|
||||
S3L_max(S3L_FRACTIONS_PER_UNIT / 2,scene.camera.focalLength - fovStep);
|
||||
S3L_max(S3L_F / 2,scene.camera.focalLength - fovStep);
|
||||
|
||||
if (state[SDL_SCANCODE_UP])
|
||||
scene.camera.transform.translation.z =
|
||||
S3L_min(S3L_FRACTIONS_PER_UNIT, scene.camera.transform.translation.z + moveStep);
|
||||
S3L_min(S3L_F, scene.camera.transform.translation.z + moveStep);
|
||||
else if (state[SDL_SCANCODE_DOWN])
|
||||
scene.camera.transform.translation.z =
|
||||
S3L_max(-S3L_FRACTIONS_PER_UNIT * 16, scene.camera.transform.translation.z - moveStep);
|
||||
S3L_max(-S3L_F * 16, scene.camera.transform.translation.z - moveStep);
|
||||
}
|
||||
|
||||
if (state[SDL_SCANCODE_KP_0])
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
|
||||
// we'll use a predefined geometry of a cube from the library:
|
||||
|
||||
S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(S3L_FRACTIONS_PER_UNIT) };
|
||||
S3L_Unit cubeVertices[] = { S3L_CUBE_VERTICES(S3L_F) };
|
||||
S3L_Index cubeTriangles[] = { S3L_CUBE_TRIANGLES };
|
||||
|
||||
S3L_Model3D cubeModel; // 3D model, has a geometry, position, rotation etc.
|
||||
|
@ -69,7 +69,7 @@ int main()
|
|||
|
||||
// shift the camera a little bit backwards so that it's not inside the cube:
|
||||
|
||||
scene.camera.transform.translation.z = -2 * S3L_FRACTIONS_PER_UNIT;
|
||||
scene.camera.transform.translation.z = -2 * S3L_F;
|
||||
|
||||
for (int i = 0; i < 200; ++i) // render 200 frames
|
||||
{
|
||||
|
|
319
small3dlib.h
319
small3dlib.h
|
@ -10,7 +10,7 @@
|
|||
license: CC0 1.0 (public domain)
|
||||
found at https://creativecommons.org/publicdomain/zero/1.0/
|
||||
+ additional waiver of all IP
|
||||
version: 0.902d
|
||||
version: 0.903d
|
||||
|
||||
Before including the library, define S3L_PIXEL_FUNCTION to the name of the
|
||||
function you'll be using to draw single pixels (this function will be called
|
||||
|
@ -68,7 +68,7 @@
|
|||
We use row vectors.
|
||||
|
||||
In 3D space, a left-handed coord. system is used. One spatial unit is split
|
||||
into S3L_FRACTIONS_PER_UNIT fractions (fixed point arithmetic).
|
||||
into S3L_FRACTIONS_PER_UNITs fractions (fixed point arithmetic).
|
||||
|
||||
y ^
|
||||
| _
|
||||
|
@ -178,7 +178,7 @@
|
|||
#define S3L_SIN_METHOD 0
|
||||
#endif
|
||||
|
||||
/** Units of measurement in 3D space. There is S3L_FRACTIONS_PER_UNIT in one
|
||||
/** Units of measurement in 3D space. There is S3L_F in one
|
||||
spatial unit. By dividing the unit into fractions we effectively achieve a
|
||||
fixed point arithmetic. The number of fractions is a constant that serves as
|
||||
1.0 in floating point arithmetic (normalization etc.). */
|
||||
|
@ -195,6 +195,7 @@ typedef
|
|||
BE REDEFINED, so rather don't do it (otherwise things may overflow etc.). */
|
||||
|
||||
#define S3L_FRACTIONS_PER_UNIT 512
|
||||
#define S3L_F S3L_FRACTIONS_PER_UNIT
|
||||
|
||||
typedef
|
||||
#if S3L_USE_WIDER_TYPES
|
||||
|
@ -350,7 +351,7 @@ typedef
|
|||
/** Distance of the near clipping plane. Points in front or EXATLY ON this
|
||||
plane are considered outside the frustum. This must be >= 0. */
|
||||
|
||||
#define S3L_NEAR (S3L_FRACTIONS_PER_UNIT / 4)
|
||||
#define S3L_NEAR (S3L_F / 4)
|
||||
#endif
|
||||
|
||||
#if S3L_NEAR <= 0
|
||||
|
@ -807,7 +808,7 @@ static inline void S3L_rotate2DPoint(S3L_Unit *x, S3L_Unit *y, S3L_Unit angle);
|
|||
#define S3L_HALF_RESOLUTION_Y (S3L_RESOLUTION_Y >> 1)
|
||||
|
||||
#define S3L_PROJECTION_PLANE_HEIGHT\
|
||||
((S3L_RESOLUTION_Y * S3L_FRACTIONS_PER_UNIT * 2) / S3L_RESOLUTION_X)
|
||||
((S3L_RESOLUTION_Y * S3L_F * 2) / S3L_RESOLUTION_X)
|
||||
|
||||
#if S3L_Z_BUFFER == 1
|
||||
#define S3L_MAX_DEPTH 2147483647
|
||||
|
@ -909,81 +910,81 @@ static inline int8_t S3L_stencilTest(
|
|||
static const S3L_Unit S3L_sinTable[S3L_SIN_TABLE_LENGTH] =
|
||||
{
|
||||
/* 511 was chosen here as a highest number that doesn't overflow during
|
||||
compilation for S3L_FRACTIONS_PER_UNIT == 1024 */
|
||||
compilation for S3L_F == 1024 */
|
||||
|
||||
(0*S3L_FRACTIONS_PER_UNIT)/511, (6*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(12*S3L_FRACTIONS_PER_UNIT)/511, (18*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(25*S3L_FRACTIONS_PER_UNIT)/511, (31*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(37*S3L_FRACTIONS_PER_UNIT)/511, (43*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(50*S3L_FRACTIONS_PER_UNIT)/511, (56*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(62*S3L_FRACTIONS_PER_UNIT)/511, (68*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(74*S3L_FRACTIONS_PER_UNIT)/511, (81*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(87*S3L_FRACTIONS_PER_UNIT)/511, (93*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(99*S3L_FRACTIONS_PER_UNIT)/511, (105*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(111*S3L_FRACTIONS_PER_UNIT)/511, (118*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(124*S3L_FRACTIONS_PER_UNIT)/511, (130*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(136*S3L_FRACTIONS_PER_UNIT)/511, (142*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(148*S3L_FRACTIONS_PER_UNIT)/511, (154*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(160*S3L_FRACTIONS_PER_UNIT)/511, (166*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(172*S3L_FRACTIONS_PER_UNIT)/511, (178*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(183*S3L_FRACTIONS_PER_UNIT)/511, (189*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(195*S3L_FRACTIONS_PER_UNIT)/511, (201*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(207*S3L_FRACTIONS_PER_UNIT)/511, (212*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(218*S3L_FRACTIONS_PER_UNIT)/511, (224*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(229*S3L_FRACTIONS_PER_UNIT)/511, (235*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(240*S3L_FRACTIONS_PER_UNIT)/511, (246*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(251*S3L_FRACTIONS_PER_UNIT)/511, (257*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(262*S3L_FRACTIONS_PER_UNIT)/511, (268*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(273*S3L_FRACTIONS_PER_UNIT)/511, (278*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(283*S3L_FRACTIONS_PER_UNIT)/511, (289*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(294*S3L_FRACTIONS_PER_UNIT)/511, (299*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(304*S3L_FRACTIONS_PER_UNIT)/511, (309*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(314*S3L_FRACTIONS_PER_UNIT)/511, (319*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(324*S3L_FRACTIONS_PER_UNIT)/511, (328*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(333*S3L_FRACTIONS_PER_UNIT)/511, (338*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(343*S3L_FRACTIONS_PER_UNIT)/511, (347*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(352*S3L_FRACTIONS_PER_UNIT)/511, (356*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(361*S3L_FRACTIONS_PER_UNIT)/511, (365*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(370*S3L_FRACTIONS_PER_UNIT)/511, (374*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(378*S3L_FRACTIONS_PER_UNIT)/511, (382*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(386*S3L_FRACTIONS_PER_UNIT)/511, (391*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(395*S3L_FRACTIONS_PER_UNIT)/511, (398*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(402*S3L_FRACTIONS_PER_UNIT)/511, (406*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(410*S3L_FRACTIONS_PER_UNIT)/511, (414*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(417*S3L_FRACTIONS_PER_UNIT)/511, (421*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(424*S3L_FRACTIONS_PER_UNIT)/511, (428*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(431*S3L_FRACTIONS_PER_UNIT)/511, (435*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(438*S3L_FRACTIONS_PER_UNIT)/511, (441*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(444*S3L_FRACTIONS_PER_UNIT)/511, (447*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(450*S3L_FRACTIONS_PER_UNIT)/511, (453*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(456*S3L_FRACTIONS_PER_UNIT)/511, (459*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(461*S3L_FRACTIONS_PER_UNIT)/511, (464*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(467*S3L_FRACTIONS_PER_UNIT)/511, (469*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(472*S3L_FRACTIONS_PER_UNIT)/511, (474*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(476*S3L_FRACTIONS_PER_UNIT)/511, (478*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(481*S3L_FRACTIONS_PER_UNIT)/511, (483*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(485*S3L_FRACTIONS_PER_UNIT)/511, (487*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(488*S3L_FRACTIONS_PER_UNIT)/511, (490*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(492*S3L_FRACTIONS_PER_UNIT)/511, (494*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(495*S3L_FRACTIONS_PER_UNIT)/511, (497*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(498*S3L_FRACTIONS_PER_UNIT)/511, (499*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(501*S3L_FRACTIONS_PER_UNIT)/511, (502*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(503*S3L_FRACTIONS_PER_UNIT)/511, (504*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(505*S3L_FRACTIONS_PER_UNIT)/511, (506*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(507*S3L_FRACTIONS_PER_UNIT)/511, (507*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(508*S3L_FRACTIONS_PER_UNIT)/511, (509*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(509*S3L_FRACTIONS_PER_UNIT)/511, (510*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(510*S3L_FRACTIONS_PER_UNIT)/511, (510*S3L_FRACTIONS_PER_UNIT)/511,
|
||||
(510*S3L_FRACTIONS_PER_UNIT)/511, (510*S3L_FRACTIONS_PER_UNIT)/511
|
||||
(0*S3L_F)/511, (6*S3L_F)/511,
|
||||
(12*S3L_F)/511, (18*S3L_F)/511,
|
||||
(25*S3L_F)/511, (31*S3L_F)/511,
|
||||
(37*S3L_F)/511, (43*S3L_F)/511,
|
||||
(50*S3L_F)/511, (56*S3L_F)/511,
|
||||
(62*S3L_F)/511, (68*S3L_F)/511,
|
||||
(74*S3L_F)/511, (81*S3L_F)/511,
|
||||
(87*S3L_F)/511, (93*S3L_F)/511,
|
||||
(99*S3L_F)/511, (105*S3L_F)/511,
|
||||
(111*S3L_F)/511, (118*S3L_F)/511,
|
||||
(124*S3L_F)/511, (130*S3L_F)/511,
|
||||
(136*S3L_F)/511, (142*S3L_F)/511,
|
||||
(148*S3L_F)/511, (154*S3L_F)/511,
|
||||
(160*S3L_F)/511, (166*S3L_F)/511,
|
||||
(172*S3L_F)/511, (178*S3L_F)/511,
|
||||
(183*S3L_F)/511, (189*S3L_F)/511,
|
||||
(195*S3L_F)/511, (201*S3L_F)/511,
|
||||
(207*S3L_F)/511, (212*S3L_F)/511,
|
||||
(218*S3L_F)/511, (224*S3L_F)/511,
|
||||
(229*S3L_F)/511, (235*S3L_F)/511,
|
||||
(240*S3L_F)/511, (246*S3L_F)/511,
|
||||
(251*S3L_F)/511, (257*S3L_F)/511,
|
||||
(262*S3L_F)/511, (268*S3L_F)/511,
|
||||
(273*S3L_F)/511, (278*S3L_F)/511,
|
||||
(283*S3L_F)/511, (289*S3L_F)/511,
|
||||
(294*S3L_F)/511, (299*S3L_F)/511,
|
||||
(304*S3L_F)/511, (309*S3L_F)/511,
|
||||
(314*S3L_F)/511, (319*S3L_F)/511,
|
||||
(324*S3L_F)/511, (328*S3L_F)/511,
|
||||
(333*S3L_F)/511, (338*S3L_F)/511,
|
||||
(343*S3L_F)/511, (347*S3L_F)/511,
|
||||
(352*S3L_F)/511, (356*S3L_F)/511,
|
||||
(361*S3L_F)/511, (365*S3L_F)/511,
|
||||
(370*S3L_F)/511, (374*S3L_F)/511,
|
||||
(378*S3L_F)/511, (382*S3L_F)/511,
|
||||
(386*S3L_F)/511, (391*S3L_F)/511,
|
||||
(395*S3L_F)/511, (398*S3L_F)/511,
|
||||
(402*S3L_F)/511, (406*S3L_F)/511,
|
||||
(410*S3L_F)/511, (414*S3L_F)/511,
|
||||
(417*S3L_F)/511, (421*S3L_F)/511,
|
||||
(424*S3L_F)/511, (428*S3L_F)/511,
|
||||
(431*S3L_F)/511, (435*S3L_F)/511,
|
||||
(438*S3L_F)/511, (441*S3L_F)/511,
|
||||
(444*S3L_F)/511, (447*S3L_F)/511,
|
||||
(450*S3L_F)/511, (453*S3L_F)/511,
|
||||
(456*S3L_F)/511, (459*S3L_F)/511,
|
||||
(461*S3L_F)/511, (464*S3L_F)/511,
|
||||
(467*S3L_F)/511, (469*S3L_F)/511,
|
||||
(472*S3L_F)/511, (474*S3L_F)/511,
|
||||
(476*S3L_F)/511, (478*S3L_F)/511,
|
||||
(481*S3L_F)/511, (483*S3L_F)/511,
|
||||
(485*S3L_F)/511, (487*S3L_F)/511,
|
||||
(488*S3L_F)/511, (490*S3L_F)/511,
|
||||
(492*S3L_F)/511, (494*S3L_F)/511,
|
||||
(495*S3L_F)/511, (497*S3L_F)/511,
|
||||
(498*S3L_F)/511, (499*S3L_F)/511,
|
||||
(501*S3L_F)/511, (502*S3L_F)/511,
|
||||
(503*S3L_F)/511, (504*S3L_F)/511,
|
||||
(505*S3L_F)/511, (506*S3L_F)/511,
|
||||
(507*S3L_F)/511, (507*S3L_F)/511,
|
||||
(508*S3L_F)/511, (509*S3L_F)/511,
|
||||
(509*S3L_F)/511, (510*S3L_F)/511,
|
||||
(510*S3L_F)/511, (510*S3L_F)/511,
|
||||
(510*S3L_F)/511, (510*S3L_F)/511
|
||||
};
|
||||
#endif
|
||||
|
||||
#define S3L_SIN_TABLE_UNIT_STEP\
|
||||
(S3L_FRACTIONS_PER_UNIT / (S3L_SIN_TABLE_LENGTH * 4))
|
||||
(S3L_F / (S3L_SIN_TABLE_LENGTH * 4))
|
||||
|
||||
void S3L_vec4Init(S3L_Vec4 *v)
|
||||
{
|
||||
v->x = 0; v->y = 0; v->z = 0; v->w = S3L_FRACTIONS_PER_UNIT;
|
||||
v->x = 0; v->y = 0; v->z = 0; v->w = S3L_F;
|
||||
}
|
||||
|
||||
void S3L_vec4Set(S3L_Vec4 *v, S3L_Unit x, S3L_Unit y, S3L_Unit z, S3L_Unit w)
|
||||
|
@ -1011,7 +1012,7 @@ void S3L_vec3Sub(S3L_Vec4 *result, S3L_Vec4 substracted)
|
|||
void S3L_mat4Init(S3L_Mat4 m)
|
||||
{
|
||||
#define M(x,y) m[x][y]
|
||||
#define S S3L_FRACTIONS_PER_UNIT
|
||||
#define S S3L_F
|
||||
|
||||
M(0,0) = S; M(1,0) = 0; M(2,0) = 0; M(3,0) = 0;
|
||||
M(0,1) = 0; M(1,1) = S; M(2,1) = 0; M(3,1) = 0;
|
||||
|
@ -1031,16 +1032,16 @@ void S3L_mat4Copy(S3L_Mat4 src, S3L_Mat4 dst)
|
|||
|
||||
S3L_Unit S3L_vec3Dot(S3L_Vec4 a, S3L_Vec4 b)
|
||||
{
|
||||
return (a.x * b.x + a.y * b.y + a.z * b.z) / S3L_FRACTIONS_PER_UNIT;
|
||||
return (a.x * b.x + a.y * b.y + a.z * b.z) / S3L_F;
|
||||
}
|
||||
|
||||
void S3L_reflect(S3L_Vec4 toLight, S3L_Vec4 normal, S3L_Vec4 *result)
|
||||
{
|
||||
S3L_Unit d = 2 * S3L_vec3Dot(toLight,normal);
|
||||
|
||||
result->x = (normal.x * d) / S3L_FRACTIONS_PER_UNIT - toLight.x;
|
||||
result->y = (normal.y * d) / S3L_FRACTIONS_PER_UNIT - toLight.y;
|
||||
result->z = (normal.z * d) / S3L_FRACTIONS_PER_UNIT - toLight.z;
|
||||
result->x = (normal.x * d) / S3L_F - toLight.x;
|
||||
result->y = (normal.y * d) / S3L_F - toLight.y;
|
||||
result->z = (normal.z * d) / S3L_F - toLight.z;
|
||||
}
|
||||
|
||||
void S3L_vec3Cross(S3L_Vec4 a, S3L_Vec4 b, S3L_Vec4 *result)
|
||||
|
@ -1167,7 +1168,7 @@ void S3L_computeModelNormals(S3L_Model3D model, S3L_Unit *dst,
|
|||
}
|
||||
}
|
||||
|
||||
n.x = S3L_FRACTIONS_PER_UNIT;
|
||||
n.x = S3L_F;
|
||||
n.y = 0;
|
||||
n.z = 0;
|
||||
|
||||
|
@ -1233,7 +1234,7 @@ void S3L_vec4Xmat4(S3L_Vec4 *v, S3L_Mat4 m)
|
|||
((vBackup.x * m[col][0]) +\
|
||||
(vBackup.y * m[col][1]) +\
|
||||
(vBackup.z * m[col][2]) +\
|
||||
(vBackup.w * m[col][3])) / S3L_FRACTIONS_PER_UNIT
|
||||
(vBackup.w * m[col][3])) / S3L_F
|
||||
|
||||
v->x = dotCol(0);
|
||||
v->y = dotCol(1);
|
||||
|
@ -1247,9 +1248,9 @@ void S3L_vec3Xmat4(S3L_Vec4 *v, S3L_Mat4 m)
|
|||
|
||||
#undef dotCol
|
||||
#define dotCol(col)\
|
||||
(vBackup.x * m[col][0]) / S3L_FRACTIONS_PER_UNIT +\
|
||||
(vBackup.y * m[col][1]) / S3L_FRACTIONS_PER_UNIT +\
|
||||
(vBackup.z * m[col][2]) / S3L_FRACTIONS_PER_UNIT +\
|
||||
(vBackup.x * m[col][0]) / S3L_F +\
|
||||
(vBackup.y * m[col][1]) / S3L_F +\
|
||||
(vBackup.z * m[col][2]) / S3L_F +\
|
||||
m[col][3]
|
||||
|
||||
vBackup.x = v->x;
|
||||
|
@ -1260,7 +1261,7 @@ void S3L_vec3Xmat4(S3L_Vec4 *v, S3L_Mat4 m)
|
|||
v->x = dotCol(0);
|
||||
v->y = dotCol(1);
|
||||
v->z = dotCol(2);
|
||||
v->w = S3L_FRACTIONS_PER_UNIT;
|
||||
v->w = S3L_F;
|
||||
}
|
||||
|
||||
#undef dotCol
|
||||
|
@ -1307,12 +1308,12 @@ S3L_Unit S3L_interpolate(S3L_Unit v1, S3L_Unit v2, S3L_Unit t, S3L_Unit tMax)
|
|||
|
||||
S3L_Unit S3L_interpolateByUnit(S3L_Unit v1, S3L_Unit v2, S3L_Unit t)
|
||||
{
|
||||
return v1 + ((v2 - v1) * t) / S3L_FRACTIONS_PER_UNIT;
|
||||
return v1 + ((v2 - v1) * t) / S3L_F;
|
||||
}
|
||||
|
||||
S3L_Unit S3L_interpolateByUnitFrom0(S3L_Unit v2, S3L_Unit t)
|
||||
{
|
||||
return (v2 * t) / S3L_FRACTIONS_PER_UNIT;
|
||||
return (v2 * t) / S3L_F;
|
||||
}
|
||||
|
||||
S3L_Unit S3L_interpolateFrom0(S3L_Unit v2, S3L_Unit t, S3L_Unit tMax)
|
||||
|
@ -1343,7 +1344,7 @@ void S3L_mat4Xmat4(S3L_Mat4 m1, S3L_Mat4 m2)
|
|||
|
||||
for (uint16_t i = 0; i < 4; ++i)
|
||||
m1[col][row] +=
|
||||
(mat1[i][row] * m2[col][i]) / S3L_FRACTIONS_PER_UNIT;
|
||||
(mat1[i][row] * m2[col][i]) / S3L_F;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1381,21 +1382,21 @@ S3L_Unit S3L_sin(S3L_Unit x)
|
|||
sign = -1;
|
||||
}
|
||||
|
||||
x %= S3L_FRACTIONS_PER_UNIT;
|
||||
x %= S3L_F;
|
||||
|
||||
if (x > S3L_FRACTIONS_PER_UNIT / 2)
|
||||
if (x > S3L_F / 2)
|
||||
{
|
||||
x -= S3L_FRACTIONS_PER_UNIT / 2;
|
||||
x -= S3L_F / 2;
|
||||
sign *= -1;
|
||||
}
|
||||
|
||||
S3L_Unit tmp = S3L_FRACTIONS_PER_UNIT - 2 * x;
|
||||
S3L_Unit tmp = S3L_F - 2 * x;
|
||||
|
||||
#define _PI2 ((S3L_Unit) (9.8696044 * S3L_FRACTIONS_PER_UNIT))
|
||||
#define _PI2 ((S3L_Unit) (9.8696044 * S3L_F))
|
||||
return sign * // Bhaskara's approximation
|
||||
(((32 * x * _PI2) / S3L_FRACTIONS_PER_UNIT) * tmp) /
|
||||
((_PI2 * (5 * S3L_FRACTIONS_PER_UNIT - (8 * x * tmp) /
|
||||
S3L_FRACTIONS_PER_UNIT)) / S3L_FRACTIONS_PER_UNIT);
|
||||
(((32 * x * _PI2) / S3L_F) * tmp) /
|
||||
((_PI2 * (5 * S3L_F - (8 * x * tmp) /
|
||||
S3L_F)) / S3L_F);
|
||||
#undef _PI2
|
||||
#endif
|
||||
}
|
||||
|
@ -1403,7 +1404,7 @@ S3L_Unit S3L_sin(S3L_Unit x)
|
|||
S3L_Unit S3L_asin(S3L_Unit x)
|
||||
{
|
||||
#if S3L_SIN_METHOD == 0
|
||||
x = S3L_clamp(x,-S3L_FRACTIONS_PER_UNIT,S3L_FRACTIONS_PER_UNIT);
|
||||
x = S3L_clamp(x,-S3L_F,S3L_F);
|
||||
|
||||
int8_t sign = 1;
|
||||
|
||||
|
@ -1433,8 +1434,8 @@ S3L_Unit S3L_asin(S3L_Unit x)
|
|||
|
||||
return sign * middle;
|
||||
#else
|
||||
S3L_Unit low = -1 * S3L_FRACTIONS_PER_UNIT / 4,
|
||||
high = S3L_FRACTIONS_PER_UNIT / 4,
|
||||
S3L_Unit low = -1 * S3L_F / 4,
|
||||
high = S3L_F / 4,
|
||||
middle;
|
||||
|
||||
while (low <= high) // binary search
|
||||
|
@ -1457,15 +1458,15 @@ S3L_Unit S3L_asin(S3L_Unit x)
|
|||
|
||||
S3L_Unit S3L_cos(S3L_Unit x)
|
||||
{
|
||||
return S3L_sin(x + S3L_FRACTIONS_PER_UNIT / 4);
|
||||
return S3L_sin(x + S3L_F / 4);
|
||||
}
|
||||
|
||||
void S3L_correctBarycentricCoords(S3L_Unit barycentric[3])
|
||||
{
|
||||
barycentric[0] = S3L_clamp(barycentric[0],0,S3L_FRACTIONS_PER_UNIT);
|
||||
barycentric[1] = S3L_clamp(barycentric[1],0,S3L_FRACTIONS_PER_UNIT);
|
||||
barycentric[0] = S3L_clamp(barycentric[0],0,S3L_F);
|
||||
barycentric[1] = S3L_clamp(barycentric[1],0,S3L_F);
|
||||
|
||||
S3L_Unit d = S3L_FRACTIONS_PER_UNIT - barycentric[0] - barycentric[1];
|
||||
S3L_Unit d = S3L_F - barycentric[0] - barycentric[1];
|
||||
|
||||
if (d < 0)
|
||||
{
|
||||
|
@ -1483,7 +1484,7 @@ void S3L_makeTranslationMat(
|
|||
S3L_Mat4 m)
|
||||
{
|
||||
#define M(x,y) m[x][y]
|
||||
#define S S3L_FRACTIONS_PER_UNIT
|
||||
#define S S3L_F
|
||||
|
||||
M(0,0) = S; M(1,0) = 0; M(2,0) = 0; M(3,0) = 0;
|
||||
M(0,1) = 0; M(1,1) = S; M(2,1) = 0; M(3,1) = 0;
|
||||
|
@ -1505,7 +1506,7 @@ void S3L_makeScaleMatrix(
|
|||
M(0,0) = scaleX; M(1,0) = 0; M(2,0) = 0; M(3,0) = 0;
|
||||
M(0,1) = 0; M(1,1) = scaleY; M(2,1) = 0; M(3,1) = 0;
|
||||
M(0,2) = 0; M(1,2) = 0; M(2,2) = scaleZ; M(3,2) = 0;
|
||||
M(0,3) = 0; M(1,3) = 0; M(2,3) = 0; M(3,3) = S3L_FRACTIONS_PER_UNIT;
|
||||
M(0,3) = 0; M(1,3) = 0; M(2,3) = 0; M(3,3) = S3L_F;
|
||||
|
||||
#undef M
|
||||
}
|
||||
|
@ -1529,7 +1530,7 @@ void S3L_makeRotationMatrixZXY(
|
|||
S3L_Unit cz = S3L_cos(byZ);
|
||||
|
||||
#define M(x,y) m[x][y]
|
||||
#define S S3L_FRACTIONS_PER_UNIT
|
||||
#define S S3L_F
|
||||
|
||||
M(0,0) = (cy * cz) / S + (sy * sx * sz) / (S * S);
|
||||
M(1,0) = (cx * sz) / S;
|
||||
|
@ -1549,7 +1550,7 @@ void S3L_makeRotationMatrixZXY(
|
|||
M(0,3) = 0;
|
||||
M(1,3) = 0;
|
||||
M(2,3) = 0;
|
||||
M(3,3) = S3L_FRACTIONS_PER_UNIT;
|
||||
M(3,3) = S3L_F;
|
||||
|
||||
#undef M
|
||||
#undef S
|
||||
|
@ -1636,9 +1637,9 @@ void S3L_vec3Normalize(S3L_Vec4 *v)
|
|||
if (l == 0)
|
||||
return;
|
||||
|
||||
v->x = (v->x * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->y = (v->y * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->z = (v->z * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->x = (v->x * S3L_F) / l;
|
||||
v->y = (v->y * S3L_F) / l;
|
||||
v->z = (v->z * S3L_F) / l;
|
||||
}
|
||||
|
||||
void S3L_vec3NormalizeFast(S3L_Vec4 *v)
|
||||
|
@ -1648,18 +1649,18 @@ void S3L_vec3NormalizeFast(S3L_Vec4 *v)
|
|||
if (l == 0)
|
||||
return;
|
||||
|
||||
v->x = (v->x * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->y = (v->y * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->z = (v->z * S3L_FRACTIONS_PER_UNIT) / l;
|
||||
v->x = (v->x * S3L_F) / l;
|
||||
v->y = (v->y * S3L_F) / l;
|
||||
v->z = (v->z * S3L_F) / l;
|
||||
}
|
||||
|
||||
void S3L_transform3DInit(S3L_Transform3D *t)
|
||||
{
|
||||
S3L_vec4Init(&(t->translation));
|
||||
S3L_vec4Init(&(t->rotation));
|
||||
t->scale.x = S3L_FRACTIONS_PER_UNIT;
|
||||
t->scale.y = S3L_FRACTIONS_PER_UNIT;
|
||||
t->scale.z = S3L_FRACTIONS_PER_UNIT;
|
||||
t->scale.x = S3L_F;
|
||||
t->scale.y = S3L_F;
|
||||
t->scale.z = S3L_F;
|
||||
t->scale.w = 0;
|
||||
}
|
||||
|
||||
|
@ -1682,7 +1683,7 @@ void S3L_project3DPointToScreen(
|
|||
|
||||
S3L_Unit s = point.w;
|
||||
|
||||
point.w = S3L_FRACTIONS_PER_UNIT;
|
||||
point.w = S3L_F;
|
||||
|
||||
S3L_vec3Xmat4(&point,m);
|
||||
|
||||
|
@ -1702,7 +1703,7 @@ void S3L_project3DPointToScreen(
|
|||
(point.z <= 0) ? 0 :
|
||||
(
|
||||
(s * camera.focalLength * S3L_RESOLUTION_X) /
|
||||
(point.z * S3L_FRACTIONS_PER_UNIT)
|
||||
(point.z * S3L_F)
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -1716,19 +1717,19 @@ void S3L_lookAt(S3L_Vec4 pointTo, S3L_Transform3D *t)
|
|||
S3L_Unit dx = v.x;
|
||||
S3L_Unit l = S3L_vec2Length(v);
|
||||
|
||||
dx = (v.x * S3L_FRACTIONS_PER_UNIT) / S3L_nonZero(l); // normalize
|
||||
dx = (v.x * S3L_F) / S3L_nonZero(l); // normalize
|
||||
|
||||
t->rotation.y = -1 * S3L_asin(dx);
|
||||
|
||||
if (v.y < 0)
|
||||
t->rotation.y = S3L_FRACTIONS_PER_UNIT / 2 - t->rotation.y;
|
||||
t->rotation.y = S3L_F / 2 - t->rotation.y;
|
||||
|
||||
v.x = pointTo.y - t->translation.y;
|
||||
v.y = l;
|
||||
|
||||
l = S3L_vec2Length(v);
|
||||
|
||||
dx = (v.x * S3L_FRACTIONS_PER_UNIT) / S3L_nonZero(l);
|
||||
dx = (v.x * S3L_F) / S3L_nonZero(l);
|
||||
|
||||
t->rotation.x = S3L_asin(dx);
|
||||
}
|
||||
|
@ -1760,7 +1761,7 @@ void S3L_transform3DSet(
|
|||
|
||||
void S3L_cameraInit(S3L_Camera *camera)
|
||||
{
|
||||
camera->focalLength = S3L_FRACTIONS_PER_UNIT;
|
||||
camera->focalLength = S3L_F;
|
||||
S3L_transform3DInit(&(camera->transform));
|
||||
}
|
||||
|
||||
|
@ -1804,7 +1805,7 @@ void S3L_pixelInfoInit(S3L_PixelInfo *p)
|
|||
{
|
||||
p->x = 0;
|
||||
p->y = 0;
|
||||
p->barycentric[0] = S3L_FRACTIONS_PER_UNIT;
|
||||
p->barycentric[0] = S3L_F;
|
||||
p->barycentric[1] = 0;
|
||||
p->barycentric[2] = 0;
|
||||
p->modelIndex = 0;
|
||||
|
@ -1888,7 +1889,7 @@ static inline S3L_Unit S3L_interpolateBarycentric(
|
|||
(value0 * barycentric[0]) +
|
||||
(value1 * barycentric[1]) +
|
||||
(value2 * barycentric[2])
|
||||
) / S3L_FRACTIONS_PER_UNIT;
|
||||
) / S3L_F;
|
||||
}
|
||||
|
||||
void S3L_mapProjectionPlaneToScreen(
|
||||
|
@ -1898,11 +1899,11 @@ void S3L_mapProjectionPlaneToScreen(
|
|||
{
|
||||
*screenX =
|
||||
S3L_HALF_RESOLUTION_X +
|
||||
(point.x * S3L_HALF_RESOLUTION_X) / S3L_FRACTIONS_PER_UNIT;
|
||||
(point.x * S3L_HALF_RESOLUTION_X) / S3L_F;
|
||||
|
||||
*screenY =
|
||||
S3L_HALF_RESOLUTION_Y -
|
||||
(point.y * S3L_HALF_RESOLUTION_X) / S3L_FRACTIONS_PER_UNIT;
|
||||
(point.y * S3L_HALF_RESOLUTION_X) / S3L_F;
|
||||
}
|
||||
|
||||
void S3L_zBufferClear(void)
|
||||
|
@ -1952,7 +1953,7 @@ void S3L_drawTriangle(
|
|||
|
||||
S3L_Vec4 *tPointSS, *lPointSS, *rPointSS; /* points in Screen Space (in
|
||||
S3L_Units, normalized by
|
||||
S3L_FRACTIONS_PER_UNIT) */
|
||||
S3L_F) */
|
||||
|
||||
S3L_Unit *barycentric0; // bar. coord that gets higher from L to R
|
||||
S3L_Unit *barycentric1; // bar. coord that gets higher from R to L
|
||||
|
@ -1997,9 +1998,9 @@ void S3L_drawTriangle(
|
|||
#undef assignPoints
|
||||
|
||||
#if S3L_FLAT
|
||||
*barycentric0 = S3L_FRACTIONS_PER_UNIT / 3;
|
||||
*barycentric1 = S3L_FRACTIONS_PER_UNIT / 3;
|
||||
*barycentric2 = S3L_FRACTIONS_PER_UNIT - 2 * (S3L_FRACTIONS_PER_UNIT / 3);
|
||||
*barycentric0 = S3L_F / 3;
|
||||
*barycentric1 = S3L_F / 3;
|
||||
*barycentric2 = S3L_F - 2 * (S3L_F / 3);
|
||||
#endif
|
||||
|
||||
p.triangleSize[0] = rPointSS->x - lPointSS->x;
|
||||
|
@ -2082,13 +2083,13 @@ void S3L_drawTriangle(
|
|||
s##Dx = p2##PointSS->x - p1##PointSS->x;\
|
||||
s##Dy = p2##PointSS->y - p1##PointSS->y;\
|
||||
initDepthFLS(s,p1,p2)\
|
||||
s##SideFLS.stepScaled = (S3L_FRACTIONS_PER_UNIT << S3L_FAST_LERP_QUALITY)\
|
||||
s##SideFLS.stepScaled = (S3L_F << S3L_FAST_LERP_QUALITY)\
|
||||
/ (s##Dy != 0 ? s##Dy : 1);\
|
||||
s##SideFLS.valueScaled = 0;\
|
||||
if (!down)\
|
||||
{\
|
||||
s##SideFLS.valueScaled =\
|
||||
S3L_FRACTIONS_PER_UNIT << S3L_FAST_LERP_QUALITY;\
|
||||
S3L_F << S3L_FAST_LERP_QUALITY;\
|
||||
s##SideFLS.stepScaled *= -1;\
|
||||
}\
|
||||
s##Inc = s##Dx >= 0 ? 1 : -1;\
|
||||
|
@ -2118,10 +2119,10 @@ void S3L_drawTriangle(
|
|||
|
||||
#if S3L_PERSPECTIVE_CORRECTION == 1
|
||||
#define Z_RECIP_NUMERATOR\
|
||||
(S3L_FRACTIONS_PER_UNIT * S3L_FRACTIONS_PER_UNIT * S3L_FRACTIONS_PER_UNIT)
|
||||
(S3L_F * S3L_F * S3L_F)
|
||||
#elif S3L_PERSPECTIVE_CORRECTION == 2
|
||||
#define Z_RECIP_NUMERATOR\
|
||||
(S3L_FRACTIONS_PER_UNIT * S3L_FRACTIONS_PER_UNIT)
|
||||
(S3L_F * S3L_F)
|
||||
#endif
|
||||
/* ^ This numerator is a number by which we divide values for the
|
||||
reciprocals. For PC == 2 it has to be lower because linear interpolation
|
||||
|
@ -2170,7 +2171,7 @@ void S3L_drawTriangle(
|
|||
S3L_Unit *tmp = barycentric##b0;\
|
||||
barycentric##b0 = barycentric##b1;\
|
||||
barycentric##b1 = tmp;\
|
||||
s0##SideFLS.valueScaled = (S3L_FRACTIONS_PER_UNIT\
|
||||
s0##SideFLS.valueScaled = (S3L_F\
|
||||
<< S3L_FAST_LERP_QUALITY) - s0##SideFLS.valueScaled;\
|
||||
s0##SideFLS.stepScaled *= -1;\
|
||||
manageSplitPerspective(s0,s1)
|
||||
|
@ -2275,13 +2276,13 @@ void S3L_drawTriangle(
|
|||
(
|
||||
S3L_interpolateFrom0(rOverZ,i,rowLength)
|
||||
* depthPC.valueScaled
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
b1PC.valueScaled =
|
||||
(
|
||||
(lOverZ - S3L_interpolateFrom0(lOverZ,i,rowLength))
|
||||
* depthPC.valueScaled
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
int8_t rowCount = S3L_PC_APPROX_LENGTH;
|
||||
#endif
|
||||
|
@ -2329,7 +2330,7 @@ void S3L_drawTriangle(
|
|||
(
|
||||
S3L_interpolateFrom0(rOverZ,nextI,rowLength)
|
||||
* nextDepthScaled
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
b0PC.stepScaled =
|
||||
(nextValue - b0PC.valueScaled) / S3L_PC_APPROX_LENGTH;
|
||||
|
@ -2338,7 +2339,7 @@ void S3L_drawTriangle(
|
|||
(
|
||||
(lOverZ - S3L_interpolateFrom0(lOverZ,nextI,rowLength))
|
||||
* nextDepthScaled
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
b1PC.stepScaled =
|
||||
(nextValue - b1PC.valueScaled) / S3L_PC_APPROX_LENGTH;
|
||||
|
@ -2366,7 +2367,7 @@ void S3L_drawTriangle(
|
|||
(
|
||||
rOverZ
|
||||
* nextDepthScaled
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
b0PC.stepScaled =
|
||||
(nextValue - b0PC.valueScaled) / maxI;
|
||||
|
@ -2405,20 +2406,20 @@ void S3L_drawTriangle(
|
|||
(
|
||||
S3L_interpolateFrom0(rOverZ,i,rowLength)
|
||||
* p.depth
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
|
||||
*barycentric1 =
|
||||
(
|
||||
(lOverZ - S3L_interpolateFrom0(lOverZ,i,rowLength))
|
||||
* p.depth
|
||||
) / (Z_RECIP_NUMERATOR / S3L_FRACTIONS_PER_UNIT);
|
||||
) / (Z_RECIP_NUMERATOR / S3L_F);
|
||||
#elif S3L_PERSPECTIVE_CORRECTION == 2
|
||||
*barycentric0 = S3L_getFastLerpValue(b0PC);
|
||||
*barycentric1 = S3L_getFastLerpValue(b1PC);
|
||||
#endif
|
||||
|
||||
*barycentric2 =
|
||||
S3L_FRACTIONS_PER_UNIT - *barycentric0 - *barycentric1;
|
||||
S3L_F - *barycentric0 - *barycentric1;
|
||||
#endif
|
||||
|
||||
#if S3L_NEAR_CROSS_STRATEGY == 3
|
||||
|
@ -2501,12 +2502,12 @@ void S3L_rotate2DPoint(S3L_Unit *x, S3L_Unit *y, S3L_Unit angle)
|
|||
S3L_Unit xBackup = *x;
|
||||
|
||||
*x =
|
||||
(angleCos * (*x)) / S3L_FRACTIONS_PER_UNIT -
|
||||
(angleSin * (*y)) / S3L_FRACTIONS_PER_UNIT;
|
||||
(angleCos * (*x)) / S3L_F -
|
||||
(angleSin * (*y)) / S3L_F;
|
||||
|
||||
*y =
|
||||
(angleSin * xBackup) / S3L_FRACTIONS_PER_UNIT +
|
||||
(angleCos * (*y)) / S3L_FRACTIONS_PER_UNIT;
|
||||
(angleSin * xBackup) / S3L_F +
|
||||
(angleCos * (*y)) / S3L_F;
|
||||
}
|
||||
|
||||
void S3L_makeWorldMatrix(S3L_Transform3D worldTransform, S3L_Mat4 m)
|
||||
|
@ -2652,7 +2653,7 @@ void _S3L_projectVertex(
|
|||
result->x = model->vertices[vertexIndex];
|
||||
result->y = model->vertices[vertexIndex + 1];
|
||||
result->z = model->vertices[vertexIndex + 2];
|
||||
result->w = S3L_FRACTIONS_PER_UNIT; // needed for translation
|
||||
result->w = S3L_F; // needed for translation
|
||||
|
||||
S3L_vec3Xmat4(result,projectionMatrix);
|
||||
|
||||
|
@ -2719,30 +2720,30 @@ void _S3L_projectTriangle(
|
|||
for (int i = 0; i < 3; ++i)
|
||||
S3L_vec4Init(&(_S3L_triangleRemapBarycentrics[i]));
|
||||
|
||||
_S3L_triangleRemapBarycentrics[0].x = S3L_FRACTIONS_PER_UNIT;
|
||||
_S3L_triangleRemapBarycentrics[1].y = S3L_FRACTIONS_PER_UNIT;
|
||||
_S3L_triangleRemapBarycentrics[2].z = S3L_FRACTIONS_PER_UNIT;
|
||||
_S3L_triangleRemapBarycentrics[0].x = S3L_F;
|
||||
_S3L_triangleRemapBarycentrics[1].y = S3L_F;
|
||||
_S3L_triangleRemapBarycentrics[2].z = S3L_F;
|
||||
#endif
|
||||
|
||||
#define interpolateVertex \
|
||||
S3L_Unit ratio =\
|
||||
((transformed[be].z - S3L_NEAR) * S3L_FRACTIONS_PER_UNIT) /\
|
||||
((transformed[be].z - S3L_NEAR) * S3L_F) /\
|
||||
(transformed[be].z - transformed[in].z);\
|
||||
transformed[in].x = transformed[be].x - \
|
||||
((transformed[be].x - transformed[in].x) * ratio) /\
|
||||
S3L_FRACTIONS_PER_UNIT;\
|
||||
S3L_F;\
|
||||
transformed[in].y = transformed[be].y -\
|
||||
((transformed[be].y - transformed[in].y) * ratio) /\
|
||||
S3L_FRACTIONS_PER_UNIT;\
|
||||
S3L_F;\
|
||||
transformed[in].z = S3L_NEAR;\
|
||||
if (beI != 0) {\
|
||||
beI->x = (beI->x * ratio) / S3L_FRACTIONS_PER_UNIT;\
|
||||
beI->y = (beI->y * ratio) / S3L_FRACTIONS_PER_UNIT;\
|
||||
beI->z = (beI->z * ratio) / S3L_FRACTIONS_PER_UNIT;\
|
||||
ratio = S3L_FRACTIONS_PER_UNIT - ratio;\
|
||||
beI->x += (beB->x * ratio) / S3L_FRACTIONS_PER_UNIT;\
|
||||
beI->y += (beB->y * ratio) / S3L_FRACTIONS_PER_UNIT;\
|
||||
beI->z += (beB->z * ratio) / S3L_FRACTIONS_PER_UNIT; }
|
||||
beI->x = (beI->x * ratio) / S3L_F;\
|
||||
beI->y = (beI->y * ratio) / S3L_F;\
|
||||
beI->z = (beI->z * ratio) / S3L_F;\
|
||||
ratio = S3L_F - ratio;\
|
||||
beI->x += (beB->x * ratio) / S3L_F;\
|
||||
beI->y += (beB->y * ratio) / S3L_F;\
|
||||
beI->z += (beB->z * ratio) / S3L_F; }
|
||||
|
||||
if (infront == 2)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue