首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GLU NURBS渲染错误

GLU NURBS渲染错误
EN

Stack Overflow用户
提问于 2011-10-17 19:38:12
回答 1查看 1.1K关注 0票数 7

编辑:这是一个库错误。我将其已报告到HOpenGL邮件列表中。

我用9点矩形方法将一个圆/椭圆表示为NURBS。

要点是p1, p2, ..., p9p9 = p1。它们如下所示:

代码语言:javascript
复制
y2 | p2 p3 p4
y1 | p1    p5
y0 | p8 p7 p6
-------------
   | x0 x1 x2

x1 = (x0 + x2) / 2
y1 = (y0 + y2) / 2

比如p1 = (x0, y1), p2 = (x0, y2)等等。

重量如下:

  • 中点1 (p1,p3,p5,p7)
  • 角点的sqrt(0.5) (p2,p4,p6,p8)

我使用两种方法将权重作为齐次坐标:

  • (x,y)和重量w变成Vertex4 (w*x) (w*y) 0 w
  • 错-它变成了Vertex4 x y 0 w

结果(对的第一,错的第二,如果它们太大的话,对不起):

你看,这两个都不是合适的圆圈(虽然第二个看起来不错),我不明白为什么。

下面是基于来自过剩示例的Lines.hs的代码:

代码语言:javascript
复制
import System.Exit ( exitWith, ExitCode(ExitSuccess) )
import Graphics.UI.GLUT
import Foreign.Marshal.Array
import Graphics.Rendering.OpenGL.GLU.NURBS

myInit :: IO ()
myInit = do
   clearColor $= Color4 0 0 0 0

display :: DisplayCallback
display = do
  clear [ ColorBuffer, DepthBuffer ]
  color (Color3 1.0 1.0 1.0 :: Color3 GLfloat)

  withNURBSObj () $ \nurbsObj -> do
    nurbsBeginEndCurve nurbsObj $
      withArrayLen knots $ \nKnots knots ->
        withArray controls $ \controls -> do
          nurbsCurve nurbsObj (fromIntegral nKnots) knots stride controls order

  flush

  where
    order = 3
    stride = 4 -- number of floats in Vertex
    controls = zipWith mkControl points weights
    mkControl (x, y) w = Vertex4 (x*w) (y*w) 0 w
    -- mkControl (x, y) w = Vertex4 x y 0 w
    knots = [0, 0, 0, 0.25, 0.25, 0.5, 0.5, 0.75, 0.75, 1, 1, 1]
    weights = let a = sqrt 0.5 in [1, a, 1, a, 1, a, 1, a, 1]
    points = [
      (x0, y1), (x0, y2), (x1, y2),
      (x2, y2), (x2, y1), (x2, y0),
      (x1, y0), (x0, y0), (x0, y1)
      ]

    y1 = (y0 + y2) / 2
    x1 = (x0 + x2) / 2
    (x0, x2) = (50, 450)
    (y0, y2) = (x0, x2)

reshape :: ReshapeCallback
reshape size@(Size w h) = do
   viewport $= (Position 0 0, size)
   matrixMode $= Projection
   loadIdentity
   ortho2D 0 (fromIntegral w) 0 (fromIntegral h)
   -- the following line is not in the original example, but it's good style...
   matrixMode $= Modelview 0

keyboard :: KeyboardMouseCallback
keyboard (Char '\27') Down _ _ = exitWith ExitSuccess
keyboard _ _ _ _ = return ()

--  Request double buffer display mode.
--  Register mouse input callback functions
main :: IO ()
main = do
   (progName, _args) <- getArgsAndInitialize
   initialDisplayMode $= [ SingleBuffered, RGBMode ]
   initialWindowSize $= Size 500 500
   initialWindowPosition $= Position 100 100
   createWindow "Test"
   myInit
   displayCallback $= display
   reshapeCallback $= Just reshape
   keyboardMouseCallback $= Just keyboard
   mainLoop

编辑:我重新检查了几次系数,在矩形表示之前,我使用了7点三角形方法,并且有非常相似的失真。因此,这将不会是一个具体的代表性问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-10-17 20:55:38

好吧,我的第一次回答是一时的不理智。无论如何,您的代码中根本没有bug :如果我编译并执行给定的代码,我会得到以下内容:

我猜你的Haskell装置坏了。

基于注释的编辑:

这是奇怪的…

我正在使用Gentoo,所以我的系统通过Portage系统提供Haskell OpenGL模块。这样我就明白了:

代码语言:javascript
复制
loki ~ # for p in OpenGL OpenGLRaw GLURaw; do ghc-pkg latest $p; done
OpenGL-2.2.1.1
ghc-pkg: cannot find package OpenGLRaw
ghc-pkg: cannot find package GLURaw

因此,我在家里安装了带有阴谋的OpenGL和GLUT模块:

代码语言:javascript
复制
datenwolf@loki ~ $  for p in OpenGL OpenGLRaw GLURaw; do ghc-pkg latest $p; done
OpenGL-2.4.0.1
OpenGLRaw-1.1.0.1
GLURaw-1.1.0.0

和安装了这些,我可以复制你的第一张照片!

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

https://stackoverflow.com/questions/7798662

复制
相关文章

相似问题

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