如何使用Haskell在OpenGL中更改纹理

时间:2011-04-21 13:02:03

标签: opengl haskell

我很难尝试使用Haskell在OpenGL中使用多个纹理。我一直在线关注NeHe tuts和其他各种OpenGL资源,但是稍微不同的电话和我的新闻的组合已经造成了障碍。

具体来说,我想要渲染两个立方体,每个立方体具有不同的纹理(目前所有6个面的纹理相同)。渲染一个立方体,纹理很好。渲染相同纹理的多个立方体也可以正常工作。但我无法弄清楚如何改变两个立方体的纹理

如果我没弄错,更改纹理的调用是:

textureBinding $ Texture2D $= Just *mytexture*

其中 mytexture 应该是textureID(TextureObject)的某种形式。 mytexture 点的内容是什么?这应该是非常容易的,但我花了2天的时间来试图弄清楚这一点无济于事。任何帮助表示赞赏。

主:

    -- imports --
import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT
import Data.IORef
import Display
import Bindings
import Control.Monad
import Textures

-- main --
main = do
    (program, _) <- getArgsAndInitialize         -- convenience, return program name and non-GLUT commands
    initialDisplayMode $= [DoubleBuffered, WithDepthBuffer] -- inital display mode
    initialWindowSize $= Size 600 600
    createWindow "OpenGL Basics"
    reshapeCallback $= Just reshape
    angle <- newIORef (0.1::GLfloat) -- linked to angle of rotation (speed?)
    delta <- newIORef (0.1::GLfloat)
    position <- newIORef (0.0::GLfloat, 0.0) -- position, pass to display
    texture Texture2D $= Enabled
    tex <- getAndCreateTextures ["goldblock","pumpkintop"]
    keyboardMouseCallback $= Just (keyboardMouse delta position) --require keys, delta, and position
    idleCallback $= Just (idle angle delta) --ref idle angle and delta
    displayCallback $= (display angle position tex) --ref display angle and delta
    cullFace $= Just Front 
    mainLoop    -- runs forever until a hard exit is called

Main 中,我调用getAndCreateTextures(从网上借来),它返回一个纹理对象列表。

显示(用于渲染):

    -- display (main) --
display angle position tex = do
    clear [ColorBuffer, DepthBuffer]
    loadIdentity --modelview
    shadeModel $= Smooth
    (x,z) <- get position --get current position from init or keys
    translate $ Vector3 x 0 z -- move to the position before drawing stuff
--    DO STUFF HERE
--    texture $ Texture2D $= Just wtfgoeshere
    preservingMatrix $ do
        a <- get angle
        rotate a $ Vector3 (1::GLfloat) 0 0
--        rotate a $ Vector3 0 0 (1::GLfloat)
        rotate a $ Vector3 0 (1::GLfloat) 0
--        scale 0.7 0.7 (0.7::GLfloat)
--        color $ Color3 (0.5::GLfloat) (0.1::GLfloat) (0.1::GLfloat)
        cubeTexture (0.1::GLfloat)
    swapBuffers

--idle (main)
idle angle delta = do
    a <- get angle      -- get existing angle
    d <- get delta      -- get delta
    angle $= a + d      -- new angle is old angle plus plus delta
    postRedisplay Nothing

2 个答案:

答案 0 :(得分:1)

getAndCreateTextures几乎可以肯定做你需要的。我在网络上找到的该名称的功能类型为IO [Maybe TextureObject],这些是您需要的TextureObject值。所以你可以做到,

[gtex, ptex] <- getAndCreateTextures ["goldblock","pumpkintop"]
textureBinding Texture2D $= gtex

例如。

答案 1 :(得分:0)

传递纹理对象。 getAndCreateTextures似乎给你一个纹理对象列表。您将其中一个传递给绑定,例如

textureBinding $ Texture2D $= Just tex[0]