diff --git a/3D-Objects/.gitignore b/3D-Objects/.gitignore new file mode 100644 index 0000000..e361010 --- /dev/null +++ b/3D-Objects/.gitignore @@ -0,0 +1 @@ +*.bvh diff --git a/configure b/configure index 24dabcd..6763a75 100755 --- a/configure +++ b/configure @@ -1,6 +1,6 @@ #! /bin/sh # Guess values for system-dependent variables and create Makefiles. -# Generated by GNU Autoconf 2.71 for renderer 2.3e. +# Generated by GNU Autoconf 2.71 for renderer 2.3f. # # Report bugs to . # @@ -611,8 +611,8 @@ MAKEFLAGS= # Identity of this package. PACKAGE_NAME='renderer' PACKAGE_TARNAME='renderer' -PACKAGE_VERSION='2.3e' -PACKAGE_STRING='renderer 2.3e' +PACKAGE_VERSION='2.3f' +PACKAGE_STRING='renderer 2.3f' PACKAGE_BUGREPORT='ttsiodras@gmail.com' PACKAGE_URL='' @@ -1350,7 +1350,7 @@ if test "$ac_init_help" = "long"; then # Omit some internal or obsolete options to make the list less imposing. # This message is too long to be a string in the A/UX 3.1 sh. cat <<_ACEOF -\`configure' configures renderer 2.3e to adapt to many kinds of systems. +\`configure' configures renderer 2.3f to adapt to many kinds of systems. Usage: $0 [OPTION]... [VAR=VALUE]... @@ -1422,7 +1422,7 @@ fi if test -n "$ac_init_help"; then case $ac_init_help in - short | recursive ) echo "Configuration of renderer 2.3e:";; + short | recursive ) echo "Configuration of renderer 2.3f:";; esac cat <<\_ACEOF @@ -1538,7 +1538,7 @@ fi test -n "$ac_init_help" && exit $ac_status if $ac_init_version; then cat <<\_ACEOF -renderer configure 2.3e +renderer configure 2.3f generated by GNU Autoconf 2.71 Copyright (C) 2021 Free Software Foundation, Inc. @@ -1895,7 +1895,7 @@ cat >config.log <<_ACEOF This file contains any messages produced by compilers while running configure, to aid debugging if configure makes a mistake. -It was created by renderer $as_me 2.3e, which was +It was created by renderer $as_me 2.3f, which was generated by GNU Autoconf 2.71. Invocation command line was $ $0$ac_configure_args_raw @@ -3565,7 +3565,7 @@ fi # Define the identity of the package. PACKAGE='renderer' - VERSION='2.3e' + VERSION='2.3f' printf "%s\n" "#define PACKAGE \"$PACKAGE\"" >>confdefs.h @@ -8391,7 +8391,7 @@ cat >>$CONFIG_STATUS <<\_ACEOF || ac_write_fail=1 # report actual input values of CONFIG_FILES etc. instead of their # values after options handling. ac_log=" -This file was extended by renderer $as_me 2.3e, which was +This file was extended by renderer $as_me 2.3f, which was generated by GNU Autoconf 2.71. Invocation command line was CONFIG_FILES = $CONFIG_FILES @@ -8459,7 +8459,7 @@ ac_cs_config_escaped=`printf "%s\n" "$ac_cs_config" | sed "s/^ //; s/'/'\\\\\\\\ cat >>$CONFIG_STATUS <<_ACEOF || ac_write_fail=1 ac_cs_config='$ac_cs_config_escaped' ac_cs_version="\\ -renderer config.status 2.3e +renderer config.status 2.3f configured by $0, generated by GNU Autoconf 2.71, with options \\"\$ac_cs_config\\" diff --git a/configure.ac b/configure.ac index 193eafe..0170c48 100644 --- a/configure.ac +++ b/configure.ac @@ -1,4 +1,4 @@ -AC_INIT([renderer], [2.3e], [ttsiodras@gmail.com]) +AC_INIT([renderer], [2.3f], [ttsiodras@gmail.com]) AC_CONFIG_HEADERS([src/config.h]) diff --git a/src/BVH.cc b/src/BVH.cc index 11ff7fa..fa83cd9 100644 --- a/src/BVH.cc +++ b/src/BVH.cc @@ -695,6 +695,9 @@ BVHNode *Recurse(int size, BBoxEntries& work, REPORTPRM(float pct=0.) int depth= newRangeL[0], newRangeL[1], newRangeR[0], newRangeR[1]); } #endif + + _mm_free(left); + _mm_free(right); return inner; } @@ -705,6 +708,8 @@ BVHNode *CreateBVH(const Scene *pScene) BBoxEntries work = (BBoxTmp*)_mm_malloc(pScene->_triangles.size()*sizeof(BBoxTmp), 16); __m128 bottom(_mm_set1_ps(FLT_MAX)), top(_mm_set1_ps(-FLT_MAX)); + ASSERT_OR_DIE(pScene->_triangles.size()); + puts("Gathering bounding box info from all triangles..."); for(unsigned j=0; j_triangles.size(); j++) { const Triangle& triangle = pScene->_triangles[j]; @@ -752,6 +757,7 @@ BVHNode *CreateBVH(const Scene *pScene) root->_bottom = b; root->_top = t; + _mm_free(work); return root; } diff --git a/src/Camera.h b/src/Camera.h index 1338cce..f7d9351 100644 --- a/src/Camera.h +++ b/src/Camera.h @@ -20,7 +20,7 @@ #ifndef __camera_h__ #define __camera_h__ -#include "3d.h" +#include "Types.h" #include "Algebra.h" struct Camera : public Vector3 { diff --git a/src/Defines.h b/src/Defines.h index 4c1910d..bf61969 100644 --- a/src/Defines.h +++ b/src/Defines.h @@ -37,4 +37,11 @@ #define TRIANGLES_PER_THREAD 50 +#define ASSERT_OR_DIE(x) do { \ + if (!(x)) { \ + fprintf(stderr, "Internal error\n"); \ + exit(1); \ + } \ +} while(0) + #endif diff --git a/src/Fillers.h b/src/Fillers.h index e5da880..af63679 100644 --- a/src/Fillers.h +++ b/src/Fillers.h @@ -20,7 +20,6 @@ #ifndef __FILLERS_H__ #define __FILLERS_H__ -#include "3d.h" #include "Screen.h" #include "LightingEq.h" diff --git a/src/Keyboard.cc b/src/Keyboard.cc index e32ea3e..d316570 100644 --- a/src/Keyboard.cc +++ b/src/Keyboard.cc @@ -34,7 +34,7 @@ Keyboard::~Keyboard() void Keyboard::poll(bool bYield) { - SDL_Event event; + SDL_Event event = {}; if (bYield) SDL_Delay(1); diff --git a/src/Loader.cc b/src/Loader.cc index 293dea1..c208bdf 100644 --- a/src/Loader.cc +++ b/src/Loader.cc @@ -76,6 +76,12 @@ struct MaterialColors { const coord Scene::MaxCoordAfterRescale = 1.2f; +#define SAFE_FREAD(a, b, c, d) do { \ + if (c != fread(a, b, c, d)) { \ + THROW("Malformed 3D file"); \ + } \ +} while(0) + void Scene::load(const char *filename) { if (filename[0] == '@' && filename[1] == 'p') { // Platform @@ -119,28 +125,29 @@ void Scene::load(const char *filename) Uint32 totalPoints = 0, totalTris = 0; Uint32 magic; - fread(&magic, 1, sizeof(Uint32), fp); + SAFE_FREAD(&magic, 1, sizeof(Uint32), fp); if (magic != TRI_MAGIC && magic != TRI_MAGICNORMAL) { // No magic, just vertices and points (no normals, no colors) - fseek(fp, 0, SEEK_SET); + (void)fseek(fp, 0, SEEK_SET); } // Calculate total number of vertices in order to reserve the vectors memory unsigned currentOffset = ftell(fp); + ASSERT_OR_DIE(currentOffset != (unsigned) - 1); unsigned currentTotalPoints = 0; unsigned currentTotalTris = 0; while(1) { unsigned temp; - fread(&temp, 1, sizeof(Uint32), fp); + (void) fread(&temp, 1, sizeof(Uint32), fp); if (feof(fp)) break; currentTotalPoints += temp; - fseek(fp, temp*(magic==TRI_MAGICNORMAL?24:12), SEEK_CUR); - fread(&temp, 1, sizeof(Uint32), fp); + (void) fseek(fp, temp*(magic==TRI_MAGICNORMAL?24:12), SEEK_CUR); + SAFE_FREAD(&temp, 1, sizeof(Uint32), fp); if (feof(fp)) break; currentTotalTris += temp; - fseek(fp, temp*24, SEEK_CUR); + (void) fseek(fp, temp*24, SEEK_CUR); } // Reserve the space, now that you know @@ -152,24 +159,24 @@ void Scene::load(const char *filename) _triangles.reserve(currentTotalTris); // Now load them inside the std::vectors... - fseek(fp, currentOffset, SEEK_SET); + (void) fseek(fp, currentOffset, SEEK_SET); do { Uint32 noOfPoints; - fread(&noOfPoints, 1, sizeof(Uint32), fp); + (void) fread(&noOfPoints, 1, sizeof(Uint32), fp); if (feof(fp)) break; for(Uint32 i=0; i=(totalPoints+noOfPoints)) { THROW("Malformed 3D file (idx1)"); } if (idx2>=(totalPoints+noOfPoints)) { THROW("Malformed 3D file (idx2)"); } if (idx3>=(totalPoints+noOfPoints)) { THROW("Malformed 3D file (idx3)"); } float r,g,b; if (magic == TRI_MAGIC || magic == TRI_MAGICNORMAL) { - fread(&r,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } - fread(&g,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } - fread(&b,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } + SAFE_FREAD(&r,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } + SAFE_FREAD(&g,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } + SAFE_FREAD(&b,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); } r*=255.; g*=255.; b*=255.; } else { r = g = b = 255.0; // No colors? White, then... :-( @@ -222,10 +229,10 @@ void Scene::load(const char *filename) Uint32 totalPoints = 0, totalTriangles = 0; - fseek(fp, 0, SEEK_END); + (void) fseek(fp, 0, SEEK_END); totalTriangles = ftell(fp)/36; totalPoints = 3*totalTriangles; - fseek(fp, 0, SEEK_SET); + (void) fseek(fp, 0, SEEK_SET); _vertices.reserve(totalPoints); _triangles.reserve(totalTriangles); @@ -233,9 +240,9 @@ void Scene::load(const char *filename) // Now load them inside the std::vectors... for(Uint32 i=0; ifaces; pMesh = pMesh->next; } + free(p3DS); } else if (!strcmp(dt, "PLY") || !strcmp(dt, "ply")) { // Only shadevis generated objects, not full blown parser! std::ifstream file(filename, std::ios::in); diff --git a/src/Rasterizers.cc b/src/Rasterizers.cc index 7124d38..429a2ea 100644 --- a/src/Rasterizers.cc +++ b/src/Rasterizers.cc @@ -32,6 +32,7 @@ #include "3d.h" #include "Screen.h" +#include "Fillers.h" #include "Wu.h" // Clip distance for the triangles (they have a point closer than this, they dont get drawn) diff --git a/src/Raytracer.cc b/src/Raytracer.cc index 54f9f29..05d679a 100644 --- a/src/Raytracer.cc +++ b/src/Raytracer.cc @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -126,20 +127,20 @@ inline bool RayIntersectsBox(const Vector3& originInWorldSpace, const Vector3& r Tnear = -FLT_MAX; Tfar = FLT_MAX; -#define CHECK_NEAR_AND_FAR_INTERSECTION(c) \ - if (rayInWorldSpace._ ## c == 0.) { \ - if (originInWorldSpace._##c < box._bottom._##c) return false; \ - if (originInWorldSpace._##c > box._top._##c) return false; \ - } else { \ - coord T1 = (box._bottom._##c - originInWorldSpace._##c)/rayInWorldSpace._##c; \ - coord T2 = (box._top._##c - originInWorldSpace._##c)/rayInWorldSpace._##c; \ - if (T1>T2) { coord tmp=T1; T1=T2; T2=tmp; } \ - if (T1 > Tnear) Tnear = T1; \ - if (T2 < Tfar) Tfar = T2; \ - if (Tnear > Tfar) \ - return false; \ - if (Tfar < 0.) \ - return false; \ +#define CHECK_NEAR_AND_FAR_INTERSECTION(c) \ + if (rayInWorldSpace._ ## c == 0.) { \ + if (originInWorldSpace._##c < box._bottom._##c) return false; \ + if (originInWorldSpace._##c > box._top._##c) return false; \ + } else { \ + coord T1 = (box._bottom._##c - originInWorldSpace._##c)/rayInWorldSpace._##c; \ + coord T2 = (box._top._##c - originInWorldSpace._##c)/rayInWorldSpace._##c; \ + if (T1>T2) { coord tmp=T1; T1=T2; T2=tmp; } \ + if (T1 > Tnear) Tnear = T1; \ + if (T2 < Tfar) Tfar = T2; \ + if (Tnear > Tfar) \ + return false; \ + if (Tfar < 0.) \ + return false; \ } CHECK_NEAR_AND_FAR_INTERSECTION(x) @@ -616,6 +617,7 @@ int CountBoxes(BVHNode *root) { if (!root->IsLeaf()) { BVHInner *p = dynamic_cast(root); + ASSERT_OR_DIE(p); return 1 + CountBoxes(p->_left) + CountBoxes(p->_right); } else return 1; @@ -625,9 +627,11 @@ unsigned CountTriangles(BVHNode *root) { if (!root->IsLeaf()) { BVHInner *p = dynamic_cast(root); + ASSERT_OR_DIE(p); return CountTriangles(p->_left) + CountTriangles(p->_right); } else { BVHLeaf *p = dynamic_cast(root); + ASSERT_OR_DIE(p); return (unsigned) p->_triangles.size(); } } @@ -638,6 +642,7 @@ void CountDepth(BVHNode *root, int depth, int& maxDepth) maxDepth=depth; if (!root->IsLeaf()) { BVHInner *p = dynamic_cast(root); + ASSERT_OR_DIE(p); CountDepth(p->_left, depth+1, maxDepth); CountDepth(p->_right, depth+1, maxDepth); } @@ -654,6 +659,7 @@ void Scene::PopulateCacheFriendlyBVH( _pCFBVH[currIdxBoxes]._top = root->_top; if (!root->IsLeaf()) { BVHInner *p = dynamic_cast(root); + ASSERT_OR_DIE(p); int idxLeft = ++idxBoxes; PopulateCacheFriendlyBVH(pFirstTriangle, p->_left, idxBoxes, idxTriList); int idxRight = ++idxBoxes; @@ -662,6 +668,7 @@ void Scene::PopulateCacheFriendlyBVH( _pCFBVH[currIdxBoxes].u.inner._idxRight = idxRight; } else { BVHLeaf *p = dynamic_cast(root); + ASSERT_OR_DIE(p); unsigned count = (unsigned) p->_triangles.size(); _pCFBVH[currIdxBoxes].u.leaf._count = 0x80000000 | count; _pCFBVH[currIdxBoxes].u.leaf._startIndexInTriIndexList = idxTriList; @@ -748,10 +755,12 @@ void Scene::UpdateBoundingVolumeHierarchy(const char *filename, bool forceRecalc puts("Cache exists, reading the pre-calculated BVH data..."); if (1 != fread(&_pCFBVH_No, sizeof(unsigned), 1, fp)) { UpdateBoundingVolumeHierarchy(filename, true); + fclose(fp); return; } if (1 != fread(&_triIndexListNo, sizeof(unsigned), 1, fp)) { UpdateBoundingVolumeHierarchy(filename, true); + fclose(fp); return; } _pCFBVH = new CacheFriendlyBVHNode[_pCFBVH_No]; @@ -762,6 +771,7 @@ void Scene::UpdateBoundingVolumeHierarchy(const char *filename, bool forceRecalc _pCFBVH = NULL; _triIndexList = NULL; UpdateBoundingVolumeHierarchy(filename, true); + fclose(fp); return; } if (_triIndexListNo != fread(_triIndexList, sizeof(int), _triIndexListNo, fp)) { @@ -770,6 +780,7 @@ void Scene::UpdateBoundingVolumeHierarchy(const char *filename, bool forceRecalc _pCFBVH = NULL; _triIndexList = NULL; UpdateBoundingVolumeHierarchy(filename, true); + fclose(fp); return; } fclose(fp); @@ -844,7 +855,9 @@ bool Scene::renderRaytracer(Camera& eye, Screen& canvas, bool antialias) if (antialias) percentage << "Anti-aliased r"; else percentage << "R"; percentage << "aytracing... hit ESCAPE to abort (" << int(100.*y/HEIGHT) << "%)"; static char asyncBufferForCaption[256]; - strcpy(asyncBufferForCaption, percentage.str().c_str()); + strncpy(asyncBufferForCaption, percentage.str().c_str(), sizeof(asyncBufferForCaption)); + asyncBufferForCaption[sizeof(asyncBufferForCaption)-1] = '\0'; + SDL_WM_SetCaption(asyncBufferForCaption, asyncBufferForCaption); canvas.ShowScreen(true,false); } diff --git a/src/Screen.cc b/src/Screen.cc index 9526a34..e0ae273 100644 --- a/src/Screen.cc +++ b/src/Screen.cc @@ -25,6 +25,7 @@ #include "3d.h" #include "Screen.h" +#include "Fillers.h" #include "OnlineHelpKeys.h" SDL_Surface *Screen::_surface = NULL; diff --git a/src/Screen.h b/src/Screen.h index 192b53f..b692b48 100644 --- a/src/Screen.h +++ b/src/Screen.h @@ -31,7 +31,7 @@ #include "Types.h" #include "Base3d.h" -#include "Fillers.h" +#include "Defines.h" #include "ScanConverter.h" #include "OnlineHelpKeys.h" diff --git a/src/Wu.cc b/src/Wu.cc index 815600d..6411c3f 100644 --- a/src/Wu.cc +++ b/src/Wu.cc @@ -653,7 +653,7 @@ static int hlineColor(SDL_Surface * dst, Sint16 x1, Sint16 x2, Sint16 y, Uint32 int dx; int pixx, pixy; Sint16 xtmp; - int result = -1; + int result; Uint8 *colorptr; Uint8 color3[3]; @@ -804,7 +804,7 @@ static int vlineColor(SDL_Surface * dst, Sint16 x, Sint16 y1, Sint16 y2, Uint32 int pixx, pixy; Sint16 h; Sint16 ytmp; - int result = -1; + int result; Uint8 *colorptr; /* diff --git a/src/renderer.cc b/src/renderer.cc index f0a59c5..f82d8b0 100644 --- a/src/renderer.cc +++ b/src/renderer.cc @@ -244,7 +244,7 @@ int main(int argc, char *argv[]) bool autoRotate = true; Scene scene; - Screen canvas(scene); + static Screen canvas(scene); coord angle1=0.0f; coord angle2=0.0f*M_PI/180.f; diff --git a/src/showShadowMap.cc b/src/showShadowMap.cc index 1d2c1ce..7e1c085 100644 --- a/src/showShadowMap.cc +++ b/src/showShadowMap.cc @@ -44,7 +44,6 @@ int main(int, char *[]) Uint32 *offsets; Uint8 *buffer; - surface = NULL; offsets = new Uint32[HEI]; for(int i=0; i