How to use glGenTextures

In C, I would do the following:

GLuint a; glGenTextures(1, &a); 

Type glGenTextures in Haskell:

 GLsizei -> Ptr GLuint -> IO () 

How can I get a value of type Ptr GLuint ?

+4
source share
2 answers

I think the exact conversion is:

 GLuint a; glGenTextures(1, &a); 

There 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 , which get the list, and reserve and initialize the buffer with this list. In addition, allocaArray can create a buffer for you without initializing it.

+2
source

First of all, I would like to point out that Haskell OpenGL bindings have a high-level implementation that does not require the user to manually manage memory .

In general, for any type of Storable a you can get a block of memory sufficient to store n elements of the specified type with mallocArray . The result of mallocArray is of type Ptr a ; as in C, you should use free to free the allocated memory space. You can also use allocaArray to temporarily allocate memory (equivalent to stack allocation in C). Here's how the OpenGL library uses allocaArray in combination 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 .

+3
source

All Articles