20 std::vector<float> data;
24 ZBuffer(
const int w,
const int h,
const float z= 1 ) : data(w*h, z), width(w), height(h) {}
26 void clear(
const float value= 1 ) { data.assign(width * height, value); }
28 float& operator() (
const int x,
const int y )
30 std::size_t offset= y * width + x;
47 virtual ~Pipeline( ) {}
50 virtual Point vertex_shader(
const int vertex_id )
const = 0;
54 virtual Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const = 0;
61struct BasicPipeline :
public Pipeline
71 : Pipeline(), mesh(_mesh), model(_model), view(_view), projection(_projection)
73 mvp= projection * view * model;
77 Point vertex_shader(
const int vertex_id )
const
80 Point p=
Point( mesh.positions().at(vertex_id) );
85 Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const
88 Vector a= mv(
Vector( mesh.normals().at(primitive_id * 3) ));
89 Vector b= mv(
Vector( mesh.normals().at(primitive_id * 3 +1) ));
90 Vector c= mv(
Vector( mesh.normals().at(primitive_id * 3 +2) ));
93 Vector n= fragment.u * c + fragment.v * a + fragment.w * b;
98 return White() * std::abs(n.z);
111 return cross(pa, pb).z;
115bool visible(
const Point p )
117 if(p.x < -1 || p.x > 1)
return false;
118 if(p.y < -1 || p.y > 1)
return false;
119 if(p.z < -1 || p.z > 1)
return false;
124int main(
int argc,
char **argv )
126 Image color(640, 320);
127 ZBuffer depth(color.width(), color.height());
130 if(mesh == Mesh::error())
132 printf(
" %d positions\n", mesh.vertex_count());
133 printf(
" %d indices\n", mesh.index_count());
137 mesh.bounds(pmin, pmax);
144 camera.projection(color.width(), color.height(), 45) );
149 for(
unsigned int i= 0; i +2 < (
unsigned int) mesh.vertex_count(); i= i +3)
152 Point a= pipeline.vertex_shader(i);
153 Point b= pipeline.vertex_shader(i+1);
154 Point c= pipeline.vertex_shader(i+2);
157 if(visible(a) ==
false && visible(b) ==
false && visible(c) ==
false)
171 float n= area(a, b, c);
181 for(
int y= 0; y < color.height(); y++)
182 for(
int x= 0; x < color.width(); x++)
186 frag.u= area(
Point(x, y, 0), a, b);
187 frag.v= area(
Point(x, y, 0), b, c);
188 frag.w= area(
Point(x, y, 0), c, a);
190 if(frag.u > 0 && frag.v > 0 && frag.w > 0)
200 frag.z= frag.u * c.z + frag.v * a.z + frag.w * b.z;
203 Color frag_color= pipeline.fragment_shader(i/3, frag);
206 if(frag.z < depth(x, y))
208 color(x, y)=
Color(frag_color, 1);
representation d'une image.
representation d'un objet / maillage.
representation de la camera, type orbiter, placee sur une sphere autour du centre de l'objet.
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().
Color White()
utilitaire. renvoie une couleur blanche.
bool write_image(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Transform Normal(const Transform &m)
renvoie la transformation a appliquer aux normales d'un objet transforme par la matrice m.
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Transform Identity()
construit la transformation identite.
Vector normalize(const Vector &v)
renvoie un vecteur unitaire / longueur == 1.
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....
representation d'une couleur (rgba) transparente ou opaque.
representation d'un point 3d.
representation d'un vecteur 3d.