17 std::vector<float> data;
21 ZBuffer(
const int w,
const int h )
25 data.assign(w * h, 1);
28 void clear(
const float value= 1 )
30 data.assign(width * height,
value);
33 float& operator() (
const int x,
const int y )
35 const unsigned long int offset= y * width + x;
55 virtual Point vertex_shader(
const int vertex_id )
const = 0;
59 virtual Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const = 0;
77 :
Pipeline(), mesh(_mesh), model(_model), view(_view), projection(_projection)
79 mvp= projection * view * model;
81 mvn= make_normal_transform(mv);
84 Point vertex_shader(
const int vertex_id )
const
87 Point p= make_point( mesh.positions[vertex_id] );
92 return transform(mv, p);
95 Color fragment_shader(
const int primitive_id,
const Fragment fragment )
const
98 Vector a= transform(mvn, make_vector( mesh.normals[primitive_id * 3] ));
99 Vector b= transform(mvn, make_vector( mesh.normals[primitive_id * 3 +1] ));
100 Vector c= transform(mvn, make_vector( mesh.normals[primitive_id * 3 +2] ));
103 Vector n= fragment.u * c + fragment.v * a + fragment.w * b;
108 Color color= make_color(1.0 - (primitive_id % 100) / 99.f,
float(primitive_id % 10) / 9.f,
float(primitive_id % 1000) / 999.f);
109 return color * std::abs(n.z);
120 Vector pa= make_vector(p, a); pa.z= 0;
121 Vector pb= make_vector(p, b); pb.z= 0;
122 return cross(pa, pb).z;
128 Vector ab= make_vector(a, b);
129 Vector ac= make_vector(a, c);
130 Vector ap= make_vector(a, p);
136 int draw( ) {
return 0; }
139 int main(
int argc,
char **argv )
149 printf(
" %u positions\n", (
unsigned int) mesh.positions.size());
150 printf(
" %u indices\n", (
unsigned int) mesh.indices.size());
154 mesh_bounds(mesh, pmin, pmax);
159 Transform view= orbiter_view_transform(camera);
160 Transform projection= orbiter_projection_transform(camera, color.width(), color.height(), 45);
161 Transform viewport= make_viewport(color.width(), color.height());
169 Point d0= transform(tinv, make_point(0, 0, 0));
170 Point d1= transform(tinv, make_point(1, 0, 0));
171 Point d2= transform(tinv, make_point(0, 1, 0));
172 Vector dx= make_vector(d0, d1);
173 Vector dy= make_vector(d0, d2);
176 for(
unsigned int i= 0; i +2 < (
unsigned int) mesh.positions.size(); i= i +3)
179 Point a= pipeline.vertex_shader(i);
180 Point b= pipeline.vertex_shader(i+1);
181 Point c= pipeline.vertex_shader(i+2);
196 float n= volume(make_point(0, 0, 0), a, b, c);
204 Point e= make_point(0, 0, 0);
212 Vector face_eba=
cross(make_vector(b), make_vector(a));
213 Vector face_ecb=
cross(make_vector(c), make_vector(b));
214 Vector face_eac=
cross(make_vector(a), make_vector(c));
220 for(
int y= 0; y < color.height(); y++)
221 for(
int x= 0; x < color.width(); x++)
233 Point p= d0 + x*dx + y*dy;
238 frag.u=
dot(make_vector(p), face_eba);
239 frag.v=
dot(make_vector(p), face_ecb);
240 frag.w=
dot(make_vector(p), face_eac);
242 if(frag.u > 0 && frag.v > 0 && frag.w > 0)
245 float t=
dot(make_vector(a),
cross(make_vector(a, b), make_vector(a, c))) /
dot(make_vector(p),
cross(make_vector(a, b), make_vector(a, c)));
246 frag.u=
dot(t * make_vector(p), face_eba);
247 frag.v=
dot(t * make_vector(p), face_ecb);
248 frag.w=
dot(t * make_vector(p), face_eac);
251 float nabc= area(a, b, c);
252 frag.u= frag.u / nabc;
253 frag.v= frag.v / nabc;
254 frag.w= frag.w / nabc;
259 frag.z= frag.u * c.z + frag.v * a.z + frag.w * b.z;
262 Color frag_color= pipeline.fragment_shader(i/3, frag);
265 if(frag.z < depth(x, y))
267 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.
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.
Point center(const Point &a, const Point &b)
renvoie le milieu du segment ab.
float distance(const Point &a, const Point &b)
renvoie la distance etre 2 points.
float dot(const Vector &u, const Vector &v)
renvoie le produit scalaire de 2 vecteurs.
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.