我正在使用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帖子,却没有发现任何特别有用的东西,也无法真正想到会导致这种情况的任何东西。
最佳答案
在您的情况下,vertex
数组是一个具有3个分量(x,y,z)的坐标的数组。因此,gl:vertex-attrib-pointer
的“size”参数必须为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
它由顶点坐标(包含3个分量的
v
),纹理坐标(包含2个分量的vt
)和法线向量(包含3个分量的vn
)组成。此外,还有人脸(
f
)。每个面指定一个具有3个顶点坐标及其属性的三角形。每个顶点包含三个索引,第一个是顶点坐标的索引,第二个是纹理坐标的索引,第3d个是法向矢量的索引。
这意味着下面的脸
f 1/1/1 2/2/1 4/4/1
定义一个三角形(具有3个顶点),其中顶点坐标(第一个索引)的索引为
1// 2// 4//
相应的纹理坐标的索引(第二个索引)是
/1/ /2/ /4/
并且对应的法线向量的索引(第三个索引)是
//1 //1 //1
你可以试试
vertices #(-0.707 -0.5 0.0
0.707 -0.5 0.0
0.0 1.0 0.0)
而不是原始顶点数组和
elements #(0 1 2)
出于调试原因,而不是原始索引,而是绘制了一个三角形。