diff --git a/include/engine.h b/include/engine.h index 84370e6..8385bf7 100644 --- a/include/engine.h +++ b/include/engine.h @@ -3,6 +3,12 @@ #include +// CONSTANTS +#define WINDOW_WIDTH (1920) +#define WINDOW_HEIGHT (1080) +#define WINDOW_FPS (30) + + // ENUMS enum Engine_Input { @@ -27,7 +33,7 @@ enum Engine_Input struct Engine_Buffer { uint32_t *buffer; - float *zbuffer; + float zbuffer[WINDOW_HEIGHT][WINDOW_WIDTH]; int width; int height; }; diff --git a/include/geometry.h b/include/geometry.h index a751f6d..3b7e574 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -6,14 +6,17 @@ #include +// CONSTANTS #define VERTEX_LIMIT (20000) #define FACE_LIMIT (30000) #define MATERIAL_LIMIT (10) +#define TEXTURE_SIZE_LIMIT (1024) +// STRUCTURES struct Texture { - ColorU32 **texels; + ColorU32 texels[TEXTURE_SIZE_LIMIT][TEXTURE_SIZE_LIMIT]; unsigned int width; unsigned int height; }; diff --git a/include/light.h b/include/light.h index f8af087..c6adea1 100644 --- a/include/light.h +++ b/include/light.h @@ -41,8 +41,7 @@ struct LightDiffuse struct LightList { LightAmbient ambient; - LightDiffuse *diffuse; - int diffuseCount; + LightDiffuse diffuse; }; diff --git a/src/engine.cpp b/src/engine.cpp index ff9cbc8..3fa1abd 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -59,11 +59,9 @@ int Engine_Init(Engine_Buffer &buffer, char *objFilename, char *mtlFilename) camera.SetFOV(90.0f, buffer.width, buffer.height); - lights.diffuse = (LightDiffuse*)malloc(sizeof(LightDiffuse)); - lights.diffuseCount = 1; lights.ambient.intensity = 1.0f; - lights.diffuse[0].intensity = 1.0f; - lights.diffuse[0].direction = Vector(1.0f, 1.0f, 1.0f); + lights.diffuse.intensity = 1.0f; + lights.diffuse.direction = Vector(1.0f, 1.0f, 1.0f); return 0; } @@ -119,15 +117,9 @@ void Engine_Render(Engine_Buffer &buffer, uint32_t input) { Vertex &vert = transVerts.data[face.vertIndex[i]]; - ColorF32 totalColor = lights.ambient.ComputeColor(material.ambient); - - for (int c = 0; c < lights.diffuseCount; ++c) - { - totalColor += lights.diffuse[c].ComputeColor( - material.diffuse, vert.normal); - } - - vert.color = totalColor; + vert.color = + lights.ambient.ComputeColor(material.ambient) + + lights.diffuse.ComputeColor(material.diffuse, vert.normal); } } diff --git a/src/geometry.cpp b/src/geometry.cpp index ac24ad0..e635196 100644 --- a/src/geometry.cpp +++ b/src/geometry.cpp @@ -206,12 +206,11 @@ void RenderMesh( // Draw the pixel if it's closer than what's in the z-buffer - int pixel = (y * buffer.width + x); - if (zInv > buffer.zbuffer[pixel]) + if (zInv > buffer.zbuffer[y][x]) { DrawPixel(buffer.buffer, buffer.width, color.u32, x, y); - buffer.zbuffer[pixel] = zInv; + buffer.zbuffer[y][x] = zInv; } } } diff --git a/src/loader.cpp b/src/loader.cpp index d6e43a2..1c0bee3 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -208,15 +208,8 @@ static int LoadTexture(char *filename, Texture &texture) fread((void*)&header, sizeof(BMP_Header), 1, fp); fseek(fp, header.bitmapOffset, SEEK_SET); - texture.texels = (ColorU32**)malloc((size_t)header.height * sizeof(ColorU32*)); - for (int row = 0; row < header.height; ++row) - { - texture.texels[row] = (ColorU32*)calloc(1, (size_t)header.width * sizeof(ColorU32)); - } - // Padding is added to image to align to 4-byte boundaries size_t paddingSize = header.width % 4; - uint8_t *padding = (uint8_t*)malloc(paddingSize * sizeof(*padding)); for (int y = 0; y < header.height; ++y) { @@ -230,7 +223,8 @@ static int LoadTexture(char *filename, Texture &texture) // Discard padding byte if (paddingSize != 0) { - fread(padding, paddingSize, 1, fp); + uint32_t padding; + fread(&padding, paddingSize, 1, fp); } } diff --git a/src/main.cpp b/src/main.cpp index f9f5494..1b49f68 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,12 +5,6 @@ #include -// CONSTANTS -const unsigned int WINDOW_WIDTH = 1920; -const unsigned int WINDOW_HEIGHT = 1080; -const unsigned int WINDOW_FPS = 30; - - // MAIN int main(int argc, char *argv[]) { @@ -31,7 +25,6 @@ int main(int argc, char *argv[]) { Engine_Buffer buffer = {}; buffer.buffer = (uint32_t*)platform.surface->pixels; - buffer.zbuffer = (float*)calloc((size_t)(platform.surface->w * platform.surface->h), sizeof(float)); buffer.width = platform.surface->w; buffer.height = platform.surface->h;