18 std::vector<float> data;
22 ZBuffer(
const int w,
const int h,
const float z= 1 ) : data(w*h, z), width(w), height(h) {}
24 void clear(
const float value= 1 ) { data.assign(width * height,
value); }
26 float& operator() (
const int x,
const int y )
28 std::size_t offset= y * width + x;
48 virtual Point vertex_shader(
const int vertex_id )
const = 0;
52 virtual Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const = 0;
69 :
Pipeline(), mesh(_mesh), model(_model), view(_view), projection(_projection)
71 mvp= projection * view * model;
75 Point vertex_shader(
const int vertex_id )
const
78 Point p=
Point( mesh.positions().at(vertex_id) );
83 Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const
86 Vector a= mv(
Vector( mesh.normals().at(primitive_id * 3) ));
87 Vector b= mv(
Vector( mesh.normals().at(primitive_id * 3 +1) ));
88 Vector c= mv(
Vector( mesh.normals().at(primitive_id * 3 +2) ));
91 Vector n= fragment.u * c + fragment.v * a + fragment.w * b;
96 return White() * std::abs(n.z);
109 return cross(pa, pb).z;
113 bool visible(
const Point p )
115 if(p.x < -1 || p.x > 1)
return false;
116 if(p.y < -1 || p.y > 1)
return false;
117 if(p.z < -1 || p.z > 1)
return false;
122 int main(
int argc,
char **argv )
124 Image color(640, 320);
125 ZBuffer depth(color.width(), color.height());
130 printf(
" %d positions\n", mesh.vertex_count());
131 printf(
" %d indices\n", mesh.index_count());
135 mesh.bounds(pmin, pmax);
142 camera.
projection(color.width(), color.height(), 45) );
147 for(
unsigned int i= 0; i +2 < (
unsigned int) mesh.vertex_count(); i= i +3)
150 Point a= pipeline.vertex_shader(i);
151 Point b= pipeline.vertex_shader(i+1);
152 Point c= pipeline.vertex_shader(i+2);
155 if(visible(a) ==
false && visible(b) ==
false && visible(c) ==
false)
169 float n= area(a, b, c);
179 for(
int y= 0; y < color.height(); y++)
180 for(
int x= 0; x < color.width(); x++)
184 frag.u= area(
Point(x, y, 0), a, b);
185 frag.v= area(
Point(x, y, 0), b, c);
186 frag.w= area(
Point(x, y, 0), c, a);
188 if(frag.u > 0 && frag.v > 0 && frag.w > 0)
198 frag.z= frag.u * c.z + frag.v * a.z + frag.w * b.z;
201 Color frag_color= pipeline.fragment_shader(i/3, frag);
204 if(frag.z < depth(x, y))
206 color(x, y)=
Color(frag_color, 1);
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet...
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
representation d'un objet / maillage.
representation d'une couleur (rgba) transparente ou opaque.
Transform Identity()
construit la transformation identite.
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Transform Normal(const Transform &m)
renvoie la transformation a appliquer aux normales d'un objet transforme par la matrice m...
Transform view() const
renvoie la transformation vue.
representation d'un vecteur 3d.
Color White()
utilitaire. renvoie une couleur blanche.
Transform projection(const float width, const float height, const float fov) const
renvoie la projection reglee pour une image d'aspect width / height, et une ouverture de fov degres...
bool value(Widgets &w, const char *label, int &value, const int value_min, const int value_max, const int value_step)
valeur editable par increment.
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
int write_image(const Image &image, const char *filename)
enregistre une image dans un fichier png.
representation d'une image.
representation d'un point 3d.
Vector cross(const Vector &u, const Vector &v)
renvoie le produit vectoriel de 2 vecteurs.
Mesh read_mesh(const char *filename)
charge un fichier wavefront .obj et renvoie un mesh compose de triangles non indexes. utiliser glDrawArrays pour l'afficher. a detruire avec Mesh::release( ).