7

我用 Haskell OpenGL 绘制了 Barth 六分仪,但出现了问题。我也在 R 中画了它(带有包rglmisc3d),没有问题。这两种算法(在 Haskell 和 R 中)非常相似。Barth sextic 是一个隐式曲面,在 Haskell 中,我使用行进立方体算法计算该曲面的三角剖分,该算法是通过将其中一个转换misc3d为 Haskell 和 C 得到的。我使用顶点法线,每个法线由隐式的梯度定义方程。

这是问题所在:

在此处输入图像描述

我不想要这些黑色阴影。当我看六分仪的背面时,旋转 180°,没有这样的阴影:

在此处输入图像描述

完整的代码可以在这个 Github repo中找到。以下是与颜色相关的代码部分:

fuchsia :: Color4 GLfloat
fuchsia = Color4 1.00 0.00 1.00 1
discord :: Color4 GLfloat
discord = Color4 0.21 0.22 0.25 1

  renderPrimitive Triangles $ mapM_ drawTriangle triangles
  swapBuffers
  where
    drawTriangle ((v1,v2,v3), (n1,n2,n3)) = do
      materialDiffuse FrontAndBack $= fuchsia
      normal n1
      vertex v1
      normal n2
      vertex v2
      normal n3
      vertex v3

  clearColor $= discord
  materialAmbient FrontAndBack $= black
  materialDiffuse FrontAndBack $= white
  materialEmission FrontAndBack $= black
  lighting $= Enabled
  lightModelTwoSide $= Enabled
  light (Light 0) $= Enabled
  position (Light 0) $= Vertex4 0 0 (-1000) 1
  ambient (Light 0) $= white
  diffuse (Light 0) $= white
  specular (Light 0) $= white

我试图改变最后一段代码中的颜色,但没有办法摆脱这些阴影。我对颜色做坏事了吗?我确信法线是正确的,因为这在 R 中有效。但是阴影出现在表面不光滑的地方,所以我想知道问题是否是由法线引起的。

R 渲染:

gfycat

编辑

我设法摆脱了这些阴影:

在此处输入图像描述

真不知道怎么了,做了这么多尝试……不过不管怎样,现在的问题是sextic的背面太轻了:

在此处输入图像描述

4

1 回答 1

2

现在渲染很好:-)

在此处输入图像描述

我不知道是什么导致了这个问题,因为我做了很多更改......这是代码的相关部分:

resize :: Double -> Size -> IO ()
resize zoom s@(Size w h) = do
  viewport $= (Position 0 0, s)
  matrixMode $= Projection
  loadIdentity
  perspective 45.0 (w'/h') 1.0 100.0
  lookAt (Vertex3 0 0 (-6+zoom)) (Vertex3 0 0 0) (Vector3 0 1 0)
  matrixMode $= Modelview 0
  where
    w' = realToFrac w
    h' = realToFrac h

main :: IO ()
main = do
  _ <- getArgsAndInitialize
  _ <- createWindow "Barth Sextic"
  windowSize $= Size 500 500
  initialDisplayMode $= [RGBMode, DoubleBuffered, WithDepthBuffer]
  clearColor $= discord
  clientState ColorArray $= Disabled -- this is a default option, I think
  materialAmbient Front $= black
  materialDiffuse Front $= white
  materialEmission Front $= Color4 0 0 0 0
  materialSpecular Front $= white
  materialShininess Front $= 50
  lighting $= Enabled
  light (Light 0) $= Enabled
  position (Light 0) $= Vertex4 500 500 (-1000) 1
  diffuse (Light 0) $= white
  specular (Light 0) $= white
  lightModelAmbient $= Color4 0.35 0.35 0.35 1
  depthMask $= Enabled -- this is default option
  depthFunc $= Just Lequal
  shadeModel $= Smooth
  fog $= Disabled -- this is default option, I think
  polygonMode $= (Fill, Fill) -- this is default option
  polygonSmooth $= Enabled
  cullFace $= Just Front
  rescaleNormal $= Enabled
  ......

我还更改了每个三角形顶点的顺序:

drawTriangle ((v1,v2,v3), (n1,n2,n3)) = do
  materialDiffuse Front $= fuchsia
  normal n1
  vertex v1
  normal n3
  vertex v3
  normal n2
  vertex v2

这个答案有点为时过早。稍后我将进行更多调查,我将进行编辑以撰写我的发现。

编辑

好吧,我做了进一步的调查,可悲的是,我的结论是我没有解释:我已经恢复了所有的更改,并且无法重现这个问题!

现在我使用这个更短的代码:

display :: ...... -> displayCallback
  ......
  renderPrimitive Triangles $
    mapM_ drawTriangle triangles
  swapBuffers
  where
    drawTriangle ((v1,v2,v3), (n1,n2,n3)) = do
      materialDiffuse Front $= fuchsia
      normal n1
      vertex v1
      normal n2
      vertex v2
      normal n3
      vertex v3

resize :: Double -> Size -> IO ()
resize zoom s@(Size w h) = do
  viewport $= (Position 0 0, s)
  matrixMode $= Projection
  loadIdentity
  perspective 45.0 (w'/h') 1.0 100.0
  lookAt (Vertex3 0 0 (-6+zoom)) (Vertex3 0 0 0) (Vector3 0 1 0)
  matrixMode $= Modelview 0
  where
    w' = realToFrac w
    h' = realToFrac h

这两个MatrixMode很重要。

main :: IO ()
main = do
  _ <- getArgsAndInitialize
  _ <- createWindow "Barth Sextic"
  windowSize $= Size 500 500
  initialDisplayMode $= [RGBMode, DoubleBuffered, WithDepthBuffer]
  clearColor $= discord
  materialAmbient Front $= black
  materialDiffuse Front $= white
  materialEmission Front $= black
  lighting $= Enabled
  light (Light 0) $= Enabled
  position (Light 0) $= Vertex4 500 500 (-1000) 1
  ambient (Light 0) $= white
  diffuse (Light 0) $= white
  specular (Light 0) $= white
  depthFunc $= Just Less
  shadeModel $= Smooth
  cullFace $= Just Back
  ......

polygonSmooth并且rescaleNormal没用。我也改变了灯的位置,但这不是导致问题的地方。这cullFace不是必需的,但它很好,因为没有可见的背面。

于 2018-11-17T20:55:43.120 回答