"World" and "Prop" are now separate concepts. Lots of code cleanup and bug fixes.
This commit is contained in:
parent
081e669a8a
commit
63932bab34
5 changed files with 511 additions and 459 deletions
|
@ -62,7 +62,7 @@ int main(void) {
|
|||
" * 3. This notice may not be removed or altered from any source distribution.\n" \
|
||||
"*/\n\n\n" \
|
||||
"#ifndef H_J3DTBLS_\n" \
|
||||
"#define H_J3DTBLS_\n\n\nstatic float sin_table[] = {\n\t" \
|
||||
"#define H_J3DTBLS_\n\n\nstatic float sinTable[] = {\n\t" \
|
||||
);
|
||||
|
||||
// Build look-up tables for speed later
|
||||
|
@ -82,7 +82,7 @@ int main(void) {
|
|||
}
|
||||
fprintf(out, "\n};\n\n");
|
||||
if (pass == 0) {
|
||||
fprintf(out, "static float cos_table[] = {\n\t");
|
||||
fprintf(out, "static float cosTable[] = {\n\t");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
158
j3d/j3d.h
158
j3d/j3d.h
|
@ -58,10 +58,18 @@ typedef struct {
|
|||
juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer
|
||||
} j3TriangleT;
|
||||
|
||||
typedef struct {
|
||||
jint16 color; // Assigned color of this face
|
||||
bool lit; // Do we care about lighting?
|
||||
bool twoSided; // Are both sides visible?
|
||||
float normalLength; // Magnatude of surface normal
|
||||
juint16 index[3]; // We do this instead of just a typedef so it works with stretch_buffer
|
||||
} j3TriangleThinT;
|
||||
|
||||
typedef struct {
|
||||
juint16 vertexCount; // How many verticies are in the list
|
||||
juint16 triangleCount; // How many triangles are in the list
|
||||
j3CoordinatesT *verticies; // List of verticies as loaded from disk (more v1, v2, v3 than x, y, z)
|
||||
j3CoordinatesT *vertices; // List of vertices as loaded from disk (more v1, v2, v3 than x, y, z)
|
||||
j3TriangleT *triangles; // Triangles built from vertex list
|
||||
j3VertexT position; // Position of object in world
|
||||
j3VertexT rotation; // Rotation of object in world
|
||||
|
@ -77,60 +85,26 @@ typedef struct {
|
|||
} j3PolyListT;
|
||||
|
||||
typedef struct {
|
||||
juint16 objectCount;
|
||||
j3ObjectT *objects;
|
||||
juint16 objectCount; // Number of objects in world
|
||||
j3ObjectT *objects; // Objects data
|
||||
j3PolyListT *polygons; // Polygons to render
|
||||
juint16 polygonCount; // Current visible polygons
|
||||
juint16 triangleCount; // Current triangles in world
|
||||
} j3WorldT;
|
||||
|
||||
typedef struct {
|
||||
juint16 vertexCount; // How many verticies are in the list
|
||||
juint16 triangleCount; // How many triangles are in the list
|
||||
j3VertexT *vertices; // List of vertices as loaded from disk (more v1, v2, v3 than x, y, z)
|
||||
j3TriangleThinT *triangles; // Triangles built from vertex list
|
||||
} j3PieceT;
|
||||
|
||||
extern jint16 _j3VarClipMinX;
|
||||
extern jint16 _j3VarClipMinY;
|
||||
extern jint16 _j3VarClipMinZ;
|
||||
extern jint16 _j3VarClipMaxX;
|
||||
extern jint16 _j3VarClipMaxY;
|
||||
extern jint16 _j3VarClipMaxZ;
|
||||
extern jint16 _j3VarViewDistance;
|
||||
extern float _j3VarAmbientLight;
|
||||
extern j3Matrix4x4T _j3VarCameraMatrix;
|
||||
extern j3VertexT _j3VarCameraLocation;
|
||||
extern j3FacingT _j3VarCameraAngle;
|
||||
extern bool _j3VarCameraLocationDirty;
|
||||
extern bool _j3VarCameraAngleDirty;
|
||||
extern j3VertexT _j3VarSunLocation;
|
||||
extern j3PolyListT *_j3Polygons;
|
||||
extern juint16 _j3PolygonCount;
|
||||
extern juint16 _j3TriangleCount;
|
||||
|
||||
|
||||
#define j3CameraMove(ob, px, py, pz) \
|
||||
_j3VarCameraLocation.x += (px); \
|
||||
_j3VarCameraLocation.y += (py); \
|
||||
_j3VarCameraLocation.z += (pz); \
|
||||
_j3VarCameraLocationDirty = true
|
||||
|
||||
#define j3CameraMoveTo(ob, px, py, pz) \
|
||||
_j3VarCameraLocation.x = (px); \
|
||||
_j3VarCameraLocation.y = (py); \
|
||||
_j3VarCameraLocation.z = (pz); \
|
||||
_j3VarCameraLocationDirty = true
|
||||
|
||||
#define j3CameraRotate(ob, px, py, pz) \
|
||||
_j3VarCameraAngle.x += (px); j3MathWrapBounds(_j3VarCameraAngle.x, 0, 360); \
|
||||
_j3VarCameraAngle.y += (py); j3MathWrapBounds(_j3VarCameraAngle.y, 0, 360); \
|
||||
_j3VarCameraAngle.z += (pz); j3MathWrapBounds(_j3VarCameraAngle.z, 0, 360); \
|
||||
_j3VarCameraAngleDirty = true
|
||||
|
||||
#define j3CameraRotateTo(ob, px, py, pz) \
|
||||
_j3VarCameraAngle.x = (px); j3MathWrapBounds(_j3VarCameraAngle.x, 0, 360); \
|
||||
_j3VarCameraAngle.y = (py); j3MathWrapBounds(_j3VarCameraAngle.y, 0, 360); \
|
||||
_j3VarCameraAngle.z = (pz); j3MathWrapBounds(_j3VarCameraAngle.z, 0, 360); \
|
||||
_j3VarCameraAngleDirty = true
|
||||
|
||||
#define j3CameraSetClippingBounds(x1, y1, x2, y2) \
|
||||
_j3VarClipMinX = (x1); j3MathCheckBounds(_j3VarClipMinX, 0, 319); \
|
||||
_j3VarClipMinY = (y1); j3MathCheckBounds(_j3VarClipMiny, 0, 199); \
|
||||
_j3VarClipMaxX = (x2); j3MathCheckBounds(_j3VarClipMaxX, 0, 319); \
|
||||
_j3VarClipMaxY = (y2); j3MathCheckBounds(_j3VarClipMaxy, 0, 199)
|
||||
typedef struct {
|
||||
juint16 pieceCount; // Number of pieces in the prop
|
||||
j3PieceT *pieces; // Piece data
|
||||
} j3PropT;
|
||||
|
||||
// Couple inline routines
|
||||
#define j3MathCheckBounds(value, min, max) \
|
||||
if (value < (min)) value = (min); \
|
||||
if (value > (max)) value = (max)
|
||||
|
@ -139,54 +113,28 @@ extern juint16 _j3TriangleCount;
|
|||
if (value < (min)) value += (max); \
|
||||
if (value > (max)) value -= (max)
|
||||
|
||||
#define j3ObjectMove(ob, px, py, pz) \
|
||||
ob.position.x += (px); \
|
||||
ob.position.y += (py); \
|
||||
ob.position.z += (pz); \
|
||||
ob.positionDirty = true
|
||||
|
||||
#define j3ObjectMoveTo(ob, px, py, pz) \
|
||||
ob.position.x = (px); \
|
||||
ob.position.y = (py); \
|
||||
ob.position.z = (pz); \
|
||||
ob.positionDirty = true
|
||||
|
||||
#define j3ObjectRotate(ob, px, py, pz) \
|
||||
ob.rotation.x += (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
||||
ob.rotation.y += (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
||||
ob.rotation.z += (pz); j3MathWrapBounds(ob.rotation.z, 0, 360); \
|
||||
ob.rotationDirty = true
|
||||
|
||||
#define j3ObjectRotateTo(ob, px, py, pz) \
|
||||
ob.rotation.x = (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
||||
ob.rotation.y = (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
||||
ob.rotation.z = (pz); j3MathWrapBounds(ob.rotation.z, 0, 360); \
|
||||
ob.rotationDirty = true
|
||||
|
||||
#define j3ObjectScale(ob, px, py, pz) \
|
||||
ob.scale.x += (px); \
|
||||
ob.scale.y += (py); \
|
||||
ob.scale.z += (pz); \
|
||||
ob.scaleDirty = true
|
||||
|
||||
#define j3ObjectScaleTo(ob, px, py, pz) \
|
||||
ob.scale.x = (px); \
|
||||
ob.scale.y = (py); \
|
||||
ob.scale.z = (pz); \
|
||||
ob.scaleDirty = true
|
||||
|
||||
|
||||
// Syntatic sugar
|
||||
#define j3DrawSolid(o) _j3DrawSolid(&(o))
|
||||
#define j3DrawWireframe(o) _j3DrawWireframe(&(o))
|
||||
#define j3ObjectReset(o) _j3ObjectReset(&(o))
|
||||
#define j3ObjectUpdate(o) _j3ObjectUpdate(&(o))
|
||||
#define j3WorldFree(w) _j3WorldFree((j3WorldT **)&(w))
|
||||
#define j3WorldLoad(w, f) _j3WorldLoad((j3WorldT **)&(w), (f))
|
||||
#define j3ObjectMove(o, px, py, pz) _j3ObjectMove(&(o), (px), (py), (pz))
|
||||
#define j3ObjectMoveTo(o, px, py, pz) _j3ObjectMoveTo(&(o), (px), (py), (pz))
|
||||
#define j3ObjectReset(o) _j3ObjectReset(&(o))
|
||||
#define j3ObjectRotate(o, px, py, pz) _j3ObjectRotate(&(o), (px), (py), (pz))
|
||||
#define j3ObjectRotateTo(o, px, py, pz) _j3ObjectRotateTo(&(o), (px), (py), (pz))
|
||||
#define j3ObjectScale(o, px, py, pz) _j3ObjectScale(&(o), (px), (py), (pz))
|
||||
#define j3ObjectScaleTo(o, px, py, pz) _j3ObjectScaleTo(&(o), (px), (py), (pz))
|
||||
#define j3PropFree(p) _j3PropFree((j3PropT **)&(p))
|
||||
#define j3PropLoad(p, f) _j3PropLoad((j3PropT **)&(p), (f))
|
||||
#define j3WorldAddProp(w, p) _j3WorldAddProp((j3WorldT **)&(w), (p))
|
||||
#define j3WorldFree(w) _j3WorldFree((j3WorldT **)&(w))
|
||||
|
||||
|
||||
// Prototypes
|
||||
void j3DrawTriangle2D(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, jint16 y3, jint16 color);
|
||||
void j3CameraMove(float x, float y, float z);
|
||||
void j3CameraMoveTo(float x, float y, float z);
|
||||
void j3CameraRotate(float x, float y, float z);
|
||||
void j3CameraRotateTo(float x, float y, float z);
|
||||
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 j3DrawWorld(j3WorldT *w);
|
||||
void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal);
|
||||
float j3MathDotProduct3D(j3Vector3DT *u, j3Vector3DT *v);
|
||||
|
@ -199,17 +147,17 @@ void j3UtilStartup(void);
|
|||
|
||||
|
||||
// Private Prototypes
|
||||
void _j3DrawSolid(j3ObjectT *o);
|
||||
void _j3DrawTriangleBottom(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);
|
||||
void _j3DrawWireframePair(j3ObjectT *o, juint16 v1, juint16 v2);
|
||||
void _j3DrawWireframe(j3ObjectT *o);
|
||||
void _j3ObjectReset(j3ObjectT *o);
|
||||
void _j3ObjectUpdateNormalLength(j3ObjectT *object, juint16 triangle);
|
||||
void _j3ObjectUpdate(j3ObjectT *o);
|
||||
bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2);
|
||||
void _j3WorldFree(j3WorldT **world);
|
||||
bool _j3WorldLoad(j3WorldT **world, char *file);
|
||||
void _j3ObjectMove(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3ObjectMoveTo(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3ObjectReset(j3ObjectT *o);
|
||||
void _j3ObjectRotate(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3ObjectRotateTo(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3ObjectScale(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3ObjectScaleTo(j3ObjectT *o, float x, float y, float z);
|
||||
void _j3PropFree(j3PropT **prop);
|
||||
jint16 _j3PropLoad(j3PropT **prop, char *file);
|
||||
jint16 _j3WorldAddProp(j3WorldT **world, j3PropT *prop);
|
||||
void _j3WorldFree(j3WorldT **world);
|
||||
|
||||
|
||||
#endif // H_J3D_
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
#define H_J3DTBLS_
|
||||
|
||||
|
||||
static float sin_table[] = {
|
||||
static float sinTable[] = {
|
||||
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.2079117, 0.2249510, 0.2419219, 0.2588190, 0.2756374, 0.2923717,
|
||||
|
@ -88,7 +88,7 @@ static float sin_table[] = {
|
|||
-0.0000003
|
||||
};
|
||||
|
||||
static float cos_table[] = {
|
||||
static float cosTable[] = {
|
||||
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.9781476, 0.9743701, 0.9702957, 0.9659258, 0.9612617, 0.9563048,
|
||||
|
|
38
j3d/main.c
38
j3d/main.c
|
@ -61,9 +61,11 @@ int main(void) {
|
|||
jint16 x;
|
||||
jint16 y;
|
||||
jint16 c;
|
||||
jlStaT *font = NULL;
|
||||
j3WorldT *world = NULL;
|
||||
bool r;
|
||||
jint16 cubeId;
|
||||
jint16 cubePieces;
|
||||
jlStaT *font = NULL;
|
||||
j3PropT *cube = NULL;
|
||||
j3WorldT *world = NULL;
|
||||
|
||||
jlUtilStartup("JoeyLib 3D");
|
||||
jlStaLoad(font, "font");
|
||||
|
@ -74,8 +76,8 @@ int main(void) {
|
|||
|
||||
printAt(font, 1, 1, "Loading object... ");
|
||||
jlDisplayPresent();
|
||||
r = j3WorldLoad(world, "cube");
|
||||
if (!r) {
|
||||
cubePieces = j3PropLoad(cube, "cube");
|
||||
if (cubePieces < 0) {
|
||||
printAt(font, 1, 1, "Object loading: Failed.");
|
||||
jlDisplayPresent();
|
||||
jlKeyWaitForAny();
|
||||
|
@ -83,14 +85,15 @@ int main(void) {
|
|||
printAt(font, 1, 1, "Object loading: Success!");
|
||||
jlDisplayPresent();
|
||||
|
||||
// Add prop to world
|
||||
cubeId = j3WorldAddProp(world, cube);
|
||||
|
||||
// Assign fake colors until we can read them from the J3D
|
||||
c = 1;
|
||||
for (x=0; x<world->objectCount; x++) {
|
||||
for (y=0; y<world->objects[x].triangleCount; y++) {
|
||||
world->objects[x].triangles[y].color = c++;
|
||||
if (c > 15) {
|
||||
c = 1;
|
||||
}
|
||||
for (y=0; y<world->objects[cubeId].triangleCount; y++) {
|
||||
world->objects[cubeId].triangles[y].color = c++;
|
||||
if (c > 15) {
|
||||
c = 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -102,9 +105,9 @@ int main(void) {
|
|||
for (y=0; y<world->objects[x].vertexCount; y++) {
|
||||
printf(" Vertex %d: %f %f %f\n",
|
||||
y,
|
||||
(double)world->objects[x].verticies[y].local.x,
|
||||
(double)world->objects[x].verticies[y].local.y,
|
||||
(double)world->objects[x].verticies[y].local.z
|
||||
(double)world->objects[x].vertices[y].local.x,
|
||||
(double)world->objects[x].vertices[y].local.y,
|
||||
(double)world->objects[x].vertices[y].local.z
|
||||
);
|
||||
}
|
||||
printf(" Triangles: %d\n", world->objects[x].triangleCount);
|
||||
|
@ -130,14 +133,8 @@ int main(void) {
|
|||
jlDrawColor(15);
|
||||
jlDrawBox(0, 0, 319, 199);
|
||||
|
||||
//***TODO*** HACK!!!
|
||||
_j3PolygonCount = 0;
|
||||
|
||||
for (x=0; x<world->objectCount; x++) {
|
||||
j3ObjectRotate(world->objects[x], 2, 4, 6); // Matching values in code I'm studying
|
||||
j3ObjectUpdate(world->objects[x]);
|
||||
//j3DrawWireframe(world->objects[x]);
|
||||
//j3DrawSolid(world->objects[x]);
|
||||
j3DrawWorld(world);
|
||||
}
|
||||
|
||||
|
@ -157,6 +154,7 @@ int main(void) {
|
|||
jlKeyRead();
|
||||
}
|
||||
|
||||
j3PropFree(cube);
|
||||
j3WorldFree(world);
|
||||
jlStaFree(font);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue