Advertisement
Guest User

clojure jogl glDrawElements

a guest
Jan 18th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns glfoo.core
  2.   (:import (javax.swing JFrame)
  3.            (javax.media.opengl.awt GLCanvas)
  4.            (javax.media.opengl GLEventListener GL2 GLAutoDrawable)
  5.            (java.nio ByteBuffer IntBuffer FloatBuffer)))
  6.  
  7. (defn make-direct-float-buffer
  8.   "Return a direct java.nio.FloatBuffer                                        
  9. elements -- sequence of numbers"
  10.   [elements]
  11.   (let [bytebuf (ByteBuffer/allocateDirect (* (count elements) 4))
  12.         floatbuf (.asFloatBuffer bytebuf)]
  13.     (.put floatbuf (float-array elements))
  14.     (.rewind floatbuf)))
  15.  
  16. (defn go []
  17.   (let [frame (JFrame. "OpenGL")
  18.         canvas (GLCanvas.)
  19.         verts [-1 1 0 0 -1 0 1 1 0]
  20.         vert-buf (make-direct-float-buffer verts)
  21.         indices (IntBuffer/wrap (int-array [0 1 2]))]
  22.     (.addGLEventListener
  23.      canvas
  24.      (proxy [GLEventListener] []
  25.        (display [#^GLAutoDrawable drawable]
  26.          (doto (.getGL drawable)
  27.            (.glClear (bit-or GL2/GL_COLOR_BUFFER_BIT
  28.                              GL2/GL_DEPTH_BUFFER_BIT))
  29.  
  30.            ;; (.glBegin GL2/GL_TRIANGLES)                                      
  31.            ;; (.glVertex3fv (float-array (subvec verts 0 3)) 0)                
  32.            ;; (.glVertex3fv (float-array (subvec verts 3 6)) 0)                
  33.            ;; (.glVertex3fv (float-array (subvec verts 6 9)) 0)                
  34.            ;; (.glEnd)                                                          
  35.  
  36.            ;; The previous commented triangle code renders fine,                
  37.            ;; but the following renders nothing.                                
  38.            (.glEnableClientState GL2/GL_VERTEX_ARRAY)
  39.            (.glVertexPointer 3 GL2/GL_FLOAT 0 vert-buf)
  40.            (.glDrawElements GL2/GL_TRIANGLES 3 GL2/GL_UNSIGNED_INT indices)
  41.            (.glDisableClientState GL2/GL_VERTEX_ARRAY)
  42.            ))
  43.        (reshape [#^GLAutoDrawable drawable x y w h]
  44.          (doto (.getGL drawable)
  45.                (.glViewport 0 0 w h)
  46.                (.glMatrixMode GL2/GL_PROJECTION)
  47.                (.glLoadIdentity)
  48.                (.glOrtho -1.5 1.5 -1.5 1.5 -1.5 1.5)
  49.                (.glMatrixMode GL2/GL_MODELVIEW)))))
  50.     (doto frame
  51.       (.add canvas)
  52.       (.setSize 500 500)
  53.       (.setVisible true))))
  54.  
  55. (comment project.clj contents
  56.   (defproject glfoo "0.1.0-SNAPSHOT"
  57.     :description "FIXME: write description"
  58.     :url "http://example.com/FIXME"
  59.     :license {:name "Eclipse Public License"
  60.               :url "http://www.eclipse.org/legal/epl-v10.html"}
  61.     :dependencies [[org.clojure/clojure "1.5.1"]
  62.                    [org.clojars.toxi/jogl "2.0.0-rc11"]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement