Skip to main content
gamedev.net
🔒 Locked

Scheme Programming

Started by DeVla May 15, 2004 at 7:35 PM 3 replies 700+ views
Original Post
DeVla
DeVla
Hello everybody iam completely new here and of course very happy to discover and learn with these superb Nehe's tutorials. Iam programming in Scheme so its a bit different from C-style programs. By chance 6 tutorials got sources in Scheme thanks for that guys... I tried to adapt the tutorial 6 to my style and only took the (load-gl-texture) from the source code of Brian. I got a little problem with my (on-paint) function, i was trying to apply a texture to a cube so i call the (load-gl-texture) and after that i call the glBindTexture to select one but i cant make rotate my cube ... there is the code :
    
    (define cube #f)
     
    ...
 
    (define/override (on-size width height)  
      (with-gl-context 
                  
       (lambda ()
         
         (gl-viewport 0 0 width height)                      
         (gl-matrix-mode 'projection)                         
         (gl-load-identity)                                   
         (let ((h (/ height width)))
           (gl-frustum -1.0 1.0 (- h) h 6.0 60.0))
         (gl-matrix-mode 'modelview)
         (gl-load-identity)
         (gl-translate 0.0 0.0 -40.0)
         
                  
         ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
                 
         (glEnable GL_TEXTURE_2D) 
         (glShadeModel GL_SMOOTH)
         "(gl-enable 'cull-face)"
         "(gl-enable 'lighting)"
         "(gl-enable 'light0)"
         (gl-enable 'depth-test)
         (glDepthFunc GL_LEQUAL)
         (glHint GL_PERSPECTIVE_CORRECTION_HINT GL_NICEST)
         
                    
         (set! cube (gl-gen-lists 1))
         
         (gl-new-list cube 'compile)
         (build-cube1) ; function builds a cube ... 
         (gl-end-list)
         
         
         
         ))
      (refresh)) 
    
     
    (define/override (on-paint) ; <=> DrawGLScene       
      (if (= rotation 360.0) ;TODO not incr infinitely
          (set! rotation 0.0)
          (set! rotation (+ 1.0 rotation)))
      
      
      (with-gl-context         
       (lambda ()          
         
         (gl-load-texture) ; BIG PROBLEM HERE

         (glClearColor 0.0 0.0 0.0 0.0)      
         (gl-clear 'color-buffer-bit 'depth-buffer-bit)          
         (glBindTexture GL_TEXTURE_2D (gl-uint-vector-ref *texture* 0))
         (gl-push-matrix) ; on 
         
         (gl-rotate rotation 0.0 5.0 1.0)
         (gl-call-list cube)
         
         (gl-pop-matrix)
         
         (swap-gl-buffers)
         (glFlush)                                     
         
         )
       )
      (refresh)) ;<======== ????? 
 
I know that doing the last refresh is paradoxal but its the only way to make my cube rotate ... edit: Use the [code][/code] tags to preserve indentation. [edited by - SiCrane on May 15, 2004 8:42:56 PM]
DeVla
DeVla
The refresh enqueues an event to repaint the window, so its natural to do it in the on-size but in the on-paint ....

The real problem is that the function (load-gl-texture) asks the user to give a path for a texture, and if its refreshed, the dialog box opens infinetly and finaly it gives a crash.
Also i tried to call this function from outside and it doesnt works.

This is Brian''s function wich is given in the lesson 6 tutorial as source in scheme :



(define (gl-load-texture)

(let* ((file (get-file)) ;Opens a dialog box for texture path (bmp (make-object bitmap% file ''bmp #f)) (dc (instantiate bitmap-dc% (bmp)))
(vec (make-vector (* (send bmp get-width) (send bmp get-height) 3)))
(data (make-string (* (send bmp get-width) (send bmp get-height) 4))) (i 0))
(send dc get-argb-pixels 0 0 (send bmp get-width) (send bmp get-height) data); cf petite explication
(letrec
([loop
(lambda ()
(if (< i (* (send bmp get-width) (send bmp get-height)))
(begin
(vector-set! vec (* i 3) (char->integer (string-ref data (+ (* i 4) 1))))
(vector-set! vec (+ (* i 3) 1) (char->integer (string-ref data (+ (* i 4) 2))))
(vector-set! vec (+ (* i 3) 2) (char->integer (string-ref data (+ (* i 4) 3))))
(set! i (+ i 1))
(loop))))])
(loop))
(glGenTextures 1 *texture*)
(glBindTexture GL_TEXTURE_2D (gl-uint-vector-ref *texture* 0))
(glTexImage2D GL_TEXTURE_2D 0 3 (send bmp get-width) (send bmp get-height)
0 GL_RGB GL_UNSIGNED_BYTE (vector->gl-ubyte-vector vec))

(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR) ; filtrage lineaire
(glTexParameteri GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR)))



on-size and on-paint (also on-char for key handling etc.) are sub methods of the class canvas% in that way i done the define/override, for example to create a frame i instanciate the class cube-canvas% inside the frame object and i do (send show [a-frame] #t) to show it.



Trurl
Trurl
Hi DeVla

I''m pretty well versed in Scheme programming for OpenGL, in fact I think you might be using my implementation of the GL wrapper. PLT Scheme?

I''d like to try to answer your question, but I''m having trouble discerning exactly what the question is... You say you can''t rotate your cube, but then you say ''refresh'' makes it rotate...You say you get a crash...

First off, you definitely don''t want to reload the texture for every on-paint call. Since you''re not deleting the texture, you''re generating one new texture object after another.

Second, the texture loader function looks ok. You shouldn''t need to bind the texture yourself in this case, since the loader does that.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.