Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns glfoo.core
- (:import (javax.swing JFrame)
- (javax.media.opengl.awt GLCanvas)
- (javax.media.opengl GLEventListener GL2 GLAutoDrawable)
- (java.nio ByteBuffer IntBuffer FloatBuffer)))
- (defn make-direct-float-buffer
- "Return a direct java.nio.FloatBuffer
- elements -- sequence of numbers"
- [elements]
- (let [bytebuf (ByteBuffer/allocateDirect (* (count elements) 4))
- floatbuf (.asFloatBuffer bytebuf)]
- (.put floatbuf (float-array elements))
- (.rewind floatbuf)))
- (defn go []
- (let [frame (JFrame. "OpenGL")
- canvas (GLCanvas.)
- verts [-1 1 0 0 -1 0 1 1 0]
- vert-buf (make-direct-float-buffer verts)
- indices (IntBuffer/wrap (int-array [0 1 2]))]
- (.addGLEventListener
- canvas
- (proxy [GLEventListener] []
- (display [#^GLAutoDrawable drawable]
- (doto (.getGL drawable)
- (.glClear (bit-or GL2/GL_COLOR_BUFFER_BIT
- GL2/GL_DEPTH_BUFFER_BIT))
- ;; (.glBegin GL2/GL_TRIANGLES)
- ;; (.glVertex3fv (float-array (subvec verts 0 3)) 0)
- ;; (.glVertex3fv (float-array (subvec verts 3 6)) 0)
- ;; (.glVertex3fv (float-array (subvec verts 6 9)) 0)
- ;; (.glEnd)
- ;; The previous commented triangle code renders fine,
- ;; but the following renders nothing.
- (.glEnableClientState GL2/GL_VERTEX_ARRAY)
- (.glVertexPointer 3 GL2/GL_FLOAT 0 vert-buf)
- (.glDrawElements GL2/GL_TRIANGLES 3 GL2/GL_UNSIGNED_INT indices)
- (.glDisableClientState GL2/GL_VERTEX_ARRAY)
- ))
- (reshape [#^GLAutoDrawable drawable x y w h]
- (doto (.getGL drawable)
- (.glViewport 0 0 w h)
- (.glMatrixMode GL2/GL_PROJECTION)
- (.glLoadIdentity)
- (.glOrtho -1.5 1.5 -1.5 1.5 -1.5 1.5)
- (.glMatrixMode GL2/GL_MODELVIEW)))))
- (doto frame
- (.add canvas)
- (.setSize 500 500)
- (.setVisible true))))
- (comment project.clj contents
- (defproject glfoo "0.1.0-SNAPSHOT"
- :description "FIXME: write description"
- :url "http://example.com/FIXME"
- :license {:name "Eclipse Public License"
- :url "http://www.eclipse.org/legal/epl-v10.html"}
- :dependencies [[org.clojure/clojure "1.5.1"]
- [org.clojars.toxi/jogl "2.0.0-rc11"]]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement