Colored and solid objects on IIgs are working.
This commit is contained in:
parent
99de9c6b23
commit
18fab39c08
3 changed files with 202 additions and 145 deletions
80
j3d/j3d.c
80
j3d/j3d.c
|
@ -25,6 +25,11 @@
|
||||||
// https://archive.org/details/BlackArt3DEBook
|
// https://archive.org/details/BlackArt3DEBook
|
||||||
|
|
||||||
|
|
||||||
|
//***TODO***
|
||||||
|
// Allow multiple objects to be loaded into a world
|
||||||
|
// Store original vertex data so we can do local rotations and save them
|
||||||
|
|
||||||
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <float.h>
|
#include <float.h>
|
||||||
|
@ -465,6 +470,10 @@ void _j3ObjectReset(j3ObjectT *o) {
|
||||||
o->scale.x = 1;
|
o->scale.x = 1;
|
||||||
o->scale.y = 1;
|
o->scale.y = 1;
|
||||||
o->scale.z = 1;
|
o->scale.z = 1;
|
||||||
|
|
||||||
|
o->positionDirty = true;
|
||||||
|
o->rotationDirty = true;
|
||||||
|
o->scaleDirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -491,6 +500,11 @@ void _j3ObjectUpdate(j3ObjectT *o) {
|
||||||
|
|
||||||
// === ROTATION ===
|
// === ROTATION ===
|
||||||
|
|
||||||
|
if (o->rotationDirty) {
|
||||||
|
|
||||||
|
// Rotation being dirty means we also need to update position later
|
||||||
|
o->positionDirty = true;
|
||||||
|
|
||||||
x = (jint16)o->rotation.x;
|
x = (jint16)o->rotation.x;
|
||||||
y = (jint16)o->rotation.y;
|
y = (jint16)o->rotation.y;
|
||||||
z = (jint16)o->rotation.z;
|
z = (jint16)o->rotation.z;
|
||||||
|
@ -628,14 +642,17 @@ void _j3ObjectUpdate(j3ObjectT *o) {
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// === SCALE & TRANSLATION ===
|
// === SCALE & TRANSLATION ===
|
||||||
|
|
||||||
|
if (o->positionDirty || o->scaleDirty) {
|
||||||
for (i=0; i<o->vertexCount; i++) {
|
for (i=0; i<o->vertexCount; i++) {
|
||||||
o->verticies[i].world.x = o->verticies[i].world.x * o->scale.x + o->position.x;
|
o->verticies[i].world.x = o->verticies[i].world.x * o->scale.x + o->position.x;
|
||||||
o->verticies[i].world.y = o->verticies[i].world.y * o->scale.y + o->position.y;
|
o->verticies[i].world.y = o->verticies[i].world.y * o->scale.y + o->position.y;
|
||||||
o->verticies[i].world.z = o->verticies[i].world.z * o->scale.z + o->position.z;
|
o->verticies[i].world.z = o->verticies[i].world.z * o->scale.z + o->position.z;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// === CAMERA SPACE ===
|
// === CAMERA SPACE ===
|
||||||
//***TODO*** Move this?
|
//***TODO*** Move this?
|
||||||
|
@ -647,6 +664,10 @@ void _j3ObjectUpdate(j3ObjectT *o) {
|
||||||
// === REMOVE BACKFACES & LIGHT ===
|
// === REMOVE BACKFACES & LIGHT ===
|
||||||
|
|
||||||
for (i=0; i<o->triangleCount; i++) {
|
for (i=0; i<o->triangleCount; i++) {
|
||||||
|
// If the scale changed, we need a new normal length
|
||||||
|
if (o->scaleDirty) {
|
||||||
|
_j3ObjectUpdateNormalLength(o, (juint16)i);
|
||||||
|
}
|
||||||
// If it's two sided, there are no backfaces
|
// If it's two sided, there are no backfaces
|
||||||
if (!o->triangles[i].twoSided) {
|
if (!o->triangles[i].twoSided) {
|
||||||
|
|
||||||
|
@ -740,6 +761,36 @@ void _j3ObjectUpdate(j3ObjectT *o) {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
o->positionDirty = false;
|
||||||
|
o->rotationDirty = false;
|
||||||
|
o->scaleDirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void _j3ObjectUpdateNormalLength(j3ObjectT *object, juint16 triangle) {
|
||||||
|
juint16 vertex0;
|
||||||
|
juint16 vertex1;
|
||||||
|
juint16 vertex2;
|
||||||
|
j3Vector3DT u;
|
||||||
|
j3Vector3DT v;
|
||||||
|
j3Vector3DT normal;
|
||||||
|
|
||||||
|
// Compute length of the two co-planer edges of the polygon, since they will be used in the computation of the dot-product later
|
||||||
|
vertex0 = object->triangles[triangle].index[0];
|
||||||
|
vertex1 = object->triangles[triangle].index[1];
|
||||||
|
vertex2 = object->triangles[triangle].index[2];
|
||||||
|
|
||||||
|
j3MathMakeVector3D((j3VertexT *)&object->verticies[vertex0].local, (j3VertexT *)&object->verticies[vertex1].local, (j3Vector3DT *)&u);
|
||||||
|
j3MathMakeVector3D((j3VertexT *)&object->verticies[vertex0].local, (j3VertexT *)&object->verticies[vertex2].local, (j3Vector3DT *)&v);
|
||||||
|
j3MathCrossProduct3D((j3Vector3DT *)&v, (j3Vector3DT *)&u, (j3Vector3DT *)&normal);
|
||||||
|
|
||||||
|
// Compute magnitude of normal, take its inverse and multiply it by
|
||||||
|
// 15, this will change the shading calculation of 15*dp/normal into
|
||||||
|
// dp*normal_length, removing one division
|
||||||
|
object->triangles[triangle].normalLength = (float)15.0 / j3MathVectorMagnatude3D((j3Vector3DT *)&normal);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -973,15 +1024,9 @@ void _j3WorldFree(j3WorldT **world) {
|
||||||
|
|
||||||
|
|
||||||
bool _j3WorldLoad(j3WorldT **world, char *file) {
|
bool _j3WorldLoad(j3WorldT **world, char *file) {
|
||||||
jint16 x;
|
juint16 x;
|
||||||
jint16 y;
|
juint16 y;
|
||||||
jint16 z;
|
juint16 z;
|
||||||
juint16 vertex0;
|
|
||||||
juint16 vertex1;
|
|
||||||
juint16 vertex2;
|
|
||||||
j3Vector3DT u;
|
|
||||||
j3Vector3DT v;
|
|
||||||
j3Vector3DT normal;
|
|
||||||
byte buffer[4];
|
byte buffer[4];
|
||||||
FILE *in;
|
FILE *in;
|
||||||
bool failed = false;
|
bool failed = false;
|
||||||
|
@ -1070,21 +1115,16 @@ bool _j3WorldLoad(j3WorldT **world, char *file) {
|
||||||
}
|
}
|
||||||
if (failed) break;
|
if (failed) break;
|
||||||
|
|
||||||
// Compute length of the two co-planer edges of the polygon, since they will be used in the computation of the dot-product later
|
_j3ObjectUpdateNormalLength(&(*world)->objects[x], y);
|
||||||
vertex0 = (*world)->objects[x].triangles[y].index[0];
|
|
||||||
vertex1 = (*world)->objects[x].triangles[y].index[1];
|
|
||||||
vertex2 = (*world)->objects[x].triangles[y].index[2];
|
|
||||||
j3MathMakeVector3D((j3VertexT *)&(*world)->objects[x].verticies[vertex0].local, (j3VertexT *)&(*world)->objects[x].verticies[vertex1].local, (j3Vector3DT *)&u);
|
|
||||||
j3MathMakeVector3D((j3VertexT *)&(*world)->objects[x].verticies[vertex0].local, (j3VertexT *)&(*world)->objects[x].verticies[vertex2].local, (j3Vector3DT *)&v);
|
|
||||||
j3MathCrossProduct3D((j3Vector3DT *)&v, (j3Vector3DT *)&u, (j3Vector3DT *)&normal);
|
|
||||||
|
|
||||||
// Compute magnitude of normal, take its inverse and multiply it by
|
// Triangles begin life un-lit
|
||||||
// 15, this will change the shading calculation of 15*dp/normal into
|
(*world)->objects[x].triangles[y].lit = false;
|
||||||
// dp*normal_length, removing one division
|
|
||||||
(*world)->objects[x].triangles[y].normalLength = (float)15.0 / j3MathVectorMagnatude3D((j3Vector3DT *)&normal);
|
|
||||||
|
|
||||||
//***TODO*** All triangles are one-sided for now
|
//***TODO*** All triangles are one-sided for now
|
||||||
(*world)->objects[x].triangles[y].twoSided = false;
|
(*world)->objects[x].triangles[y].twoSided = false;
|
||||||
|
|
||||||
|
//***TODO*** All triangles are white for now
|
||||||
|
(*world)->objects[x].triangles[y].color = 15;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
22
j3d/j3d.h
22
j3d/j3d.h
|
@ -68,6 +68,9 @@ typedef struct {
|
||||||
j3VertexT position; // Position of object in world
|
j3VertexT position; // Position of object in world
|
||||||
j3VertexT rotation; // Rotation of object in world
|
j3VertexT rotation; // Rotation of object in world
|
||||||
j3VertexT scale; // Scale of object in world
|
j3VertexT scale; // Scale of object in world
|
||||||
|
bool positionDirty; // Did the position change?
|
||||||
|
bool rotationDirty; // Did the rotation change?
|
||||||
|
bool scaleDirty; // Did the scale change?
|
||||||
} j3ObjectT;
|
} j3ObjectT;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
|
@ -93,32 +96,38 @@ typedef struct {
|
||||||
#define j3ObjectMove(ob, px, py, pz) \
|
#define j3ObjectMove(ob, px, py, pz) \
|
||||||
ob.position.x += (px); \
|
ob.position.x += (px); \
|
||||||
ob.position.y += (py); \
|
ob.position.y += (py); \
|
||||||
ob.position.z += (pz)
|
ob.position.z += (pz); \
|
||||||
|
ob.positionDirty = true
|
||||||
|
|
||||||
#define j3ObjectMoveTo(ob, px, py, pz) \
|
#define j3ObjectMoveTo(ob, px, py, pz) \
|
||||||
ob.position.x = (px); \
|
ob.position.x = (px); \
|
||||||
ob.position.y = (py); \
|
ob.position.y = (py); \
|
||||||
ob.position.z = (pz)
|
ob.position.z = (pz); \
|
||||||
|
ob.positionDirty = true
|
||||||
|
|
||||||
#define j3ObjectRotate(ob, px, py, pz) \
|
#define j3ObjectRotate(ob, px, py, pz) \
|
||||||
ob.rotation.x += (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
ob.rotation.x += (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
||||||
ob.rotation.y += (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
ob.rotation.y += (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
||||||
ob.rotation.z += (pz); j3MathWrapBounds(ob.rotation.z, 0, 360)
|
ob.rotation.z += (pz); j3MathWrapBounds(ob.rotation.z, 0, 360); \
|
||||||
|
ob.rotationDirty = true
|
||||||
|
|
||||||
#define j3ObjectRotateTo(ob, px, py, pz) \
|
#define j3ObjectRotateTo(ob, px, py, pz) \
|
||||||
ob.rotation.x = (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
ob.rotation.x = (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \
|
||||||
ob.rotation.y = (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
ob.rotation.y = (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \
|
||||||
ob.rotation.z = (pz); j3MathWrapBounds(ob.rotation.z, 0, 360)
|
ob.rotation.z = (pz); j3MathWrapBounds(ob.rotation.z, 0, 360); \
|
||||||
|
ob.rotationDirty = true
|
||||||
|
|
||||||
#define j3ObjectScale(ob, px, py, pz) \
|
#define j3ObjectScale(ob, px, py, pz) \
|
||||||
ob.scale.x += (px); \
|
ob.scale.x += (px); \
|
||||||
ob.scale.y += (py); \
|
ob.scale.y += (py); \
|
||||||
ob.scale.z += (pz)
|
ob.scale.z += (pz); \
|
||||||
|
ob.scaleDirty = true
|
||||||
|
|
||||||
#define j3ObjectScaleTo(ob, px, py, pz) \
|
#define j3ObjectScaleTo(ob, px, py, pz) \
|
||||||
ob.scale.x = (px); \
|
ob.scale.x = (px); \
|
||||||
ob.scale.y = (py); \
|
ob.scale.y = (py); \
|
||||||
ob.scale.z = (pz)
|
ob.scale.z = (pz); \
|
||||||
|
ob.scaleDirty = true
|
||||||
|
|
||||||
|
|
||||||
// Syntatic sugar
|
// Syntatic sugar
|
||||||
|
@ -149,6 +158,7 @@ void _j3DrawTriangleTop(jint16 x1, jint16 y1, jint16 x2,jint16 y2, jint16 x3, ji
|
||||||
void _j3DrawWireframePair(j3ObjectT *o, juint16 v1, juint16 v2);
|
void _j3DrawWireframePair(j3ObjectT *o, juint16 v1, juint16 v2);
|
||||||
void _j3DrawWireframe(j3ObjectT *o);
|
void _j3DrawWireframe(j3ObjectT *o);
|
||||||
void _j3ObjectReset(j3ObjectT *o);
|
void _j3ObjectReset(j3ObjectT *o);
|
||||||
|
void _j3ObjectUpdateNormalLength(j3ObjectT *object, juint16 triangle);
|
||||||
void _j3ObjectUpdate(j3ObjectT *o);
|
void _j3ObjectUpdate(j3ObjectT *o);
|
||||||
bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2);
|
bool _j3UtilClipLine(jint16 *x1, jint16 *y1, jint16 *x2, jint16 *y2);
|
||||||
void _j3WorldFree(j3WorldT **world);
|
void _j3WorldFree(j3WorldT **world);
|
||||||
|
|
11
j3d/main.c
11
j3d/main.c
|
@ -87,7 +87,7 @@ int main(void) {
|
||||||
c = 1;
|
c = 1;
|
||||||
for (x=0; x<world->objectCount; x++) {
|
for (x=0; x<world->objectCount; x++) {
|
||||||
for (y=0; y<world->objects[x].triangleCount; y++) {
|
for (y=0; y<world->objects[x].triangleCount; y++) {
|
||||||
world->objects[x].triangles->color = c++;
|
world->objects[x].triangles[y].color = c++;
|
||||||
if (c > 15) {
|
if (c > 15) {
|
||||||
c = 1;
|
c = 1;
|
||||||
}
|
}
|
||||||
|
@ -137,8 +137,15 @@ int main(void) {
|
||||||
j3DrawSolid(world->objects[x]);
|
j3DrawSolid(world->objects[x]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c = 0;
|
||||||
|
for (x=0; x<world->objects[0].triangleCount; x++) {
|
||||||
|
if (world->objects[0].triangles[x].visible) {
|
||||||
|
c++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
printAt(font, 1, 1, "Verticies: %d", world->objects[0].vertexCount);
|
printAt(font, 1, 1, "Verticies: %d", world->objects[0].vertexCount);
|
||||||
printAt(font, 1, 2, "Triangles: %d", world->objects[0].triangleCount);
|
printAt(font, 1, 2, "Triangles: %d (%d visible) ", world->objects[0].triangleCount, c);
|
||||||
|
|
||||||
jlDisplayPresent();
|
jlDisplayPresent();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue