From: bradb
Subject: Macro helpers for array access
Date: 
Message-ID: <1133482174.971165.43270@g49g2000cwa.googlegroups.com>
I'm still learing Lisp.  At the moment I am (slowly) writing a text
editor, and gathering my thoughts for my next project which will be
something using OpenGL.
I think that for doing an OGL app I will need precise access to memory.
 Memory will need to be handed off to OGL for rendering, and therefore
should be in a format that OGL likes.
I am pretty sure that I will need to allocate (and therefore free) this
memory outside of the Lisp garbage collector.
My first question is: what is the best way to allocate this memory?
Use a non-portable call, or use a somewhat portable CFFI call to C's
malloc?
Question 2 is: can Lisp now access this memory with ELT?  Is there a
faster method?

Assuming that the problems of allocating and accessing memory are not
too hard, I think that next I should write some macros to ease the pain
of accessing the arrays.

I thought I'd need a macro that is used to define structures that map
onto the arrays:
(def-array-struct vector
  (x float)
  (y float)
  (z float))
This would create information that lets you map a vector on top of an
array at a specific address, and create the helper functions/macros.

The helper macro signatures might look a little like
(vector index member array), and get called as
(vector 0 x verticies) which could translate to something like (elt
verticies (+ vector-offset-x (* index vector-stride)))

I would also think that higher level with- style macros would be
useful, ie

(with-vectors ((name index))  array)
(with-vectors ((v0 0) (v1 1)) verts
  (setf (v0 x) (v1 y))

Or, perhaps better
(with-vectors name array)....
(with-vectors v verts
 (setf (v 0 x) (v 1 y))

This all feels very doable - is it?  Is this guru level stuff?  Is this
a good way to start?
My current concern is how structures with different member types might
fare, eg
(def-array-struct OGLpoint
  (x float) (y float) (z float)
  (r byte) (g byte) (b byte))

Please note that I am not actually asking how to do this (at least not
yet, not before I've attempted and failed :) - just if this is a good
idea, how hard it is, and how Lispy it is.

Cheers
Brad

From: ·········@googlemail.com
Subject: Re: Macro helpers for array access
Date: 
Message-ID: <1133545171.562725.190960@f14g2000cwb.googlegroups.com>
Why don't you just use an existing Common Lisp OpenGL binding like the
one in CL-SDL. If you want to learn how it works have a look at the
source. Of course you might suffer from NIH (not invented here) syndrom
but using an existing library is usually better...
From: bradb
Subject: Re: Macro helpers for array access
Date: 
Message-ID: <1133549150.750614.282860@g49g2000cwa.googlegroups.com>
I don't think you understand what I mean.  I will be using an existing
OGL binding, or at worst auto-generating one.  But I still need to
access the data that OGL will need in a format that OGL will like.  I
may be wrong, but I suspect that the existing OGL bindings will just be
Lisp wrappers to the C OpenGL calls.
These array helpers are more about how I will manipulate the data that
OGL is going to render.

Cheers
Brad
From: Tomcat
Subject: Re: Macro helpers for array access
Date: 
Message-ID: <1133566480.038383.52500@g47g2000cwa.googlegroups.com>
Using SBCL it ends up looking something like this

(sb-alien:define-alien-type gpu-vertex
    (sb-alien:struct gpu-vertex
            (position (sb-alien:array sb-alien:float 3))
            (normal (sb-alien:array sb-alien:float 3))))

(defvar gpu-vertices (sb-alien:make-alien gpu-vertex 12))
(setf (sb-alien:deref (sb-alien:slot (sb-alien:deref gpu-vertices 0)
'position) ) 1.0)
(sb-alien:free-alien gpu-vertices)

Most other lisps will let you do something similiar, or you can do it
more portably using cffi. 

Tom