|
@@ -47,21 +47,33 @@ bool doZBuffer = true;
|
|
|
bool doClipping = true;
|
|
|
bool doPerspectiveCorrection = true;
|
|
|
|
|
|
-static mesh_t mesh;
|
|
|
+static mesh_t worldMesh[10];
|
|
|
+static int meshCount = 0;
|
|
|
static int windowHeight;
|
|
|
static int windowWidth;
|
|
|
static camera_t camera;
|
|
|
static light_t globalLight;
|
|
|
|
|
|
-static enum displayMode_t currentDisplayMode = DISPLAY_MODE_WIREFRAME;
|
|
|
+static enum displayMode_t currentDisplayMode = DISPLAY_MODE_TEXTURED;
|
|
|
static matrix4_t projectionMatrix;
|
|
|
static double updateTime, renderTime, waitTime;
|
|
|
static int trianglesCount, trianglesTotal;
|
|
|
static triangle_t *projectedTriangles = NULL;
|
|
|
static const vec3_t origin = {0, 0, 0 };
|
|
|
|
|
|
+void loadNewMesh(const char *obj, const char *tex)
|
|
|
+{
|
|
|
+ /* Load 3D model */
|
|
|
+ loadOBJFile(&worldMesh[meshCount],obj);
|
|
|
+ /* Load texture */
|
|
|
+ loadTextureDateFromPng(&worldMesh[meshCount].texture, tex);
|
|
|
+
|
|
|
+ meshCount++;
|
|
|
+}
|
|
|
+
|
|
|
void setup()
|
|
|
{
|
|
|
+ int i;
|
|
|
double FOV = 60;
|
|
|
double aspectRatioY = (double)windowHeight / windowWidth;
|
|
|
double aspectRatioX = (double)windowWidth / windowHeight;
|
|
@@ -75,17 +87,27 @@ void setup()
|
|
|
createLight(&globalLight, vec3(0, 0, 1));
|
|
|
|
|
|
/* Load object */
|
|
|
- loadOBJFile(&mesh,"assets/dragon.obj");
|
|
|
- //loadCubeMeshData(&mesh);
|
|
|
-
|
|
|
- /* Load texture */
|
|
|
- loadTextureDateFromPng(&mesh.texture, "assets/f117.png");
|
|
|
-
|
|
|
- trianglesTotal = arrayGetSize(mesh.faces);
|
|
|
+ loadNewMesh("assets/f22.obj", "assets/f22.png");
|
|
|
+ worldMesh[0].translation.z = 5;
|
|
|
+ worldMesh[0].translation.x = -5;
|
|
|
+ worldMesh[0].rotation.y = 0.607;
|
|
|
+ worldMesh[0].rotation.x = -0.3;
|
|
|
+
|
|
|
+ loadNewMesh("assets/f117.obj", "assets/f117.png");
|
|
|
+ worldMesh[1].translation.z = 5;
|
|
|
+ worldMesh[0].translation.x = 5;
|
|
|
+ worldMesh[1].rotation.y = 0.607;
|
|
|
+ worldMesh[1].rotation.x = -0.3;
|
|
|
+
|
|
|
+ trianglesTotal = 0;
|
|
|
+ for(i = 0; i < meshCount; i++)
|
|
|
+ {
|
|
|
+ trianglesTotal += arrayGetSize(worldMesh[i].faces);
|
|
|
+ }
|
|
|
|
|
|
createCamera(&camera);
|
|
|
cameraSetTarget(&camera, vec3(0, 0, 1));
|
|
|
- cameraSetUp(&camera, vec3(0, 1, 1));
|
|
|
+ cameraSetUp(&camera, vec3(0, 1, 0));
|
|
|
}
|
|
|
|
|
|
void processInput()
|
|
@@ -147,8 +169,8 @@ void processInput()
|
|
|
case SDLK_a: cameraRotateBy(&camera, -1.0 * deltaTime, 0); break;
|
|
|
case SDLK_q: cameraRotateBy(&camera, 0, -1.0 * deltaTime); break;
|
|
|
case SDLK_e: cameraRotateBy(&camera, 0, 1.0 * deltaTime); break;
|
|
|
- case SDLK_UP: cameraMoveAbsolute(&camera, 0, 3.0 * deltaTime, 0); break;
|
|
|
- case SDLK_DOWN: cameraMoveAbsolute(&camera, 0, -3.0 * deltaTime, 0); break;
|
|
|
+ case SDLK_UP: cameraMoveRelative(&camera, 0, 3.0 * deltaTime, 0); break;
|
|
|
+ case SDLK_DOWN: cameraMoveRelative(&camera, 0, -3.0 * deltaTime, 0); break;
|
|
|
}
|
|
|
break;
|
|
|
|
|
@@ -206,40 +228,23 @@ static void projectTriangle(triangle_t *workTriangle, vec3_t *triangleNormal, te
|
|
|
arrayAdd(projectedTriangles, currentTriangle);
|
|
|
}
|
|
|
|
|
|
-void update()
|
|
|
+static void processMesh(mesh_t *mesh, matrix4_t *worldMatrix)
|
|
|
{
|
|
|
uint32_t i, j;
|
|
|
uint32_t faceCount;
|
|
|
uint32_t numberOfClippedTriangles;
|
|
|
- uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
|
|
|
- matrix4_t worldMatrix;
|
|
|
- uint32_t ticks = getMicroSecTime();
|
|
|
triangle_t trianglesAfterClipping[MAX_NUM_POLYGON_TRIANGLES];
|
|
|
+ faceCount = arrayGetSize(mesh->faces);
|
|
|
+ matrix4_t transformMatrix = mat4Identity;
|
|
|
|
|
|
- if (timeToWait <= FRAME_TARGET_TIME)
|
|
|
- {
|
|
|
- SDL_Delay(timeToWait);
|
|
|
- }
|
|
|
- deltaTime = (SDL_GetTicks() - previousFrameTime) / 1000.0;
|
|
|
- previousFrameTime = SDL_GetTicks();
|
|
|
-
|
|
|
- waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
|
|
|
- ticks = getMicroSecTime();
|
|
|
-
|
|
|
- mesh.translation.z = 5;
|
|
|
|
|
|
- worldMatrix = mat4Identity;
|
|
|
- worldMatrix = mat4ProdMat4(mat4Scale(mesh.scale.x, mesh.scale.y, mesh.scale.z), worldMatrix);
|
|
|
- worldMatrix = mat4ProdMat4(mat4RotationZ(mesh.rotation.z), worldMatrix);
|
|
|
- worldMatrix = mat4ProdMat4(mat4RotationY(mesh.rotation.y), worldMatrix);
|
|
|
- worldMatrix = mat4ProdMat4(mat4RotationX(mesh.rotation.x), worldMatrix);
|
|
|
- worldMatrix = mat4ProdMat4(mat4Translate(mesh.translation.x, mesh.translation.y, mesh.translation.z), worldMatrix);
|
|
|
- /* Last apply the view matrix */
|
|
|
- worldMatrix = mat4ProdMat4(cameraGetViewMatrix(&camera), worldMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(mat4Scale(mesh->scale.x, mesh->scale.y, mesh->scale.z), transformMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(mat4RotationZ(mesh->rotation.z), transformMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(mat4RotationY(mesh->rotation.y), transformMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(mat4RotationX(mesh->rotation.x), transformMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(mat4Translate(mesh->translation.x, mesh->translation.y, mesh->translation.z), transformMatrix);
|
|
|
+ transformMatrix = mat4ProdMat4(*worldMatrix, transformMatrix);
|
|
|
|
|
|
- arrayEmpty(projectedTriangles);
|
|
|
-
|
|
|
- faceCount = arrayGetSize(mesh.faces);
|
|
|
for(i = 0; i < faceCount; i++)
|
|
|
{
|
|
|
polygon_t polygon;
|
|
@@ -248,15 +253,15 @@ void update()
|
|
|
|
|
|
vec3_t vertices[3] =
|
|
|
{
|
|
|
- mesh.vertices[mesh.faces[i].a],
|
|
|
- mesh.vertices[mesh.faces[i].b],
|
|
|
- mesh.vertices[mesh.faces[i].c],
|
|
|
+ mesh->vertices[mesh->faces[i].a],
|
|
|
+ mesh->vertices[mesh->faces[i].b],
|
|
|
+ mesh->vertices[mesh->faces[i].c],
|
|
|
};
|
|
|
|
|
|
for(j = 0; j < 3; j++)
|
|
|
{
|
|
|
transformedVertices[j] = vec4FromVec3(vertices[j]);
|
|
|
- transformedVertices[j] = mat4ProdVec4(worldMatrix, transformedVertices[j]);
|
|
|
+ transformedVertices[j] = mat4ProdVec4(transformMatrix, transformedVertices[j]);
|
|
|
}
|
|
|
|
|
|
vec3_t vectorBA = vec3SubVectors(vec3FromVec4(transformedVertices[1]), vec3FromVec4(transformedVertices[0]));
|
|
@@ -284,15 +289,15 @@ void update()
|
|
|
polygon = createPolygonFromTriangle(vec3FromVec4(transformedVertices[0]),
|
|
|
vec3FromVec4(transformedVertices[1]),
|
|
|
vec3FromVec4(transformedVertices[2]),
|
|
|
- mesh.faces[i].a_uv,
|
|
|
- mesh.faces[i].b_uv,
|
|
|
- mesh.faces[i].c_uv);
|
|
|
+ mesh->faces[i].a_uv,
|
|
|
+ mesh->faces[i].b_uv,
|
|
|
+ mesh->faces[i].c_uv);
|
|
|
|
|
|
clipPolygon(&polygon);
|
|
|
createTriangleFromPolygon(&polygon, trianglesAfterClipping, &numberOfClippedTriangles);
|
|
|
for(j = 0; j < numberOfClippedTriangles; j++)
|
|
|
{
|
|
|
- projectTriangle(&trianglesAfterClipping[j], &triangleNormal, &mesh.texture, mesh.faces[i].colour);
|
|
|
+ projectTriangle(&trianglesAfterClipping[j], &triangleNormal, &mesh->texture, mesh->faces[i].colour);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
@@ -307,15 +312,43 @@ void update()
|
|
|
},
|
|
|
.textureCoordinates =
|
|
|
{
|
|
|
- [0] = { mesh.faces[i].a_uv.u, mesh.faces[i].a_uv.v },
|
|
|
- [1] = { mesh.faces[i].b_uv.u, mesh.faces[i].b_uv.v },
|
|
|
- [2] = { mesh.faces[i].c_uv.u, mesh.faces[i].c_uv.v }
|
|
|
+ [0] = { mesh->faces[i].a_uv.u, mesh->faces[i].a_uv.v },
|
|
|
+ [1] = { mesh->faces[i].b_uv.u, mesh->faces[i].b_uv.v },
|
|
|
+ [2] = { mesh->faces[i].c_uv.u, mesh->faces[i].c_uv.v }
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- projectTriangle(&workTriangle, &triangleNormal, &mesh.texture, mesh.faces[i].colour);
|
|
|
+ projectTriangle(&workTriangle, &triangleNormal, &mesh->texture, mesh->faces[i].colour);
|
|
|
};
|
|
|
}
|
|
|
+}
|
|
|
+
|
|
|
+void update()
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
|
|
|
+ matrix4_t worldMatrix;
|
|
|
+ uint32_t ticks = getMicroSecTime();
|
|
|
+
|
|
|
+
|
|
|
+ if (timeToWait <= FRAME_TARGET_TIME)
|
|
|
+ {
|
|
|
+ SDL_Delay(timeToWait);
|
|
|
+ }
|
|
|
+ deltaTime = (SDL_GetTicks() - previousFrameTime) / 1000.0;
|
|
|
+ previousFrameTime = SDL_GetTicks();
|
|
|
+
|
|
|
+ waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
|
|
|
+ ticks = getMicroSecTime();
|
|
|
+
|
|
|
+ worldMatrix = cameraGetViewMatrix(&camera);
|
|
|
+
|
|
|
+ arrayEmpty(projectedTriangles);
|
|
|
+
|
|
|
+ for(i = 0; i < meshCount; i++)
|
|
|
+ {
|
|
|
+ processMesh(&worldMesh[i], &worldMatrix);
|
|
|
+ }
|
|
|
|
|
|
if (!doZBuffer)
|
|
|
{
|
|
@@ -401,7 +434,7 @@ void render()
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
{
|
|
|
- int param_i;
|
|
|
+ int param_i, i;
|
|
|
bool fullScreen = false;
|
|
|
MAX_DEBUG_LEVEL = TLOG_DEBUG;
|
|
|
|
|
@@ -438,8 +471,12 @@ no_more_params:
|
|
|
}
|
|
|
|
|
|
arrayFree(projectedTriangles);
|
|
|
- textureCleanup(&mesh.texture);
|
|
|
- meshFree(&mesh);
|
|
|
+
|
|
|
+ for(i = 0; i < meshCount; i++)
|
|
|
+ {
|
|
|
+ textureCleanup(&worldMesh[i].texture);
|
|
|
+ meshFree(&worldMesh[i]);
|
|
|
+ }
|
|
|
destroyWindow();
|
|
|
|
|
|
return 0;
|