2011-04-21 18 views
1

我被困在使用Haskell獲取多個紋理在OpenGL中工作。我一直在網上關注NeHe Tuts和一些其他各種OpenGL資源,但稍微不同的調用和我的新手的組合已經造成了障礙。如何使用Haskell更改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 

主要我打電話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 

回答

1

getAndCreateTextures幾乎可以肯定是在做你所需要的。我在網上找到的那個名字的功能有類型IO [Maybe TextureObject],那些是你需要的TextureObject值。所以你可以做,

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

例如。

+0

丁丁丁!這允許我加載多個紋理並將它們傳遞給渲染部分。接下來的想法,隨着紋理列表的增長,在當前的設置中,我必須傳遞每個紋理才能夠調用它們。爲了簡化多個紋理的傳遞,我只需在一個單獨的區域中調用getAndCreateTextures並將大量紋理作爲一個變量返回,是正確的? – llanoraw 2011-04-21 17:57:57

+0

@llanoraw通常你會分開加載紋理和它們的用法。您可能能夠在啓動時加載所有紋理,或者您可能希望基於當前可見的場景一次加載幾個紋理。您可能想考慮將幾何與配對中的必要'TextureObject'配對,然後渲染該記錄。 – Anthony 2011-04-21 20:12:59

0

您傳遞紋理對象。 getAndCreateTextures貌似給了你一個紋理對象列表。你這些信息傳遞給綁定的一個,像

textureBinding $ Texture2D $= Just tex[0] 
+0

這就是我在想什麼,儘管它無法運行。我得到一個類型不匹配:期望類型TextureTarget,實際類型IO() – llanoraw 2011-04-21 17:10:16