Skip to content

Commit ad7de18

Browse files
committed
Cleanup unfinished chapters
1 parent 69fb9bc commit ad7de18

24 files changed

+1259
-658
lines changed

01-preface.rst

+6-40
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ Preface
1010
This book is open-access (i.e. it's free to read at `this address
1111
<http://www.labri.fr/perso/nrougier/python+opengl>`_) because I believe
1212
knowledge should be free. However, if you think the book is worth a few
13-
dollars, you can `Buy the book`_. This money will help me to travel to Python
14-
conferences and to write other books as well. If you don't have money, it's
15-
fine. Just enjoy the book and spread the word about it. The teaser image above
16-
comes from the `artwork section
13+
dollars, you can give me a few euros (`5€
14+
<https://www.paypal.me/NicolasPRougier/5>`_ or `10€
15+
<https://www.paypal.me/NicolasPRougier/10>`_). This money will help me to
16+
travel to Python conferences and to write other books as well. If you don't
17+
have money, it's fine. Just enjoy the book and spread the word about it. The
18+
teaser image above comes from the `artwork section
1719
<http://www.labri.fr/perso/nrougier/artwork/index.html>`_ of my website. It has
1820
been made some years ago using the `Povray <http://www.povray.org>`_
1921
(Persistence of Vision) raytracer. I like it very much because it is a kind of
@@ -175,42 +177,6 @@ The code is licensed under the `OSI-approved BSD 2-Clause License
175177

176178

177179

178-
Buy the book
179-
-------------------------------------------------------------------------------
180-
181-
As you may have realized by now, the book is free for you to read
182-
online. However, some people prefer to have a PDF version or even a dead-tree
183-
version. For this to happen, I need to design a latex template for producing a
184-
nice PDF. That's a lot of work and I don't really have time since I'm also (and
185-
mainly) a `researcher
186-
<http://www.labri.fr/perso/nrougier/research/index.html>`_ with several
187-
students to supervise, researches to do, articles and grants to write, talks to
188-
prepare, etc.
189-
190-
Consequently, if you really want to have a PDF version, you'll have to
191-
explicitly express your interest by contributing a small amount of
192-
money. Then,
193-
194-
* if the total reach **5,000 euros**, I'll produce the PDF
195-
* if the total reach **10,000 euros**, I'll have the book printed and sold (20 euros).
196-
197-
However, be warned that you won't get refund if the first goal is not
198-
reached. In such case, consider your payment as a donation to the online
199-
version. If you find this unfair, remember you have the choice to give or not
200-
and the online version is free and open source...
201-
202-
.. image:: images/chapter-01/crowdfunding.png
203-
:width: 100%
204-
205-
206-
.. class:: button
207-
208-
* `5€ <https://www.paypal.me/NicolasPRougier/5>`_
209-
* `10€ <https://www.paypal.me/NicolasPRougier/10>`_
210-
* `25€ <https://www.paypal.me/NicolasPRougier/25>`_
211-
* `50€ <https://www.paypal.me/NicolasPRougier/50>`_
212-
213-
214180
.. --- Links ------------------------------------------------------------------
215181
.. _Nicolas P. Rougier:
216182
http://www.labri.fr/perso/nrougier/

10-polygons.rst

+60-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Rendering polygons
32
===============================================================================
43

@@ -32,8 +31,8 @@ line that cross a concave polygon at more than two points as shown on the
3231
figure above with the red lines.
3332

3433

35-
Convex polygons
36-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
34+
Convex polygons
35+
+++++++++++++++
3736

3837
For convex polygons, we have to consider two cases:
3938

@@ -95,8 +94,8 @@ are actually inside the polygon area.
9594
See `convex-polygon-fan.py <code/chapter-10/convex-polygon-fan.py>`_
9695

9796

98-
Concave polygons
99-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
97+
Concave polygons
98+
++++++++++++++++
10099

101100
For concave polygons, we could consider the two aforementionned cases where
102101
points are either ordered and describe the contour of the polygon or points are
@@ -211,15 +210,15 @@ Note that we also need to activate the stencil test in the `on_init` window even
211210
.. _SVG Specification: https://www.w3.org/TR/SVG/painting.html
212211

213212

214-
Non-zero fill rule
215-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
213+
Non-zero fill rule
214+
++++++++++++++++++
216215

217216
The non-zero fill rule implementation is easy because it corresponds to the
218217
default triangulation we've just seen above and no extra work is necessary.
219218

220219

221-
Odd-even fill rule
222-
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
220+
Odd-even fill rule
221+
++++++++++++++++++
223222

224223
In order to enforce the odd-even fill rule, we need to use a 2-pass
225224
rendering. The first pass will write to the stencil buffer according to the
@@ -303,13 +302,60 @@ stencil buffer:
303302
gl.glStencilOp(gl.GL_KEEP, gl.GL_KEEP, gl.GL_KEEP)
304303
305304
306-
Gradient and pattern
307-
-------------------------------------------------------------------------------
308-
309-
Antialiasing
310-
-------------------------------------------------------------------------------
311305
306+
312307
Exercises
313308
-------------------------------------------------------------------------------
314309

310+
Polygon gradients
311+
+++++++++++++++++
312+
313+
.. figure:: images/chapter-10/radial-gradient.png
314+
:figwidth: 25%
315+
:figclass: right
316+
317+
Figure
318+
319+
Radial gradient.
320+
321+
322+
The SVG specification considers two kind of color gradients (i.e. smooth
323+
transition from one color to another), radial and linear. Using the vertices
324+
coordinates inside the shader, it is thus very easy to create those gradients.
325+
In order to do that, you need to compute (for every fragment) a scalar that
326+
indicate tells the amount of color 1 and color 2 respectively and try to render
327+
the image on the right.
328+
329+
Solution: `radial-gradient.py <code/chapter-10/radial-gradient.py>`_
330+
331+
332+
Polygon Patterns
333+
++++++++++++++++
334+
335+
.. figure:: images/chapter-10/pattern.png
336+
:figwidth: 25%
337+
:figclass: right
338+
339+
Figure
340+
341+
Patterns.
342+
343+
We can also use any texture to pain the polygon. It's only a matter of
344+
assigning the right texture to polygon vertices. Try to render the image on the
345+
right using this `texture <images/chapter-10/wave.png>`_
346+
347+
Solution: `pattern.py <code/chapter-10/pattern.py>`_
348+
349+
350+
Antialiasing
351+
++++++++++++
352+
353+
As you have noticed, the polygon we've renderered so far are not antialised
354+
(because we've been using only raw triangles). While it might be possible to
355+
write a specific shader to take car of antiliasing on the border, it is far
356+
more easier to draw an antialiased polygon in two steps. First, we draw the
357+
interio of the polygon and then, we render a half-line on the contour. We need
358+
a half-line because we do not want the line to cover the already rendered
359+
polygon. There is no real difficulty and this is a good exercise. I will use
360+
the best proposed solution to be included here.
315361

11-meshes.rst

+132-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,138 @@
11
Rendering a mesh
22
===============================================================================
33

4-
.. contents:: .
5-
:local:
6-
:depth: 2
7-
:class: toc chapter-11
4+
Work in Progress.
85

9-
10-
Implicit surfaces
11-
-----------------
6+
..
7+
.. contents:: .
8+
:local:
9+
:depth: 2
10+
:class: toc chapter-11
1211
13-
Height fields
14-
-------------
1512

16-
Mesh models
17-
-----------
13+
Ok, it is now time to go 3D! I'm pretty sure you're reading this book only for
14+
this. I won't explain everything since you'll find a lot of online resources
15+
dedicated to 3D rendering using OpenGL. Instead, I'll concentrate on common
16+
techniques found in scientific visualization. The teaser image is a Klein
17+
bottle rendered using glumpy. Sources are available from the
18+
`geometric-parametric.py
19+
<https://github.com/glumpy/glumpy/blob/master/examples/geometry-parametric.py>`_
20+
example.
21+
22+
23+
Parametric surfaces
24+
-------------------
25+
26+
From Wikipedia:
27+
28+
*A parametric surface is a surface in the Euclidean space ℝ³ which is
29+
defined by a parametric equation with two parameters f(u,v) : ℝ² →
30+
ℝ³. Parametric representation is a very general way to specify a surface,
31+
as well as implicit representation.*
32+
33+
Surfaces such as `Boy's surface
34+
<https://en.wikipedia.org/wiki/Boy%27s_surface>`_, `Klein bottle
35+
<https://en.wikipedia.org/wiki/Klein_bottle>`_, `Möbius strip
36+
<https://en.wikipedia.org/wiki/Klein_bottle>`_ are all parametric surfaces that
37+
can be described using more or less simple equations. Let's consider the Boy's
38+
surface. Considering two parameters (u,v) in [0,π], the (x,y,z) surface writes (thanks `Mayavi <http://docs.enthought.com/mayavi/mayavi/auto/example_boy.html>`_):
39+
40+
.. code:: python
41+
42+
x = 2 / 3. * (cos(u) * cos(2 * v)
43+
+ sqrt(2) * sin(u) * cos(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
44+
y = 2 / 3. * (cos(u) * sin(2 * v) -
45+
sqrt(2) * sin(u) * sin(v)) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
46+
z = -sqrt(2) * cos(u) * cos(u) / (sqrt(2) - sin(2 * u) * sin(3 * v))
47+
48+
49+
What is nice with such (u,v) parameterization is that we can easily triangulate
50+
the surface. We only have to iterate over u and v and compute the corresponding
51+
vertices:
52+
53+
.. code:: python
54+
55+
vtype = [('position', np.float32, 3)]
56+
vcount += 1
57+
ucount += 1
58+
n = vcount*ucount
59+
Un = np.repeat(np.linspace(0, 1, ucount, endpoint=True), vcount)
60+
U = umin+Un*(umax-umin) # normalization
61+
Vn = np.tile (np.linspace(0, 1, vcount, endpoint=True), ucount)
62+
V = vmin+Vn*(vmax-vmin) # normalization
63+
vertices = np.zeros(n, dtype=vtype)
64+
for i,(u,v) in enumerate(zip(U,V)):
65+
x,y,z = func(u,v)
66+
vertices["position"][i] = x,y,z
67+
68+
69+
Then (and because this a regular surface), we can easily build the
70+
corresponding indices:
71+
72+
.. code:: python
73+
74+
for i in range(ucount-1):
75+
for j in range(vcount-1):
76+
indices.append(i*(vcount) + j )
77+
indices.append(i*(vcount) + j+1 )
78+
indices.append(i*(vcount) + j+vcount+1)
79+
indices.append(i*(vcount) + j+vcount )
80+
indices.append(i*(vcount) + j+vcount+1)
81+
indices.append(i*(vcount) + j )
82+
indices = np.array(indices, dtype=itype)
83+
84+
85+
.. figure:: images/chapter-11/boy-tesselation.png
86+
:figwidth: 100%
87+
88+
Figure
89+
90+
Tesselated Boy's surface using 16, 32, 64 and 128 steps.
91+
See `boy-tesselation.py <code/chapter-11/boy-tesselation.py>`_
92+
93+
94+
.. figure:: movies/chapter-11/boy.mp4
95+
:figclass: right
96+
:loop:
97+
:autoplay:
98+
:controls:
99+
:figwidth: 35%
100+
101+
Figure
102+
103+
Colored Boy's surface using an approximated Viridis colormap by `Jérôme
104+
Liard <https://www.shadertoy.com/user/blackjero>`_.
105+
See `boy.py <code/chapter-11/boy.py>`_
106+
107+
108+
We can make the rendering a bit nicer using colors according to (u,v)
109+
coordinates. For this, we just need to provide the fragment shader with the
110+
(uv) coordinate and decide how to paint the surface acccording to u, v or both.
111+
Furthermore, instead of drawing the surface twices to display the grid lines,
112+
we can directly render them in the same shader.
113+
114+
.. code:: glsl
115+
116+
varying vec2 v_uv;
117+
void main()
118+
{
119+
float x = 1 - sin(v_uv.x);
120+
vec4 color = vec4(vec3(x), 1.0);
121+
vec4 black = vec4(vec3(0.0), 1.0);
122+
vec2 d = fract((v_uv/M_PI)*64.0);
123+
vec2 f = fwidth(d);
124+
vec2 a = smoothstep(0.99-f, 0.99+f, d);
125+
gl_FragColor = mix(color, black, max(a.x,a.y));
126+
}
127+
128+
129+
130+
Height fields
131+
-------------
132+
133+
Height fields are also a very common object in scientific visualization. It
134+
allows to visualize a function of the type z = f(x,y) over a specific domain.
135+
136+
137+
Mesh models
138+
-----------

12-text.rst

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
Rendering text
22
===============================================================================
33

4-
.. contents:: .
5-
:local:
6-
:depth: 2
7-
:class: toc chapter-12
4+
Work in Progress.
85

9-
10-
Failsafe text
11-
-------------
6+
..
7+
.. contents:: .
8+
:local:
9+
:depth: 2
10+
:class: toc chapter-12
1211
13-
2D text
14-
-------
1512

16-
3D text
17-
-------
13+
Failsafe text
14+
-------------
15+
16+
2D text
17+
-------
18+
19+
3D text
20+
-------

13-framebuffer.rst

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
Framebuffer
22
===============================================================================
33

4-
.. contents:: .
5-
:local:
6-
:depth: 2
7-
:class: toc chapter-13
4+
Work in Progress.
85

9-
Post-processing
10-
---------------
6+
..
7+
.. contents:: .
8+
:local:
9+
:depth: 2
10+
:class: toc chapter-13
1111
12-
General computation
13-
-------------------
12+
Post-processing
13+
---------------
1414

15-
Color picking
16-
-------------
15+
General computation
16+
-------------------
17+
18+
Color picking
19+
-------------

0 commit comments

Comments
 (0)