#f is a null pointer for glut callbacks
[clinton/guile-figl.git] / doc / gl.texi
CommitLineData
22c0d71c
AW
1
2@node GL
3@chapter GL
4
5
6@menu
7* About OpenGL:: Know the past to understand the present.
8* GL Contexts:: Finding a square of pixels.
9* Rendering:: How to paint.
10* GL API:: The OpenGL interface, organized by section.
11* GL Enumerations:: Enumerated values.
12* Low-Level GL:: Primitive interface to OpenGL.
13* GL Extensions:: Beyond core OpenGL.
14@end menu
15
16
17@node About OpenGL
18@section About OpenGL
19
20OpenGL is a standard API for drawing three-dimensional graphics. From
21its origin in Silicon Graphics's workstations the early 1990s, today
22it has become ubiquitous, with implementations on mobile phones,
23televisions, tablets, desktops, and even web browsers.
24
25OpenGL has been able to achieve such widespread adoption not just
26because it co-evolved with powerful graphics hardware, but also
27because it was conceived of as an interface specification and not a
28piece of source code. In fact, these days it is a family of APIs,
29available in several flavors and versions:
30
31@table @asis
32@item OpenGL 1.x
33This series of specifications started with the original releases in
341992, and ended with OpenGL 1.5 in 2003. This era corresponds to a
35time when graphics cards were less powerful and more special-purpose,
36with dedicated hardware to handle such details as fog and lighting.
37As such the OpenGL 1.x API reflects the capabilities of these special
38units.
39
40@item OpenGL 2.x
41By the early 2000s, graphics hardware had become much more
42general-purpose and needed a more general-purpose API. The so-called
43@dfn{fixed-function rendering pipeline} of the earlier years was
44replaced with a @dfn{programmable rendering pipeline}, in which
45effects that would have required special hardware were instead
46performed by custom programs running on the graphics card. OpenGL
47added support for allocating @dfn{buffer objects} on the graphics
48card, and for @dfn{shader programs}, which did the actual rendering.
49In time, this buffer-focused API came to be the preferred form of
50talking to the GL.
51
52@item OpenGL ES
53OpenGL ES was a ``cut-down'' version of OpenGL 2.x, designed to be
54small enough to appeal to embedded device vendors. OpenGL ES 1.x
55removed some of the legacy functionality from OpenGL, while adding
56interfaces to use fixed-point math, for devices without floating-point
57units. OpenGL ES 2.x went farther still, removing the fixed-function
58pipeline entirely. OpenGL ES 2.x is common on current smart phone
59platforms.
60
61@item OpenGL 3.x and above
62The OpenGL 3.x series followed the lead of OpenGL ES, first
63deprecating (in 3.0) and then removing (in 3.1) the fixed-function
64pipeline. OpenGL 3.0 was released in 2008, but the free Mesa
65impementation only began supporting it in 2012, so it is currently
66(@value{UPDATED}) less common.
67@end table
68
69Figl wraps the OpenGL 2.1 API. It's a ubiquitous subset of the OpenGL
70implementations that are actually deployed in the wild; its legacy API
71looks back to OpenGL 1.x, while the buffer-oriented API is compatible
72with OpenGL ES.
73
74The full OpenGL 2.1 specification is available at
75@uref{http://www.opengl.org/registry/doc/glspec21.20061201.pdf}.
76
77
78@node GL Contexts
79@section GL Contexts
80
81All this talk about drawing is very well and good, but how do you
82actually get a canvas? Interestingly enough, this is outside the
83purview of the OpenGL specification. There are specific ways to get
84an @dfn{OpenGL context} for each different windowing system that is
85out there. OpenGL is all crayons and no paper.
86
87For the X window system, there is a standard API for creating a GL
88context given a window (or a drawable), @dfn{GLX}. @xref{GLX}, for
89more information on its binding in Guile.
90
91Bseides creating contexts from native windows or drawables, each
92backend also supports functions to make a context @dfn{current}. The
93OpenGL API is stateful; you can think of each call as taking an
94implicit @dfn{current context} parameter, which holds the current
95state of the GL and is operated on by the function in question.
96Contexts are thread-specific, and one context should not be active on
97more than one thread at a time.
98
99All calls to OpenGL functions must be made while a context is active;
100otherwise the result is undefined. Hopefully while you are getting
101used to this rule, your driver is nice enough not to crash on you if
102you call a function outside a GL context, but it's not even required
103to do that. Backend-specific functions may or may not require a
104context to be current; for example, Windows requires a context to be
105current, wheras GLX does not.
106
107There have been a few attempts at abstracting away the need for
108calling API specific to a given windowing system, notably GLUT and
109EGL. GLUT is the older of the two, and though it is practically
110unchanged since the mid-1990s, it is still widely used on desktops.
111@xref{GLUT}, for more on GLUT.
112
113EGL is technically part of OpenGL ES, and was designed with the modern
114OpenGL API and mobile hardware in mind, though it also works on the
115desktop. Figl does not yet have an EGL binding.
116
117
118@node Rendering
119@section Rendering
120
121To draw with OpenGL, you obtain a drawing context (@pxref{GL
122Contexts}) and send @dfn{the GL} some geometry. (You can think of the
123GL as a layer over your graphics card.) You can give the GL points,
124lines, and triangles in three-dimensional space. You configure your
125GL to render a certain part of space, and it takes your geometry,
126rasterizes it, and writes it to the screen (when you tell it to).
127
128That's the basic idea. You can customize most parts of this
129@dfn{rendering pipeline}, by specifying attributes of your geometry
130with the OpenGL API, and by programmatically operating on the geometry
131and the pixels with programs called @dfn{shaders}.
132
133GL is an @dfn{immediate-mode} graphics API, which is to say that it
134doesn't keep around a scene graph of objects. Instead, at every frame
135you as the OpenGL user have to tell the GL what is in the world, and
136how to paint it. It's a fairly low-level interface, but a powerful
137one. See
138@uref{http://www.opengl.org/wiki/Rendering_Pipeline_Overview}, for
139more details.
140
141In the old days of OpenGL 1.0, it was common to call a function to
142paint each individual vertex. You'll still see this style in some old
143tutorials. This quickly gets expensive if you have a lot of vertexes,
144though. This style, known as @dfn{Legacy OpenGL}, was deprecated and
145even removed from some versions of OpenGL. See
146@uref{http://www.opengl.org/wiki/Legacy_OpenGL}, for more on the older
147APIs.
148
149Instead, the newer thing to do is to send the geometry to the GL in a
150big array buffer, and have the GL draw geometry from the buffer. The
151newer functions like @code{glGenBuffers} allocate buffers, returning
152an integer that @dfn{names} a buffer managed by the GL. You as a user
153can update the contents of the buffer, but when drawing you reference
154the buffer by name. This has the advantage of reducing the chatter
155and data transfer between you and the GL, though it can be less
156convenient to use.
157
158So which API should you use? Use what you feel like using, if you
159have a choice. Legacy OpenGL isn't going away any time soon on the
160desktop. Sometimes you don't have a choice, though; for example, when
161targeting a device that only supports OpenGL ES 2.x, legacy OpenGL is
162unavailable.
163
164But if you want some advice, we suggest that you use the newer APIs.
165Not only will your code be future-proof and more efficient on the GL
166level, reducing the number of API calls improves performance, and it
167can reduce the amount of heap allocation in your program. All
168floating-point numbers are currently allocated on the heap in Guile,
169and doing less floating-point math in tight loops can only be a good
170thing.
171
172
173@node GL API
174@section GL API
175
176The procedures exported from the @code{(figl gl)} module are
177documented below, organized by their corresponding section in the
178OpenGL 2.1 specification.
179
180@example
181(use-modules (figl gl))
182@end example
183
184See @uref{http://www.opengl.org/registry/doc/glspec21.20061201.pdf},
185for more information.
186
187@menu
188* OpenGL Operation::
189* Rasterization::
190* Per Fragment Operations::
191* Special Functions::
192* State and State Requests::
193@end menu
194
195
196@node OpenGL Operation
197@subsection OpenGL Operation
198
199@subsubsection Begin/End Paradigm
200
201@defmac gl-begin begin-mode body ...
202Begin immediate-mode drawing with @var{begin-mode}, evaluate
203the sequence of @var{body} expressions, and then end drawing (as with
204@code{glBegin} and @code{glEnd}).
205
206The values produced by the last @var{body} expression are returned to
207the continuation of the @code{gl-begin}.
208@end defmac
209
210@defun gl-edge-flag boundary?
211Flag edges as either boundary or nonboundary. Note that the edge mode
212is only significant if the @code{polygon-mode} is @code{line} or
213@code{point}.
214@end defun
215
216@subsubsection Vertex Specification
217
218@defun gl-vertex x y [z=0.0] [w=1.0]
219Draw a vertex at the given coordinates.
220@end defun
221
222The following procedures modify the current per-vertex state. Drawing
223a vertex captures the current state and associates it with the
224vertex.
225
226@defun gl-texture-coordinate s [t=0.0] [r=0.0] [q=1.0]
227Set the current texture coordinate.
228@end defun
229
230@defun gl-multi-texture-coordinate texture s [t=0.0] [r=0.0] [q=1.0]
231Set the current texture coordinate for a specific texture unit.
232@end defun
233
234@defun gl-color red green blue [alpha=1.0]
235Set the current color.
236@end defun
237
238@defun gl-vertex-attribute index x [y=0.0] [z=0.0] [w=1.0]
239Set the current value of a generic vertex attribute.
240@end defun
241
242@defun gl-normal x y z
243Set the current normal vector. By default the normal should have unit
244length, though setting @code{(enable-cap rescale-normal)} or
245@code{(enable-cap normalize)} can change this.
246@end defun
247
248@defun gl-fog-coordinate coord
249Set the current fog coordinate.
250@end defun
251
252@defun gl-secondary-color red green blue
253Set the current secondary color.
254@end defun
255
256@defun gl-index c
257Set the current color index.
258@end defun
259
260@subsubsection Rectangles
261
262@defun gl-rectangle x1 y1 x2 y2
263Draw a rectangle in immediate-mode with a given pair of corner
264points.
265@end defun
266
267@subsubsection Coordinate Transformation
268
269@defun gl-depth-range near-val far-val
270Specify the mapping of the near and far clipping planes, respectively,
271to window coordinates.
272@end defun
273
274@defun gl-viewport x y width height
275Set the viewport: the pixel position of the lower-left corner of the
276viewport rectangle, and the width and height of the viewport.
277@end defun
278
279@defun gl-load-matrix m [#:transpose=#f]
280Load a matrix. @var{m} should be a packed vector in column-major
281order.
282
283Note that Guile's two-dimensional arrays are stored in row-major
284order, so you might need to transpose the matrix as it is loaded (via
285the @code{#:transpose} keyword argument).
286@end defun
287
288@defun gl-multiply-matrix m [#:transpose=#f]
289Multiply the current matrix by @var{m}. As with
290@code{gl-load-matrix}, you might need to transpose the matrix first.
291@end defun
292
293@defun set-gl-matrix-mode matrix-mode
294Set the current matrix mode. See the @code{matrix-mode} enumerator.
295@end defun
296
297@defmac with-gl-push-matrix body ...
298Save the current matrix, evaluate the sequence of @var{body}
299expressions, and restore the saved matrix.
300@end defmac
301
302@defun gl-load-identity
303Load the identity matrix.
304@end defun
305
306@defun gl-rotate angle x y z
307Rotate the current matrix about the vector
308@code{(@var{x},@var{y},@var{z})}. @var{angle} should be specified in
309degrees.
310@end defun
311
312@defun gl-translate x y z
313Translate the current matrix.
314@end defun
315
316@defun gl-scale x y z
317Scale the current matrix.
318@end defun
319
320@defun gl-frustum left right bottom top near-val far-val
321Multiply the current matrix by a perspective matrix. @var{left},
322@var{right}, @var{bottom}, and @var{top} are the coordinates of the
323corresponding clipping planes. @var{near-val} and @var{far-val}
324specify the distances to the near and far clipping planes.
325@end defun
326
327@defun gl-ortho left right bottom top near-val far-val
328Multiply the current matrix by a perspective matrix. @var{left},
329@var{right}, @var{bottom}, and @var{top} are the coordinates of the
330corresponding clipping planes. @var{near-val} and @var{far-val}
331specify the distances to the near and far clipping planes.
332@end defun
333
334@defun set-gl-active-texture texture
335Set the active texture unit.
336@end defun
337
338@defun gl-enable enable-cap
339@defunx gl-disable enable-cap
340Enable or disable server-side GL capabilities.
341@end defun
342
343@subsubsection Colors and Coloring
344
345@defun set-gl-shade-model mode
346Select flat or smooth shading.
347@end defun
348
349
350@node Rasterization
351@subsection Rasterization
352
353
354@node Per Fragment Operations
355@subsection Per-Fragment Operations
356
357@defun set-gl-stencil-function stencil-function k [#:mask] [#:face]
358Set the front and/or back function and the reference value @var{k} for
359stencil testing. Without the @var{face} keyword argument, both
360functions are set. The default @var{mask} is all-inclusive.
361@end defun
362
363@defun set-gl-stencil-operation stencil-fail depth-fail depth-pass [#:face]
364Set the front and/or back stencil test actions. Without the
365@var{face} keyword argument, both stencil test actions are set. See
366the @code{stencil-op} enumeration for possible values for
367@var{stencil-fail}, @var{depth-fail}, and @var{depth-pass}.
368@end defun
369
370@defun set-gl-blend-equation mode-rgb [mode-alpha=mode-rgb]
371Set the blend equation. With one argument, set the same blend
372equation for all components. Pass two arguments to specify a separate
373equation for the alpha component.
374@end defun
375
376@defun set-gl-blend-function src-rgb dest-rgb [src-alpha=src-rgb] [dest-alpha=dest-rgb]
377Set the blend function. With two arguments, set the same blend
378function for all components. Pass an additional two arguments to
379specify separate functions for the alpha components.
380@end defun
381
382@defun set-gl-scissor x y width height
383Define the scissor box. The box is defined in window coordinates,
384with (@var{x},@var{y}) being the lower-left corner of the box.
385@end defun
386
387@defun set-gl-sample-coverage value invert
388Specify multisample coverage parameters.
389@end defun
390
391@defun set-gl-alpha-function func ref
392Specify the alpha test function. See the @code{alpha-function}
393enumerator.
394@end defun
395
396@defun set-gl-depth-function func
397Specify the depth test function. See the @code{depth-function}
398enumerator.
399p@end defun
400
401@defun set-gl-blend-color r g b a
402Specify the blend color.
403@end defun
404
405@defun set-gl-logic-operation opcode
406Specify a logical pixel operation for color index rendering.
407@end defun
408
409@subsubsection Whole Framebuffer Operations
410
411@defun set-gl-draw-buffers buffers
412Specify a list of color buffers to be drawn into. @var{buffers}
413should be a list of @code{draw-buffer-mode} enumerated values.
414@end defun
415
416@defun set-gl-stencil-mask mask [#:face]
417Control the writing of individual bits into the front and/or back
418stencil planes. With one argument, the stencil mask for both states
419are set.
420@end defun
421
422@defun set-gl-draw-buffer mode
423Specify the buffer or buffers to draw into.
424@end defun
425
426@defun set-gl-index-mask mask
427Control the writing of individual bits into the color index buffers.
428@end defun
429
430@defun set-gl-color-mask red? green? blue? alpha?
431Enable and disable writing of frame buffer color components.
432@end defun
433
434@defun set-gl-depth-mask enable?
435Enable and disable writing into the depth buffer.
436@end defun
437
438@defun gl-clear mask
439Clear a set of buffers to pre-set values. Use the
440@code{clear-buffer-mask} enumerator to specify which buffers to clear.
441@end defun
442
443@defun set-gl-clear-color r g b a
444Set the clear color for the color buffers.
445@end defun
446
447@defun set-gl-clear-index c
448Set the clear index for the color index buffers.
449@end defun
450
451@defun set-gl-clear-depth depth
452Set the clear value for the depth buffer.
453@end defun
454
455@defun set-gl-clear-stencil-value s
456Set the clear value for the stencil buffer.
457@end defun
458
459@defun set-gl-clear-accumulation-color r g b a
460Set the clear color for the accumulation buffer.
461@end defun
462
463@defun set-gl-accumulation-buffer-operation op value
464Operate on the accumulation buffer. @var{op} may be one of the
465@code{accum-op} enumerated values. The interpretation of @var{value}
466depends on @var{op}.
467@end defun
468
469@subsubsection Drawing, Reading and Copying Pixels
470
471@defun set-gl-read-buffer mode
472Select a color buffer source for pixels. Use @code{read-buffer-mode}
473to select a mode.
474@end defun
475
476@defun gl-copy-pixels x y width height type
477Copy pixels from a screen-aligned rectangle in the frame buffer to a
478region relative to the current raster position. @var{type} selects
479which buffer to copy from.
480@end defun
481
482
483@node Special Functions
484@subsection Special Functions
485
486
487@node State and State Requests
488@subsection State and State Requests
489
490@subsubsection Querying GL State
491
492@defmac with-gl-push-attrib bits body ...
493Save part of the current state, evaluation the sequence of @var{body}
494expressions, then restore the state. Use @code{attrib-mask} to
495specify which parts of the state to save.
496@end defmac
497
498
499@node GL Enumerations
500@section GL Enumerations
501@include low-level-gl-enums.texi
502
503
504@node Low-Level GL
505@section Low-Level GL
506@include low-level-gl.texi
507
508
509@node GL Extensions
510@section GL Extensions
511
512@quotation
513The future is already here -- it's just not very evenly distributed.
514
515-- William Gibson
516@end quotation
517
518Before interfaces end up in core OpenGL, the are usually present as
519vendor-specific or candidate extensions. Indeed, the making of an
520OpenGL standard these days seems to be a matter of simply collecting a
521set of mature extensions and making them coherent.
522
523Figl doesn't currently provide specific interfaces for extensions.
524Perhaps it should, but that's a lot of work that we haven't had time
525to do. Contributions are welcome.
526
527In the meantime, if you know enough about GL to know that you need an
528extension, you can define one yourself -- after all, Figl is all a
529bunch of Scheme code anyway.
530
531For example, let's say you decide that you need to render to a
532framebuffer object. You go to @uref{http://www.opengl.org/registry/}
533and pick out an extension, say
534@uref{http://www.opengl.org/registry/specs/ARB/framebuffer_object.txt}.
535
536This extension defines a procedure, @code{GLboolean
537glIsRenderBuffer(GLuint)}. So you define it:
538
539@example
540(use-modules (figl gl runtime) (figl gl types))
541(define-gl-procedure (glIsRenderBuffer (buf GLuint) -> GLboolean)
542 "Render buffer predicate. Other docs here.")
543@end example
544
545And that's that. It's a low-level binding, but what did you expect?
546
547Note that you'll still need to check for the availability of this
548extension at runtime with @code{(glGetString GL_EXTENSIONS)}.
549