Bug fixes, start of fixed point experiments.

This commit is contained in:
Scott Duensing 2019-09-24 18:06:44 -05:00
parent 9d1a7b9087
commit 1395307241
4 changed files with 122 additions and 109 deletions

View file

@ -41,28 +41,30 @@ int main(void) {
} }
fprintf(out, \ fprintf(out, \
"/*\n" \ "/*\n"
" * JoeyLib 3D\n" \ " * JoeyLib 3D\n"
" * Copyright (C) 2019 Scott Duensing <scott@kangaroopunch.com>\n" \ " * Copyright (C) 2019 Scott Duensing <scott@kangaroopunch.com>\n"
" *\n" \ " *\n"
" * This software is provided 'as-is', without any express or implied\n" \ " * This software is provided 'as-is', without any express or implied\n"
" * warranty. In no event will the authors be held liable for any damages\n" \ " * warranty. In no event will the authors be held liable for any damages\n"
" * arising from the use of this software.\n" \ " * arising from the use of this software.\n"
" *\n" \ " *\n"
" * Permission is granted to anyone to use this software for any purpose,\n" \ " * Permission is granted to anyone to use this software for any purpose,\n"
" * including commercial applications, and to alter it and redistribute it\n" \ " * including commercial applications, and to alter it and redistribute it\n"
" * freely, subject to the following restrictions:\n" \ " * freely, subject to the following restrictions:\n"
" *\n" \ " *\n"
" * 1. The origin of this software must not be misrepresented; you must not\n" \ " * 1. The origin of this software must not be misrepresented; you must not\n"
" * claim that you wrote the original software. If you use this software\n" \ " * claim that you wrote the original software. If you use this software\n"
" * in a product, an acknowledgment in the product documentation would be\n" \ " * in a product, an acknowledgment in the product documentation would be\n"
" * appreciated but is not required.\n" \ " * appreciated but is not required.\n"
" * 2. Altered source versions must be plainly marked as such, and must not be\n" \ " * 2. Altered source versions must be plainly marked as such, and must not be\n"
" * misrepresented as being the original software.\n" \ " * misrepresented as being the original software.\n"
" * 3. This notice may not be removed or altered from any source distribution.\n" \ " * 3. This notice may not be removed or altered from any source distribution.\n"
"*/\n\n\n" \ "*/\n\n\n"
"#ifndef H_J3DTBLS_\n" \ "#ifndef H_J3DTBLS_\n"
"#define H_J3DTBLS_\n\n\nstatic float sinTable[] = {\n\t" \ "#define H_J3DTBLS_\n\n"
"#include \"j3d.h\"\n\n"
"nstatic jreal sinTable[] = {\n\t"
); );
// Build look-up tables for speed later // Build look-up tables for speed later
@ -82,7 +84,7 @@ int main(void) {
} }
fprintf(out, "\n};\n\n"); fprintf(out, "\n};\n\n");
if (pass == 0) { if (pass == 0) {
fprintf(out, "static float cosTable[] = {\n\t"); fprintf(out, "static jreal cosTable[] = {\n\t");
} }
} }

132
j3d/j3d.c
View file

@ -27,13 +27,11 @@
//***TODO*** //***TODO***
// Not all faces are rendering until they rotate out and back in // Not all faces are rendering until they rotate out and back in
// Store original vertex data so we can do local rotations and save them
// Some fields like objectCount are juint16 but should be jint16 // Some fields like objectCount are juint16 but should be jint16
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <float.h>
#include <string.h> #include <string.h>
#include "j3d.h" #include "j3d.h"
@ -45,9 +43,12 @@
segment "j3d"; segment "j3d";
#define M_PI 3.1415926 #define M_PI 3.1415926
#define fabsf fabs #define fabsf fabs
#define FLT_EPSILON 1.19209290E-07F
#else #else
#include <float.h>
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpadded" #pragma GCC diagnostic ignored "-Wpadded"
@ -74,7 +75,7 @@ static jint16 _j3VarClipMaxX = 319;
static jint16 _j3VarClipMaxY = 199; static jint16 _j3VarClipMaxY = 199;
static jint16 _j3VarClipMaxZ = 3000; static jint16 _j3VarClipMaxZ = 3000;
static jint16 _j3VarViewDistance = 200; static jint16 _j3VarViewDistance = 200;
static float _j3VarAmbientLight = 6; static jreal _j3VarAmbientLight = 6;
static j3Matrix4x4T _j3VarCameraMatrix; static j3Matrix4x4T _j3VarCameraMatrix;
static j3VertexT _j3VarCameraLocation; static j3VertexT _j3VarCameraLocation;
static j3FacingT _j3VarCameraAngle; static j3FacingT _j3VarCameraAngle;
@ -82,14 +83,14 @@ static j3DirtynessT _j3VarCameraHowDirty = DIRTYNESS_ALL;
static j3VertexT _j3VarSunLocation; static j3VertexT _j3VarSunLocation;
#define ASPECT_RATIO (float)0.8 #define ASPECT_RATIO (jreal)0.8
#define INVERSE_ASPECT_RATIO (float)1.25 #define INVERSE_ASPECT_RATIO (jreal)1.25
#define HALF_SCREEN_WIDTH 160 #define HALF_SCREEN_WIDTH 160
#define HALF_SCREEN_HEIGHT 100 #define HALF_SCREEN_HEIGHT 100
void j3CameraMove(float x, float y, float z) { void j3CameraMove(jreal x, jreal y, jreal z) {
_j3VarCameraLocation.x += x; _j3VarCameraLocation.x += x;
_j3VarCameraLocation.y += y; _j3VarCameraLocation.y += y;
_j3VarCameraLocation.z += z; _j3VarCameraLocation.z += z;
@ -97,7 +98,7 @@ void j3CameraMove(float x, float y, float z) {
} }
void j3CameraMoveTo(float x, float y, float z) { void j3CameraMoveTo(jreal x, jreal y, jreal z) {
_j3VarCameraLocation.x = x; _j3VarCameraLocation.x = x;
_j3VarCameraLocation.y = y; _j3VarCameraLocation.y = y;
_j3VarCameraLocation.z = z; _j3VarCameraLocation.z = z;
@ -105,7 +106,7 @@ void j3CameraMoveTo(float x, float y, float z) {
} }
void j3CameraRotate(float x, float y, float z) { void j3CameraRotate(jreal x, jreal y, jreal z) {
_j3VarCameraAngle.x += x; _j3VarCameraAngle.x += x;
_j3VarCameraAngle.y += y; _j3VarCameraAngle.y += y;
_j3VarCameraAngle.z += z; _j3VarCameraAngle.z += z;
@ -113,7 +114,7 @@ void j3CameraRotate(float x, float y, float z) {
} }
void j3CameraRotateTo(float x, float y, float z) { void j3CameraRotateTo(jreal x, jreal y, jreal z) {
_j3VarCameraAngle.x = (jint16)x; _j3VarCameraAngle.x = (jint16)x;
_j3VarCameraAngle.y = (jint16)y; _j3VarCameraAngle.y = (jint16)y;
_j3VarCameraAngle.z = (jint16)z; _j3VarCameraAngle.z = (jint16)z;
@ -181,7 +182,7 @@ void j3DrawTriangle2D(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint
_j3DrawTriangleBottom(x1, y1, x2, y2, x3, y3); _j3DrawTriangleBottom(x1, y1, x2, y2, x3, y3);
} else { } else {
// General triangle that's needs to be broken up along long edge // General triangle that's needs to be broken up along long edge
newX = x1 + (jint16)((float)(y2 - y1) * (float)(x3 - x1) / (float)(y3 - y1)); newX = x1 + (jint16)((jreal)(y2 - y1) * (jreal)(x3 - x1) / (jreal)(y3 - y1));
// Draw each sub-triangle // Draw each sub-triangle
_j3DrawTriangleBottom(x1, y1, newX, y2, x2, y2); _j3DrawTriangleBottom(x1, y1, newX, y2, x2, y2);
_j3DrawTriangleTop(x2, y2, newX, y2, x3, y3); _j3DrawTriangleTop(x2, y2, newX, y2, x3, y3);
@ -190,11 +191,11 @@ void j3DrawTriangle2D(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint
void _j3DrawTriangleBottom(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint16 y3) { void _j3DrawTriangleBottom(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint16 y3) {
float dxRight; jreal dxRight;
float dxLeft; jreal dxLeft;
float xs; jreal xs;
float xe; jreal xe;
float height; jreal height;
jint16 tempX; jint16 tempX;
jint16 tempY; jint16 tempY;
jint16 right; jint16 right;
@ -215,14 +216,14 @@ void _j3DrawTriangleBottom(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3,
dxRight = (x3 - x1) / height; dxRight = (x3 - x1) / height;
// Set starting points // Set starting points
xs = (float)x1; xs = (jreal)x1;
xe = (float)x1 + (float)0.5; xe = (jreal)x1 + (jreal)0.5;
// Perform y clipping // Perform y clipping
if (y1 < _j3VarClipMinY) { if (y1 < _j3VarClipMinY) {
// Compute new xs and ys // Compute new xs and ys
xs = xs + dxLeft * (float)(-y1 + _j3VarClipMinY); xs = xs + dxLeft * (jreal)(-y1 + _j3VarClipMinY);
xe = xe + dxRight * (float)(-y1 + _j3VarClipMinY); xe = xe + dxRight * (jreal)(-y1 + _j3VarClipMinY);
// Reset y1 // Reset y1
y1 = _j3VarClipMinY; y1 = _j3VarClipMinY;
} }
@ -269,11 +270,11 @@ void _j3DrawTriangleBottom(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3,
void _j3DrawTriangleTop(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint16 y3) { void _j3DrawTriangleTop(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint16 y3) {
float dxRight; jreal dxRight;
float dxLeft; jreal dxLeft;
float xs; jreal xs;
float xe; jreal xe;
float height; jreal height;
jint16 tempX; jint16 tempX;
jint16 tempY; jint16 tempY;
jint16 right; jint16 right;
@ -294,14 +295,14 @@ void _j3DrawTriangleTop(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, ji
dxRight = (x3 - x2) / height; dxRight = (x3 - x2) / height;
// Set starting points // Set starting points
xs = (float)x1; xs = (jreal)x1;
xe = (float)x2 + (float)0.5; xe = (jreal)x2 + (jreal)0.5;
// Perform y clipping // Perform y clipping
if (y1 < _j3VarClipMinY) { if (y1 < _j3VarClipMinY) {
// Compute new xs and ys // Compute new xs and ys
xs = xs + dxLeft * (float)(-y1 + _j3VarClipMinY); xs = xs + dxLeft * (jreal)(-y1 + _j3VarClipMinY);
xe = xe + dxRight * (float)(-y1 + _j3VarClipMinY); xe = xe + dxRight * (jreal)(-y1 + _j3VarClipMinY);
// Reset y1 // Reset y1
y1 = _j3VarClipMinY; y1 = _j3VarClipMinY;
} }
@ -352,15 +353,15 @@ void j3DrawWorld(j3WorldT *w) {
juint16 vertex1; juint16 vertex1;
juint16 vertex2; juint16 vertex2;
juint16 vertex3; juint16 vertex3;
float x1; jreal x1;
float y1; jreal y1;
float z1; jreal z1;
float x2; jreal x2;
float y2; jreal y2;
float z2; jreal z2;
float x3; jreal x3;
float y3; jreal y3;
float z3; jreal z3;
j3ObjectT *o; j3ObjectT *o;
j3PolyListT *p; j3PolyListT *p;
@ -419,7 +420,7 @@ void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal) {
} }
float j3MathDotProduct3D(j3Vector3DT *u, j3Vector3DT *v) { jreal j3MathDotProduct3D(j3Vector3DT *u, j3Vector3DT *v) {
// Compute the dot product of two vectors // Compute the dot product of two vectors
return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z)); return( (u->x * v->x) + (u->y * v->y) + (u->z * v->z));
} }
@ -464,13 +465,13 @@ void j3MathMakeVector3D(j3VertexT *init, j3VertexT *term, j3Vector3DT *result) {
} }
float j3MathVectorMagnatude3D(j3Vector3DT *v) { jreal j3MathVectorMagnatude3D(j3Vector3DT *v) {
// Compute the magnitude of a vector // Compute the magnitude of a vector
return((float)sqrt((double)(v->x * v->x + v->y * v->y + v->z * v->z))); return((jreal)sqrt((double)(v->x * v->x + v->y * v->y + v->z * v->z)));
} }
void _j3ObjectMove(j3ObjectT *o, float x, float y, float z) { void _j3ObjectMove(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->position.x += x; o->position.x += x;
o->position.y += y; o->position.y += y;
o->position.z += z; o->position.z += z;
@ -478,7 +479,7 @@ void _j3ObjectMove(j3ObjectT *o, float x, float y, float z) {
} }
void _j3ObjectMoveTo(j3ObjectT *o, float x, float y, float z) { void _j3ObjectMoveTo(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->position.x = x; o->position.x = x;
o->position.y = y; o->position.y = y;
o->position.z = z; o->position.z = z;
@ -503,7 +504,7 @@ void _j3ObjectReset(j3ObjectT *o) {
} }
void _j3ObjectRotate(j3ObjectT *o, float x, float y, float z) { void _j3ObjectRotate(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->rotation.x += x; j3MathWrapBounds(o->rotation.x, 0, 360); o->rotation.x += x; j3MathWrapBounds(o->rotation.x, 0, 360);
o->rotation.y += y; j3MathWrapBounds(o->rotation.y, 0, 360); o->rotation.y += y; j3MathWrapBounds(o->rotation.y, 0, 360);
o->rotation.z += z; j3MathWrapBounds(o->rotation.z, 0, 360); o->rotation.z += z; j3MathWrapBounds(o->rotation.z, 0, 360);
@ -511,7 +512,7 @@ void _j3ObjectRotate(j3ObjectT *o, float x, float y, float z) {
} }
void _j3ObjectRotateTo(j3ObjectT *o, float x, float y, float z) { void _j3ObjectRotateTo(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->rotation.x = x; j3MathWrapBounds(o->rotation.x, 0, 360); o->rotation.x = x; j3MathWrapBounds(o->rotation.x, 0, 360);
o->rotation.y = y; j3MathWrapBounds(o->rotation.y, 0, 360); o->rotation.y = y; j3MathWrapBounds(o->rotation.y, 0, 360);
o->rotation.z = z; j3MathWrapBounds(o->rotation.z, 0, 360); o->rotation.z = z; j3MathWrapBounds(o->rotation.z, 0, 360);
@ -519,7 +520,7 @@ void _j3ObjectRotateTo(j3ObjectT *o, float x, float y, float z) {
} }
void _j3ObjectScale(j3ObjectT *o, float x, float y, float z) { void _j3ObjectScale(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->scale.x += x; o->scale.x += x;
o->scale.y += y; o->scale.y += y;
o->scale.z += z; o->scale.z += z;
@ -527,7 +528,7 @@ void _j3ObjectScale(j3ObjectT *o, float x, float y, float z) {
} }
void _j3ObjectScaleTo(j3ObjectT *o, float x, float y, float z) { void _j3ObjectScaleTo(j3ObjectT *o, jreal x, jreal y, jreal z) {
o->scale.x = x; o->scale.x = x;
o->scale.y = y; o->scale.y = y;
o->scale.z = z; o->scale.z = z;
@ -543,8 +544,8 @@ void _j3ObjectUpdate(j3WorldT *w, jint16 index) {
juint16 vertex0; juint16 vertex0;
juint16 vertex1; juint16 vertex1;
juint16 vertex2; juint16 vertex2;
float dot; jreal dot;
float intensity; jreal intensity;
j3Vector3DT u; j3Vector3DT u;
j3Vector3DT v; j3Vector3DT v;
j3Vector3DT normal; j3Vector3DT normal;
@ -881,7 +882,7 @@ void _j3ObjectUpdate(j3WorldT *w, jint16 index) {
vertex0 = o->triangles[i].index[0]; vertex0 = o->triangles[i].index[0];
vertex1 = o->triangles[i].index[1]; vertex1 = o->triangles[i].index[1];
vertex2 = o->triangles[i].index[2]; vertex2 = o->triangles[i].index[2];
o->triangles[i].averageDepth = (float)0.3333333 * (o->vertices[vertex0].camera.z + o->vertices[vertex1].camera.z + o->vertices[vertex2].camera.z); o->triangles[i].averageDepth = (jreal)0.3333333 * (o->vertices[vertex0].camera.z + o->vertices[vertex1].camera.z + o->vertices[vertex2].camera.z);
// Add to visible polygon list // Add to visible polygon list
w->polygons[w->polygonCount].object = o; w->polygons[w->polygonCount].object = o;
w->polygons[w->polygonCount].triangleNumber = (juint16)i; w->polygons[w->polygonCount].triangleNumber = (juint16)i;
@ -917,13 +918,13 @@ void _j3ObjectUpdateNormalLength(j3ObjectT *object, juint16 triangle) {
// Compute magnitude of normal, take its inverse and multiply it by // Compute magnitude of normal, take its inverse and multiply it by
// 15, this will change the shading calculation of 15*dp/normal into // 15, this will change the shading calculation of 15*dp/normal into
// dp*normal_length, removing one division // dp*normal_length, removing one division
object->triangles[triangle].normalLength = (float)15.0 / j3MathVectorMagnatude3D((j3Vector3DT *)&normal); object->triangles[triangle].normalLength = (jreal)15.0 / j3MathVectorMagnatude3D((j3Vector3DT *)&normal);
} }
int _j3PolyCompare(j3PolyListT *arg1, j3PolyListT *arg2) { int _j3PolyCompare(j3PolyListT *arg1, j3PolyListT *arg2) {
float z1; jreal z1;
float z2; jreal z2;
z1 = arg1->object->triangles[arg1->triangleNumber].averageDepth; z1 = arg1->object->triangles[arg1->triangleNumber].averageDepth;
z2 = arg2->object->triangles[arg2->triangleNumber].averageDepth; z2 = arg2->object->triangles[arg2->triangleNumber].averageDepth;
@ -941,8 +942,8 @@ int _j3PolyCompare(j3PolyListT *arg1, j3PolyListT *arg2) {
bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) { bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
jint16 xi; jint16 xi;
jint16 yi; jint16 yi;
float dx; jreal dx;
float dy; jreal dy;
bool point1 = false; // End points visible? bool point1 = false; // End points visible?
bool point2 = false; bool point2 = false;
bool rightEdge = false; // Which edges are the endpoints beyond? bool rightEdge = false; // Which edges are the endpoints beyond?
@ -986,7 +987,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
rightEdge = true; rightEdge = true;
// Find intersection with right edge // Find intersection with right edge
if (fabsf(dx) > FLT_EPSILON) { // dx != 0 if (fabsf(dx) > FLT_EPSILON) { // dx != 0
yi = (jint16)((float)0.5 + (dy / dx) * (_j3VarClipMaxX - *x1) + *y1); yi = (jint16)((jreal)0.5 + (dy / dx) * (_j3VarClipMaxX - *x1) + *y1);
} else { } else {
yi = -1; // Invalidate intersection yi = -1; // Invalidate intersection
} }
@ -995,7 +996,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
leftEdge = true; leftEdge = true;
// Find intersection with left edge // Find intersection with left edge
if (fabsf(dx) > FLT_EPSILON) { // dx != 0 if (fabsf(dx) > FLT_EPSILON) { // dx != 0
yi = (jint16)((float)0.5 + (dy / dx) * (_j3VarClipMinX - *x1) + *y1); yi = (jint16)((jreal)0.5 + (dy / dx) * (_j3VarClipMinX - *x1) + *y1);
} else { } else {
yi = -1; // Invalidate intersection yi = -1; // Invalidate intersection
} }
@ -1006,7 +1007,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
bottomEdge = true; bottomEdge = true;
// Find intersection with right edge // Find intersection with right edge
if (fabsf(dy) > FLT_EPSILON) { // dy != 0 if (fabsf(dy) > FLT_EPSILON) { // dy != 0
xi = (jint16)((float)0.5 + (dx / dy) * (_j3VarClipMaxY - *y1) + *x1); xi = (jint16)((jreal)0.5 + (dx / dy) * (_j3VarClipMaxY - *y1) + *x1);
} else { } else {
xi = -1; // Invalidate inntersection xi = -1; // Invalidate inntersection
} }
@ -1015,7 +1016,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
topEdge = true; topEdge = true;
// Find intersection with top edge // Find intersection with top edge
if (fabsf(dy) > FLT_EPSILON) { // dy != 0 if (fabsf(dy) > FLT_EPSILON) { // dy != 0
xi = (jint16)((float)0.5 + (dx / dy) * (_j3VarClipMinY - *y1) + *x1); xi = (jint16)((jreal)0.5 + (dx / dy) * (_j3VarClipMinY - *y1) + *x1);
} else { } else {
xi = -1; // Invalidate intersection xi = -1; // Invalidate intersection
} }
@ -1064,7 +1065,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
rightEdge = true; rightEdge = true;
// Find intersection with right edge // Find intersection with right edge
if (fabsf(dx) > FLT_EPSILON) { // dx != 0 if (fabsf(dx) > FLT_EPSILON) { // dx != 0
yi = (jint16)((float)0.5 + (dy / dx) * (_j3VarClipMaxX - *x2) + *y2); yi = (jint16)((jreal)0.5 + (dy / dx) * (_j3VarClipMaxX - *x2) + *y2);
} else { } else {
yi = -1; // Invalidate inntersection yi = -1; // Invalidate inntersection
} }
@ -1073,7 +1074,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
leftEdge = true; leftEdge = true;
// Find intersection with left edge // Find intersection with left edge
if (fabsf(dx) > FLT_EPSILON) { // dx != 0 if (fabsf(dx) > FLT_EPSILON) { // dx != 0
yi = (jint16)((float)0.5 + (dy / dx) * (_j3VarClipMinX - *x2) + *y2); yi = (jint16)((jreal)0.5 + (dy / dx) * (_j3VarClipMinX - *x2) + *y2);
} else { } else {
yi = -1; // Invalidate intersection yi = -1; // Invalidate intersection
} }
@ -1084,7 +1085,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
bottomEdge = true; bottomEdge = true;
// Find intersection with right edge // Find intersection with right edge
if (fabsf(dy) > FLT_EPSILON) { // dy != 0 if (fabsf(dy) > FLT_EPSILON) { // dy != 0
xi = (jint16)((float)0.5 + (dx / dy) * (_j3VarClipMaxY - *y2) + *x2); xi = (jint16)((jreal)0.5 + (dx / dy) * (_j3VarClipMaxY - *y2) + *x2);
} else { } else {
xi = -1; // invalidate inntersection xi = -1; // invalidate inntersection
} }
@ -1093,7 +1094,7 @@ bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2) {
topEdge = true; topEdge = true;
// Find intersection with top edge // Find intersection with top edge
if (fabsf(dy) > FLT_EPSILON) { // dy != 0 if (fabsf(dy) > FLT_EPSILON) { // dy != 0
xi = (jint16)((float)0.5 + (dx / dy) * (_j3VarClipMinY - *y2) + *x2); xi = (jint16)((jreal)0.5 + (dx / dy) * (_j3VarClipMinY - *y2) + *x2);
} else { } else {
xi = -1; // invalidate inntersection xi = -1; // invalidate inntersection
} }
@ -1141,9 +1142,9 @@ void j3UtilStartup(void) {
_j3VarCameraAngle.y = 0; _j3VarCameraAngle.y = 0;
_j3VarCameraAngle.z = 0; _j3VarCameraAngle.z = 0;
_j3VarSunLocation.x = (float)-0.913913; _j3VarSunLocation.x = (jreal)-0.913913;
_j3VarSunLocation.y = (float) 0.389759; _j3VarSunLocation.y = (jreal) 0.389759;
_j3VarSunLocation.z = (float)-0.113369; _j3VarSunLocation.z = (jreal)-0.113369;
} }
@ -1220,6 +1221,7 @@ jint16 _j3PropLoad(j3PropT **prop, char *file) {
// Iterate and read vertices // Iterate and read vertices
for (y=0; y<(*prop)->pieces[x].vertexCount; y++) { for (y=0; y<(*prop)->pieces[x].vertexCount; y++) {
// Read one at a time in case the struct gets padded // Read one at a time in case the struct gets padded
// These really are type 'float' - don't use 'jreal' here
if (fread(&(*prop)->pieces[x].vertices[y].x, sizeof(float), 1, in) != 1) { if (fread(&(*prop)->pieces[x].vertices[y].x, sizeof(float), 1, in) != 1) {
failed = true; failed = true;
break; break;

View file

@ -27,6 +27,14 @@
#include "joey.h" #include "joey.h"
// Sad start to fixed point math
#ifdef JOEY_IIGS
//typedef extended jreal;
typedef float jreal;
#else
typedef float jreal;
#endif
typedef enum { typedef enum {
DIRTYNESS_CLEAN = 0x00, DIRTYNESS_CLEAN = 0x00,
DIRTYNESS_SCALE = 0x01, DIRTYNESS_SCALE = 0x01,
@ -35,7 +43,7 @@ typedef enum {
DIRTYNESS_ALL = 0xFF DIRTYNESS_ALL = 0xFF
} j3DirtynessT; } j3DirtynessT;
typedef float j3Matrix4x4T[4][4]; typedef jreal j3Matrix4x4T[4][4];
typedef struct { typedef struct {
jint16 x; jint16 x;
@ -44,9 +52,9 @@ typedef struct {
} j3FacingT; } j3FacingT;
typedef struct { typedef struct {
float x; jreal x;
float y; jreal y;
float z; jreal z;
} j3VertexT, j3Vector3DT; } j3VertexT, j3Vector3DT;
typedef struct { typedef struct {
@ -64,8 +72,8 @@ typedef struct {
bool lit; // Do we care about lighting? bool lit; // Do we care about lighting?
bool visible; // Can we see this triangle? bool visible; // Can we see this triangle?
bool twoSided; // Are both sides visible? bool twoSided; // Are both sides visible?
float normalLength; // Magnatude of surface normal jreal normalLength; // Magnatude of surface normal
float averageDepth; // Average Z depth of this triangle jreal averageDepth; // Average Z depth of this triangle
juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer
} j3TriangleT; } j3TriangleT;
@ -73,7 +81,7 @@ typedef struct {
jint16 color; // Assigned color of this face jint16 color; // Assigned color of this face
bool lit; // Do we care about lighting? bool lit; // Do we care about lighting?
bool twoSided; // Are both sides visible? bool twoSided; // Are both sides visible?
float normalLength; // Magnatude of surface normal jreal normalLength; // Magnatude of surface normal
juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer
} j3TriangleThinT; } j3TriangleThinT;
@ -138,31 +146,31 @@ typedef struct {
// Prototypes // Prototypes
void j3CameraMove(float x, float y, float z); void j3CameraMove(jreal x, jreal y, jreal z);
void j3CameraMoveTo(float x, float y, float z); void j3CameraMoveTo(jreal x, jreal y, jreal z);
void j3CameraRotate(float x, float y, float z); void j3CameraRotate(jreal x, jreal y, jreal z);
void j3CameraRotateTo(float x, float y, float z); void j3CameraRotateTo(jreal x, jreal y, jreal z);
void j3CameraSetClippingBounds(jint16 x1, jint16 y1, jint16 x2, jint16 y2); void j3CameraSetClippingBounds(jint16 x1, jint16 y1, jint16 x2, jint16 y2);
void j3DrawTriangle2D(jint16 x1, jint16 y1, jint16 x2, jint16 y2, jint16 x3, jint16 y3, jint16 color); void j3DrawTriangle2D(jint16 x1, jint16 y1, jint16 x2, jint16 y2, jint16 x3, jint16 y3, jint16 color);
void j3DrawWorld(j3WorldT *w); void j3DrawWorld(j3WorldT *w);
void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal); void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal);
float j3MathDotProduct3D(j3Vector3DT *u, j3Vector3DT *v); jreal j3MathDotProduct3D(j3Vector3DT *u, j3Vector3DT *v);
void j3MathMatrix4x4Identity(j3Matrix4x4T result); void j3MathMatrix4x4Identity(j3Matrix4x4T result);
void j3MathMatrix4x4Mult(j3Matrix4x4T a, j3Matrix4x4T b, j3Matrix4x4T result); void j3MathMatrix4x4Mult(j3Matrix4x4T a, j3Matrix4x4T b, j3Matrix4x4T result);
void j3MathMakeVector3D(j3VertexT *init, j3VertexT *term, j3Vector3DT *result); void j3MathMakeVector3D(j3VertexT *init, j3VertexT *term, j3Vector3DT *result);
float j3MathVectorMagnatude3D(j3Vector3DT *v); jreal j3MathVectorMagnatude3D(j3Vector3DT *v);
void j3UtilShutdown(void); void j3UtilShutdown(void);
void j3UtilStartup(void); void j3UtilStartup(void);
// Private Prototypes // Private Prototypes
void _j3ObjectMove(j3ObjectT *o, float x, float y, float z); void _j3ObjectMove(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3ObjectMoveTo(j3ObjectT *o, float x, float y, float z); void _j3ObjectMoveTo(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3ObjectReset(j3ObjectT *o); void _j3ObjectReset(j3ObjectT *o);
void _j3ObjectRotate(j3ObjectT *o, float x, float y, float z); void _j3ObjectRotate(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3ObjectRotateTo(j3ObjectT *o, float x, float y, float z); void _j3ObjectRotateTo(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3ObjectScale(j3ObjectT *o, float x, float y, float z); void _j3ObjectScale(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3ObjectScaleTo(j3ObjectT *o, float x, float y, float z); void _j3ObjectScaleTo(j3ObjectT *o, jreal x, jreal y, jreal z);
void _j3PropFree(j3PropT **prop); void _j3PropFree(j3PropT **prop);
jint16 _j3PropLoad(j3PropT **prop, char *file); jint16 _j3PropLoad(j3PropT **prop, char *file);
jint16 _j3WorldAddProp(j3WorldT **world, j3PropT *prop); jint16 _j3WorldAddProp(j3WorldT **world, j3PropT *prop);

View file

@ -23,8 +23,9 @@
#ifndef H_J3DTBLS_ #ifndef H_J3DTBLS_
#define H_J3DTBLS_ #define H_J3DTBLS_
#include "j3d.h"
static float sinTable[] = { nstatic jreal sinTable[] = {
0.0000000, 0.0174524, 0.0348995, 0.0523360, 0.0697565, 0.0871557, 0.0000000, 0.0174524, 0.0348995, 0.0523360, 0.0697565, 0.0871557,
0.1045285, 0.1218693, 0.1391731, 0.1564345, 0.1736482, 0.1908090, 0.1045285, 0.1218693, 0.1391731, 0.1564345, 0.1736482, 0.1908090,
0.2079117, 0.2249510, 0.2419219, 0.2588190, 0.2756374, 0.2923717, 0.2079117, 0.2249510, 0.2419219, 0.2588190, 0.2756374, 0.2923717,
@ -88,7 +89,7 @@ static float sinTable[] = {
-0.0000003 -0.0000003
}; };
static float cosTable[] = { static jreal cosTable[] = {
1.0000000, 0.9998477, 0.9993908, 0.9986295, 0.9975641, 0.9961947, 1.0000000, 0.9998477, 0.9993908, 0.9986295, 0.9975641, 0.9961947,
0.9945219, 0.9925462, 0.9902681, 0.9876883, 0.9848078, 0.9816272, 0.9945219, 0.9925462, 0.9902681, 0.9876883, 0.9848078, 0.9816272,
0.9781476, 0.9743701, 0.9702957, 0.9659258, 0.9612617, 0.9563048, 0.9781476, 0.9743701, 0.9702957, 0.9659258, 0.9612617, 0.9563048,