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

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,11 +578,6 @@ void _j3ObjectUpdate(j3ObjectT *o) {
// === ROTATION ===
if (o->rotationDirty) {
// Rotation being dirty means we also need to update position later
o->positionDirty = true;
x = (jint16)o->rotation.x;
y = (jint16)o->rotation.y;
z = (jint16)o->rotation.z;
@ -654,12 +649,12 @@ void _j3ObjectUpdate(j3ObjectT *o) {
final[0][0] = cos_table[z];
final[0][1] = sin_table[z];
final[1][0] = -cos_table[x]*sin_table[z];
final[1][1] = cos_table[x]*cos_table[z];
final[1][0] = -cos_table[x] * sin_table[z];
final[1][1] = cos_table[x] * cos_table[z];
final[1][2] = sin_table[x];
final[2][0] = sin_table[x]*sin_table[z];
final[2][1] = -sin_table[x]*cos_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];
for (i=0; i<o->vertexCount; i++) {
@ -707,7 +702,7 @@ void _j3ObjectUpdate(j3ObjectT *o) {
rotateZ[1][0] = -sin_table[z];
rotateZ[1][1] = cos_table[z];
j3MathMatrix4x4Mult(rotateX, rotateZ, temp);
j3MathMatrix4x4Mult(rotateX, rotateY, temp);
j3MathMatrix4x4Mult(temp, rotateZ, final);
for (i=0; i<o->vertexCount; i++) {
@ -720,22 +715,18 @@ void _j3ObjectUpdate(j3ObjectT *o) {
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;
}
}
// === 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);
@ -746,7 +737,7 @@ void _j3ObjectUpdate(j3ObjectT *o) {
translate[3][1] = -_j3VarCameraLocation.y;
translate[3][2] = -_j3VarCameraLocation.z;
// Z matrix
// X matrix
rotateX[1][1] = ( cos_table[_j3VarCameraAngle.x]);
rotateX[1][2] = -( sin_table[_j3VarCameraAngle.x]);
rotateX[2][1] = -(-sin_table[_j3VarCameraAngle.x]);
@ -770,23 +761,23 @@ void _j3ObjectUpdate(j3ObjectT *o) {
_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].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].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];
}
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();