PerduGames
PerduGames

Reputation: 1218

How to write opengl ES 2.0/3.0 in Common Lisp?

I'm want write one code that works in android and linux, so I think that I should start with cl-opengl, but the android uses opengl ES, so I think that I should use opengl ES 2.0 or 3.0, but I'm confusing with all this, I'm using sdl2 for create window and cl-opengl, but how to use opengl ES in common lisp? I must write two code one in opengl ES for Android and other code in opengl for Linux? Or I can write one code in opengl ES that work in both platforms? How to write opengl ES with cl-opengl? I'm almost lost.

And I think build to Android by ECL.

I try today again, and I follow the tutorial below:

https://pt.wikibooks.org/wiki/Programa%C3%A7%C3%A3o_com_OpenGL/Modern_OpenGL_Introduction

I can draw the triangle with my code in C. But when I try with cl-opengl not:

(unless (gl::features-present-p (>= :glsl-version 1.2))
  (format t "Not support glsl 120.~%")
  (finish-output))
(let ((buffers (gl:gen-buffers 2))
      (vertex-buffer nil)
      (arr (gl:alloc-gl-array :float 6))
      (verts #(0.0 0.8
           -0.8 -0.8
           0.8 -0.8))
      (vs (gl:create-shader :vertex-shader))
      (fs (gl:create-shader :fragment-shader))
      (program nil)
      (attribute-coord2d nil))
  (setf vertex-buffer (elt buffers 0))
  (gl:bind-buffer :array-buffer vertex-buffer)
  (dotimes (i (length verts))
    (setf (gl:glaref arr i) (aref verts i)))
  (gl:buffer-data :array-buffer :static-draw arr)
  (gl:free-gl-array arr)
  (gl:shader-source vs *vertex-shader*)
  (gl:compile-shader vs)
  (gl:shader-source fs *fragment-shader*)
  (gl:compile-shader fs)
  (setf program (gl:create-program))
  (gl:attach-shader program vs)
  (gl:attach-shader program fs)
  (gl:link-program program)
  (setf attribute-coord2d (gl:get-attrib-location program "coord2d"))
  (gl:color 1.0 1.0 1.0 1.0)
  (gl:clear :color-buffer-bit)
  (gl:use-program program)
  (gl:enable-vertex-attrib-array attribute-coord2d)
  (gl:vertex-attrib-pointer attribute-coord2d
                2
                :float
                :false
                0
                vertex-buffer)
  (gl:draw-arrays vertex-buffer 0 3)
  (gl:disable-vertex-attrib-array attribute-coord2d)
  (gl:delete-program program))

With the code above I get:

enter image description here

Upvotes: 2

Views: 406

Answers (1)

mystery
mystery

Reputation: 19513

GL_INVALID_ENUM probably means you passed the wrong constant somewhere, and it says this happened in DRAW-ARRAYS. I haven't used cl-opengl or Common Lisp before, but glDrawArrays' first parameter should be the type of primitive (both in GL and GL ES), e.g. GL_TRIANGLES. If cl-opengl is a 1:1 mapping to the GL API, then I assume your (gl:draw-arrays vertex-buffer 0 3) line is wrong, and should be something more like (gl:draw-arrays gl:triangles 0 3)?

Also (gl:vertex-attrib-pointer attribute-coord2d 2 :float :false 0 vertex-buffer) is suspicious to me. I think that last parameter should be 0. It's notionally a pointer, but in modern OpenGL and OpenGL ES (when you're using an array buffer like you are) it's actually an offset into the buffer.

Upvotes: 4

Related Questions