sphere drawing, but stretched horizontally... have to figure that out yet

git-svn-id: svn://anubis/fart/trunk@40 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
Josh Holtrop 2009-01-23 16:00:43 +00:00
parent 97b9fa9eab
commit a1c81e5adc
4 changed files with 28 additions and 8 deletions

View File

@ -52,6 +52,9 @@ Scene::Scene(map<string, const char *> options,
m_verbose = true; m_verbose = true;
} }
} }
/* view plane distance is calculated based on the field of view */
m_view_plane_dist = (m_height / 2.0) / atan(m_vfov / 2.0);
} }
Scene::~Scene() Scene::~Scene()
@ -69,8 +72,8 @@ Scene::~Scene()
void Scene::load(const char * filename) void Scene::load(const char * filename)
{ {
/* TODO: parse file somehow */ /* TODO: parse file somehow */
Shape * shape = new Sphere(2.0); Shape * shape = new Sphere(1.0);
m_transform.translate(2.0, 6.0, 1.0); m_transform.translate(2.0, 2.0, 0.0);
shape->setTransform(m_transform); shape->setTransform(m_transform);
m_shapes.push_back(shape); m_shapes.push_back(shape);
} }
@ -110,12 +113,10 @@ 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 >> 1); double rx = (x + 0.5) - (m_width / 2.0);
double rz = (m_height >> 1) - (y + 0.5); double rz = (m_height / 2.0) - (y + 0.5);
/* ry is calculated based on the field of view */
double ry = (m_height / 2.0) / atan(m_vfov / 2.0);
Ray ray(Vector(0, 0, 0), Vector(rx, ry, rz)); Ray ray(Vector(0, 0, 0), Vector(rx, m_view_plane_dist, rz));
/* 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();
@ -128,5 +129,18 @@ void Scene::renderPixel(int x, int y, unsigned char * pixel)
/* then intersect this inversely transformed ray with the shape */ /* then intersect this inversely transformed ray with the shape */
Solver::Result intersections = (*it)->intersect(transformed_ray); Solver::Result intersections = (*it)->intersect(transformed_ray);
if (intersections.numResults > 0)
{
pixel[BMP_RED] = 0xFF;
pixel[BMP_GREEN] = 0xFF;
pixel[BMP_BLUE] = 0xFF;
}
else
{
pixel[BMP_RED] = 0x0;
pixel[BMP_GREEN] = 0x0;
pixel[BMP_BLUE] = 0x0;
}
} }
} }

View File

@ -31,6 +31,7 @@ class Scene
/* private data */ /* private data */
std::vector<Shape *> m_shapes; std::vector<Shape *> m_shapes;
Transform m_transform; Transform m_transform;
double m_view_plane_dist;
/* framebuffer */ /* framebuffer */
unsigned char * m_data; unsigned char * m_data;

View File

@ -1,10 +1,13 @@
#include "Sphere.h" #include "Sphere.h"
#include "util/Solver.h" #include "util/Solver.h"
#include <iostream>
using namespace std;
Sphere::Sphere(double radius) Sphere::Sphere(double radius)
{ {
m_radius = radius; m_radius = radius;
m_radius2 = radius * radius;
} }
Solver::Result Sphere::intersect(const Ray & ray) Solver::Result Sphere::intersect(const Ray & ray)
@ -16,7 +19,8 @@ Solver::Result Sphere::intersect(const Ray & ray)
+ ray.getOrigin()[2] * ray.getDirection()[2] ), + ray.getOrigin()[2] * ray.getDirection()[2] ),
ray.getOrigin()[0] * ray.getOrigin()[0] ray.getOrigin()[0] * ray.getOrigin()[0]
+ ray.getOrigin()[1] * ray.getOrigin()[1] + ray.getOrigin()[1] * ray.getOrigin()[1]
+ ray.getOrigin()[2] * ray.getOrigin()[2] ); + ray.getOrigin()[2] * ray.getOrigin()[2]
- m_radius2);
Solver::Result quadSolutions = solver.solve(); Solver::Result quadSolutions = solver.solve();
int resIdx = 0; int resIdx = 0;
for (int i = 0; i < quadSolutions.numResults; i++) for (int i = 0; i < quadSolutions.numResults; i++)

View File

@ -12,6 +12,7 @@ class Sphere : public Shape
private: private:
double m_radius; double m_radius;
double m_radius2;
}; };
#endif #endif