Add jitter to area light and example render of it.
This commit is contained in:
@@ -12,6 +12,7 @@
|
||||
#include <tuple.h>
|
||||
#include <colour.h>
|
||||
#include <renderstat.h>
|
||||
#include <sequence.h>
|
||||
#include <stdio.h>
|
||||
|
||||
class World;
|
||||
@@ -37,6 +38,8 @@ public:
|
||||
uint32_t samples;
|
||||
uint32_t uSteps;
|
||||
uint32_t vSteps;
|
||||
bool jitter;
|
||||
Sequence jitterBy;
|
||||
|
||||
public:
|
||||
Light(LightType type = POINT_LIGHT, Tuple position=Point(0, 0, 0),
|
||||
@@ -44,7 +47,7 @@ public:
|
||||
{ stats.addLight(); };
|
||||
Light(LightType type, Tuple corner, Tuple fullUVec, uint32_t uSteps, Tuple fullVVec, uint32_t vSteps,
|
||||
Colour intensity, bool jitter = false): type(type), corner(corner), uVec(fullUVec / uSteps), uSteps(uSteps),
|
||||
vVec(fullVVec / vSteps), vSteps(vSteps), intensity(intensity)
|
||||
vVec(fullVVec / vSteps), vSteps(vSteps), intensity(intensity), jitter(jitter)
|
||||
{
|
||||
this->samples = this->vSteps * this->uSteps;
|
||||
this->position = this->corner + ((fullUVec + fullVVec) / 2);
|
||||
|
||||
50
source/include/sequence.h
Normal file
50
source/include/sequence.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* DoRayMe - a quick and dirty Raytracer
|
||||
* Sequence header
|
||||
*
|
||||
* Created by Manoël Trapier
|
||||
* Copyright (c) 2020 986-Studio.
|
||||
*
|
||||
*/
|
||||
#ifndef DORAYME_SEQUENCE_H
|
||||
#define DORAYME_SEQUENCE_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
|
||||
class Sequence
|
||||
{
|
||||
private:
|
||||
double *list;
|
||||
uint32_t listPos;
|
||||
uint32_t listSize;
|
||||
public:
|
||||
Sequence() : list(nullptr), listPos(0), listSize(0) {
|
||||
/* Need to bootstrap rand here */
|
||||
srand(time(NULL));
|
||||
}
|
||||
Sequence(double *list, uint32_t listSize) : list(list), listPos(0), listSize(listSize) { };
|
||||
|
||||
static double frand(void)
|
||||
{
|
||||
return rand() / ((double) RAND_MAX);
|
||||
}
|
||||
|
||||
|
||||
double next() {
|
||||
if (this->listSize == 0)
|
||||
{
|
||||
return frand();
|
||||
}
|
||||
else
|
||||
{
|
||||
uint32_t pos = this->listPos;
|
||||
this->listPos = (this->listPos + 1) % this->listSize;
|
||||
return this->list[pos];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif /* DORAYME_SEQUENCE_H */
|
||||
@@ -47,5 +47,14 @@ double Light::intensityAt(World &w, Tuple point)
|
||||
|
||||
Tuple Light::pointOnLight(uint32_t u, uint32_t v)
|
||||
{
|
||||
return this->corner + this->uVec * (u+0.5) + this->vVec * (v+0.5);
|
||||
if (this->jitter)
|
||||
{
|
||||
/* For some reason, for the test to pass, I need to get the sequence for V first, then U contrary to what
|
||||
* the bonus chapter says
|
||||
*/
|
||||
return this->corner +
|
||||
this->vVec * (v + this->jitterBy.next()) +
|
||||
this->uVec * (u + this->jitterBy.next());
|
||||
}
|
||||
return this->corner + this->uVec * (u + 0.5) + this->vVec * (v + 0.5);
|
||||
}
|
||||
Reference in New Issue
Block a user