123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- /*
- * 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 <libc.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>
- #include <light.h>
- #include <camera.h>
- bool isRunning = true;
- int previousFrameTime = 0;
- typedef enum displayMode_t
- {
- DISPLAY_MODE_WIREFRAME,
- DISPLAY_MODE_SOLID,
- DISPLAY_MODE_SOLID_WIRE,
- DISPLAY_MODE_TEXTURED,
- DISPLAY_MODE_TEXTURED_WIRE,
- } displayMode_t;
- bool displayVertex = false;
- bool doNotCull = false;
- bool doZBuffer = true;
- enum displayMode_t currentDisplayMode = DISPLAY_MODE_SOLID;
- matrix4_t projectionMatrix;
- matrix4_t viewMatrix;
- light_t globalLight;
- double updateTime, renderTime, waitTime;
- int trianglesCount, trianglesTotal;
- triangle_t *projectedTriangles = NULL;
- const vec3_t origin = {0, 0, 0 };
- const vec3_t up = {0, 1, 0 };
- void setup()
- {
- frameBuffer = (uint32_t *)calloc(windowWidth * windowHeight, sizeof(uint32_t));
- if (!frameBuffer)
- {
- Log(TLOG_PANIC, NULL, "Memory allocation error.");
- goto exit;
- }
- zBuffer = (double *)calloc(windowWidth * windowHeight, sizeof(double));
- if (!frameBuffer)
- {
- Log(TLOG_PANIC, NULL, "Memory allocation error.");
- goto exit;
- }
- frameBufferTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA32, 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);
- globalLight.direction.x = 0;
- globalLight.direction.y = 0;
- globalLight.direction.z = 1;
- vec3Normalize(&globalLight.direction);
- /* Load texture */
- //meshTexture = (colour_t *)REDBRICK_TEXTURE;
- loadTextureDateFromPng("assets/f22.png");
- /* Load object */
- loadOBJFile("assets/f22.obj");
- //loadCubeMeshData();
- trianglesTotal = arrayGetSize(mesh.faces);
- 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_SOLID_WIRE;
- break;
- case SDLK_5:
- displayVertex = false;
- currentDisplayMode = DISPLAY_MODE_TEXTURED;
- break;
- case SDLK_6:
- displayVertex = false;
- currentDisplayMode = DISPLAY_MODE_TEXTURED_WIRE;
- break;
- case SDLK_c:
- doNotCull = false;
- break;
- case SDLK_d:
- doNotCull = true;
- break;
- case SDLK_f:
- doPerpectiveCorrection = false;
- break;
- case SDLK_v:
- doPerpectiveCorrection = true;
- break;
- case SDLK_b:
- doZBuffer = true;
- break;
- case SDLK_g:
- doZBuffer = false;
- break;
- }
- break;
- default:
- break;
- }
- }
- uint32_t getMicroSecTime()
- {
- struct timeval curTime;
- gettimeofday(&curTime, NULL);
- return ((curTime.tv_sec) * 1000 * 1000) + (curTime.tv_usec);
- }
- void update()
- {
- uint32_t i, j;
- uint32_t faceCount;
- uint32_t timeToWait = FRAME_TARGET_TIME - (SDL_GetTicks() - previousFrameTime);
- matrix4_t worldMatrix;
- uint32_t ticks = getMicroSecTime();
- vec3_t target = { 0, 0, 4 };
- if (timeToWait <= FRAME_TARGET_TIME)
- {
- SDL_Delay(timeToWait);
- }
- previousFrameTime = SDL_GetTicks();
- waitTime = (waitTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
- ticks = getMicroSecTime();
- //mesh.rotation.x += 0.02;
- mesh.translation.z = 4;
- camera.position.x += 0.008;
- camera.position.y += 0.008;
- viewMatrix = mat4LookAt(camera.position, target, up);
- 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);
- /* Not sure for now */
- //worldMatrix = mat4ProdMat4(viewMatrix, worldMatrix);
- arrayEmpty(projectedTriangles);
- faceCount = arrayGetSize(mesh.faces);
- for(i = 0; i < faceCount; i++)
- {
- triangle_t currentTriangle =
- {
- .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 }
- }
- };
- vec4_t transformedVertices[3];
- vec3_t triangleNormal;
- vec3_t vertices[3] =
- {
- 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(viewMatrix, transformedVertices[j]);
- }
- vec3_t vectorBA = vec3SubVectors(vec3FromVec4(transformedVertices[1]), vec3FromVec4(transformedVertices[0]));
- vec3_t vectorCA = vec3SubVectors(vec3FromVec4(transformedVertices[2]), vec3FromVec4(transformedVertices[0]));
- vec3Normalize(&vectorBA);
- vec3Normalize(&vectorCA);
- triangleNormal = vec3Cross(vectorBA, vectorCA);
- vec3Normalize(&triangleNormal);
- if (!doNotCull)
- {
- vec3_t cameraRay = vec3SubVectors(origin, 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.;
- /* Screen Y axis increase down, 3D world Y grows up, so we need to invert. */
- projected.y *= -1.0;
- projected.x += (windowWidth / 2.);
- projected.y += (windowHeight / 2.);
- currentTriangle.points[j].x = projected.x;
- currentTriangle.points[j].y = projected.y;
- currentTriangle.points[j].w = projected.w;
- currentTriangle.points[j].z = projected.z;
- }
- double lightLevel = -vec3Dot(triangleNormal, globalLight.direction);
- currentTriangle.colour = applyLight(mesh.faces[i].colour, lightLevel);
- currentTriangle.averageDepth = (transformedVertices[0].z +
- transformedVertices[1].z +
- transformedVertices[2].z) / 3.0;
- currentTriangle.texture = meshTexture;
- arrayAdd(projectedTriangles, currentTriangle);
- }
- if (!doZBuffer)
- {
- trianglesCount = arrayGetSize(projectedTriangles);
- qsort(projectedTriangles, trianglesCount, sizeof(triangle_t), compareTrianglesZOrder);
- }
- updateTime = (updateTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
- }
- void render()
- {
- int i;
- uint32_t ticks = getMicroSecTime();
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- int triangleCount = arrayGetSize(projectedTriangles);
- for(i = 0; i < triangleCount; i++)
- {
- switch(currentDisplayMode)
- {
- case DISPLAY_MODE_SOLID:
- case DISPLAY_MODE_SOLID_WIRE:
- drawFilledTriangle(&projectedTriangles[i]);
- break;
- case DISPLAY_MODE_TEXTURED:
- case DISPLAY_MODE_TEXTURED_WIRE:
- drawTextureTriangle(&projectedTriangles[i]);
- break;
- default:
- break;
- }
- switch (currentDisplayMode)
- {
- case DISPLAY_MODE_SOLID_WIRE:
- case DISPLAY_MODE_TEXTURED_WIRE:
- 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;
- default:
- 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);
- }
- }
- }
- renderFrameBuffer();
- clearFrameBuffer(MAKE_RGB(0, 0, 0));
- clearZBuffer();
- renderTime = (renderTime + (getMicroSecTime() - ticks) / 1000.) / 2.0;
- drawText(5, 5, 0x11FF22, "Wait time: %02.2f ms", waitTime);
- drawText(5, 17, 0x11FF22, "Update time: %02.2f ms", updateTime);
- drawText(5, 29, 0x11FF22, "Render time: %02.2f ms", renderTime);
- drawText(5, 41, 0x11FF22, "FPS: %.2f", 1000 / (waitTime + updateTime + renderTime));
- drawText(5, 53, 0x11FF22, "Triangles: %d / %d", triangleCount, trianglesTotal);
- drawText(5, 65, 0x11FF22, "Cull: %s | PeCo: %s | ZB: %s",
- doNotCull?"N":"Y", doPerpectiveCorrection?"Y":"N", doZBuffer?"Y":"N");
- 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);
- textureCleanup();
- meshFree();
- destroyWindow();
- return 0;
- }
|