2011-04-21 3 views
1

하스켈을 사용하여 OpenGL에서 여러 텍스처를 사용하려고했습니다. 나는 NeHe tuts와 여러 가지 다른 OpenGL 리소스를 온라인에서 따라 왔지만 약간 다른 호출과 내 초등의 조합이로드 블록을 발생 시켰습니다.하스켈을 사용하여 OpenGL에서 텍스처를 변경하는 방법

구체적으로 말하자면 각각 다른 질감의 큐브를 렌더링하고 싶습니다 (당분간 모든 6 개면에 대해 동일한 텍스처). 하나의 입방체를 텍스처와 함께 렌더링하는 것이 좋습니다. 동일한 텍스처의 여러 큐브를 렌더링하면 잘 작동합니다. 나는 질감을 변경하려면 전화를 틀리지 않는 경우하지만 두 큐브

에 대한 질감을 변경하는 방법을 알아낼 수 없었던 것입니다 : mytexture이 예상되는

textureBinding $ Texture2D $= Just *mytexture* 

이되고 어떤 형태의 textureID (TextureObject). mytexture 자리에 도대체 무엇이 들어 있습니까? 이것은 정말 쉬워야한다. 그러나 나는 2 일 동안이 부분을 아무 소용이없는 것으로 생각하려고 노력했다. 어떤 도움을 주셔서 감사합니다.

홈페이지 : 나는 (네트에서 차용) getAndCreateTextures 전화

-- 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 

홈페이지 에서, 텍스처 개체의 목록을 반환합니다. (렌더링)

디스플레이 :

-- 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

+0

@llanoraw 일반적으로 텍스처로드와 사용법을 분리합니다. 시작할 때 모든 텍스처를로드 할 수 있거나 현재 표시된 장면을 기반으로 한 번에 몇 개를로드하려고 할 수 있습니다. 지오메트리를 레코드의 필요한 'TextureObject'(들)와 쌍으로 연결 한 다음 해당 레코드를 렌더링하는 것이 좋습니다. – Anthony

0

당신은 텍스처 객체를 전달합니다. getAndCreateTextures은 겉보기에 텍스처 개체 목록을 제공합니다. 당신은 거의 확실하게 당신이 필요로하고있다

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

이것은 내가 생각한 것입니다. 실행에 실패하더라도. 타입 불일치가 발생합니다 : 예상 타입 TextureTarget, 실제 타입 IO() – llanoraw

관련 문제