【发布时间】:2011-04-21 21:34:09
【问题描述】:
我一直在尝试使用 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
【问题讨论】: