OpenGLRaw Tutorial

I'm trying to follow this book on OpenGL: http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html I'm trying to follow the examples using the OpenGLRaw package as the OpenGL package doesn't map very neatly to any of the examples. For instance, it is not clear to me now to create and use the vertex array used in the example that I linked to. However, I'm struggling a bit because I haven't used Haskell's foreign interface before. Here is an attempt which is expected to draw a triangle, but instead draws nothing: http://hpaste.org/83837 Does anyone know of a tutorial for OpenGLRaw or the foreign interface that might help me understand how to marshall data around? It seems like many people turn to OpenGLRaw when they're learning OpenGL so that they can follow the tutorials. I imagine it would be useful to have a guide out there that covers how to actually use it.

Hi, Please look at: http://code.haskell.org/GLUT/examples/RedBook/ The Red Book is the first resource you should try (with these examples translated to Haskell). Emanuel

On 11/03/13 06:45, Michael Baker wrote:
I'm trying to follow this book on OpenGL: http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html
I'm trying to follow the examples using the OpenGLRaw package as the OpenGL package doesn't map very neatly to any of the examples. For instance, it is not clear to me now to create and use the vertex array used in the example that I linked to.
However, I'm struggling a bit because I haven't used Haskell's foreign interface before. Here is an attempt which is expected to draw a triangle, but instead draws nothing: http://hpaste.org/83837
Does anyone know of a tutorial for OpenGLRaw or the foreign interface that might help me understand how to marshall data around? It seems like many people turn to OpenGLRaw when they're learning OpenGL so that they can follow the tutorials. I imagine it would be useful to have a guide out there that covers how to actually use it.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
I don't think that it's the best idea to learn OpenGL using Haskell simply because of how drastically different C and Haskell are and for the reason you mentioned yourself. Nevertheless, you can find many examples at [1]. You can find things such as the red book examples translated into Haskell as well as links to other resources. [1] - http://www.haskell.org/haskellwiki/Opengl -- Mateusz K.

Instead of using some complex functions, you should probably try to simply use simpler constructs to see if everything is correctly setup: glBegin gl_TRIANGLES glVertex3f 0.75 0.75 0.0 glVertex3f 0.75 (-0.75) 0.0 glVertex3f (-0.75) (-0.75) 0.0 glEnd or glBegin gl_TRIANGLES glVertex4f 0.75 0.75 0.0 1 glVertex4f 0.75 (-0.75) 0.0 1 glVertex4f (-0.75) (-0.75) 0.0 1 glEnd Your program shows something using this. I haven't looked at the tutorial you're following but you should stick to the OpenGL references as i've found that most tutorials are crap (there are still good ones like NeHe stuff i think). Back to your problem: you should use GLfloat and not Float in your vertex definition. Your buffer definition should be: withArray verticies $ \ary -> glBufferData gl_ARRAY_BUFFER -- (fromIntegral $ sizeOf ary) (fromIntegral (length verticies * sizeOf (0::GLfloat))) ary gl_STATIC_DRAW The C version may get away with (sizeof vertices) because it uses the actual array (may be false) size but you have to compute the size of the array that is the number of elements times the size of elements. You're trying to draw from a VBO so i think you should not use --glEnableVertexAttribArray 0 You use the VBO: You need to enable client state glEnableClientState gl_VERTEX_ARRAY Bind the buffer that holds the data glBindBuffer gl_ARRAY_BUFFER buffer Set the bound buffer to be the vertex coords glVertexPointer 4 gl_FLOAT 0 nullPtr Call the draw glDrawArrays gl_TRIANGLES 0 3 glDisableClientState gl_VERTEX_ARRAY This may be explained properly in: http://nehe.gamedev.net/tutorial/vertex_buffer_objects/22002/ On Mon, 2013-03-11 at 01:45 -0500, Michael Baker wrote:
I'm trying to follow this book on OpenGL: http://www.arcsynthesis.org/gltut/Basics/Tut01%20Following% 20the%20Data.html
I'm trying to follow the examples using the OpenGLRaw package as the OpenGL package doesn't map very neatly to any of the examples. For instance, it is not clear to me now to create and use the vertex array used in the example that I linked to.
However, I'm struggling a bit because I haven't used Haskell's foreign interface before. Here is an attempt which is expected to draw a triangle, but instead draws nothing: http://hpaste.org/83837
Does anyone know of a tutorial for OpenGLRaw or the foreign interface that might help me understand how to marshall data around? It seems like many people turn to OpenGLRaw when they're learning OpenGL so that they can follow the tutorials. I imagine it would be useful to have a guide out there that covers how to actually use it.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Michael Baker
However, I'm struggling a bit because I haven't used Haskell's foreign interface before. Here is an attempt which is expected to draw a triangle, but instead draws nothing: http://hpaste.org/83837
The first thing I notice is that you never set a vertex color, which is black by default in all cases I've encountered. So it may be drawing, but you may simply not see it. =) Before you begin drawing put this somewhere: glColor3f 1 0 0 That should draw a red shape. Another option is to set a different clear color: glClearColor 1 0 0 1 That sets a red background.
Does anyone know of a tutorial for OpenGLRaw or the foreign interface that might help me understand how to marshall data around? It seems like many people turn to OpenGLRaw when they're learning OpenGL so that they can follow the tutorials. I imagine it would be useful to have a guide out there that covers how to actually use it.
My recommendation is to go with the higher level OpenGL library. The main difference is that the `gl` prefix is dropped and the numerous similar functions (`color2f`, `color3f`, `color4f`, etc.) are collapsed into a single polymorphic function `color`: import qualified Graphics.Rendering.OpenGL as GL GL.color (GL.Color3 1 0 (0 :: GL.GLfloat)) This looks more complicated, but it makes other things a lot easier, so in the end you win. Greets, Ertugrul -- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.

Thanks for the advice everyone.
I'm using Haskell rather than C/C++ because I'm not particularly interested
in writing an entire application in C/C++. If I learn OpenGL using C, then
I'm still going to have to learn to use the bindings in whatever language I
ultimately decide to use. Haskell seems well suited to this given all of
its nice foreign interface packages. Additionally, knowing how to interface
with C libraries seems like a useful skill to have.
I've already gone through the first few chapters of the red book using the
higher level OpenGL. The concern that lead me to try OpenGLRaw instead is
that drawing every vertex/color/etc with a separate function call seems
incredibly inefficient. I imagine I'm going to have to learn to use buffers
at some point. In fact, there is a section about them in the second chapter
of the red book, which I skipped because I couldn't figure out how to do it
with the high level OpenGL package. Also, it seems like the high level
package only truly supports the 2.1 spec, whereas the Raw package supports
the 3.2 spec.
If everyone legitimately thinks that the higher level library is the way to
go for writing actual OpenGL programs then I'll give it another shot. It
looks like the Haskell translations of the red book examples might get me
unblocked in that respect. I just felt like I would be better served by
learning how to use the lower level bindings, which are closer to actual
OpenGL.
On Mon, Mar 11, 2013 at 6:43 AM, Ertugrul Söylemez
Michael Baker
wrote: However, I'm struggling a bit because I haven't used Haskell's foreign interface before. Here is an attempt which is expected to draw a triangle, but instead draws nothing: http://hpaste.org/83837
The first thing I notice is that you never set a vertex color, which is black by default in all cases I've encountered. So it may be drawing, but you may simply not see it. =)
Before you begin drawing put this somewhere:
glColor3f 1 0 0
That should draw a red shape. Another option is to set a different clear color:
glClearColor 1 0 0 1
That sets a red background.
Does anyone know of a tutorial for OpenGLRaw or the foreign interface that might help me understand how to marshall data around? It seems like many people turn to OpenGLRaw when they're learning OpenGL so that they can follow the tutorials. I imagine it would be useful to have a guide out there that covers how to actually use it.
My recommendation is to go with the higher level OpenGL library. The main difference is that the `gl` prefix is dropped and the numerous similar functions (`color2f`, `color3f`, `color4f`, etc.) are collapsed into a single polymorphic function `color`:
import qualified Graphics.Rendering.OpenGL as GL
GL.color (GL.Color3 1 0 (0 :: GL.GLfloat))
This looks more complicated, but it makes other things a lot easier, so in the end you win.
Greets, Ertugrul
-- Not to be or to be and (not to be or to be and (not to be or to be and (not to be or to be and ... that is the list monad.
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On 03/11/2013 11:41 AM, Michael Baker wrote:
The concern that lead me to try OpenGLRaw instead is that drawing every vertex/color/etc with a separate function call seems incredibly inefficient. I imagine I'm going to have to learn to use buffers at some point.
You are correct. Partly for that reason, glBegin()/glEnd()/glVertex*() don't even exist in OpenGL ES and are deprecated since OpenGL 3.0. It's a shame that so many OpenGL tutorials still teach them. Note to others: your program depends on glfw-b (or anyway, Hackage 'glfw-b' works and Hackage 'glfw' doesn't). glEnableVertexAttribArray/glVertexAttribPointer are for passing data to shader programs, I believe. Everyone should use shader programs (GLSL). GL without shader programs uses the "fixed-function pipeline" which is deprecated. But I haven't used shaders yet, so I've attached how to do it with VBOs and the fixed-function pipeline. I used glInterleavedArrays and glDrawArrays. (That's probably not the only way to do it; OpenGL has lots of legacy and semi-redundant functions.) As jean verdier noted, your byte count argument to glBufferData was wrong; "fromIntegral $ length verticies * sizeOf (head verticies)" fixes it. Four coordinates per vertex is (as far as I know) not very useful for two-dimensional or three-dimensional shapes. Doing glGenBuffers every frame without glDeleteBuffers leaks buffers. Either save them or delete them. I didn't fix this. Control.Exception's bracket or bracket_ can be useful for this sort of thing. I also attached a version that can attach a color to each vertex. Haskell FFI peeps, is there a better way to do this than writing a Storable instance for each GL buffer data layout? -Isaac (who has been learning modern OpenGL for http://www.lasercake.net/ )

Excellent Isaac, thanks! On Mon, Mar 11, 2013 at 12:37 PM, Isaac Dupree < ml@isaac.cedarswampstudios.org> wrote:
On 03/11/2013 11:41 AM, Michael Baker wrote:
The concern that lead me to try OpenGLRaw instead is that drawing every vertex/color/etc with a separate function call seems incredibly inefficient. I imagine I'm going to have to learn to use buffers at some point.
You are correct. Partly for that reason, glBegin()/glEnd()/glVertex*() don't even exist in OpenGL ES and are deprecated since OpenGL 3.0. It's a shame that so many OpenGL tutorials still teach them.
Note to others: your program depends on glfw-b (or anyway, Hackage 'glfw-b' works and Hackage 'glfw' doesn't).
glEnableVertexAttribArray/**glVertexAttribPointer are for passing data to shader programs, I believe. Everyone should use shader programs (GLSL). GL without shader programs uses the "fixed-function pipeline" which is deprecated. But I haven't used shaders yet, so I've attached how to do it with VBOs and the fixed-function pipeline. I used glInterleavedArrays and glDrawArrays. (That's probably not the only way to do it; OpenGL has lots of legacy and semi-redundant functions.)
As jean verdier noted, your byte count argument to glBufferData was wrong; "fromIntegral $ length verticies * sizeOf (head verticies)" fixes it.
Four coordinates per vertex is (as far as I know) not very useful for two-dimensional or three-dimensional shapes.
Doing glGenBuffers every frame without glDeleteBuffers leaks buffers. Either save them or delete them. I didn't fix this. Control.Exception's bracket or bracket_ can be useful for this sort of thing.
I also attached a version that can attach a color to each vertex. Haskell FFI peeps, is there a better way to do this than writing a Storable instance for each GL buffer data layout?
-Isaac (who has been learning modern OpenGL for http://www.lasercake.net/)
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (6)
-
Emanuel Koczwara
-
Ertugrul Söylemez
-
Isaac Dupree
-
jean verdier
-
Mateusz Kowalczyk
-
Michael Baker