multisampling working
git-svn-id: svn://anubis/fart/trunk@46 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
d88f1ab5fd
commit
dad8497bf4
@ -55,6 +55,9 @@ Scene::Scene(map<string, const char *> options,
|
||||
|
||||
/* view plane distance is calculated based on the field of view */
|
||||
m_view_plane_dist = (m_height / 2.0) / tan(M_PI * m_vfov / 360.0);
|
||||
m_sample_span = 1.0 / m_multisample_level;
|
||||
m_half_sample_span = m_sample_span / 2.0;
|
||||
m_multisample_level_squared = m_multisample_level * m_multisample_level;
|
||||
}
|
||||
|
||||
Scene::~Scene()
|
||||
@ -113,10 +116,35 @@ void Scene::render()
|
||||
void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
||||
{
|
||||
/* calculate the ray going from the camera through this pixel */
|
||||
double rx = (x + 0.5) - (m_width / 2.0);
|
||||
double rz = (m_height / 2.0) - (y + 0.5);
|
||||
double red = 0.0, green = 0.0, blue = 0.0;
|
||||
for (int i = 0; i < m_multisample_level; i++)
|
||||
{
|
||||
for (int j = 0; j < m_multisample_level; j++)
|
||||
{
|
||||
double rx = (x + i * m_sample_span + m_half_sample_span)
|
||||
- (m_width / 2.0);
|
||||
double rz = (m_height / 2.0)
|
||||
- (y + j * m_sample_span + m_half_sample_span);
|
||||
|
||||
Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz));
|
||||
Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz));
|
||||
|
||||
Vector color = traceRay(ray);
|
||||
|
||||
red += color[0];
|
||||
green += color[1];
|
||||
blue += color[2];
|
||||
}
|
||||
}
|
||||
|
||||
/* take the average of all the samples as the final pixel value */
|
||||
pixel[BMP_RED] = (unsigned char) (0xFF * red / m_multisample_level_squared);
|
||||
pixel[BMP_GREEN] = (unsigned char) (0xFF * green / m_multisample_level_squared);
|
||||
pixel[BMP_BLUE] = (unsigned char) (0xFF * blue / m_multisample_level_squared);
|
||||
}
|
||||
|
||||
Vector Scene::traceRay(const Ray & ray)
|
||||
{
|
||||
Vector color;
|
||||
|
||||
/* loop through all shapes in the scene */
|
||||
for (vector<Shape *>::iterator it = m_shapes.begin();
|
||||
@ -138,15 +166,20 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
|
||||
dot = -dot;
|
||||
if (dot < 0.0)
|
||||
dot = 0.0;
|
||||
pixel[BMP_RED] = 0xFF * dot;
|
||||
pixel[BMP_GREEN] = 0xFF * dot;
|
||||
pixel[BMP_BLUE] = 0xFF * dot;
|
||||
#if 0
|
||||
color[0] = dot;
|
||||
color[1] = dot;
|
||||
color[2] = dot;
|
||||
#endif
|
||||
color[0] = color[1] = color[2] = 1.0;
|
||||
}
|
||||
else
|
||||
{
|
||||
pixel[BMP_RED] = 0x0;
|
||||
pixel[BMP_GREEN] = 0x0;
|
||||
pixel[BMP_BLUE] = 0x0;
|
||||
color[0] = 0.0;
|
||||
color[1] = 0.0;
|
||||
color[2] = 0.0;
|
||||
}
|
||||
}
|
||||
return color;
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,7 @@
|
||||
#include <string>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include "util/Ray.h"
|
||||
#include "shapes/Shape.h"
|
||||
|
||||
class Scene
|
||||
@ -19,6 +20,7 @@ class Scene
|
||||
/* private methods */
|
||||
void load(const char * filename);
|
||||
void renderPixel(int x, int y, unsigned char * pixel);
|
||||
Vector traceRay(const Ray & ray);
|
||||
|
||||
/* rendering parameters */
|
||||
int m_width;
|
||||
@ -32,6 +34,9 @@ class Scene
|
||||
std::vector<Shape *> m_shapes;
|
||||
Transform m_transform;
|
||||
double m_view_plane_dist;
|
||||
int m_multisample_level_squared;
|
||||
double m_sample_span;
|
||||
double m_half_sample_span;
|
||||
|
||||
/* framebuffer */
|
||||
unsigned char * m_data;
|
||||
|
Loading…
x
Reference in New Issue
Block a user