user181351
user181351

Reputation:

How to use glGenTextures

In C I would do the following:

GLuint a;
glGenTextures(1, &a);

The type of glGenTextures in Haskell is:

GLsizei -> Ptr GLuint -> IO ()

How can I get a value of the type Ptr GLuint ?

Upvotes: 4

Views: 1495

Answers (2)

Zhen
Zhen

Reputation: 4283

I think the exact traslation of:

GLuint a;
glGenTextures(1, &a);

Is:

import Foreign.Marshal.Utils( with )

with a $ \dir_a -> glGenTextures 1 dir_a

If you need to pass an array, you can use withArray that get a list, and reserve and initilialize a buffer with that list. Also allocaArray can create for you the buffer without initialize it.

Upvotes: 2

Mikhail Glushenkov
Mikhail Glushenkov

Reputation: 15078

First of all, I'd like to point out that Haskell OpenGL bindings have a high-level incarnation that doesn't require the user to do manual memory management.

In general, for any Storable type a you can obtain a block of memory sufficient to hold n elements of the said type with mallocArray. The result of mallocArray has type Ptr a; like in C, you should use free to free allocated memory space afterwards. You can also use allocaArray to allocate memory temporarily (the equivalent of stack allocation in C). Here's how the OpenGL library uses allocaArray in conjunction with glGenTextures:

   genObjectNames n =
      allocaArray n $ \buf -> do
        glGenTextures (fromIntegral n) buf
        fmap (map TextureObject) $ peekArray n buf

You can find more information on these topics in the Haskell Wiki.

Upvotes: 3

Related Questions