123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323 |
- /*
- * 3D Engine
- * main.c:
- * Based on pikuma.com 3D software renderer in C
- * Copyright (c) 2021 986-Studio. All rights reserved.
- *
- * Created by Manoël Trapier on 01/03/2021.
- */
- #include <stdio.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <SDL.h>
- #include <log.h>
- #include <array.h>
- #include <matrix.h>
- #include <triangle.h>
- #include <display.h>
- #include <vector.h>
- #include <mesh.h>
- bool isRunning = true;
- int previousFrameTime = 0;
- typedef enum displayMode_t
- {
- DISPLAY_MODE_WIREFRAME,
- DISPLAY_MODE_SOLID,
- DISPLAY_MODE_BOTH,
- } displayMode_t;
- bool displayVertex = false;
- bool doNotCull = false;
- enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
- matrix4_t projectionMatrix;
- vec3_t cameraPosition =
- {
- .x = 0,
- .y = 0,
- .z = 0,
- };
- triangle_t *projectedTriangles = NULL;
- void setup()
- {
- frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
- if (!frameBuffer)
- {
- Log(TLOG_PANIC, NULL, "Memory allocation error.");
- goto exit;
- }
- frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, windowWidth,
- windowHeight);
- if (frameBufferTexture == NULL)
- {
- Log(TLOG_PANIC, NULL, "SDL Texture creation error: %s", SDL_GetError());
- goto exit;
- }
- projectionMatrix = mat4Perspective((double)windowHeight / windowWidth, 90, 0.1, 100);
- //loadOBJFile("assets/cube.obj");
- loadCubeMeshData();
- exit:
- return;
- }
- void processInput()
- {
- SDL_Event event;
- SDL_PollEvent(&event);
- switch (event.type)
- {
- case SDL_QUIT:isRunning = false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- isRunning = false;
- break;
- case SDLK_1:
- displayVertex = true;
- currentDisplayMode = DISPLAY_MODE_WIREFRAME;
- break;
- case SDLK_2:
- displayVertex = false;
- currentDisplayMode = DISPLAY_MODE_WIREFRAME;
- break;
- case SDLK_3:
- displayVertex = false;
- currentDisplayMode = DISPLAY_MODE_SOLID;
- break;
- case SDLK_4:
- displayVertex = false;
- currentDisplayMode = DISPLAY_MODE_BOTH;
- break;
- case SDLK_c:
- doNotCull = false;
- break;
- case SDLK_d:
- doNotCull = true;
- break;
- }
- break;
- default:
- break;
- }
- }
- void update()
- {
- uint32_t i, j;
- uint32_t faceCount;
- uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
- matrix4_t worldMatrix;
- if (timeToWait <= FRAME_TARGET_TIME)
- {
- SDL_Delay(timeToWait);
- }
- previousFrameTime = SDL_GetTicks();
- mesh.rotation.x += 0.01;
- 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);
- arrayEmpty(projectedTriangles);
- faceCount = arrayGetSize(mesh.faces);
- for(i = 0; i < faceCount; i++)
- {
- triangle_t currentTriangle;
- vec4_t transformedVertices[3];
- vec3_t vertices[3] =
- {
- mesh.vertices[mesh.faces[i].a - 1],
- mesh.vertices[mesh.faces[i].b - 1],
- mesh.vertices[mesh.faces[i].c - 1],
- };
- for(j = 0; j < 3; j++)
- {
- transformedVertices[j] = vec4FromVec3(vertices[j]);
- transformedVertices[j] = mat4ProdVec4(worldMatrix, transformedVertices[j]);
- }
- if (!doNotCull)
- {
- vec3_t vectorBA = vec3SubVectors(vec3FromVec4(transformedVertices[1]), vec3FromVec4(transformedVertices[0]));
- vec3_t vectorCA = vec3SubVectors(vec3FromVec4(transformedVertices[2]), vec3FromVec4(transformedVertices[0]));
- vec3Normalize(&vectorBA);
- vec3Normalize(&vectorCA);
- vec3_t triangleNormal = vec3Cross(vectorBA, vectorCA);
- vec3Normalize(&triangleNormal);
- vec3_t cameraRay = vec3SubVectors(cameraPosition, vec3FromVec4(transformedVertices[0]));
- vec3Normalize(&cameraRay);
- double cameraDotTriangle = vec3Dot(triangleNormal, cameraRay);
- if (cameraDotTriangle < 0)
- {
- continue;
- }
- }
- for (j = 0 ; j < 3 ; j++)
- {
- vec4_t projected = mat4ProdVec4Project(projectionMatrix, transformedVertices[j]);
- projected.x *= windowWidth / 2.;
- projected.y *= windowHeight / 2.;
- projected.x += (windowWidth / 2.);
- projected.y += (windowHeight / 2.);
- currentTriangle.points[j].x = projected.x;
- currentTriangle.points[j].y = projected.y;
- }
- currentTriangle.colour = mesh.faces[i].colour;
- currentTriangle.averageDepth = (transformedVertices[0].z +
- transformedVertices[1].z +
- transformedVertices[2].z) / 3.0;
- arrayAdd(projectedTriangles, currentTriangle);
- }
- qsort(projectedTriangles,
- arrayGetSize(projectedTriangles),
- sizeof(triangle_t),
- compareTrianglesZOrder);
- }
- void render()
- {
- int i;
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- #if 1
- int triangleCount = arrayGetSize(projectedTriangles);
- for(i = 0; i < triangleCount; i++)
- {
- switch(currentDisplayMode)
- {
- case DISPLAY_MODE_SOLID:
- drawFilledTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
- projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
- projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
- projectedTriangles[i].colour);
- break;
- case DISPLAY_MODE_WIREFRAME:
- drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
- projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
- projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
- projectedTriangles[i].colour);
- break;
- case DISPLAY_MODE_BOTH:
- drawFilledTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
- projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
- projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
- projectedTriangles[i].colour);
- drawTriangle(projectedTriangles[i].points[0].x, projectedTriangles[i].points[0].y,
- projectedTriangles[i].points[1].x, projectedTriangles[i].points[1].y,
- projectedTriangles[i].points[2].x, projectedTriangles[i].points[2].y,
- 0xFF000000 | ~projectedTriangles[i].colour);
- break;
- }
- if (displayVertex)
- {
- int j;
- for(j = 0; j < 3; j++)
- {
- drawRectangle(projectedTriangles[i].points[j].x - 1,
- projectedTriangles[i].points[j].y - 1,
- 3, 3,
- 0xFFFF0000);
- }
- }
- }
- #else
- drawTriangle(300, 100, 50, 400, 500, 700, 0xFF00FF00);
- drawFilledTriangle(300, 100, 50, 400, 500, 700, 0xFFFF0000);
- #endif
- renderFrameBuffer();
- clearFrameBuffer(0xFF000000);
- SDL_RenderPresent(renderer);
- }
- int main(int argc, char *argv[])
- {
- int param_i;
- bool fullScreen = false;
- MAX_DEBUG_LEVEL = TLOG_DEBUG;
- Log(TLOG_ALWAYS, NULL, "Booting 3D Engine (version %s)!", VERSION);
- for (param_i = 1 ; (param_i < argc) && (argv[param_i][0] == '-') ; param_i++)
- {
- switch (argv[param_i][1])
- {
- default: exit(-1); /* Option not recognized */
- case 'f': fullScreen = true; break;
- case 'w': windowWidth = atoi(argv[++param_i]);
- case 'h': windowHeight = atoi(argv[++param_i]); break;
- #ifdef DYNA_LOG_LEVEL
- case 'l': MAX_DEBUG_LEVEL = atoi(argv[++param_i]); break;
- #endif
- case '-': goto no_more_params; /* Could use break, but this is more clear */
- }
- }
- no_more_params:
- isRunning = initialiseWindow(fullScreen);
- setup();
- while (isRunning)
- {
- processInput();
- update();
- render();
- }
- arrayFree(projectedTriangles);
- meshFree();
- destroyWindow();
- return 0;
- }
|