00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef _CANVAS_H_
00036 #define _CANVAS_H_
00037
00038 #include "RGB.h"
00039 #include "xmath.h"
00040 #include "Point.h"
00041 #include "color.h"
00042
00043 #include <string>
00044 #include <cassert>
00045 #include <set>
00046
00047 using namespace std;
00048
00049 namespace columbia
00050 {
00051
00052 struct ScannedResult
00053 {
00054 ScannedResult(int X, int Y, float Z, RGB const& Col) : x(X), y(Y), z(Z), col(Col) { }
00055 bool operator<(ScannedResult const& rhs) const { return (y<rhs.y) || ( (y==rhs.y) && (x<rhs.x) ); }
00056 int x, y;
00057 float z;
00058 RGB col;
00059 };
00060
00061 class Canvas
00062 {
00063 public:
00064 explicit Canvas(int resoH = 512, int resoV = 512) : hReso(resoH), vReso(resoV), filename("tmp.ppm"), data(0), z_buffer(0) { init(); }
00065 ~Canvas() { delete [] data; delete [] z_buffer; }
00066
00067 void Clear(RGB const&);
00068
00069 RGB* operator[] (int r) { assert(r>=0 && r<vReso); return data + r*hReso; }
00070 RGB& Pixel(int row, int col);
00071 float& Z(int row, int col);
00072
00073
00074
00075
00076
00077
00078
00079
00080 void ScanLineSegment(int x1, int y1, float z1, RGB const& col1, int x2, int y2, float z2, RGB const& col2, set<ScannedResult>* output=0);
00081
00082
00083 void WireTriangle(Point const& P1, RGB const& col1, Point const& P2, RGB const& col2, Point const& P3, RGB const& col3);
00084 void SolidTriangle(Point const& P1, RGB const& col1, Point const& P2, RGB const& col2, Point const& P3, RGB const& col3);
00085
00086 void WritePPM(const char* = 0) const;
00087
00088 int hReso, vReso;
00089 std::string filename;
00090
00091
00092 private:
00093 RGB* data;
00094 float* z_buffer;
00095
00096 bool swap_xy, flip_y;
00097
00098 void init();
00099
00100 void scanLineSegment(int x1, int y1, float z1, RGB const& col1, int x2, int y2, float z2, RGB const& col2, set<ScannedResult>* output=0);
00101
00102 void writePixel(int X, int Y, float cur_z, RGB const& col, set<ScannedResult>* output);
00103
00104 double eye_at;
00105 Point perspectiveProjection(Point const&);
00106 };
00107
00108 inline void Canvas :: writePixel(int X, int Y, float cur_z, RGB const& col, set<ScannedResult>* output)
00109 {
00110 if(swap_xy) swap(X,Y);
00111 if(flip_y) Y = -Y;
00112
00113 if(output)
00114 {
00115 output->insert( ScannedResult(X, Y, cur_z, col) );
00116 }
00117 else if( (X >= -hReso/2 && X < hReso/2) &&
00118 (Y >= -vReso/2 && Y < vReso/2) )
00119 {
00120 if( cur_z > Z(Y + vReso/2, X + hReso/2) )
00121 {
00122 if(col == Red)
00123 {
00124
00125
00126
00127 }
00128
00129 Z(Y + vReso/2, X + hReso/2) = cur_z;
00130 Pixel(Y + vReso/2, X + hReso/2) = col;
00131 }
00132 else if(col == Red)
00133 {
00134
00135
00136
00137 }
00138
00139 }
00140 }
00141
00142
00143 inline RGB& Canvas :: Pixel(int row, int col)
00144 {
00145 if(row >= vReso) { cout << "row = " << row << endl; throw "row number overflow\n"; }
00146 if(row < 0) { cout << "1row = " << row << endl; throw "row number underflow\n"; }
00147 if(col >= hReso) { cout << "col = " << col << endl; throw "col number overrflow\n"; }
00148 if(col < 0) { cout << "col = " << col << endl; throw "col number underflow\n"; }
00149 return *(data + row * hReso + col);
00150 }
00151
00152 inline float& Canvas :: Z(int row, int col)
00153 {
00154 if(row >= vReso) { cout << "row = " << row << endl; throw "row number overflow\n"; }
00155 if(row < 0) { cout << "1row = " << row << endl; throw "row number underflow\n"; }
00156 if(col >= hReso) { cout << "col = " << col << endl; throw "col number overrflow\n"; }
00157 if(col < 0) { cout << "col = " << col << endl; throw "col number underflow\n"; }
00158 return *(z_buffer + row * hReso + col);
00159 }
00160
00161 inline Point Canvas :: perspectiveProjection(Point const& P)
00162 {
00163 double scale = eye_at / (eye_at - P.z);
00164 return Point(P.x * scale, P.y * scale, P.z);
00165 }
00166
00167
00168 }
00169
00170 #endif