Skip to content

Commit

Permalink
Minor cleanups, after oh-so-many years
Browse files Browse the repository at this point in the history
  • Loading branch information
ttsiodras committed Nov 19, 2023
1 parent e6c6ef8 commit b31abe5
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 60 deletions.
1 change: 1 addition & 0 deletions 3D-Objects/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.bvh
20 changes: 10 additions & 10 deletions configure
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>.
#
Expand Down Expand Up @@ -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='[email protected]'
PACKAGE_URL=''

Expand Down Expand Up @@ -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]...
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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\\"
Expand Down
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([renderer], [2.3e], [[email protected]])
AC_INIT([renderer], [2.3f], [[email protected]])

AC_CONFIG_HEADERS([src/config.h])

Expand Down
6 changes: 6 additions & 0 deletions src/BVH.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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<pScene->_triangles.size(); j++) {
const Triangle& triangle = pScene->_triangles[j];
Expand Down Expand Up @@ -752,6 +757,7 @@ BVHNode *CreateBVH(const Scene *pScene)
root->_bottom = b;
root->_top = t;

_mm_free(work);
return root;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#ifndef __camera_h__
#define __camera_h__

#include "3d.h"
#include "Types.h"
#include "Algebra.h"

struct Camera : public Vector3 {
Expand Down
7 changes: 7 additions & 0 deletions src/Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion src/Fillers.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef __FILLERS_H__
#define __FILLERS_H__

#include "3d.h"
#include "Screen.h"
#include "LightingEq.h"

Expand Down
2 changes: 1 addition & 1 deletion src/Keyboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Keyboard::~Keyboard()

void Keyboard::poll(bool bYield)
{
SDL_Event event;
SDL_Event event = {};

if (bYield)
SDL_Delay(1);
Expand Down
60 changes: 34 additions & 26 deletions src/Loader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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<noOfPoints; i++) {
float x,y,z;
fread(&x,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&y,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&z,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&x,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&y,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&z,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }

float nx=0.,ny=0.,nz=0.;
if (magic == TRI_MAGICNORMAL) {
fread(&nx,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&ny,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&nz,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&nx,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&ny,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&nz,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
} else {
nx = ny = nz = 0; // Will be calculated in fix_normals()
}
Expand All @@ -178,23 +185,23 @@ void Scene::load(const char *filename)
}

Uint32 noOfTris;
fread(&noOfTris, 1, sizeof(Uint32), fp);
SAFE_FREAD(&noOfTris, 1, sizeof(Uint32), fp);
if (feof(fp)) { THROW("Malformed 3D file"); }

for(Uint32 i=0; i<noOfTris; i++) {
Uint32 idx1,idx2,idx3;
fread(&idx1,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&idx2,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
fread(&idx3,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&idx1,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&idx2,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
SAFE_FREAD(&idx3,1,4,fp); if (feof(fp)) { THROW("Malformed 3D file"); }
if (idx1>=(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... :-(
Expand Down Expand Up @@ -222,20 +229,20 @@ 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);

// Now load them inside the std::vectors...
for(Uint32 i=0; i<totalPoints; i++) {
float x,y,z;
fread(&y,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }
fread(&z,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }
fread(&x,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }
SAFE_FREAD(&y,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }
SAFE_FREAD(&z,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }
SAFE_FREAD(&x,1,4,fp); if (feof(fp)) { fclose(fp); THROW("Malformed 3D file"); }

float nx=0.,ny=0.,nz=0.;
assert(_vertices.size() < _vertices.capacity());
Expand Down Expand Up @@ -343,6 +350,7 @@ void Scene::load(const char *filename)
currentTotalPoints += 3*pMesh->faces;
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);
Expand Down
1 change: 1 addition & 0 deletions src/Rasterizers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit b31abe5

Please sign in to comment.