From 9e556ed5343384fcf9258fe0f37af3c1fb9b9b3d Mon Sep 17 00:00:00 2001 From: Roland Hieber Date: Tue, 12 Jan 2010 18:08:26 +0100 Subject: [PATCH] code from assignment 1 --- Camera.cxx | 29 ++++++++ Camera.hxx | 20 ++++++ Image.cxx | 73 +++++++++++++++++++ Image.hxx | 28 ++++++++ Makefile | 26 +++++++ MicroTrace.cxx | 74 +++++++++++++++++++ PerspectiveCamera.cxx | 46 ++++++++++++ PerspectiveCamera.hxx | 35 +++++++++ Primitive.hxx | 12 ++++ Ray.cxx | 42 +++++++++++ Ray.hxx | 27 +++++++ Triangle.cxx | 51 +++++++++++++ Triangle.hxx | 16 +++++ Vec3f.cxx | 163 ++++++++++++++++++++++++++++++++++++++++++ Vec3f.hxx | 85 ++++++++++++++++++++++ 15 files changed, 727 insertions(+) create mode 100644 Camera.cxx create mode 100644 Camera.hxx create mode 100644 Image.cxx create mode 100644 Image.hxx create mode 100644 Makefile create mode 100644 MicroTrace.cxx create mode 100644 PerspectiveCamera.cxx create mode 100644 PerspectiveCamera.hxx create mode 100644 Primitive.hxx create mode 100644 Ray.cxx create mode 100644 Ray.hxx create mode 100644 Triangle.cxx create mode 100644 Triangle.hxx create mode 100644 Vec3f.cxx create mode 100644 Vec3f.hxx diff --git a/Camera.cxx b/Camera.cxx new file mode 100644 index 0000000..7664b15 --- /dev/null +++ b/Camera.cxx @@ -0,0 +1,29 @@ +#include "Camera.hxx" + +Camera::Camera() + : m_resX(-1), + m_resY(-1) +{ +} + +Camera::Camera(int resX, int resY) + : m_resX(resX), + m_resY(resY) +{ +} + +Camera::~Camera() +{ +} + +int +Camera::resX() const +{ + return m_resX; +} + +int +Camera::resY() const +{ + return m_resY; +} diff --git a/Camera.hxx b/Camera.hxx new file mode 100644 index 0000000..51f9865 --- /dev/null +++ b/Camera.hxx @@ -0,0 +1,20 @@ +#ifndef CAMERA_HXX +#define CAMERA_HXX + +#include "Ray.hxx" + +class Camera +{ +public: + Camera(int resX, int resY); + virtual ~Camera(); + virtual bool InitRay(float x, float y, Ray &ray) = 0; + + int resX() const; + int resY() const; +protected: + Camera(); + + int m_resX, m_resY; +}; +#endif diff --git a/Image.cxx b/Image.cxx new file mode 100644 index 0000000..b45cac9 --- /dev/null +++ b/Image.cxx @@ -0,0 +1,73 @@ +#include +#include +#include + +#include "Image.hxx" + +Image::Image(int resX, int resY) +: + m_resX(resX), + m_resY(resY) +{ + assert (resX > 0 && resY > 0); + std::cerr << "(Image): ResX = " << m_resX << ", resY = " << m_resY << std::endl; + m_pixel = new Vec3f[m_resX*m_resY]; +} + +Image::~Image() +{ + delete [] m_pixel; +} + +Image::Image() +{ +} + +Image::Image(const Image& o) +{ + operator=(o); +} + +Image& +Image::operator=(const Image& o) +{ + return *this; +} + +Vec3f& +Image::operator()(int x, int y) +{ + assert(x >= 0 && x < m_resX); + assert(y >= 0 && y < m_resY); + + return m_pixel[y*m_resX+x]; +} + +void Image::WritePPM(const std::string& fileName) +{ + std::cerr << "(Image): Writing to file " << fileName << std::endl; + std::ofstream file(fileName.c_str()); + + if(!file.is_open()) + { + std::cerr << "(Image): Could not open file " << fileName << std::endl; + return; + } + + file << "P3" << std::endl; + file << m_resX << " " << m_resY << " " << 255 << std::endl; + for (int y=m_resY-1;y>=0;y--) + { + for (int x=0;x + +#include "Vec3f.hxx" + +class Image +{ +public: + Image(int resX,int resY); + ~Image(); + + //! pixel access operator read/write + Vec3f& operator()(int x, int y); + + void WritePPM(const std::string& fileName);/*in Image.cxx*/ + +private: + Image(); + Image(const Image& ); + Image& operator=(const Image& ); + + int m_resX, m_resY; + Vec3f* m_pixel; +}; + +#endif diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2223440 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +.SUFFIXES: .cxx .a + +all: MicroTrace + +CC = g++ +CFLAGS = -O3 -Wall + +OBJ = MicroTrace.o\ + Vec3f.o\ + Camera.o\ + PerspectiveCamera.o\ + Image.o\ + Ray.o + +%.o: %.cxx *.hxx + $(CC) $(CFLAGS) -c $< -o $@ + +%.a: + ar r $@ $< + + + +MicroTrace: $(OBJ) + +clean: + rm *.o *.a MicroTrace diff --git a/MicroTrace.cxx b/MicroTrace.cxx new file mode 100644 index 0000000..d0b1d6e --- /dev/null +++ b/MicroTrace.cxx @@ -0,0 +1,74 @@ +#include + +#include "Vec3f.hxx" +//#include "Sphere.hxx" +//#include "Triangle.hxx" +//#include "InfinitePlane.hxx" + +#include "Image.hxx" +#include "PerspectiveCamera.hxx" + +void RenderFrame(Camera &camera, + const std::string& fileName) +{ + /* scene objects */ + + /* + Sphere s1(Vec3f(-2,1.7,0),2); + Sphere s2(Vec3f(1,-1,1),2.2); + Sphere s3(Vec3f(3,0.8,-2),2);*/ + //InfinitePlane p1(Vec3f(0,-1,0),Vec3f(0,1,0)); + + /* + Triangle t1(Vec3f(-2,3.7,0),Vec3f(1,2,1),Vec3f(3,2.8,-2)); + Triangle t2(Vec3f(3,2,3),Vec3f(3,2,-3),Vec3f(-3,2,-3)); + */ + Image img(camera.resX(),camera.resY()); // image array + Ray ray; // primary ray + + for(int y=0;y