Skip to content

Commit

Permalink
refactor: moved depth-of-field sampling to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Gregungory committed Dec 3, 2021
1 parent ddd5615 commit 7309df8
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ set(rtrad_SOURCES
instance.c
interp2d.c
invmat4.c
jitteraperture.c
lamps.c
linregr.c
loadbsdf.c
Expand Down
10 changes: 6 additions & 4 deletions src/common/Rmakefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# RCSid: $Id: Rmakefile,v 2.88 2021/11/19 22:51:31 greg Exp $
# RCSid: $Id: Rmakefile,v 2.89 2021/12/03 18:10:48 greg Exp $
#
# Makefile for ray tracing library routines
#
Expand All @@ -21,7 +21,7 @@ RTOBJ = cone.o face.o free_os.o instance.o readobj.o readoct.o otypes.o \
PICOBJ = color.o header.o image.o lamps.o resolu.o rexpr.o spec_rgb.o \
colrops.o font.o tonemap.o tmapcolrs.o tmapluv.o tmaptiff.o \
tmap16bit.o bmpfile.o falsecolor.o depthcodec.o normcodec.o \
idmap.o fltdepth.o
idmap.o fltdepth.o jitteraperture.o

UTLOBJ = ezxml.o ccolor.o ccyrgb.o bsdf.o bsdf_m.o bsdf_t.o loadbsdf.o \
disk2square.o hilbert.o interp2d.o triangulate.o
Expand Down Expand Up @@ -124,12 +124,14 @@ readobj.o readoct.o sceneio.o: otypes.h

multisamp.o urand.o: random.h

cone.o face.o free_os.o image.o instance.o objset.o \
cone.o face.o free_os.o instance.o objset.o \
octree.o modobject.o readfargs.o otypes.o mesh.o \
readmesh.o readobj.o readoct.o sceneio.o: standard.h \
rtmisc.h rtio.h rtmath.h rterror.h fvect.h mat4.h tiff.h

image.o: view.h
image.o: view.h rtio.h rtmath.h mat4.h fvect.h rtio.h paths.h

jitteraperture.o: view.h rtmath.h mat4.h fvect.h rtio.h

caldefn.o calexpr.o calfunc.o calprnt.o: calcomp.h

Expand Down
63 changes: 63 additions & 0 deletions src/common/jitteraperture.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#ifndef lint
static const char RCSid[] = "$Id: jitteraperture.c,v 2.1 2021/12/03 18:10:48 greg Exp $";
#endif
/*
* jitteraperture.c - routine to sample depth-of-field
*
* External symbols declared in view.h
*/

#include "copyright.h"

#include <stdio.h>
#include "rtmath.h"
#include "random.h"
#include "view.h"


int
jitteraperture( /* random aperture shift for depth-of-field */
FVECT orig, /* assigned previously by... */
FVECT direc, /* ...viewray() call */
VIEW *v,
double dia
)
{
double vc, df[2];
int i;
/* are we even needed? */
if (dia <= FTINY)
return(1);
/* get random point on disk */
SDsquare2disk(df, frandom(), frandom());
df[0] *= .5*dia;
df[1] *= .5*dia;
if ((v->type == VT_PER) | (v->type == VT_PAR)) {
double adj = 1.0; /* basic view cases */
if (v->type == VT_PER)
adj /= DOT(direc, v->vdir);
df[0] /= sqrt(v->hn2);
df[1] /= sqrt(v->vn2);
for (i = 3; i--; ) {
vc = v->vp[i] + adj*v->vdist*direc[i];
orig[i] += df[0]*v->hvec[i] +
df[1]*v->vvec[i] ;
direc[i] = vc - orig[i];
}
} else { /* difficult view cases */
double dfd = PI/4.*dia*(.5 - frandom());
if ((v->type != VT_ANG) & (v->type != VT_PLS)) {
if (v->type != VT_CYL)
df[0] /= sqrt(v->hn2);
df[1] /= sqrt(v->vn2);
}
for (i = 3; i--; ) {
vc = v->vp[i] + v->vdist*direc[i];
orig[i] += df[0]*v->hvec[i] +
df[1]*v->vvec[i] +
dfd*v->vdir[i] ;
direc[i] = vc - orig[i];
}
}
return(normalize(direc) != 0.0);
}
3 changes: 2 additions & 1 deletion src/common/view.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* RCSid $Id: view.h,v 2.20 2018/04/27 05:00:29 greg Exp $ */
/* RCSid $Id: view.h,v 2.21 2021/12/03 18:10:48 greg Exp $ */
/*
* view.h - header file for image generation.
*
Expand Down Expand Up @@ -61,6 +61,7 @@ extern VIEW stdview;
extern char *setview(VIEW *v);
extern void normaspect(double va, double *ap, int *xp, int *yp);
extern double viewray(FVECT orig, FVECT direc, VIEW *v, double x, double y);
extern int jitteraperture(FVECT orig, FVECT direc, VIEW *v, double dia);
extern int viewloc(FVECT ip, VIEW *v, FVECT p);
extern void pix2loc(RREAL loc[2], RESOLU *rp, int px, int py);
extern void loc2pix(int pp[2], RESOLU *rp, double lx, double ly);
Expand Down
47 changes: 5 additions & 42 deletions src/rt/rpict.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef lint
static const char RCSid[] = "$Id: rpict.c,v 2.96 2021/12/03 16:50:05 greg Exp $";
static const char RCSid[] = "$Id: rpict.c,v 2.97 2021/12/03 18:10:48 greg Exp $";
#endif
/*
* rpict.c - routines and variables for picture generation.
Expand Down Expand Up @@ -662,7 +662,7 @@ pixvalue( /* compute pixel value */
{
RAY thisray;
FVECT lorg, ldir;
double hpos, vpos, vdist, lmax;
double hpos, vpos, lmax;
int i;
/* compute view ray */
setcolor(col, 0.0, 0.0, 0.0);
Expand All @@ -671,8 +671,6 @@ pixvalue( /* compute pixel value */
if ((thisray.rmax = viewray(thisray.rorg, thisray.rdir,
&ourview, hpos, vpos)) < -FTINY)
return(0.0);

vdist = ourview.vdist;
/* set pixel index */
samplendx = pixnumber(x,y,hres,vres);
/* optional motion blur */
Expand All @@ -687,45 +685,10 @@ pixvalue( /* compute pixel value */
}
if (normalize(thisray.rdir) == 0.0)
return(0.0);
vdist = (1.-d)*vdist + d*lastview.vdist;
}
/* optional depth-of-field */
if (dblur > FTINY) {
double vc, df[2];
/* random point on disk */
SDsquare2disk(df, frandom(), frandom());
df[0] *= .5*dblur;
df[1] *= .5*dblur;
if ((ourview.type == VT_PER) | (ourview.type == VT_PAR)) {
double adj = 1.0;
if (ourview.type == VT_PER)
adj /= DOT(thisray.rdir, ourview.vdir);
df[0] /= sqrt(ourview.hn2);
df[1] /= sqrt(ourview.vn2);
for (i = 3; i--; ) {
vc = ourview.vp[i] + adj*vdist*thisray.rdir[i];
thisray.rorg[i] += df[0]*ourview.hvec[i] +
df[1]*ourview.vvec[i] ;
thisray.rdir[i] = vc - thisray.rorg[i];
}
} else { /* non-standard view case */
double dfd = PI/4.*dblur*(.5 - frandom());
if ((ourview.type != VT_ANG) & (ourview.type != VT_PLS)) {
if (ourview.type != VT_CYL)
df[0] /= sqrt(ourview.hn2);
df[1] /= sqrt(ourview.vn2);
}
for (i = 3; i--; ) {
vc = ourview.vp[i] + vdist*thisray.rdir[i];
thisray.rorg[i] += df[0]*ourview.hvec[i] +
df[1]*ourview.vvec[i] +
dfd*ourview.vdir[i] ;
thisray.rdir[i] = vc - thisray.rorg[i];
}
}
if (normalize(thisray.rdir) == 0.0)
return(0.0);
}
/* depth-of-field */
if (!jitteraperture(thisray.rorg, thisray.rdir, &ourview, dblur))
return(0.0);

rayorigin(&thisray, PRIMARY, NULL, NULL);

Expand Down

0 comments on commit 7309df8

Please sign in to comment.