Skip to content

Commit

Permalink
ray4: delete XYZW defines
Browse files Browse the repository at this point in the history
- Use numeric indices instead.
- Also change some parsed command-line values from ushort to int.
  • Loading branch information
hollasch committed Nov 2, 2024
1 parent 2bc709b commit 3cbbb14
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 47 deletions.
90 changes: 56 additions & 34 deletions ray4/src/r4_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ ImageHeader iheader = { // Output Image Header

Vector4 Gx, Gy, Gz; // Ray-Grid Basis Vectors
Point4 Gorigin; // Ray-Grid Origin Point
ushort res[3] = {0,0,0}; // Full Output Image Resolution
int res[3] = {0,0,0}; // Full Output Image Resolution
ulong scanlsize; // Scanline Size
ulong slbuff_count; // Number of Lines in Scanline Buffer
char *scanbuff; // Scanline Buffer
Expand Down Expand Up @@ -206,22 +206,22 @@ void Halt (const char *message, ...) {

//__________________________________________________________________________________________________

char *GetField (char *str, ushort *value) {
char *GetField (char *str, int &value) {
// These subroutine process the command-line arguments. The first two routines get each field of
// the resolution, aspect ratio, and scan range triples.

if (!str)
return nullptr;

if (!*str) {
*value = 0;
value = 0;
return str;
}

if ((*str < '0') || ('9' < *str))
return nullptr;

*value = static_cast<ushort>(atoi(str));
value = static_cast<ushort>(atoi(str));

while (('0' <= *str) && (*str <= '9'))
++str;
Expand All @@ -232,28 +232,28 @@ char *GetField (char *str, ushort *value) {
//__________________________________________________________________________________________________

char *GetRange (
char *str, // Source String
ushort *val1, // First Destination Value of Range
ushort *val2) // Second Destination Value of Range
char *str, // Source String
int &val1, // First Destination Value of Range
int &val2) // Second Destination Value of Range
{
if (!str)
return nullptr;

if (!*str) {
*val1 = *val2 = 0;
val1 = val2 = 0;
return str;
}

if (*str == '_') {
*val1 = 0;
*val2 = 0xFFFF;
val1 = 0;
val2 = 0xFFFF;
return (str[1] == ':') ? (str+2) : (str+1);
}

if ((*str < '0') || ('9' < *str))
return nullptr;

*val1 = *val2 = static_cast<ushort>(atoi (str));
val1 = val2 = static_cast<ushort>(atoi (str));

while (('0' <= *str) && (*str <= '9'))
++str;
Expand All @@ -271,7 +271,7 @@ char *GetRange (
if ((*str < '0') || ('9' < *str))
return nullptr;

*val2 = static_cast<ushort>(atoi (str));
val2 = static_cast<ushort>(atoi (str));
while (('0' <= *str) && (*str <= '9'))
++str;

Expand Down Expand Up @@ -362,15 +362,20 @@ void ProcessArgs (int argc, char *argv[]) {
switch (oc) {

case 'a': {
if (ptr = GetField(ptr,&iheader.aspect[0]), (!ptr || !*ptr))
int aspect[3];
if (ptr = GetField(ptr,aspect[0]), (!ptr || !*ptr))
Halt ("Invalid X argument for -a option.");

if (ptr = GetField(ptr,&iheader.aspect[1]), !ptr)
if (ptr = GetField(ptr,aspect[1]), !ptr)
Halt ("Invalid Y argument for -a option.");

if (ptr = GetField(ptr,&iheader.aspect[2]), !ptr)
if (ptr = GetField(ptr,aspect[2]), !ptr)
Halt ("Invalid Z argument for -a option.");

iheader.aspect[0] = aspect[0];
iheader.aspect[1] = aspect[1];
iheader.aspect[2] = aspect[2];

break;
}

Expand Down Expand Up @@ -410,38 +415,55 @@ void ProcessArgs (int argc, char *argv[]) {
case 'r': {
char* resArg = ptr;

ptr = GetField(ptr, &res[X]);
int resValues[3];

ptr = GetField(ptr, resValues[0]);

if (!ptr || !*ptr) {
res[Y] = res[Z] = res[X];
resValues[1] = resValues[2] = resValues[0];
} else {
ptr = GetField(ptr, &res[Y]);
ptr = GetField(ptr, resValues[1]);

if (!ptr || !*ptr)
res[Z] = res[X];
resValues[2] = resValues[0];
else
ptr = GetField(ptr, &res[Z]);
ptr = GetField(ptr, resValues[2]);
}

if (ptr && *ptr)
Halt("Bad resolution argument to -r option (%s).", resArg);

res[0] = resValues[0];
res[1] = resValues[1];
res[2] = resValues[2];

break;
}

case 's': {
ptr = GetRange(ptr,&iheader.start[0],&iheader.end[0]);
int rangeStart[3];
int rangeEnd[3];

ptr = GetRange(ptr,rangeStart[0],rangeEnd[0]);
if (!ptr || !*ptr)
Halt ("Bad X field argument to -s option.");

ptr = GetRange(ptr,&iheader.start[1],&iheader.end[1]);
ptr = GetRange(ptr,rangeStart[1],rangeEnd[1]);
if (!ptr)
Halt ("Bad Y field argument to -s option.");

ptr = GetRange(ptr,&iheader.start[2],&iheader.end[2]);
ptr = GetRange(ptr,rangeStart[2],rangeEnd[2]);
if (!ptr)
Halt ("Bad Z field argument to -s option.");

iheader.start[0] = rangeStart[0];
iheader.start[1] = rangeStart[1];
iheader.start[2] = rangeStart[2];

iheader.end[0] = rangeEnd[0];
iheader.end[1] = rangeEnd[1];
iheader.end[2] = rangeEnd[2];

break;
}

Expand Down Expand Up @@ -551,12 +573,12 @@ void CalcRayGrid (void) {
double GNx = 2.0 * lineOfSightNorm * tan(degreeToRadian*Vangle/2.0);

double GNy = GNx
* ((double) res[Y] / (double) res[X])
* ((double)iheader.aspect[Y]/ (double)iheader.aspect[X]);
* ((double) res[1] / (double) res[0])
* ((double)iheader.aspect[1]/ (double)iheader.aspect[0]);

double GNz = GNx
* ((double) res[Z] / (double) res[X])
* ((double)iheader.aspect[Z]/ (double)iheader.aspect[X]);
* ((double) res[2] / (double) res[0])
* ((double)iheader.aspect[2]/ (double)iheader.aspect[0]);

// Scale each grid basis vector.

Expand All @@ -570,9 +592,9 @@ void CalcRayGrid (void) {

// Finally, scale the grid basis vectors down by the corresponding resolutions.

Gx /= res[X];
Gy /= res[Y];
Gz /= res[Z];
Gx /= res[0];
Gy /= res[1];
Gz /= res[2];

Gorigin += (Gx/2) + (Gy/2) + (Gz/2);
}
Expand All @@ -586,10 +608,10 @@ void FireRays () {
char *scanptr = scanbuff; // Scanline Buffer Pointer
bool eflag = true; // Even RGB Boundary Flag

for (auto Zindex = iheader.start[Z]; Zindex <= iheader.end[Z]; ++Zindex) {
for (auto Zindex = iheader.start[2]; Zindex <= iheader.end[2]; ++Zindex) {
Point4 Zorigin = Gorigin + (Zindex*Gz);
for (auto Yindex = iheader.start[Y]; Yindex <= iheader.end[Y]; ++Yindex) {
printf ("%6u %6u\r", iheader.end[Z] - Zindex, iheader.end[Y] - Yindex);
for (auto Yindex = iheader.start[1]; Yindex <= iheader.end[1]; ++Yindex) {
printf ("%6u %6u\r", iheader.end[2] - Zindex, iheader.end[1] - Yindex);
fflush (stdout);

Point4 Yorigin = Zorigin + (Yindex*Gy);
Expand All @@ -599,7 +621,7 @@ void FireRays () {
eflag = true;
}

for (auto Xindex = iheader.start[X]; Xindex <= iheader.end[X]; ++Xindex) {
for (auto Xindex = iheader.start[0]; Xindex <= iheader.end[0]; ++Xindex) {
Color color; // Pixel Color
Vector4 dir; // Ray Direction Vector
Point4 Gpoint; // Current Grid Point
Expand Down
16 changes: 8 additions & 8 deletions ray4/src/r4_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -750,16 +750,16 @@ void Process_TetPar (TetPar *tp) {
// Find the dominant axis of the normal vector and load up the ax1, ax2 and ax3 fields
// accordingly.

auto dominant1 = (fabs(tp->normal[X]) > fabs(tp->normal[Y])) ? X : Y;
auto dominant2 = (fabs(tp->normal[Z]) > fabs(tp->normal[W])) ? Z : W;
int dominant1 = (fabs(tp->normal.x) > fabs(tp->normal.y)) ? 0 : 1;
int dominant2 = (fabs(tp->normal.z) > fabs(tp->normal.w)) ? 2 : 3;
if (fabs(tp->normal[dominant1]) > fabs(tp->normal[dominant2])) {
tp->ax1 = (dominant1 == X) ? Y : X;
tp->ax2 = Z;
tp->ax3 = W;
tp->ax1 = (dominant1 == 0) ? 1 : 0;
tp->ax2 = 2;
tp->ax3 = 3;
} else {
tp->ax1 = X;
tp->ax2 = Y;
tp->ax3 = (dominant2 == Z) ? W : Z;
tp->ax1 = 0;
tp->ax2 = 1;
tp->ax3 = (dominant2 == 2) ? 3 : 2;
}
}

Expand Down
5 changes: 0 additions & 5 deletions ray4/src/ray4.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,6 @@ const double pi=3.14159265358979323846;
const double degreeToRadian = pi / 180.0;
const double radianToDegree = 180.0 / pi;

#define X 0
#define Y 1
#define Z 2
#define W 3

enum class ObjType { // Object Type ID's
None,
Sphere,
Expand Down

0 comments on commit 3cbbb14

Please sign in to comment.