multisampling working

git-svn-id: svn://anubis/fart/trunk@46 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-25 23:01:46 +00:00
parent d88f1ab5fd
commit dad8497bf4
2 changed files with 47 additions and 9 deletions

View File

@ -55,6 +55,9 @@ Scene::Scene(map<string, const char *> options,
/* view plane distance is calculated based on the field of view */ /* 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_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() Scene::~Scene()
@ -113,10 +116,35 @@ void Scene::render()
void Scene::renderPixel(int x, int y, unsigned char * pixel) void Scene::renderPixel(int x, int y, unsigned char * pixel)
{ {
/* calculate the ray going from the camera through this pixel */ /* calculate the ray going from the camera through this pixel */
double rx = (x + 0.5) - (m_width / 2.0); double red = 0.0, green = 0.0, blue = 0.0;
double rz = (m_height / 2.0) - (y + 0.5); 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 */ /* loop through all shapes in the scene */
for (vector<Shape *>::iterator it = m_shapes.begin(); for (vector<Shape *>::iterator it = m_shapes.begin();
@ -138,15 +166,20 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
dot = -dot; dot = -dot;
if (dot < 0.0) if (dot < 0.0)
dot = 0.0; dot = 0.0;
pixel[BMP_RED] = 0xFF * dot; #if 0
pixel[BMP_GREEN] = 0xFF * dot; color[0] = dot;
pixel[BMP_BLUE] = 0xFF * dot; color[1] = dot;
color[2] = dot;
#endif
color[0] = color[1] = color[2] = 1.0;
} }
else else
{ {
pixel[BMP_RED] = 0x0; color[0] = 0.0;
pixel[BMP_GREEN] = 0x0; color[1] = 0.0;
pixel[BMP_BLUE] = 0x0; color[2] = 0.0;
} }
} }
return color;
} }

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <map> #include <map>
#include <vector> #include <vector>
#include "util/Ray.h"
#include "shapes/Shape.h" #include "shapes/Shape.h"
class Scene class Scene
@ -19,6 +20,7 @@ class Scene
/* private methods */ /* private methods */
void load(const char * filename); void load(const char * filename);
void renderPixel(int x, int y, unsigned char * pixel); void renderPixel(int x, int y, unsigned char * pixel);
Vector traceRay(const Ray & ray);
/* rendering parameters */ /* rendering parameters */
int m_width; int m_width;
@ -32,6 +34,9 @@ class Scene
std::vector<Shape *> m_shapes; std::vector<Shape *> m_shapes;
Transform m_transform; Transform m_transform;
double m_view_plane_dist; double m_view_plane_dist;
int m_multisample_level_squared;
double m_sample_span;
double m_half_sample_span;
/* framebuffer */ /* framebuffer */
unsigned char * m_data; unsigned char * m_data;