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) { void j3MathCrossProduct3D(j3Vector3DT *u, j3Vector3DT *v, j3Vector3DT *normal) {
// Compute the cross product between two vectors // Compute the cross product between two vectors
normal->x = (u->y*v->z - u->z*v->y); normal->x = (u->y * v->z - u->z * v->y);
normal->y = -(u->x*v->z - u->z*v->x); normal->y = -(u->x * v->z - u->z * v->x);
normal->z = (u->x*v->y - u->y*v->x); normal->z = (u->x * v->y - u->y * v->x);
} }
@ -578,215 +578,206 @@ void _j3ObjectUpdate(j3ObjectT *o) {
// === ROTATION === // === 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 j3MathMatrix4x4Identity(final);
o->positionDirty = true;
x = (jint16)o->rotation.x; // What angles are we rotating on? By knowing this we can optimize some.
y = (jint16)o->rotation.y; if (x) axis += 4;
z = (jint16)o->rotation.z; 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. case 2: // Final matrix = y
if (x) axis += 4; final[0][0] = cos_table[y];
if (y) axis += 2; final[0][2] = -sin_table[y];
if (z) axis += 1; 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 3: // Final matrix = y * z
case 1: // Final matrix = z final[0][0] = cos_table[y] * cos_table[z];
final[0][0] = cos_table[z]; final[0][1] = cos_table[y] * sin_table[z];
final[0][1] = sin_table[z]; final[0][2] = -sin_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].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 2: // Final matrix = y final[1][0] = -sin_table[z];
final[0][0] = cos_table[y]; final[1][1] = cos_table[z];
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;
case 3: // Final matrix = y * z final[2][0] = sin_table[y] * cos_table[z];
final[0][0] = cos_table[y] * cos_table[z]; final[2][1] = sin_table[y] * sin_table[z];
final[0][1] = cos_table[y] * sin_table[z]; final[2][2] = cos_table[y];
final[0][2] = -sin_table[y];
final[1][0] = -sin_table[z]; for (i=0; i<o->vertexCount; i++) {
final[1][1] = cos_table[z]; 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]; case 4: // Final matrix = x
final[2][1] = sin_table[y] * sin_table[z]; final[1][1] = cos_table[x];
final[2][2] = cos_table[y]; 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++) { case 5: // Final matrix = x * z
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]; final[0][0] = cos_table[z];
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]; final[0][1] = sin_table[z];
o->verticies[i].world.z = o->verticies[i].local.x * final[0][2] + o->verticies[i].local.z * final[2][2];
}
break;
case 4: // Final matrix = x final[1][0] = -cos_table[x] * sin_table[z];
final[1][1] = cos_table[x]; final[1][1] = cos_table[x] * cos_table[z];
final[1][2] = sin_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;
case 5: // Final matrix = x * z final[2][0] = sin_table[x] * sin_table[z];
final[0][0] = cos_table[z]; final[2][1] = -sin_table[x] * cos_table[z];
final[0][1] = sin_table[z]; final[2][2] = cos_table[x];
final[1][0] = -cos_table[x]*sin_table[z]; for (i=0; i<o->vertexCount; i++) {
final[1][1] = cos_table[x]*cos_table[z]; 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];
final[1][2] = sin_table[x]; 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]; case 6: // Final matrix = x * y
final[2][1] = -sin_table[x]*cos_table[z]; final[0][0] = cos_table[y];
final[2][2] = cos_table[x]; final[0][2] = -sin_table[y];
for (i=0; i<o->vertexCount; i++) { final[1][0] = sin_table[x] * sin_table[y];
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]; final[1][1] = cos_table[x];
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]; final[1][2] = sin_table[x] * cos_table[y];
o->verticies[i].world.z = o->verticies[i].local.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
case 6: // Final matrix = x * y final[2][0] = cos_table[x] * sin_table[y];
final[0][0] = cos_table[y]; final[2][1] = -sin_table[x];
final[0][2] = -sin_table[y]; final[2][2] = cos_table[x] * cos_table[y];
final[1][0] = sin_table[x] * sin_table[y]; for (i=0; i<o->vertexCount; i++) {
final[1][1] = cos_table[x]; 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];
final[1][2] = sin_table[x] * cos_table[y]; 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]; case 7: // Final matrix = x * y * z
final[2][1] = -sin_table[x]; j3MathMatrix4x4Identity(rotateX);
final[2][2] = cos_table[x] * cos_table[y]; 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++) { j3MathMatrix4x4Identity(rotateY);
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]; rotateY[0][0] = cos_table[y];
o->verticies[i].world.y = o->verticies[i].local.y * final[1][1] + o->verticies[i].local.z * final[2][1]; rotateY[0][2] = -sin_table[y];
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]; rotateY[2][0] = sin_table[y];
} rotateY[2][2] = cos_table[y];
break;
case 7: // Final matrix = x * y * z j3MathMatrix4x4Identity(rotateZ);
j3MathMatrix4x4Identity(rotateX); rotateZ[0][0] = cos_table[z];
rotateX[1][1] = cos_table[x]; rotateZ[0][1] = sin_table[z];
rotateX[1][2] = sin_table[x]; rotateZ[1][0] = -sin_table[z];
rotateX[2][1] = -sin_table[x]; rotateZ[1][1] = cos_table[z];
rotateX[2][2] = cos_table[x];
j3MathMatrix4x4Identity(rotateY); j3MathMatrix4x4Mult(rotateX, rotateY, temp);
rotateY[0][0] = cos_table[y]; j3MathMatrix4x4Mult(temp, rotateZ, final);
rotateY[0][2] = -sin_table[y];
rotateY[2][0] = sin_table[y];
rotateY[2][2] = cos_table[y];
j3MathMatrix4x4Identity(rotateZ); for (i=0; i<o->vertexCount; i++) {
rotateZ[0][0] = cos_table[z]; 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];
rotateZ[0][1] = sin_table[z]; 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];
rotateZ[1][0] = -sin_table[z]; 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];
rotateZ[1][1] = cos_table[z]; }
break;
j3MathMatrix4x4Mult(rotateX, rotateZ, temp); default:
j3MathMatrix4x4Mult(temp, rotateZ, final); 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.y * final[1][2] + o->verticies[i].local.z * final[2][2];
}
break;
default:
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 when we add multiple object stuff and re-do this function //***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
// Create the global inverse transformation matrix used to transform world coordinate to camera coordinates j3MathMatrix4x4Identity(translate);
j3MathMatrix4x4Identity(translate); j3MathMatrix4x4Identity(rotateX);
j3MathMatrix4x4Identity(rotateX); j3MathMatrix4x4Identity(rotateY);
j3MathMatrix4x4Identity(rotateY); j3MathMatrix4x4Identity(rotateZ);
j3MathMatrix4x4Identity(rotateZ);
translate[3][0] = -_j3VarCameraLocation.x; translate[3][0] = -_j3VarCameraLocation.x;
translate[3][1] = -_j3VarCameraLocation.y; translate[3][1] = -_j3VarCameraLocation.y;
translate[3][2] = -_j3VarCameraLocation.z; translate[3][2] = -_j3VarCameraLocation.z;
// Z matrix // X matrix
rotateX[1][1] = ( cos_table[_j3VarCameraAngle.x]); rotateX[1][1] = ( cos_table[_j3VarCameraAngle.x]);
rotateX[1][2] = -( sin_table[_j3VarCameraAngle.x]); rotateX[1][2] = -( sin_table[_j3VarCameraAngle.x]);
rotateX[2][1] = -(-sin_table[_j3VarCameraAngle.x]); rotateX[2][1] = -(-sin_table[_j3VarCameraAngle.x]);
rotateX[2][2] = ( cos_table[_j3VarCameraAngle.x]); rotateX[2][2] = ( cos_table[_j3VarCameraAngle.x]);
// Y matrix // Y matrix
rotateY[0][0] = ( cos_table[_j3VarCameraAngle.y]); rotateY[0][0] = ( cos_table[_j3VarCameraAngle.y]);
rotateY[0][2] = -(-sin_table[_j3VarCameraAngle.y]); rotateY[0][2] = -(-sin_table[_j3VarCameraAngle.y]);
rotateY[2][0] = -( sin_table[_j3VarCameraAngle.y]); rotateY[2][0] = -( sin_table[_j3VarCameraAngle.y]);
rotateY[2][2] = ( cos_table[_j3VarCameraAngle.y]); rotateY[2][2] = ( cos_table[_j3VarCameraAngle.y]);
// Z matrix // Z matrix
rotateZ[0][0] = ( cos_table[_j3VarCameraAngle.z]); rotateZ[0][0] = ( cos_table[_j3VarCameraAngle.z]);
rotateZ[0][1] = -( sin_table[_j3VarCameraAngle.z]); rotateZ[0][1] = -( sin_table[_j3VarCameraAngle.z]);
rotateZ[1][0] = -(-sin_table[_j3VarCameraAngle.z]); rotateZ[1][0] = -(-sin_table[_j3VarCameraAngle.z]);
rotateZ[1][1] = ( cos_table[_j3VarCameraAngle.z]); rotateZ[1][1] = ( cos_table[_j3VarCameraAngle.z]);
j3MathMatrix4x4Mult(translate, rotateX, result1); j3MathMatrix4x4Mult(translate, rotateX, result1);
j3MathMatrix4x4Mult(result1, rotateY, result2); j3MathMatrix4x4Mult(result1, rotateY, result2);
j3MathMatrix4x4Mult(result2, rotateZ, _j3VarCameraMatrix); j3MathMatrix4x4Mult(result2, rotateZ, _j3VarCameraMatrix);
_j3VarCameraLocationDirty = false; _j3VarCameraLocationDirty = false;
_j3VarCameraAngleDirty = false; _j3VarCameraAngleDirty = false;
}
if (o->positionDirty || o->rotationDirty) { for (i=0; i<o->vertexCount; i++) {
for (i=0; i<o->vertexCount; i++) { o->verticies[i].camera.x =
o->verticies[i].camera.x = o->verticies[i].world.x * _j3VarCameraMatrix[0][0] +
o->verticies[i].world.x * _j3VarCameraMatrix[0][0] + o->verticies[i].world.y * _j3VarCameraMatrix[1][0] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][0] + o->verticies[i].world.z * _j3VarCameraMatrix[2][0] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][0] + _j3VarCameraMatrix[3][0]; _j3VarCameraMatrix[3][0];
o->verticies[i].camera.y = o->verticies[i].camera.y =
o->verticies[i].world.x * _j3VarCameraMatrix[0][1] + o->verticies[i].world.x * _j3VarCameraMatrix[0][1] +
o->verticies[i].world.y * _j3VarCameraMatrix[1][1] + o->verticies[i].world.y * _j3VarCameraMatrix[1][1] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][1] + _j3VarCameraMatrix[3][1]; o->verticies[i].world.z * _j3VarCameraMatrix[2][1] +
o->verticies[i].camera.z = _j3VarCameraMatrix[3][1];
o->verticies[i].world.x * _j3VarCameraMatrix[0][2] + o->verticies[i].camera.z =
o->verticies[i].world.y * _j3VarCameraMatrix[1][2] + o->verticies[i].world.x * _j3VarCameraMatrix[0][2] +
o->verticies[i].world.z * _j3VarCameraMatrix[2][2] + _j3VarCameraMatrix[3][2]; o->verticies[i].world.y * _j3VarCameraMatrix[1][2] +
} o->verticies[i].world.z * _j3VarCameraMatrix[2][2] +
_j3VarCameraMatrix[3][2];
} }
// === REMOVE BACKFACES & LIGHT === // === REMOVE BACKFACES & LIGHT ===
@ -889,7 +880,8 @@ void _j3ObjectUpdate(j3ObjectT *o) {
} }
// Did this triangle survive culling? // 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 // Find average z depth in camera space
vertex0 = o->triangles[i].index[0]; vertex0 = o->triangles[i].index[0];
vertex1 = o->triangles[i].index[1]; 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 // 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 = (float)15.0 / j3MathVectorMagnatude3D((j3Vector3DT *)&normal);
} }

View file

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