00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __screen_hpp__
00022 #define __screen_hpp__
00023
00024 #include <string>
00025
00026 #include "math3d.hpp"
00027
00029 struct color3
00030 {
00031 color3() { }
00032 color3(const vect3d& v) :
00033 b((unsigned char)prec2double(v.z * 255)),
00034 g((unsigned char)prec2double(v.y * 255)),
00035 r((unsigned char)prec2double(v.x * 255)) { }
00036 color3(unsigned char r, unsigned char g, unsigned char b) : b(b), g(g), r(r) { }
00037
00038 friend bool operator== (const color3&l, const color3&r) { return l.r == r.r && l.g == r.g && l.b == r.b; }
00039 friend bool operator!= (const color3&l, const color3&r) { return !(l == r); }
00040
00041 unsigned char b, g, r;
00042 } ATTRIBUTE_PACKED;
00043
00045 class Screen
00046 {
00047 public:
00048 typedef color3 color;
00049
00050 static const color WHITE;
00051 static const color RED;
00052 static const color GREEN;
00053 static const color BLUE;
00054 static const color BLACK;
00055
00062 Screen (size_t w, size_t h, prec_t sw, prec_t sh, const color& bg = WHITE);
00063 Screen (const Screen& copy);
00064 ~Screen ();
00065
00066 Screen& operator= (const Screen& copy);
00067
00068 void clear(const color& c = WHITE);
00069
00071 void draw_axes (prec_t interval = 0.0, const color& c = BLUE);
00072
00074 void set_pixel (size_t x, size_t y, const color& c);
00076 void set_pixel (const vect3d& p, const color& c);
00078 void set_pixel_merge (size_t x, size_t y, prec_t factor, const color& c);
00079
00081 void translate (const vect3d& p, size_t& out_x, size_t& out_y);
00083 void translate (const vect3d& p, prec_t& out_x, prec_t& out_y, prec_t& out_z);
00085 void reverse (size_t x, size_t y, vect3d& out_p);
00086
00087 prec_t get_dx () { return dx; }
00088 prec_t get_dy () { return dy; }
00089 prec_t get_dw () { return dx; }
00090 prec_t get_dh () { return dy; }
00091
00093 void write (const char *path);
00094
00096 void write (const std::string& path) { write (path.c_str()); }
00097
00098 prec_t get_space_width () { return space_width; }
00099
00100 size_t get_width () const { return width; }
00101 size_t get_height () const { return height; }
00102
00103 enum eplane { UNDEFINED = 0, PLANE_XY = 1, PLANE_YZ, PLANE_ZX };
00105 void set_axis (eplane plane, prec_t distance = 0.0);
00107 void set_axis (const vect3d& pos0, const vect3d& xaxix, const vect3d& yaxis);
00109 void set_perspective (bool p) { is_perspective = p; }
00111 void set_zbuffer (bool enable);
00112
00114 void draw_line (const vect3d& p0, const vect3d& p1, const color& c);
00115
00117 void draw_cross (const vect3d& p0, const color& c, prec_t len = 0.05);
00118
00119 const vect3d& get_center () const { return center; }
00120
00121 private:
00122 size_t width, height;
00123 prec_t space_width, space_height;
00124 prec_t dw, dh;
00125 prec_t dx, dy;
00126 color *smap;
00127 prec_t *zbuf;
00128 vect3d center, normal, xdir, ydir;
00129 prec_t plane_d;
00130 eplane plane_type;
00131 bool is_perspective;
00132
00133 void set_pixel_prec (prec_t x, prec_t y, const color& c);
00134 };
00135
00136 #endif // !__screen_hpp__
00137