125{
126 Image color(640, 320);
127 ZBuffer depth(color.width(), color.height());
128
130 if(mesh == Mesh::error())
131 return 1;
132 printf(
" %d positions\n", mesh.vertex_count());
133 printf(
" %d indices\n", mesh.index_count());
134
135
137 mesh.bounds(pmin, pmax);
139
141 mesh,
143 camera.view(),
144 camera.projection(color.width(), color.height(), 45) );
145
147
148
149 for(unsigned int i= 0; i +2 < (unsigned int) mesh.vertex_count(); i= i +3)
150 {
151
152 Point a= pipeline.vertex_shader(i);
153 Point b= pipeline.vertex_shader(i+1);
154 Point c= pipeline.vertex_shader(i+2);
155
156
157 if(visible(a) == false && visible(b) == false && visible(c) == false)
158 continue;
159
160
161
162
163
164
165 a= viewport(a);
166 b= viewport(b);
167 c= viewport(c);
168
169
170
171 float n= area(a, b, c);
172 if(n < 0)
173 continue;
174
175
176
177
178
179
180
181 for(int y= 0; y < color.height(); y++)
182 for(int x= 0; x < color.width(); x++)
183 {
184
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);
189
190 if(frag.u > 0 && frag.v > 0 && frag.w > 0)
191 {
192
193 frag.u= frag.u / n;
194 frag.v= frag.v / n;
195 frag.w= frag.w / n;
196
197 frag.x= x;
198 frag.y= y;
199
200 frag.z= frag.u * c.z + frag.v * a.z + frag.w * b.z;
201
202
203 Color frag_color= pipeline.fragment_shader(i/3, frag);
204
205
206 if(frag.z < depth(x, y))
207 {
208 color(x, y)=
Color(frag_color, 1);
209 depth(x, y)= frag.z;
210 }
211
212
213
214
215
216 }
217 }
218 }
219
221 return 0;
222}
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().
bool write_image(const Image &image, const char *filename, const bool flipY)
enregistre une image au format .png
Transform Viewport(const float width, const float height)
renvoie la matrice representant une transformation viewport.
Transform Identity()
construit la transformation identite.
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.