No triangle with OpenGL (DerelictGLFW and DerelictGL3)

Mike Parker via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jun 11 20:46:31 PDT 2016


On Sunday, 12 June 2016 at 02:16:52 UTC, Peter Lewis wrote:
> Hi all.
> I am trying to create a basic OpenGL triangle in a GLFW 
> instance. The window works, I can change the background colour 
> and everything but for the life of me I can't get the triangle 
> to show up. Instead of trying to put everything in the post, I 
> have put it on github. (https://github.com/werl/d_glfw_tests) I 
> am currently following a tutorial I found 
> (http://learnopengl.com/#!Getting-started/Hello-Triangle).
> Any help is appreciated, Thanks!

Your problem is this line:

glBufferData(GL_ARRAY_BUFFER, verts.sizeof, &verts, 
GL_STATIC_DRAW);

First, the array declarations in the example C code create static 
arrays. Calling sizeof(array) on a static array in C will give 
you the combined size of all the elements. For a dynamic array, 
it will give you the size of a pointer.

The same is true in D, but there's a difference in the 
declaration syntax. float[9] foo; is a static array, but float[] 
bar; is *always* a dynamic array. foo.size of will give you 36. 
bar.size of will give you 8 in 32-bit and 16 in 64-bit, because 
its giving you the size of the length and pointer that make up 
the array itself, not of the contents.

Second, &verts is giving you a pointer to the array itself (i.e. 
the length and pointer pair) and *not* the contents. D arrays are 
not C arrays!

With the two changes below, your triangle renders fine.

glBufferData(GL_ARRAY_BUFFER, verts.length * float.sizeof, 
verts.ptr, GL_STATIC_DRAW);


More information about the Digitalmars-d-learn mailing list