File: one_familiar_triangle.rst

package info (click to toggle)
python-moderngl 5.12.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,700 kB
  • sloc: python: 15,758; cpp: 14,665; makefile: 14
file content (42 lines) | stat: -rw-r--r-- 1,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
.. py:currentmodule:: moderngl

One familiar triangle
=====================

As with any graphics library guide, we also have a guide on how to draw a triangle.
Below is a slightly modified line drawing code from the :doc:`previous tutorial <../first_rendering/rendering>`. The following code draws one triangle:

.. rubric:: Entire source

.. literalinclude:: triangle_draw.py
    :emphasize-lines: 35-41, 43, 51
    :linenos:

When you run the code you will see this:

.. figure:: triangle.png
    :alt: Triangle
    :align: center
    :figclass: align-center

    Triangle

As you may have noticed, we only specified three colors for each vertex, but OpenGL interpolates our triangle and we see a soft transition of colors.

At this point you can try out the fragment shader, for example, let's draw a lighting effect:

.. rubric:: Fragment shader

.. literalinclude:: lighted.frag.glsl
    :language: glsl
    :emphasize-lines: 8-9

.. figure:: lighted_triangle.png
    :alt: Lighted triangle
    :align: center
    :figclass: align-center

    Lighted triangle
    
Shaders are not very fond of branching algorithms and operations such as ``if (condition) {action} else {action}``. It is recommended to use formulas more often.