opengl - 在 Haskell 中用 opengl 画线

标签 opengl haskell

我正在尝试使用 opengl 创建一个围棋板。为此,我正在尝试绘制一堆线来创建网格。但是,每个教程站点(包括 opengl 的)都有 C++ 示例,而 Haskell wiki 并没有很好地解释它。我是opengl的新手,想要一个教程。

最佳答案

我假设您想使用 OpenGL 2.1 或更早版本。对于 OpenGL 3.0,您需要不同的代码。

所以,在 C 中你会这样写:

glBegin(GL_LINES);
glVertex3f(1, 2, 3);
glVertex3f(5, 6, 7);
glEnd();

你在 Haskell 中这样写:
renderPrimitive Lines $ do
  vertex $ Vertex3 1 2 3
  vertex $ Vertex3 5 6 7

使用此代码,因为我使用了例如1而不是一些变量,你可能会得到关于模棱两可的类型的错误(所以你应该用 1 替换 (1 :: GLfloat) ),但是如果你使用已经具有类型的实际变量 GLfloat ,你不应该这样做。

这是一个在窗口中绘制白色对角线的完整程序:
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT

main :: IO ()
main = do
  -- Initialize OpenGL via GLUT
  (progname, _) <- getArgsAndInitialize

  -- Create the output window
  createWindow progname

  -- Every time the window needs to be updated, call the display function
  displayCallback $= display

  -- Let GLUT handle the window events, calling the displayCallback as fast as it can
  mainLoop

display :: IO ()
display = do
  -- Clear the screen with the default clear color (black)
  clear [ ColorBuffer ]

  -- Render a line from the bottom left to the top right
  renderPrimitive Lines $ do
    vertex $ (Vertex3 (-1) (-1)  0 :: Vertex3 GLfloat)
    vertex $ (Vertex3   1    1   0 :: Vertex3 GLfloat)

  -- Send all of the drawing commands to the OpenGL server
  flush

默认的 OpenGL 固定函数投影使用 (-1, -1) 表示窗口的左下角,使用 (1, 1) 表示窗口的右上角。您需要更改投影矩阵以获得不同的坐标空间。

有关此类更完整示例,请参阅 the Haskell port of the NEHE tutorials .他们使用 RAW OpenGL 绑定(bind),这更像是 C 绑定(bind)。

关于opengl - 在 Haskell 中用 opengl 画线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9300773/

相关文章:

c++ - 代码风格 - 我应该如何根据许多可能的值验证整数?

OpenGL glViewport 和 glScale

c++ - 使用正射投影裁剪后的边缘伪影

parsing - 如何使 Attoparsec 解析器成功而不消耗(如 parsec LookAhead)

haskell - 如何在 Haskell 的 TChan 上的生产者/消费者情况下限制生产者?

opengl - 保留GLBlendFunc

c++ - 如何防止 opengl 绘图拉伸(stretch)到窗口大小?

haskell - Haskell程序的-hc配置文件中PINNED是什么意思?

list - Haskell List With Guards错误

haskell - 查看模式与模式防护