haskell-opengl / OpenGLRaw

Haskell bindings to OpenGL (direct C bindings)
http://www.haskell.org/haskellwiki/OpenGL
BSD 3-Clause "New" or "Revised" License
47 stars 19 forks source link

GL functions are located but never called #20

Closed tcsavage closed 10 years ago

tcsavage commented 10 years ago

I have encountered a very strange issue on Windows where functions are located (or at least searched for) by wglGetProcAddress but never seem to be called. I first encountered this problem when trying to create some textures but seems to be a much larger problem.

Here is a fairly minimal test case which illustrates the problem.

module Main where

import Graphics.Rendering.OpenGL
import Graphics.UI.GLUT

-- To simplify loading data into textures
import Graphics.GLUtil
import Graphics.GLUtil.JuicyTextures

main :: IO ()
main = do
    -- Setup
    initialDisplayMode $= [DoubleBuffered]
    initialWindowSize $= (Size 600 600)
    (progname, args) <- getArgsAndInitialize
    wnd <- createWindow "Test case"

    -- No GL functions are actually called in this next block for some reason (except glActiveTexture)
    clearColor $= Color4 0 0 0 0
    textureObject <- genObjectName
    readTexInfo "texture.png" $ reloadTexture textureObject
    activeTexture $= TextureUnit 0
    textureBinding Texture2D $= Just textureObject

    -- Print the texture object to show that we did get a number back
    print textureObject

    -- Run the program
    displayCallback $= display
    putStrLn "Running loop"
    mainLoop

display :: IO ()
display = do
    clear [ColorBuffer]
    -- Keeping it simple. I get the same result using shaders and VBOs
    -- No GL functions are ever called in here either
    renderPrimitive Quads $ do
        texCoord (TexCoord2 0 0 :: TexCoord2 GLdouble)
        vertex (Vertex2 (-1) 1 :: Vertex2 GLdouble)
        texCoord (TexCoord2 1 0 :: TexCoord2 GLdouble)
        vertex (Vertex2 1 1 :: Vertex2 GLdouble)
        texCoord (TexCoord2 1 1 :: TexCoord2 GLdouble)
        vertex (Vertex2 1 (-1) :: Vertex2 GLdouble)
        texCoord (TexCoord2 0 1 :: TexCoord2 GLdouble)
        vertex (Vertex2 (-1) (-1) :: Vertex2 GLdouble)
    swapBuffers

This should simply load a texture and display it, but all I see is a white screen. I have executed this program under gDEBugger and the following is a log of all the OpenGL-related function calls it saw:

wglGetProcAddress(glClearColor) 
wglGetProcAddress(glGenTextures) 
wglGetProcAddress(glBindTexture) 
wglGetProcAddress(glTexImage2D) 
wglGetProcAddress(glActiveTexture) 
glActiveTexture(GL_TEXTURE0) 
wglGetCurrentContext() 
wglGetCurrentDC() 
wglGetCurrentContext() 
wglGetCurrentDC() 
glViewport(0, 0, 600, 600) 
wglGetCurrentContext() 
wglGetCurrentDC() 
wglGetCurrentContext() 
wglGetCurrentDC() 
wglGetProcAddress(glClear) 
wglGetProcAddress(glBegin) 
wglGetProcAddress(glTexCoord2d) 
wglGetProcAddress(glVertex2d) 
wglGetProcAddress(glEnd) 
wglSwapBuffers(0x4B012FAB) 

As you can see, while there are a couple of GL functions executed, most of them aren't. What's weirder still is that even though glGenTextures never seems to be executed, textureObject still seems to get a value.

It seems to me like the functions aren't being located correctly, but if that's the case, where does the value of textureObject come from? And shouldn't it be erroring instead?

Any idea what's going on here?

GHC 7.6.3 OpenGLRaw 1.4 OpenGL 2.9 GLUT 2.5

tcsavage commented 10 years ago

Upon returning to have another look at this I have discovered the source of my confusion: it turns out that gDEBugger just doesn't see all of the GL function calls made by the library (for some reason) and can't track the context state correctly. This coupled with my forgetting to set a texture filter (hence the texture not displaying) let me to falsely assume something weird was going on with the library not calling functions.

My problem has resolved itself, feel free to close this issue.