Few bug fixes and we're working\!

This commit is contained in:
Scott Duensing 2019-09-07 21:19:56 -05:00
parent b9557cd3a3
commit 081e669a8a
2 changed files with 167 additions and 177 deletions

342
j3d/j3d.c
View file

@ -476,9 +476,9 @@ void j3DrawWorld(j3WorldT *w) {
void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal) {
// Compute the cross product between two vectors
normal->x = (u->y*v->z - u->z*v->y);
normal->y = -(u->x*v->z - u->z*v->x);
normal->z = (u->x*v->y - u->y*v->x);
normal->x = (u->y * v->z - u->z * v->y);
normal->y = -(u->x * v->z - u->z * v->x);
normal->z = (u->x * v->y - u->y * v->x);
}
@ -578,215 +578,206 @@ void _j3ObjectUpdate(j3ObjectT *o) {
// === ROTATION ===
if (o->rotationDirty) {
x = (jint16)o->rotation.x;
y = (jint16)o->rotation.y;
z = (jint16)o->rotation.z;
// Rotation being dirty means we also need to update position later
o->positionDirty = true;
j3MathMatrix4x4Identity(final);
x = (jint16)o->rotation.x;
y = (jint16)o->rotation.y;
z = (jint16)o->rotation.z;
// What angles are we rotating on? By knowing this we can optimize some.
if (x) axis += 4;
if (y) axis += 2;
if (z) axis += 1;
j3MathMatrix4x4Identity(final);
switch (axis) {
case 1: // Final matrix = z
final[0][0] = cos_table[z];
final[0][1] = sin_table[z];
final[1][0] = -sin_table[z];
final[1][1] = cos_table[z];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1];
o->verticies[i].world.z = o->verticies[i].local.z;
}
break;
// What angles are we rotating on? By knowing this we can optimize some.
if (x) axis += 4;
if (y) axis += 2;
if (z) axis += 1;
case 2: // Final matrix = y
final[0][0] = cos_table[y];
final[0][2] = -sin_table[y];
final[2][0] = sin_table[y];
final[2][2] = cos_table[y];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.y;
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.z * final[2][2];
}
break;
switch (axis) {
case 1: // Final matrix = z
final[0][0] = cos_table[z];
final[0][1] = sin_table[z];
final[1][0] = -sin_table[z];
final[1][1] = cos_table[z];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1];
o->verticies[i].world.z = o->verticies[i].local.z;
}
break;
case 3: // Final matrix = y * z
final[0][0] = cos_table[y] * cos_table[z];
final[0][1] = cos_table[y] * sin_table[z];
final[0][2] = -sin_table[y];
case 2: // Final matrix = y
final[0][0] = cos_table[y];
final[0][2] = -sin_table[y];
final[2][0] = sin_table[y];
final[2][2] = cos_table[y];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.y;
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[1][0] = -sin_table[z];
final[1][1] = cos_table[z];
case 3: // Final matrix = y * z
final[0][0] = cos_table[y] * cos_table[z];
final[0][1] = cos_table[y] * sin_table[z];
final[0][2] = -sin_table[y];
final[2][0] = sin_table[y] * cos_table[z];
final[2][1] = sin_table[y] * sin_table[z];
final[2][2] = cos_table[y];
final[1][0] = -sin_table[z];
final[1][1] = cos_table[z];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[2][0] = sin_table[y] * cos_table[z];
final[2][1] = sin_table[y] * sin_table[z];
final[2][2] = cos_table[y];
case 4: // Final matrix = x
final[1][1] = cos_table[x];
final[1][2] = sin_table[x];
final[2][1] = -sin_table[x];
final[2][2] = cos_table[x];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x;
o->verticies[i].world.y = o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.z * final[2][2];
}
break;
case 5: // Final matrix = x * z
final[0][0] = cos_table[z];
final[0][1] = sin_table[z];
case 4: // Final matrix = x
final[1][1] = cos_table[x];
final[1][2] = sin_table[x];
final[2][1] = -sin_table[x];
final[2][2] = cos_table[x];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x;
o->verticies[i].world.y = o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[1][0] = -cos_table[x] * sin_table[z];
final[1][1] = cos_table[x] * cos_table[z];
final[1][2] = sin_table[x];
case 5: // Final matrix = x * z
final[0][0] = cos_table[z];
final[0][1] = sin_table[z];
final[2][0] = sin_table[x] * sin_table[z];
final[2][1] = -sin_table[x] * cos_table[z];
final[2][2] = cos_table[x];
final[1][0] = -cos_table[x]*sin_table[z];
final[1][1] = cos_table[x]*cos_table[z];
final[1][2] = sin_table[x];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[2][0] = sin_table[x]*sin_table[z];
final[2][1] = -sin_table[x]*cos_table[z];
final[2][2] = cos_table[x];
case 6: // Final matrix = x * y
final[0][0] = cos_table[y];
final[0][2] = -sin_table[y];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[1][0] = sin_table[x] * sin_table[y];
final[1][1] = cos_table[x];
final[1][2] = sin_table[x] * cos_table[y];
case 6: // Final matrix = x * y
final[0][0] = cos_table[y];
final[0][2] = -sin_table[y];
final[2][0] = cos_table[x] * sin_table[y];
final[2][1] = -sin_table[x];
final[2][2] = cos_table[x] * cos_table[y];
final[1][0] = sin_table[x] * sin_table[y];
final[1][1] = cos_table[x];
final[1][2] = sin_table[x] * cos_table[y];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
final[2][0] = cos_table[x] * sin_table[y];
final[2][1] = -sin_table[x];
final[2][2] = cos_table[x] * cos_table[y];
case 7: // Final matrix = x * y * z
j3MathMatrix4x4Identity(rotateX);
rotateX[1][1] = cos_table[x];
rotateX[1][2] = sin_table[x];
rotateX[2][1] = -sin_table[x];
rotateX[2][2] = cos_table[x];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
j3MathMatrix4x4Identity(rotateY);
rotateY[0][0] = cos_table[y];
rotateY[0][2] = -sin_table[y];
rotateY[2][0] = sin_table[y];
rotateY[2][2] = cos_table[y];
case 7: // Final matrix = x * y * z
j3MathMatrix4x4Identity(rotateX);
rotateX[1][1] = cos_table[x];
rotateX[1][2] = sin_table[x];
rotateX[2][1] = -sin_table[x];
rotateX[2][2] = cos_table[x];
j3MathMatrix4x4Identity(rotateZ);
rotateZ[0][0] = cos_table[z];
rotateZ[0][1] = sin_table[z];
rotateZ[1][0] = -sin_table[z];
rotateZ[1][1] = cos_table[z];
j3MathMatrix4x4Identity(rotateY);
rotateY[0][0] = cos_table[y];
rotateY[0][2] = -sin_table[y];
rotateY[2][0] = sin_table[y];
rotateY[2][2] = cos_table[y];
j3MathMatrix4x4Mult(rotateX, rotateY, temp);
j3MathMatrix4x4Mult(temp, rotateZ, final);
j3MathMatrix4x4Identity(rotateZ);
rotateZ[0][0] = cos_table[z];
rotateZ[0][1] = sin_table[z];
rotateZ[1][0] = -sin_table[z];
rotateZ[1][1] = cos_table[z];
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
j3MathMatrix4x4Mult(rotateX, rotateZ, temp);
j3MathMatrix4x4Mult(temp, rotateZ, final);
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].world.x = o->verticies[i].local.x * final[0][0] + o->verticies[i].local.y * final[1][0] + o->verticies[i].local.z * final[2][0];
o->verticies[i].world.y = o->verticies[i].local.x * final[0][1] + o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
default:
break;
}
default:
break;
}
// === SCALE & TRANSLATION ===
if (o->positionDirty || o->scaleDirty) {
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.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;
}
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.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;
}
// === CAMERA SPACE ===
//***TODO*** Move this when we add multiple object stuff and re-do this function
if (_j3VarCameraLocationDirty || _j3VarCameraAngleDirty) {
// Create the global inverse transformation matrix used to transform world coordinate to camera coordinates
j3MathMatrix4x4Identity(translate);
j3MathMatrix4x4Identity(rotateX);
j3MathMatrix4x4Identity(rotateY);
j3MathMatrix4x4Identity(rotateZ);
// Create the global inverse transformation matrix used to transform world coordinate to camera coordinates
j3MathMatrix4x4Identity(translate);
j3MathMatrix4x4Identity(rotateX);
j3MathMatrix4x4Identity(rotateY);
j3MathMatrix4x4Identity(rotateZ);
translate[3][0] = -_j3VarCameraLocation.x;
translate[3][1] = -_j3VarCameraLocation.y;
translate[3][2] = -_j3VarCameraLocation.z;
translate[3][0] = -_j3VarCameraLocation.x;
translate[3][1] = -_j3VarCameraLocation.y;
translate[3][2] = -_j3VarCameraLocation.z;
// Z matrix
rotateX[1][1] = ( cos_table[_j3VarCameraAngle.x]);
rotateX[1][2] = -( sin_table[_j3VarCameraAngle.x]);
rotateX[2][1] = -(-sin_table[_j3VarCameraAngle.x]);
rotateX[2][2] = ( cos_table[_j3VarCameraAngle.x]);
// X matrix
rotateX[1][1] = ( cos_table[_j3VarCameraAngle.x]);
rotateX[1][2] = -( sin_table[_j3VarCameraAngle.x]);
rotateX[2][1] = -(-sin_table[_j3VarCameraAngle.x]);
rotateX[2][2] = ( cos_table[_j3VarCameraAngle.x]);
// Y matrix
rotateY[0][0] = ( cos_table[_j3VarCameraAngle.y]);
rotateY[0][2] = -(-sin_table[_j3VarCameraAngle.y]);
rotateY[2][0] = -( sin_table[_j3VarCameraAngle.y]);
rotateY[2][2] = ( cos_table[_j3VarCameraAngle.y]);
// Y matrix
rotateY[0][0] = ( cos_table[_j3VarCameraAngle.y]);
rotateY[0][2] = -(-sin_table[_j3VarCameraAngle.y]);
rotateY[2][0] = -( sin_table[_j3VarCameraAngle.y]);
rotateY[2][2] = ( cos_table[_j3VarCameraAngle.y]);
// Z matrix
rotateZ[0][0] = ( cos_table[_j3VarCameraAngle.z]);
rotateZ[0][1] = -( sin_table[_j3VarCameraAngle.z]);
rotateZ[1][0] = -(-sin_table[_j3VarCameraAngle.z]);
rotateZ[1][1] = ( cos_table[_j3VarCameraAngle.z]);
// Z matrix
rotateZ[0][0] = ( cos_table[_j3VarCameraAngle.z]);
rotateZ[0][1] = -( sin_table[_j3VarCameraAngle.z]);
rotateZ[1][0] = -(-sin_table[_j3VarCameraAngle.z]);
rotateZ[1][1] = ( cos_table[_j3VarCameraAngle.z]);
j3MathMatrix4x4Mult(translate, rotateX, result1);
j3MathMatrix4x4Mult(result1, rotateY, result2);
j3MathMatrix4x4Mult(result2, rotateZ, _j3VarCameraMatrix);
j3MathMatrix4x4Mult(translate, rotateX, result1);
j3MathMatrix4x4Mult(result1, rotateY, result2);
j3MathMatrix4x4Mult(result2, rotateZ, _j3VarCameraMatrix);
_j3VarCameraLocationDirty = false;
_j3VarCameraAngleDirty = false;
}
_j3VarCameraLocationDirty = false;
_j3VarCameraAngleDirty = false;
if (o->positionDirty || o->rotationDirty) {
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].camera.x =
o->verticies[i].world.x * _j3VarCameraMatrix[0][0] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][0] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][0] + _j3VarCameraMatrix[3][0];
o->verticies[i].camera.y =
o->verticies[i].world.x * _j3VarCameraMatrix[0][1] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][1] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][1] + _j3VarCameraMatrix[3][1];
o->verticies[i].camera.z =
o->verticies[i].world.x * _j3VarCameraMatrix[0][2] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][2] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][2] + _j3VarCameraMatrix[3][2];
}
for (i=0; i<o->vertexCount; i++) {
o->verticies[i].camera.x =
o->verticies[i].world.x * _j3VarCameraMatrix[0][0] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][0] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][0] +
_j3VarCameraMatrix[3][0];
o->verticies[i].camera.y =
o->verticies[i].world.x * _j3VarCameraMatrix[0][1] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][1] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][1] +
_j3VarCameraMatrix[3][1];
o->verticies[i].camera.z =
o->verticies[i].world.x * _j3VarCameraMatrix[0][2] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][2] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][2] +
_j3VarCameraMatrix[3][2];
}
// === REMOVE BACKFACES & LIGHT ===
@ -889,7 +880,8 @@ void _j3ObjectUpdate(j3ObjectT *o) {
}
// Did this triangle survive culling?
if (o->triangles[i].visible) {
//***TODO*** Our visible flag is backwards for some reason. Not sure why yet.
if (!o->triangles[i].visible) {
// Find average z depth in camera space
vertex0 = o->triangles[i].index[0];
vertex1 = o->triangles[i].index[1];
@ -933,8 +925,6 @@ void _j3ObjectUpdateNormalLength(j3ObjectT *object, juint16 triangle) {
// 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);
}

View file

@ -74,7 +74,7 @@ int main(void) {
printAt(font, 1, 1, "Loading object... ");
jlDisplayPresent();
r = j3WorldLoad(world, "pyramid");
r = j3WorldLoad(world, "cube");
if (!r) {
printAt(font, 1, 1, "Object loading: Failed.");
jlDisplayPresent();