Skip to content

Instantly share code, notes, and snippets.

@woxtu
Last active February 10, 2016 22:17
Show Gist options
  • Save woxtu/7156835 to your computer and use it in GitHub Desktop.
Save woxtu/7156835 to your computer and use it in GitHub Desktop.
Draw a triangle using GLSL and VBO in Rouge
;; Rouge 0.0.15
(require "glut")
(require "opengl")
(def vertex-shader "
attribute vec3 position;
attribute vec4 color;
varying vec4 vertex_color;
void main(void) {
vertex_color = color;
gl_Position = vec4(position, 1.0);
}")
(def fragment-shader "
varying vec4 vertex_color;
void main(void) {
gl_FragColor = vertex_color;
}")
(GLUT/Init)
(GLUT/InitDisplayMode GLUT.GLUT_RGBA)
(GLUT/InitWindowSize 500 500)
(GLUT/CreateWindow "Draw a triangle using GLSL and VBO in Rouge")
(let [program (GL/CreateProgram)]
(.each [{:type GL.VERTEX_SHADER :source vertex-shader}
{:type GL.FRAGMENT_SHADER :source fragment-shader}] ;; We need doseq
| [x]
(let [shader (GL/CreateShader (x :type))]
(GL/ShaderSource shader (x :source))
(GL/CompileShader shader)
(GL/AttachShader program shader)
(GL/DeleteShader shader)))
(GL/LinkProgram program)
(GL/UseProgram program)
(.each [{:attr "position" :array [0.0 0.5 0.0, 0.577 -0.5 0.0, -0.577 -0.5 0.0]}
{:attr "color" :array [1.0 0.0 0.0 1.0, 0.0 1.0 0.0 1.0, 0.0 0.0 1.0 1.0]}]
| [x]
(let [buffer (first (GL/GenBuffers 1))
location (GL/GetAttribLocation program (x :attr))]
(GL/BindBuffer GL.ARRAY_BUFFER buffer)
(GL/BufferData GL.ARRAY_BUFFER (* (count (x :array)) 4) (.pack (x :array) "f*") GL.STATIC_DRAW)
(GL/EnableVertexAttribArray location)
(GL/VertexAttribPointer location (/ (count (x :array)) 3) GL.FLOAT false 0 0)
(GL/BindBuffer GL.ARRAY_BUFFER 0))))
(GLUT/DisplayFunc
(fn []
(GL/Clear GL_COLOR_BUFFER_BIT)
(GL/DrawArrays GL.TRIANGLES 0 3)
(GL/Flush)
(GLUT/SwapBuffers)))
(GLUT/ReshapeFunc
(fn [w h]
(GL/Viewport 0 0 w h)))
(GLUT/MainLoop)
@kivikakk
Copy link

すごい!アタシこそこんなことなんて、したことないの!

@woxtu
Copy link
Author

woxtu commented Nov 11, 2013

I'm glad you like it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment