Changeset 31368 in project


Ignore:
Timestamp:
09/11/14 03:11:41 (7 years ago)
Author:
acharlton
Message:

wiki/gl-utils: Update to 0.2.0

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/gl-utils

    r31327 r31368  
    33Provides a higher level interface to OpenGL. The following modules are included:
    44
    5 * opengl-utils: Exports all of the modules in this egg
    6 * opengl-utils-core: Convenience procedures for OpenGL, abstracting over common tasks
    7 * opengl-utils-srfi-4: OpenGL-safe numeric vectors
    8 * opengl-utils-ply: [[http://paulbourke.net/dataformats/ply/|PLY]] file loading
     5* gl-utils: Exports all of the modules in this egg
     6* gl-utils-core: Convenience procedures for OpenGL, abstracting over common tasks
     7* gl-utils-bytevector: OpenGL-safe R7RS style bytevectors with extended accessors
     8* gl-utils-mesh: Convenient vertex array interface for creation, modification, and use
     9* gl-utils-ply: [[http://paulbourke.net/dataformats/ply/|PLY]] file loading
     10* gl-utils-srfi-4: OpenGL-safe numeric vectors
    911
    1012
     
    1416* miscmacros
    1517* srfi-42
     18* opengl-glew
     19* gl-math
    1620* make
    1721
     
    7781Create a framebuffer with a texture and depth renderbuffer attached. The texture and renderbuffer are given the dimensions {{WITH}} and {{HEIGHT}}. {{CHANNELS}} is the number of channels that the texture has: 1, 2, 3, or 4, corresponding to {{+red+}}, {{+rg+}}, {{+rgb+, and}}+rgba+ respectively, defaulting to 4. {{TYPE}} is the type of the texture data which defaults to {{+unsigned-byte+}}. Returns three values: The framebuffer, the texture, and the renderbuffer.
    7882
    79 <procedure> (make-vao VERTEX-DATA INDEX-DATA ATTRIBUTES [USAGE])</procedure>
    80 
    81 {{make-vao}} generalizes the typically repetitious code used to initialize vertex attribute objects. It deals with the case of having packed vertex data ({{VERTEX-DATA}}) and a vector of indices ({{INDEX-DATA}}) for those vertexes. This data may be passed as any sort of (srfi-4) vector or a blob.
    82 
    83 {{ATTRIBUTES}} is the list of data necessary for the vertex attributes, in the form of {{((LOCATION TYPE N [normalize?: NORMALIZE?]) ...)}}. {{LOCATION}} is the attribute location which may be given as {{#f}} if the attribute is not used. {{TYPE}} is the type of data corresponding to the given attribute, given as a keyword. For possible types, see {{type->gl-type}}. {{N}} is the number of elements for the given attribute. The keyword {{normalize?:}} accepts a boolean argument which instructs OpenGL to normalize the attribute or not. Defaults to {{#f}}.
    84 
    85 The optional {{USAGE}} must be one of {{+stream-data+}}, {{+stream-read+}}, {{+stream-copy+}}, {{+static-data+}}, {{+static-read+}}, {{+static-copy+}}, {{+dynamic-data+}}, {{+dynamic-read+}}, {{+dynamic-copy+}}. Defaults to {{+static-draw+}}.
    86 
    87 {{make-vao}} returns the ID of a vertex array object. This object should be deleted with {{delete-vertex-array}} when no longer used. Intermediate vertex buffers are generated and deleted, thus only the returned vertex array ID needs to be managed.
    88 
    8983<procedure> (->pointer VECTOR)</procedure>
    9084
     
    9791<procedure> (type->bytes TYPE)</procedure>
    9892
    99 Returns the size of {{TYPE}} (as accepted by {{type->gl-type}}) in number of bytes.
    100 
    101 <procedure> (type->gl-type TYPE)</procedure>
     93Returns the size of {{TYPE}} (as accepted by {{type->gl}}) in number of bytes.
     94
     95<procedure> (type->gl TYPE)</procedure>
    10296
    10397Converts the keyword {{TYPE}} into a OpenGL type enum value. Accepted types (grouped by synonyms) are:
     
    113107
    114108
     109==== gl-utils-bytevector
     110r7rs style bytevectors with unsafe accessors. As in gl-utils-srfi4, bytevectors are created in non-garbage-collected memory. They will still be freed when no longer used.
     111
     112<procedure> (bytevector BYTE ...)</procedure>
     113
     114Returns a newly allocated bytevector containing its arguments.
     115
     116<procedure> (make-bytevector K [BYTE] [NONGC] [FINALIZE])</procedure>
     117
     118Return a newly-allocated bytevector of length {{K}}. If the optional fill {{BYTE}} is specified, it specifies the initial value for each slot in the bytevector.
     119
     120The optional arguments {{NONGC}} and {{FINALIZE}} define whether the vector should be allocated in a memory area not subject to garbage collection and whether the associated storage should be automatically freed (using finalization) when there are no references from Scheme variables and data. {{NONGC}} defaults to #t (the vector will be located in non-garbage-collected memory) and {{FINALIZE}} defaults to #t. Note that the {{FINALIZE}} argument is only used when {{NONGC}} is true.
     121
     122<procedure> (bytevector-length BYTEVECTOR)</procedure>
     123
     124Return the length in bytes of {{BYTEVECTOR}}.
     125
     126<procedure> (bytevector? BYTEVECTOR)</procedure>
     127
     128Returns true if {{BYTEVECTOR}} is a bytevector, false otherwise.
     129
     130<procedure> (bytevector-u8-set! BYTEVECTOR K UNSIGNED-BYTE)</procedure>
     131<procedure> (bytevector-s8-set! BYTEVECTOR K BYTE)</procedure>
     132<procedure> (bytevector-u16-set! BYTEVECTOR K UNSIGNED-SHORT)</procedure>
     133<procedure> (bytevector-s16-set! BYTEVECTOR K SHORT)</procedure>
     134<procedure> (bytevector-u32-set! BYTEVECTOR K UNSIGNED-INT)</procedure>
     135<procedure> (bytevector-s32-set! BYTEVECTOR K INT)</procedure>
     136<procedure> (bytevector-u64-set! BYTEVECTOR K UNSIGNED-LONG)</procedure>
     137<procedure> (bytevector-s64-set! BYTEVECTOR K LONG)</procedure>
     138<procedure> (bytevector-f32-set! BYTEVECTOR K FLOAT)</procedure>
     139<procedure> (bytevector-f64-set! BYTEVECTOR K DOUBLE)</procedure>
     140
     141Sets the byte {{K}} of the given bytevector to be the value of the given fixnum or flonum. These functions are unsafe, so be sure {{K}} is a valid location in the bytevector.
     142
     143<procedure> (bytevector-u8-ref BYTEVECTOR K)</procedure>
     144<procedure> (bytevector-s8-ref BYTEVECTOR K)</procedure>
     145<procedure> (bytevector-u16-ref BYTEVECTOR K)</procedure>
     146<procedure> (bytevector-s16-ref BYTEVECTOR K)</procedure>
     147<procedure> (bytevector-u32-ref BYTEVECTOR K)</procedure>
     148<procedure> (bytevector-s32-ref BYTEVECTOR K)</procedure>
     149<procedure> (bytevector-u64-ref BYTEVECTOR K)</procedure>
     150<procedure> (bytevector-s64-ref BYTEVECTOR K)</procedure>
     151<procedure> (bytevector-f32-ref BYTEVECTOR K)</procedure>
     152<procedure> (bytevector-f64-ref BYTEVECTOR K)</procedure>
     153
     154Returns the fixnum or flonum of the given size located at byte {{K}} of the given bytevector. These functions are unsafe, so be sure {{K}} is a valid location in the bytevector.
     155
     156<procedure> (bytevector–>pointer BYTEVECTOR)</procedure>
     157
     158Returns the pointer to {{BYTEVECTOR}}’s data.usage
     159
     160<procedure> (bytevector-append BYTEVECTOR . BYTEVECTORSS)</procedure>
     161
     162Returns a newly allocated bytevector whose elements are the concatenation of the elements in the given bytevectors. If only one argument is passed to {{bytevector-append}}, it is assumed that it is a list of bytevectors.
     163
     164<procedure> (bytevector-copy BYTEVECTOR [START] [END])</procedure>
     165
     166Returns a newly allocated copy of the elements of the given bytevector between {{START}} and {{END}}.
     167
     168<procedure> (bytevector-copy! TO AT FROM [START] [END])</procedure>
     169
     170Copies the elements of bytevector {{FROM}} between {{START}} and {{END}} to bytevector {{TO}}, starting at {{AT}}. It is an error if {{AT}} is less than zero or greater than the length of {{TO}}. It is also an error if {{(- (bytevector-length TO) AT)}} is less than {{(- END START)}}.
     171
     172
     173==== gl-utils-mesh
     174<procedure> (make-mesh vertices: VERTICES [indices: INDICES] [mode: MODE])</procedure>
     175
     176Create a new mesh. {{VERTICES}} is a list of key value pairs that specifies the mesh’s vertex data. It should be in the form:
     177
     178    (attributes: ATTRIBUTES [initial-elements: INITIAL-ELEMENTS] [n-vertices: N-VERTICES])
     179
     180{{ATTRIBUTES}} is a list in the form:
     181
     182    (NAME TYPE N [normalized: NORMALIZED] [location: LOCATION])
     183
     184where {{NAME}} is the attribute name (as a symbol), {{TYPE}} is the type of the attribute as accepted by {{type->gl}}, {{N}} is the number of elements in the attribute, {{NORMALIZED}} is a boolean value indicating whether the attribute’s values should be normalized (defaulting to {{#f}}), and {{LOCATION}} is the shader attribute-location of the attribute (defaulting to -1 and settable by {{mesh-attribute-locations-set!}}).
     185
     186{{INITIAL-ELEMENTS}} is either a bytevector or a list of {{(NAME . VALUE)}} pairs where name is the name of the attribute to set (as per the name given in {{ATTRIBUTES}}) and {{VALUE}} is the initial contents of that attribute. When a list is given and more than one attribute is given initial-elements, the {{VALUE}}s should represent the same number of vertices. Values associated with attributes that are {{NORMALIZED}} should be provided as float between {{0.0}} and {{1.0}} (for unsigned types) or {{-1.0}} and {{1.0}}, which are then normalized. If {{INITIAL-ELEMENTS}} is given as a bytevector, that bytevector is used as the entire mesh’s vertex data and {{N-VERTICES}} – the number of vertices – must be provided.
     187
     188{{INDICES}} is an optional list of key value pairs that specifies the mesh’s index data, if it exists. It should be in the form:
     189
     190    (type: TYPE [initial-elements: INITIAL-ELEMENTS] [n-indices: N-INDICES])
     191
     192{{TYPE}} is a type keyword (as accepted by {{type->gl}}) that must be a valid type for an element array buffer (i.e. an unsigned fixnum). {{INITIAL-ELEMENTS}} is either a bytevector or a list of values. If {{INITIAL-ELEMENTS}} is given as a bytevector,  {{N-INDICES}} – the number of indices – must be provided.
     193
     194{{MODE}} is the keyword (as accepted by {{mode->gl}}) that defines what the mesh is supposed to represent. Defaults to {{#:triangles}}.
     195
     196<record> (mesh VERTEX-ATTRIBUTES INDEX-TYPE VERTEX-DATA INDEX-DATA N-VERTICES N-INDICES VERTEX-BUFFER INDEX-BUFFER VAO STRIDE MODE USAGE)</record>
     197
     198The type of record returned by {{make-mesh}}. {{VERTEX-ATTRIBUTES}} is a list of {{vertex-attribute}} records. {{INDEX-TYPE}} is the type given to the {{indices:}} argument of {{make-mesh}}. {{VERTEX-DATA}} and {{INDEX-DATA}} are the bytevectors representing the vertex and index data. {{N-VERTICES}} and {{N-INDICES}} are the number of vertices and indices present in the data. {{VERTEX-BUFFER}}, {{INDEX-BUFFER}} and {{VAO}} are the vertex buffers and VAO created by {{mesh-make-vao!}}. {{STRIDE}} is the number of bytes between the start of consecutive vertices. {{MODE}} is the value of the {{mode:}} argument provided to {{make-mesh}}. {{USAGE}} is the buffer usage that is set with {{make-mesh-vao!}}.
     199
     200<record> (vertex-attribute name type number normalized location)</record>
     201
     202The type of record returned by {{mesh-vertex-attributes}}. Getters for all of the fields are provided.
     203
     204<procedure> (mesh-attribute-locations-set! MESH LOCATIONS)</procedure>
     205
     206Set the shader attribute locations of the attributes of {{MESH}}. {{LOCATIONS}} is a list of {{(ATTRIBUTE-NAME . LOCATION)}} pairs.
     207
     208<procedure> (mesh-make-vao! MESH [USAGE])</procedure>
     209
     210Create a vertex attribute object (VAO) for {{MESH}}. {{USAGE}} is the buffer usage hint keyword as accepted by {{usage->gl}}, defaulting to {{#:static}}. Vertex buffer objects (VBOs) are created for the vertex and index data. The VAO binds these buffers, and sets the vertex attribute pointers. If the usage is one of the static types, the vertex and index data of the mesh are deleted, as are the vertex and index buffers. Attribute locations should be set for the mesh with {{mesh-attribute-locations-set!}} before calling {{mesh-make-vao!}}. The VBOs and VAO created by {{make-mesh-vao!}} are managed and should not be deleted.
     211
     212<procedure> (mesh-vertex-ref MESH ATTRIBUTE VERTEX)</procedure>
     213
     214Return a vector containing the values of the attribute named {{ATTRIBUTE}} corresponding to the {{VERTEX}}th vertex of the {{MESH}}. The result will be a srfi-4 numeric vector, corresponding to the type of the given attribute.
     215
     216<procedure> (mesh-vertex-set! MESH ATTRIBUTE VERTEX VALUE)</procedure>
     217
     218Set the {{VERTEX}}th vertex of the {{MESH}}’s attribute named {{ATTRIBUTE}} to the values provided by the srfi-4 numeric vector {{VALUE}}. {{VALUE}} must correspond to the type of the attribute, and should be the same length as the attribute. Using a {{VALUE}} that is too short is unsafe.
     219
     220If {{mesh-make-vao!}} has been called already, {{mesh-vertex-set!}} should be called inside a {{with-mesh}}. If {{mesh-make-vao!}} was called with a static usage, {{mesh-vertex-set!}} will result in an error, since there is no longer any vertex data to set.
     221
     222<procedure> (with-mesh MESH THUNK)</procedure>
     223
     224Calls {{THUNK}} with the VBO of the {{MESH}}’s vertex buffer bound. If the mesh has been modified by {{mesh-vertex-set!}}, the vertex buffer’s data will be updated before the VBO is unbound. When the usage hint given to {{mesh-make-vao!}} is dynamic, {{buffer-sub-data}} will be used to update the buffer data. When the usage hint is stream, {{buffer-data}} will be used.
     225
     226<procedure> (mesh-copy! TO AT FROM [START] [END])</procedure>
     227
     228Similar to {{bytevector-copy!}}, copies the vertices of mesh {{FROM}} between vertices {{START}} and {{END}} to mesh {{TO}}, starting at vertex {{AT}}.
     229
     230<procedure> (mesh-copy MESH)</procedure>
     231
     232Creates a fresh copy of {{MESH}}.
     233
     234<procedure> (mesh-append MESH . MESHES)</procedure>
     235
     236Creates a new mesh resulting from appending the vertices of the given meshes together. The indices are also appended and modified so they point to the same vertices. The attributes of all the meshes are assumed to be the same, otherwise bad things will probably happen. If only one argument is passed to {{mesh-append}} it is assumed that it is a list of meshes.
     237
     238<procedure> (mesh-transform! POSITION-NAME MESH TRANSFORM)</procedure>
     239
     240Destructively modifies the {{POSITION-NAME}} attribute of {{MESH}} by the [[http://wiki.call-cc.org/eggref/4/gl-math|gl-math]] matrix {{TRANSFORM}}. {{POSITION-NAME}} must be the name of a three element float attribute of {{MESH}}.
     241
     242<procedure> (mesh-transform-append POSITION-NAME MESH-TRANSFORM-PAIR . MESH-TRANSFORM-PAIRS)</procedure>
     243
     244Creates a new mesh resulting by appending all the given meshes together, then transforming the attribute named by {{POSITION-NAME}} by the given [[http://wiki.call-cc.org/eggref/4/gl-math|gl-math]] transform matrices. {{MESH-TRANSFORM-PAIR}} and {{MESH-TRANSFORM-PAIRS}} are {{(MESH . TRANFORM)}} pairs. {{POSITION-NAME}} must be the name of a three element float attribute of {{MESH}}. The attributes of all the meshes are assumed to be the same, otherwise bad things will probably happen. If only two arguments are passed to {{mesh-transform-append}} it is assumed that the second is a list of mesh/transform pairs.
     245
     246<procedure> (usage->gl USAGE)</procedure>
     247
     248Converts the keyword {{USAGE}} into a OpenGL usage enum value. Accepted usages (grouped by synonyms) are:
     249
     250* {{dynamic:}} {{dynamic-draw:}}
     251* {{stream:}} {{stream-draw:}}
     252* {{static:}} {{static-draw:}}
     253* {{dynamic-read:}}
     254* {{stream-read:}}
     255* {{static-read:}}
     256* {{dynamic-copy:}}
     257* {{stream-copy:}}
     258* {{static-copy:}}
     259
     260This line is here to prevent a Markdown parsing error :|
     261
     262<procedure> (mode->gl MODE)</procedure>
     263
     264Converts the keyword {{MODE}} into a OpenGL mode enum value. Accepted modes are:
     265
     266* {{points:}}
     267* {{line-strip:}}
     268* {{line-loop:}}
     269* {{line-strip-adjacency:}}
     270* {{lines-adjacency:}}
     271* {{triangle-strip:}}
     272* {{triangle-fan:}}
     273* {{triangles:}}
     274* {{triangle-strip-adjacency:}}
     275* {{triangles-adjacency:}}
     276* {{patches:}}
     277
     278
     279==== gl-utils-ply
     280<procedure> (load-ply FILE BUFFER-SPEC)</procedure>
     281
     282Loads a [[http://paulbourke.net/dataformats/ply/|PLY]] file. {{FILE}} is a path that may be pointing either to a gziped PLY file or a regular PLY file. {{BUFFER-SPEC}} is a list in the form {{((NAME VARS) ...)}} where {{NAME}} is the name of an element in the PLY file and {{VARS}} is either a list of property names or, in the case of a property list, a single name. Two values are returned: a list of bytevectors which correspond to the buffers named in {{BUFFER-SPEC}} and a list of the elements that are in the PLY file in the form of:
     283
     284    (element-name n-elements (property-name property-type))
     285
     286Or, when an element is a property list:
     287
     288    (element-name n-elements (property-name (list: list-length-type element-type)))
     289
     290The buffers returned are packed with the contents of the properties named in the {{BUFFER-SPEC}}. Thus, for a PLY file that has element {{vertex}} with properties {{float x}}, {{float y}}, {{float z}}, {{float confidence}}, {{uchar r}}, {{uchar g}}, and {{uchar b}}, as well as an element {{face}} with a property list {{uchar ushort vertex_index}}, the following {{BUFFER-SPEC}} could be used:
     291
     292    (load-ply "example.ply.gz" '((vertex: (x y z r g b)) (face: vertex_index)))
     293
     294This buffer spec would result in a list of two u8vectors being returned: one with the packed elements corresponding to properties {{x}}, {{y}}, {{z}}, {{r}}, {{g}}, and {{b}} (with the corresponding property types), and the second containing the vertex indices.
     295
     296<procedure> (load-ply-mesh FILE vertex: VERTEX face: FACE)</procedure>
     297
     298Similar to load-ply, but returns a mesh. {{FILE}} is a PLY file (which may be gziped). The PLY file must contain at least the elements {{vertex}} and {{face}} (other elements will be ignored). {{VERTEX}} is a list of {{(ATTRIBUTE-NAME PROPERTY-NAME ...)}} elements, which specifies which PLY properties are associate with which attribute, in which order. Attributes are all normalized. All properties named by each element of {{VERTEX}} must be of the same type. {{FACE}} is the name of the face property list.
     299
     300Again, for a PLY file that has element {{vertex}} with properties {{float x}}, {{float y}}, {{float z}}, {{float confidence}}, {{uchar r}}, {{uchar g}}, and {{uchar b}}, as well as a element {{face}} with a property list {{uchar ushort vertex_index}}, the following could be used:
     301
     302    (load-ply-mesh "example.ply" vertex: `((position x y z)
     303                                          (color r g b))
     304                                face: vertex_index)
     305
     306This would create a mesh with vertex attributes {{(position #:float 3)}} and {{(color #:unsigned-byte 3 normalized: #t)}} and the {{#:unsigned-short}} indices given by {{vertex_index}}.
     307
     308
    115309==== gl-utils-srfi4
    116310gl-utils-srfi4 reexports a version of [[http://api.call-cc.org/doc/srfi-4|srfi-4]] that gives preference to vectors being created in non-garbage collected memory. This is useful for use with OpenGL, since it is often desirable to pass vectors to OpenGL that will remain in one place. All srfi-4 functions not mentioned below are reexported without changes.
    117311
    118 The {{NNNvector}} and {{list->NNNvector}} constructors have been modified so that they return vectors in non-garbage collected memory.
    119 
    120 The {{make-NNNvector}} constructors act as their srfi-4 counterparts, except they return vectors in non-garbage collected memory by default.
    121 
    122 
    123 ==== gl-utils-ply
    124 <procedure> (load-ply FILE BUFFER-SPEC)</procedure>
    125 
    126 Loads a [[http://paulbourke.net/dataformats/ply/|PLY]] file. {{FILE}} is a path that may be pointing either to a gziped PLY file or a regular PLY file. {{BUFFER-SPEC}} is a list in the form {{((NAME VARS) ...)}} where {{NAME}} is the name of an element in the PLY file and {{VARS}} is either a list of property names or, in the case of a property list, a single name. Two values are returned: a list of u8vectors which correspond to the buffers named in {{BUFFER-SPEC}} and a list of the elements that are in the PLY file in the form of:
    127 
    128     (element-name n-elements (property-name property-type))
    129 
    130 Or, when an element is a property list:
    131 
    132     (element-name n-elements (property-name (list: list-length-type element-type)))
    133 
    134 The buffers returned are packed with the contents of the properties named in the {{BUFFER-SPEC}}. Thus, for a PLY file that has element {{vertex}} with properties {{float x}}, {{float y}}, {{float z}}, {{float confidence}}, {{uchar r}}, {{uchar g}}, and {{uchar b}}, as well as an element {{face}} with a property list {{uchar ushort vertex_index}}, the following {{BUFFER-SPEC}} could be used:
    135 
    136     (load-ply "example.ply.gz" '((vertex: (x y z r g b)) (face: vertex_index)))
    137 
    138 This buffer spec would result in a list of two u8vectors being returned: one with the packed elements corresponding to properties {{x}}, {{y}}, {{z}}, {{r}}, {{g}}, and {{b}} (with the corresponding property types), and the second containing the vertex indices.
    139 
    140 <procedure> (load-ply-vao FILE vertex: VERTEX face: FACE)</procedure>
    141 
    142 Similar to {{load-ply}}, but returns a number of values:
    143 
    144 * A vertex array ID as generated by {{make-vao}}.
    145 * A u8vector representing the vertex data of the model
    146 * A u8vector representing the index data of the model
    147 * The number of vertices of the model
    148 * The GL enum value of the type of primitive used for the model (e.g. {{+triangles+}})
    149 * The GL enum value of the element data type
    150 
    151 {{FILE}} is a PLY file (which may be gziped). The PLY file must contain at least the elements {{vertex}} and {{face}} (other elements will be ignored). {{VERTEX}} is a list of {{(attribute-location property-name ...)}} elements, which specifies how the vertex buffers of the VAO will be arranged. All properties named by each element of {{VERTEX}} must be of the same type. {{FACE}} is the name of the face property list.
    152 
    153 Again, for a PLY file that has element {{vertex}} with properties {{float x}}, {{float y}}, {{float z}}, {{float confidence}}, {{uchar r}}, {{uchar g}}, and {{uchar b}}, as well as a element {{face}} with a property list {{uchar ushort vertex_index}}, the following could be used:
    154 
    155     (load-ply-vao "example.ply" vertex: `((,vertex-location x y z)
    156                                           (,color-location r g b))
    157                                 face: vertex_index)
     312The {{NNNvector}} and {{list->NNNvector}} constructors have been modified so that they return vectors in non-garbage collected memory. They will still be freed when no longer used.
     313
     314The {{make-NNNvector}} constructors act as their srfi-4 counterparts, except they return vectors in non-garbage collected memory by default. They will still be freed when non longer used.
    158315
    159316
     
    161318This example depends on the [[http://wiki.call-cc.org/eggref/4/opengl-glew|opengl-glew]] egg, the [[http://wiki.call-cc.org/eggref/4/glfw3|glfw3]] egg for window and context creation, and the [[http://wiki.call-cc.org/eggref/4/gl-math|gl-math]] egg for matrix math.
    162319
     320For more examples, check out the [[https://github.com/AlexCharlton/gl-utils/tree/master/examples|examples directory]].
     321
    163322<enscript highlight="scheme">   
    164323(import chicken scheme)
    165 (use (prefix glfw3 glfw:) (prefix opengl-glew gl:) gl-math (prefix gl-utils gl:)
    166      gl-utils-srfi-4)
     324(use (prefix glfw3 glfw:) (prefix opengl-glew gl:) gl-math gl-utils)
    167325
    168326(define *vertex*
    169327#<<END
    170328#version 330
    171 in vec2 vertex;
     329in vec2 position;
    172330in vec3 color;
    173331out vec3 c;
     
    175333
    176334void main(){
    177    gl_Position = MVP * vec4(vertex, 0.0, 1.0);
     335   gl_Position = MVP * vec4(position, 0.0, 1.0);
    178336   c = color;
    179337}
     
    192350)
    193351
    194 (define vertex-data (f32vector -1 -1 1 0 0
    195                                1 -1 0 1 0
    196                                1 1 0 0 1
    197                                -1 1 1 0 1))
    198 
    199 (define index-data (u16vector 0 1 2
    200                               0 2 3))
    201 
    202 (define vao (make-parameter #f))
     352(define rect (make-mesh
     353              vertices: '(attributes: ((position #:float 2)
     354                                       (color #:unsigned-byte 3
     355                                              normalized: #t))
     356                          initial-elements: ((position . (-1 -1
     357                                                           1 -1
     358                                                           1  1
     359                                                           -1  1))
     360                                             (color . (255 0   0
     361                                                       0   255 0
     362                                                       0   0   255
     363                                                       255 0   255))))
     364              indices: '(type: #:ushort
     365                         initial-elements: (0 1 2
     366                                            0 2 3))))
    203367
    204368(define program (make-parameter #f))
     
    220384                        (m* projection-matrix
    221385                            (m* view-matrix model-matrix)))
    222   (gl:bind-vertex-array (vao))
    223   (gl:draw-elements-base-vertex gl:+triangles+ 6 (gl:type->gl-type ushort:) #f 0)
    224 
    225   (gl:check-error)
     386  (gl:bind-vertex-array (mesh-vao rect))
     387  (gl:draw-elements-base-vertex (mode->gl (mesh-mode rect))
     388                                (mesh-n-indices rect)
     389                                (type->gl (mesh-index-type rect))
     390                                #f 0)
     391
     392  (check-error)
    226393  (gl:bind-vertex-array 0))
    227394
     
    233400  (print (gl:supported? "GL_ARB_framebuffer_object"))
    234401
    235   (set! *vertex* (gl:make-shader gl:+vertex-shader+ *vertex*))
    236   (set! *fragment* (gl:make-shader gl:+fragment-shader+ *fragment*))
    237 
    238   (program (gl:make-program (list *vertex* *fragment*)))
    239 
    240   (vao (gl:make-vao vertex-data index-data
    241                     `((,(gl:get-attrib-location (program) "vertex") float: 2)
    242                       (,(gl:get-attrib-location (program) "color") float: 3))))
     402  (set! *vertex* (make-shader gl:+vertex-shader+ *vertex*))
     403  (set! *fragment* (make-shader gl:+fragment-shader+ *fragment*))
     404  (program (make-program (list *vertex* *fragment*)))
     405
     406  (mesh-attribute-locations-set! rect `((position . ,(gl:get-attrib-location
     407                                                      (program) "position"))
     408                                        (color . ,(gl:get-attrib-location
     409                                                   (program) "color"))))
     410  (mesh-make-vao! rect)
    243411  (let loop ()
    244      (glfw:swap-buffers (glfw:window))
    245      (gl:clear (bitwise-ior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
    246      (render)
    247      (glfw:poll-events) ; Because of the context version, initializing GLEW results in a harmless invalid enum
    248      (unless (glfw:window-should-close (glfw:window))
    249        (loop))))
     412    (glfw:swap-buffers (glfw:window))
     413    (gl:clear (bitwise-ior gl:+color-buffer-bit+ gl:+depth-buffer-bit+))
     414    (render)
     415    (glfw:poll-events) ; Because of the context version, initializing GLEW results in a harmless invalid enum
     416    (unless (glfw:window-should-close (glfw:window))
     417      (loop))))
    250418</enscript>
    251419
    252420
    253421=== Version history
     422
     423==== Version 0.2.0
     42410 September 2014
     425
     426* Add gl-utils-bytevector module
     427* Add gl-utils-mesh module
     428* Remove now-unneeded (and broken) {{make-vao}}, {{make-ply-vao}}
     429
    254430
    255431==== Version 0.1.2
Note: See TracChangeset for help on using the changeset viewer.