I’m stuck trying to get multiple textures working in OpenGL using Haskell. I’ve been following the NeHe tuts and some various other OpenGL resources online, but the combination of slightly different calls and my newbness has caused a roadblock.
To be specific I want to render two cubes, each with a different texture (same texture for all 6 faces for the time being). Rendering one cube, with a texture is fine. Rendering multiple cubes of the same texture works fine as well. But I’ve not been able to figure out how to change textures for the two cubes
If I’m not mistaken the call to change textures is:
textureBinding $ Texture2D $= Just *mytexture*
where mytexture is supposed to be some form of textureID (a TextureObject). What the heck goes into the mytexture spot? This should be really easy but I’ve spent the better part of 2 days trying to figure this out to no avail. Any help is appreciated.
Main:
-- 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
In Main I call getAndCreateTextures (borrowed from the net), which returns a list of texture objects.
Display (for rendering):
-- 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
getAndCreateTexturesis almost certainly doing what you need. The function of that name I found on the web has typeIO [Maybe TextureObject], those are theTextureObjectvalues you need. So you could do,for example.