gem-vertex-array error in cl-opengl

Asked 2 years ago, Updated 2 years ago, 85 views

I was trying OpenGL's tutorial in Common Lisp, but
(gl:gen-vertex-array) will fail.

OpenGL signalled (1282.INVALID-OPERATION) from GEN-VERTEX-ARRAYS.
Condition of type CL-OPENGL-BINDINGS: OPENGL-ERROR

Continue with debugger

The value                                                                       
  (1)     
is not of type 
  (UNSIGNED-BYTE32)
   Condition of type Type-ERROR

says
I get an error when I translate the above site, but if I evaluate (gen-vertex-array) alone in repl, I don't get an error.
Could you tell me how I can solve this problem?
Source code is

 (require:asdf)
(asdf:oos' asdf:load-op:cl-glfw3)
(asdf:oos' asdf:load-op:cl-opengl)
(asdf:oos' asdf:load-op:cl-glu)
(asdf:oos' asdf:load-op:trivial-main-thread)
(require'cffi)
(defpackage glfw-test)
  (:use:cl:trivial-main-thread)
  (:export#:run)
       #:bound))
(in-package:glfw-test)

(defclass shader()
   ((vbuff:accessor vertex-buffer)
   (ibuff: accessor index-buffer)
   (vshader: accessor vertex-shader)
   (fshader: accessor fragment-shader)
   (varray:accessor vertex-array)
   (program: accessor program))

(defvar*vertex-shader-source*
  "# version 330 core
   layout(location=0)invec3apos;
   void main()
   {
   gl_position=vec4(apos.x, apos.y, apos.z, 1.0);
   }
   ")
(defvar* fragment-shader-source*
  "# version 330 core
   outvec4fragcolor;
   void main()
   {
   fragcolor=vec4 (1.0f, 0.5f, 0.2f, 1.0f);
   }
   ")

(defgeneric shader-init (shader)
  (: documentation "Initialize Shader")
(defmethod shader-init ((shader shader))
  ;;Vertex shader
  (setf(vertex-shader shader)(gl:create-shader:vertex-shader))
  (gl:shader-source(vertex-shader shader) *vertex-shader-source*)
  (gl:compile-shader (vertex-shader shader))
  ;; fragment shader
  (setf(fragment-shader shader)(gl:create-shader:fragment-shader))
  (gl:shader-source(fragment-shader shader) *fragment-shader-source*)
  (gl:compile-shader (fragment-shader shader))
  ;; shader program
  (setf(program shader)(gl:create-program))
  (gl: attach-shader (program shader) (vertex-shader shader))
  (gl: attach-shader (program shader) (fragment-shader shader))
  (gl:link-program (program shader))
  ;; Remove used shaders
  (gl:delete-shader (vertex-shader shader))
  (gl:delete-shader (fragment-shader shader))
  ;; array
  (let((vertices(gl:alloc-gl-array:float9))
    (verts# (-0.5-0.50.0)
         0.5 -0.5 0.0
         0.0 0.5 0.0)))
    (dotimes(i(length verts))
      (setf(gl:glarefverticesi)(arefvertsi)))

    (setf(vertex-array shader)(gl:gen-vertex-array))
    (setf(vertex-buffer shader)(gl:gen-buffers1))

    (gl: bind-vertex-array (vertex-buffer shader))

    (gl:bind-buffer:array-buffer (vertex-buffer shader))
    (gl:buffer-data:array-buffer:static-draw values)
    (gl:free-gl-array values); make free

  (gl:vertex-attrib-pointer03:float nil3 (cffi:null-pointer))
  (gl:enable-vertex-attrib-array0)
  ;; Unarray
  (gl:bind-buffer:array-buffer0)
  (gl: bind-vertex-array 0)

  (gl:use-program(program shader)))

(defun main-loop()
  (gl: clear-color 0.20.30.31.0)
  (gl:clear:color-buffer-bit)
  )

(defun main()
  (with-body-in-main-thread()
    (glfw:with-init-window(:width480:height400:title "test glfw")
      (let((shdr(make-instance'shader)))
    (shader-init shdr)
      ;; main loop
      (loop until (glfw: window-should-close-p)
     do(gl:clear:depth-buffer-bit)
     ;; drawing
       (main-loop)
       (glfw:swap-buffers)
       (glfw:poll-events);; window does not appear unless response to the event is written; cannot be cleared
       (sleep(/160)); Update to 60fps
       )
      ))))

(defun run()
  #+ sbcl(sb-int: with-float-traps-masked(:invalid)(main)); SBCL will fail without it
  # - sbcl(main)
  )
(run)

It says . The library was previously introduced with quicklisp. If you load it with repl, you will get an error.

common-lisp opengl

2022-09-30 17:30

1 Answers

This line is the problem.

 (gl: bind-vertex-array (vertex-buffer shader))

You should bind (vertex-array shader) instead of (vertex-buffer shader).Correcting this part will prevent errors in (CL-OPENGL-BINDINGS:BIND-VERTEX-ARRAY(1)), but then (CL-OPENGL-BINDINGS:BIND-BUFFER:ARRAY-BUFFER(1)).This is because the gl:gen-buffers return value is set to (vertex-buffer shader) (the correct 0th element is setf).

In order to take advantage of GLSL 3.30, you may also need to configure :context-version-major in glfw:with-init-window in some environments.If "error:GLSL3.30 is not supported." appears in the log when you compile the shader script, please refer to it.

First of all, I think shader-vao.lisp in cl-opengl examples will be helpful.


2022-09-30 17:30

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.