首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我已经在过剩的窗口上呈现了一个shere,现在我如何用键盘使它移动呢?

我已经在过剩的窗口上呈现了一个shere,现在我如何用键盘使它移动呢?
EN

Stack Overflow用户
提问于 2013-04-21 16:32:54
回答 1查看 853关注 0票数 2

剪切和粘贴code示例代码,我向显示器呈现了一个球体。现在我如何用键盘在x,y和z轴上移动它。还有一个在线资源可以学习CL-OpenGL的游戏编程,或者如何制作像墙壁、泥土、房屋和字符这样的东西,这样我就可以创建一个3D世界,并在其中移动角色,并为其提供游戏逻辑。

到目前为止,我的代码如下:

代码语言:javascript
复制
(defclass glut-teapot-window (glut:window)
()
(:default-initargs :width 948 :height 990 :title "glut-teapot.lisp" 
                  :mode '(:single :rgb :depth)))

(defmethod glut:display-window :before ((window glut-teapot-window))
(gl:clear-color 0 0 0 0)
(gl:cull-face :back)
(gl:depth-func :less)
(gl:disable :dither)
(gl:shade-model :smooth)
(gl:light-model :light-model-local-viewer 1)
(gl:color-material :front :ambient-and-diffuse)
(gl:enable :light0 :lighting :cull-face :depth-test))

(defmethod glut:display ((window glut-teapot-window))
(gl:load-identity)
(gl:translate 0 0 -5)
(gl:rotate 100 1 1 0)
(gl:light :light0 :position '(6000 1000 15 0))
(gl:light :light0 :position '(2000 300 3000 0))
(gl:clear :color-buffer :depth-buffer)
(gl:color 1 1 1)
(gl:front-face :cw)
(glut:Solid-Sphere 1 10 10)
(gl:front-face :ccw)
(gl:flush))

(defmethod glut:reshape ((window glut-teapot-window) width height)
(gl:viewport 0 0 width height)
(gl:matrix-mode :projection)
(gl:load-identity)
(glu:perspective 50 (/ width height) 0.5 20)
(gl:matrix-mode :modelview)
(gl:load-identity))

(defmethod glut:keyboard ((window glut-teapot-window) key x y)
(declare (ignore x y))
(when (eql key #\Esc)
(glut:destroy-current-window)))

(defun glut-teapot ()
(glut:display-window (make-instance 'glut-teapot-window)))
(glut-teapot)
EN

回答 1

Stack Overflow用户

发布于 2013-04-22 08:06:02

由于我不知道您使用opengl的经验,所以我将在一年前为我编写这个答案(当时我对opengl一无所知,并试图使用普通的lisp来学习)。

cl-opengl库是opengl的一个比较严密的包装器,我的意思是大多数opengl函数在cl-opengl中有一个直接等价的函数。这很好,因为这意味着您可以使用c& c++为opengl提供在线教程,并且只需要一点精神上的压力,就可以将它们转换为lisp。

它是一个非常紧密的opengl包装器,它的缺点是opengl不是很灵活,我的意思是,良好opengl代码的风格与良好的lisp代码非常不同。不过,一旦掌握了cl的诀窍,这肯定是可以改进的。

接下来,opengl是一个库,其中包含了两个非常不同的范例。有旧的风格opengl (它使用gl和gl-end)和新的风格opengl,它是基于着色器的可能是什么,如果你想要在未来的幻想效果!令人痛苦的是,大多数好的在线教程都是针对老式opengl的,或者是以一种我觉得非常令人困惑的方式将两者混合在一起。因此,我个人建议学习现代opengl的教程如下:http://www.arcsynthesis.org/gltut/

一旦你完成了前四章,你就能理解足够的opengl来使用opengl wiki,而不会有太多的痛苦。我这么说是因为一开始我发现opengl语言wiki非常混乱,因为我不知道所有的信息应该如何结合在一起。

现在是过剩。过剩是cool...But,它很快就会受到限制。我真的建议改用SDL。幸运的是,一些才华横溢的人在lisp中很好地使用了SDL,并将其命名为lispbuilder。http://code.google.com/p/lispbuilder/wiki/LispbuilderSDL

好吧,那你该怎么走?:

  • 快:我想你已经在用这个了,但如果不是,现在就去拿吧!它让普通lisp的最新套餐变得容易多了。http://www.quicklisp.org/beta/
  • 接下来,获取lispbuilder (ql:quickload :lispbuilder)
  • 使用弧形合成教程熟悉opengl。这确实是获得理解的最好方式,我需要能够对现代opengl进行编码。
  • 用lisp编写弧形合成教程中的例子。
  • 制作一些函数和宏,使opengl在lisp中更好地工作。

在这条路上,我只领先你几个月,开始工作真的很痛苦,但完全值得。很抱歉,如果你对opengl很在行,那么这个答案对你来说太基本了!如果是这样的话,让我知道,我会尝试提出一个更好的答案。

下面是在SDL中设置opengl窗口和上下文的基本代码,用于处理事件(您的用户输入将转到这里)和重新绘制框架(还没有什么可绘制的,但您将用弧形合成教程中的内容填充它!)

此外,我还添加了一个助手函数和宏。首先是“可持续的”,这意味着如果代码中发生了错误,那么在错误结果中将有一个选项来继续程序(非常方便,这样您就不必一次又一次地启动演示)。此外,还有更新的swank,这意味着即使在运行演示时,仍然可以使用Slime (您使用的是黏液吗?如果得不到的话!)

代码语言:javascript
复制
(defun init ()
  (print 'initialise-stuff-here))

(defun draw ()
  (gl:clear-color 0.0 0.0 0.0 0.0)
  (gl:clear-depth 1.0)
  (gl:clear :color-buffer-bit :depth-buffer-bit)
  (gl:flush)
  (sdl:update-display))

(defun reshape (width height)
  (print width )
  (print height)
  (gl:viewport 0 0 width height))

(defmacro continuable (&body body)
  "Helper macro since we use continue restarts a lot
   <remember to hit C in slime or pick the restart so errors don't kill the app>"
  `(restart-case (progn ,@body)
     (continue () :report "Continue")))

(defun update-swank ()
  (let ((connection (or swank::*emacs-connection*
                        (swank::default-connection))))
    (when connection
      (swank::handle-requests connection t))))

(defun run-demo ()
  (sdl:with-init ()
    (sdl:window
     640 480 :opengl t
     :resizable t
     :opengl-attributes '((:sdl-gl-doublebuffer 1)
                          (:sdl-gl-alpha-size 0)
                          (:sdl-gl-depth-size 16)
                          (:sdl-gl-stencil-size 8)
                          (:sdl-gl-red-size 8)
                          (:sdl-gl-green-size 8)
                          (:sdl-gl-blue-size 8)))
    (init)
    (reshape 640 480)
    (setf cl-opengl-bindings:*gl-get-proc-address* #'sdl-cffi::sdl-gl-get-proc-address)
    (sdl:with-events ()
      (:quit-event () t)
      (:VIDEO-RESIZE-EVENT (:w width :h height)
                           (reshape width height))
      (:idle ()
             (continuable (update-swank))
             (continuable (draw))))))

好了,就这样,玩得开心!

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16133462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档