unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Daniel Llorens <daniel.llorens@bluewin.ch>
To: "Luis Souto Graña" <luissoutobueu@gmail.com>
Cc: guile-user <guile-user@gnu.org>
Subject: Re: Function set-gl-vertex-array in Guile-opengl
Date: Sat, 26 Jan 2019 13:52:04 +0100	[thread overview]
Message-ID: <36DB9AAC-0B1F-4E3F-9FEC-B25A180F07CE@bluewin.ch> (raw)
In-Reply-To: <CA+0Zd=8=xKQtc2i4uK=c4AEi07PCX8bM653Qp1dJDhJRTObNRw@mail.gmail.com>



> On 26 Jan 2019, at 02:33, Luis Souto Graña <luissoutobueu@gmail.com> wrote:


>>> 
>>> ,apropos set-gl-vertex-array
>> (gl): set-gl-vertex-array    #<procedure set-gl-vertex-array (type
>> bv-or-pointer #:optional size #:key stride offset)>
>> 
>> 
>>> (set-gl-vertex-array (bytevector->pointer points) 3)
>> 


You should call set-gl-vertex-array like this:

(set-gl-vertex-array (bytevector->pointer points) #:stride 3)

That's what the #:key of the help means.

You can read about that here:

https://www.gnu.org/software/guile/manual/html_node/lambda_002a-and-define_002a.html#lambda_002a-and-define_002a


> I don't know how to get #vu8(0 0 240 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 240...
> from srfi-4. The coordinates are for a SquareAnnulus: https://github.com/slackmoehrle/Computer-Graphics-Through-OpenGL-2nd/blob/master/Chapter3/SquareAnnulus1/squareAnnulus1.cpp
> 
> 10 points x 3 coordinates = 30

I don't follow. You should still use (* n 4). Each f32 takes 4 bytes in the bytevector. bytevector-ieee-single-native-ref/set! takes byte addresses, not float addresses. The way you do it, you're putting the first 30.0 at byte 0, and the second 30.0 at byte 30, so it's not even aligned.

scheme@(guile-user)> (bytevector-ieee-single-native-ref #f32(1.2 3.4 5.6) 0)
$1 = 1.2000000476837158
scheme@(guile-user)> (bytevector-ieee-single-native-ref #f32(1.2 3.4 5.6) 4)
$2 = 3.4000000953674316
scheme@(guile-user)> (bytevector-ieee-single-native-ref #f32(1.2 3.4 5.6) 8)
$3 = 5.599999904632568

As you can see, in Guile, SRFI-4 vectors are bytevectors. The other way, check it out:

scheme@(guile-user)> (import (rnrs bytevectors))
scheme@(guile-user)> (bytevector-u8-ref #f32(3.14) 0)
$1 = 195
scheme@(guile-user)> (bytevector-u8-ref #f32(3.14) 1)
$2 = 245
scheme@(guile-user)> (bytevector-u8-ref #f32(3.14) 2)
$3 = 72
scheme@(guile-user)> (bytevector-u8-ref #f32(3.14) 3)
$4 = 64
scheme@(guile-user)> (bytevector-ieee-single-native-ref #u8(195 245 72 64) 0)
$5 = 3.140000104904175

But you shouldn't need to care about any of this. You can pass an f32 vector to any function that takes bytevectors if that bytevector is supposed to contain floats.

Don't use bytevector-ieee-single-ref/set!. Use make-f32vector, f32vector-set! and f32-vector-ref instead. Those take float addresses.

It's unnecessary and error prone to use the bytevector functions to handle typed vectors unless you really need to address the bytes.

Don't define your own f32vector. Use SRFI-4.

Regards

	Daniel






> 
> If anyone could make an minimal example with set-gl-vertex-array I would be very grateful.
> 
> El vie., 25 ene. 2019 a las 19:25, Daniel Llorens (<daniel.llorens@bluewin.ch>) escribió:
> 
> Hi Luis, 
> 
> I don't really have an answer to your question, but I wanted to point out that Guile already has make-f32vector, f32vector-set!, etc. You don't need to define your own. IMO it's not a good idea to use bytevector-xxx-set!/ref to operate on typed vectors unless you are type punning.
> 
> The make-f32vector, etc. functions are in SRFI-4, they aren't specific to Guile.
> 
> PS I didn't understand the purpose of (* n 30) on your code. The original you link has (* n 4) which is what I'd expect for f32.
> 
> Regards
> 
> 	Daniel
> 
> 
>> From: Luis Souto Graña <luissoutobueu@gmail.com>
>> Subject: Function set-gl-vertex-array in Guile-opengl
>> Date: 25 January 2019 at 11:36:33 CET
>> To: guile-user@gnu.org
>> 
>> 
>> Hello, I'm trying to use the function set-gl-vertex-array in Guile-opengl .
>> It needs a bytevector as an argument. I wrote this doing a copy-paste from
>> here: https://github.com/marcomaggi/vicare/blob/master/attic/lab/gears.scm
>> 
>> (use-modules (rnrs bytevectors))
>> (use-modules (system foreign))
>> 
>> (define (f32vector . lst)
>>  (define-syntax f32set!
>>    (syntax-rules ()
>>      ((_ bv n value)
>>       (bytevector-ieee-single-native-set! bv (* n 30) value))))
>>  (let ((bv (make-bytevector (* (length lst) 30))))
>>    (let loop ((i 0) (lst lst))
>>      (cond ((null? lst) bv)
>>        (else
>>         (f32set! bv i (car lst))
>>         (loop (+ i 1) (cdr lst)))))))
>> 
>> (define points (f32vector 30.0 30.0 0.0
>>                      10.0 10.0 0.0
>>                      70.0 30.0 0.0
>>                      90.0 10.0 0.0
>>                      70.0 70.0 0.0
>>                      90.0 90.0 0.0
>>                      30.0 70.0 0.0
>>                      10.0 90.0 0.0
>>                      30.0 30.0 0.0
>>                      10.0 10.0 0.0))
>> 
>>> points
>> #vu8(0 0 240 65 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 240
>> .....
>> 
>>> (bytevector-length points)
>> 900
>> 
>>> (bytevector->pointer points)
>> #<pointer 0x56090d909aa0>
>> 
>> 
>> So, it works well.
>> 
>> Now, if I write:
>> 
>> (use-modules (gl) (glut))
>> 
>>> ,apropos set-gl-vertex-array
>> (gl): set-gl-vertex-array    #<procedure set-gl-vertex-array (type
>> bv-or-pointer #:optional size #:key stride offset)>
>> 
>> 
>>> (set-gl-vertex-array (bytevector->pointer points) 3)
>> 
>> ERROR: In procedure scm-error:
>> unhandled array-pointer type 3
>> 
>> Can someone tell me what the solution is?
>> 
>> 
>> 
>> _______________________________________________
>> guile-user mailing list
>> guile-user@gnu.org
>> https://lists.gnu.org/mailman/listinfo/guile-user
> 




  reply	other threads:[~2019-01-26 12:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.136.1548435627.3559.guile-user@gnu.org>
2019-01-25 18:25 ` Function set-gl-vertex-array in Guile-opengl Daniel Llorens
2019-01-26  1:33   ` Luis Souto Graña
2019-01-26 12:52     ` Daniel Llorens [this message]
2019-01-26 12:58       ` Daniel Llorens
2019-01-26 16:05         ` Luis Souto Graña
2019-01-26 15:39           ` Daniel Llorens
2019-01-26 19:00             ` Luis Souto Graña
2019-01-28 13:30             ` Luis Souto Graña
2019-01-31 11:14               ` Luis Souto Graña
2019-02-09 18:59                 ` Luis Souto Graña
2019-01-25 10:36 Luis Souto Graña
2019-01-26 10:33 ` Catonano
2019-01-26 16:02   ` Luis Souto Graña

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=36DB9AAC-0B1F-4E3F-9FEC-B25A180F07CE@bluewin.ch \
    --to=daniel.llorens@bluewin.ch \
    --cc=guile-user@gnu.org \
    --cc=luissoutobueu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).