/* * JoeyLib 3D * Copyright (C) 2019 Scott Duensing * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages * arising from the use of this software. * * Permission is granted to anyone to use this software for any purpose, * including commercial applications, and to alter it and redistribute it * freely, subject to the following restrictions: * * 1. The origin of this software must not be misrepresented; you must not * claim that you wrote the original software. If you use this software * in a product, an acknowledgment in the product documentation would be * appreciated but is not required. * 2. Altered source versions must be plainly marked as such, and must not be * misrepresented as being the original software. * 3. This notice may not be removed or altered from any source distribution. */ #ifndef H_J3D_ #define H_J3D_ #include "joey.h" typedef float j3Matrix4x4T[4][4]; typedef struct { float x; float y; float z; } j3VertexT; typedef struct { j3VertexT local; // Original vertex positions j3VertexT world; // After rotation, scale, translation (in that order) j3VertexT camera; // Camera space } j3CoordinatesT; typedef struct { int index[3]; // We do this instead of just a typedef so it works with stretch_buffer } j3TriangleT; typedef struct { int vertexCount; // How many verticies are in the list int triangleCount; // How many triangles are in the list j3CoordinatesT *verticies; // List of verticies as loaded from disk (more v1, v2, v3 than x, y, z) j3TriangleT *triangles; // Triangles built from vertex list j3VertexT position; // Position of object in world j3VertexT rotation; // Rotation of object in world j3VertexT scale; // Scale of object in world } j3ObjectT; typedef struct { j3ObjectT *objects; } j3WorldT; #define jtCameraSetClippingBounds(x1, y1, x2, y2) \ clipMinX = (x1); j3MathCheckBounds(clipMinX, 0, 319); \ clipMinY = (y1); j3MathCheckBounds(clipMiny, 0, 199); \ clipMaxX = (x2); j3MathCheckBounds(clipMaxX, 0, 319); \ clipMaxY = (y2); j3MathCheckBounds(clipMaxy, 0, 199) #define j3MathCheckBounds(value, min, max) \ if (value < (min)) value = (min); \ if (value > (max)) value = (max) #define j3MathWrapBounds(value, min, max) \ if (value < (min)) value += (max); \ if (value > (max)) value -= (max) #define j3ObjectMove(ob, px, py, pz) \ ob.position.x += (px); \ ob.position.y += (py); \ ob.position.z += (pz) #define j3ObjectMoveTo(ob, px, py, pz) \ ob.position.x = (px); \ ob.position.y = (py); \ ob.position.z = (pz) #define j3ObjectRotate(ob, px, py, pz) \ ob.rotation.x += (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \ ob.rotation.y += (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \ ob.rotation.z += (pz); j3MathWrapBounds(ob.rotation.z, 0, 360) #define j3ObjectRotateTo(ob, px, py, pz) \ ob.rotation.x = (px); j3MathWrapBounds(ob.rotation.x, 0, 360); \ ob.rotation.y = (py); j3MathWrapBounds(ob.rotation.y, 0, 360); \ ob.rotation.z = (pz); j3MathWrapBounds(ob.rotation.z, 0, 360) #define j3ObjectScale(ob, px, py, pz) \ ob.scale.x += (px); \ ob.scale.y += (py); \ ob.scale.z += (pz) #define j3ObjectScaleTo(ob, px, py, pz) \ ob.scale.x = (px); \ ob.scale.y = (py); \ ob.scale.z = (pz) // Syntatic sugar #define j3DrawWireframe(o) _j3DrawWireframe(&(o)) #define j3ObjectReset(o) _j3ObjectReset(&(o)) #define j3ObjectUpdate(o) _j3ObjectUpdate(&(o)) #define j3WorldFree(w) _j3WorldFree((j3WorldT **)&(w)) #define j3WorldLoad(w, f) _j3WorldLoad((j3WorldT **)&(w), (f)) // Prototypes void j3MathMatrix4x4Identity(j3Matrix4x4T result); void j3MathMatrix4x4Mult(j3Matrix4x4T a, j3Matrix4x4T b, j3Matrix4x4T result); void j3UtilShutdown(void); void j3UtilStartup(void); // Private Prototypes void _j3DrawWireframePair(j3ObjectT *o, int v1, int v2); void _j3DrawWireframe(j3ObjectT *o); void _j3ObjectReset(j3ObjectT *o); void _j3ObjectUpdate(j3ObjectT *o); bool _j3UtilClipLine(int *x1, int *y1, int *x2, int *y2); void _j3WorldFree(j3WorldT **world); bool _j3WorldLoad(j3WorldT **world, char *file); #endif // H_J3D_