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