Closed noloop closed 4 years ago
I managed to make it work, the secret is that you need to multiply the projection matrix by the position in the vertex-shader, like is showed below:
(require :sdl2)
(require :cl-opengl)
(require :cl-glut)
(defun runtime-load-shader (shader-type shader-src)
(let ((shader (gl:create-shader shader-type)))
(gl:shader-source shader shader-src)
(gl:compile-shader shader)
shader))
(defun alloc-array-buffer (verts)
(let ((array-buffer (gl:gen-buffer))
(arr (gl:alloc-gl-array :float (length verts))))
(gl:bind-buffer :array-buffer array-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)
array-buffer))
(defun create-new-program (program)
(let ((verts #(0.0 280.0
-280.0 -280.0
280.0 -280.0))
(vs (runtime-load-shader :vertex-shader "#version 120
uniform mat4 projectionMatrix;
attribute vec2 coord2d;
void main(void) {
gl_Position = projectionMatrix * vec4(coord2d, 0.0, 1.0);
}
"))
(fs (runtime-load-shader :fragment-shader "#version 120
void main(void) {
gl_FragColor[0] = 0.0;
gl_FragColor[1] = 0.0;
gl_FragColor[2] = 1.0;
}
"))
(coord2d nil))
(gl:attach-shader program vs)
(gl:attach-shader program fs)
(gl:link-program program)
(alloc-array-buffer verts)
(setf coord2d (gl:get-attrib-location program "coord2d"))
(gl:use-program program)
(gl:enable-vertex-attrib-array coord2d)
(gl:vertex-attrib-pointer coord2d
2
:float
:false
0
(cffi:null-pointer))))
(defun draw-triangle ()
(gl:draw-arrays :triangles 0 3))
(defun program-shutdown (program)
(gl:delete-program program))
(defun sdl2-test ()
(sdl2:with-init (:everything)
(sdl2:with-window (win :flags '(:shown :opengl))
(sdl2:with-gl-context (gl-context win)
(let ((program (gl:create-program)))
(create-new-program program)
(gl:viewport 0 0 800 600)
(gl:matrix-mode :projection)
(gl:load-identity)
(gl:ortho -400 400 -300 300 -1.0 1.0)
(when program
(let ((proj-mat (gl:get-float :projection-matrix)))
(gl:uniform-matrix
(gl:get-uniform-location program "projectionMatrix")
4
(vector proj-mat))))
(gl:matrix-mode :modelview)
(gl:load-identity)
(sdl2:with-event-loop (:method :poll)
(:idle ()
(gl:clear :color-buffer)
(gl:color 1.0 0.0 0.0)
(draw-triangle)
(sdl2:gl-swap-window win))
(:quit () t)))))))
(sdl2-test)
I tried configure the gl:ortho with the cl-sdl2, but it isn't working:
When I configure any value to
gl:ortho
is the same that configure to:I tried like this:
I tested it and it just doesn't work using shaders, but it works in the obsolete
begin - end
mode of the cl-sdl2 example, like this: