glDrawElements不呈现来自obj文件的输入

时间:2018-05-26 17:16:30

标签: opengl lisp common-lisp wavefront

我正在使用cl-opengl在lisp中编写一个对象加载器,当我尝试使用glDrawElements渲染加载的顶点/元素时,我留下了一个空白屏幕。

(require :cl-opengl)
(require :sdl2)

(defvar *vertices* nil)
(defvar *elements* nil)

(setf *vertices* (make-array 9 :fill-pointer 0))
(setf *elements* (make-array 9 :fill-pointer 0))

(defvar *vertex-shader* "
#version 330 core 
layout (location = 0) in vec3 aPos;

void main() {
    gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
}
")

(defvar *fragment-shader* "
#version 330 core
out vec4 FragColor;

void main() {
    FragColor = vec4(0.95f, 0.98f, 0.65f, 1.0f);
}
")

(defun split-str-1 (string &optional (separator " ") (r nil))
  (let ((n (position separator string
                     :from-end t
                     :test #'(lambda (x y)
                               (find y x :test #'string=)))))
    (if n
        (split-str-1 (subseq string 0 n) separator (cons (subseq string (1+ n)) r))
      (cons string r))))

(defun split-str (string &optional (separator " "))
  (split-str-1 string separator))

(defun parse-float (number)
  (with-input-from-string (in number)
    (read in)))

(defun load-obj (file-name)
   (let ((file (open file-name)))
     (with-open-stream (source file)
       (loop for line = (read-line source nil nil)
          while line do
            (let* ((split-line (split-str line " "))
                   (header (car split-line))
                   (rest (cdr split-line)))
              (cond ((string= header "v")
                     (dolist (vertex rest)
                       (vector-push-extend (parse-float vertex) vertices)))
                    ((string= header "f")
                     (dolist (face rest)
                       (let ((element (parse-integer (car (split-str face "/")))))
                          (vector-push-extend (- element 1) elements))))))))))

(defun main ()
  (load-obj "tortoise.obj")
  (sdl2:with-init (:everything)
  (sdl2:gl-set-attr :context-profile-mask 0)
  (sdl2:gl-set-attr :context-major-version 3)
  (sdl2:gl-set-attr :context-minor-version 3)

  (sdl2:with-window (win :flags `(:shown :opengl))
    (sdl2:with-gl-context (gl-context win)
    (sdl2:gl-make-current win gl-context)
    (gl:viewport 0 0 800 600)
    (gl:clear-color 0.957 0.376 0.286 1.0)

    (let ((glarray (gl:alloc-gl-array :float (length vertices)))
          (glarray-2 (gl:alloc-gl-array :unsigned-short (length elements))))
      (dotimes (i (length elements))
        (setf (gl:glaref glarray-2 i) (aref elements i)))
      (dotimes (i (length vertices))
        (setf (gl:glaref glarray i) (aref vertices i)))

      (let ((vbo (gl:gen-buffer))
            (vao (gl:gen-vertex-array))
            (ebo (gl:gen-buffer)))
        (gl:bind-vertex-array vao)
        (gl:bind-buffer :array-buffer vbo)
        (gl:buffer-data :array-buffer :static-draw glarray)
        (gl:free-gl-array glarray)
        (gl:bind-buffer :element-array-buffer ebo)
        (gl:buffer-data :element-array-buffer :static-draw glarray-2)
        (gl:vertex-attrib-pointer 0 4 :float nil 0 0)
        (gl:enable-vertex-attrib-array 0)

        (let ((vertex-shader (gl:create-shader :vertex-shader))
              (fragment-shader (gl:create-shader :fragment-shader)))
          (gl:shader-source vertex-shader *vertex-shader*)
          (gl:shader-source fragment-shader *fragment-shader*)
          (gl:compile-shader vertex-shader)
          (gl:compile-shader fragment-shader)
          (print (gl:get-shader-info-log vertex-shader))
          (print (gl:get-shader-info-log fragment-shader))

          (let ((program (gl:create-program)))
            (gl:attach-shader program vertex-shader)
            (gl:attach-shader program fragment-shader)
            (gl:link-program program)
            (gl:delete-shader vertex-shader)
            (gl:delete-shader fragment-shader)
            (gl:use-program program)))

        (sdl2:with-event-loop (:method :poll)
          (:idle ()
                 (gl:clear :color-buffer)
                 (gl:bind-vertex-array vao)
                 (gl:draw-elements :triangles glarray-2)
                 (gl:flush)
                 (sdl2:gl-swap-window win))
          (:quit () t))))))))

我已经尝试过多个obj文件,结果是一样的;没有任何东西被画到屏幕上。我已经查看了其他一些SO帖子,并且没有发现任何特别有用的东西,并且无法想到任何可能导致这种情况发生的事情。

1 个答案:

答案 0 :(得分:4)

在您的情况下,vertex数组是一个坐标为3个分量(x,y,z)的数组。所以"尺寸" gl:vertex-attrib-pointer的参数必须为3而不是4:

(gl:vertex-attrib-pointer 0 3 :float nil 0 0)

注意,通过使用4的大小,顶点坐标的规格未对齐。最后,顶点数组被越界访问。

您对 obj 文件索引的假设可能有误。

通常, obj 文件如下所示:

v -1.000000 0.000000 1.000000
v 1.000000 0.000000 1.000000
v -1.000000 0.000000 -1.000000
v 1.000000 0.000000 -1.000000

vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000

vn 0.0000 1.0000 0.0000

f 1/1/1 2/2/1 4/4/1
f 1/1/1 4/4/1 3/3/1

它由顶点坐标(v,包含3个组件),纹理坐标(vt 2个组件)和法向量(vn 3个组件)组成。
还有面(f)。每个面指定一个三角形,具有3个顶点坐标及其属性 每个顶点由三个索引组成,第一个是顶点坐标的索引,第二个是纹理坐标的索引,3d一个是法向量的索引。

这意味着以下面孔

f 1/1/1 2/2/1 4/4/1

定义一个三角形(有3个顶点),其中顶点坐标(第一个索引)的索引是

1//   2//   4//

相应纹理坐标(第二指数)的索引是

/1/   /2/   /4/

和相应的法向量(第3个指数)的指数是

//1   //1   //1

您可以尝试

vertices #(-0.707 -0.5 0.0 
            0.707 -0.5 0.0
            0.0    1.0 0.0)

而不是orignal顶点数组和

elements #(0 1 2)

而不是orignal索引,为了调试原因,绘制一个三角形。