www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Converting (casting?) a dynamic array to a fixed array?

reply "WhatMeWorry" <kheaser gmail.com> writes:
This following code works fine. A triangle is displayed.

         GLfloat[6] verts = [  0.0,  1.0,
                              -1.0, -1.0,
                               1.0, -1.0 ];

         glGenBuffers(1, &vbo);
         glBindBuffer(GL_ARRAY_BUFFER, vbo);  //   Some of the 
types are:

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



Then, all I do is take out the 6 so that the static array becomes 
a dynamic one. It compiles fine.

          GLfloat[] verts = [  0.0,  1.0,
                              -1.0, -1.0,
                               1.0, -1.0 ];

However, when I run it, the triangle disappears.

According to OpenGL, glBufferData shows:  void glBufferData( 
ignore, GLsizeiptr size, const GLvoid * data, ignore);

So I thought the best solution would be to simply cast the 
dynamic array to a pointer?

So I tried:
glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid *) 
&verts, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid *) 
verts, GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, verts.sizeof, verts.ptr, 
GL_STATIC_DRAW);
and
glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid *) 
verts.ptr, GL_STATIC_DRAW);
and
nothing but more blank screens.

Any ideas?  Thanks.
May 03 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Monday, 4 May 2015 at 02:47:24 UTC, WhatMeWorry wrote:
         glBufferData(GL_ARRAY_BUFFER, verts.sizeof, &verts, 
 GL_STATIC_DRAW);
Try (GL_ARRAY_BUFFER, verts.length, verts.ptr, GL_STATIC_DRAW) or maybe: (GL_ARRAY_BUFFER, verts.length * verts[0].sizeof, verts.ptr, GL_STATIC_DRAW) I'm not sure exactly what the function needs, but using the .length and .ptr properties on a dynamic array will probably work better than casting, and definitely work better than casting the address of it. (A dynamic array is actually a struct { size_t length; T* ptr; } object instead of raw memory like a static array, so taking the address of it actually gives the address of that length variable, not the data. the ptr attribute gets the pointer to the data. BTW static arrays also have .length and .ptr properties you can use the same way.)
May 03 2015
prev sibling parent reply "rsw0x" <anonymous anonymous.com> writes:
On Monday, 4 May 2015 at 02:47:24 UTC, WhatMeWorry wrote:
 This following code works fine. A triangle is displayed.

         GLfloat[6] verts = [  0.0,  1.0,
                              -1.0, -1.0,
                               1.0, -1.0 ];

         glGenBuffers(1, &vbo);
         glBindBuffer(GL_ARRAY_BUFFER, vbo);  //   Some of the 
 types are:

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



 Then, all I do is take out the 6 so that the static array 
 becomes a dynamic one. It compiles fine.

          GLfloat[] verts = [  0.0,  1.0,
                              -1.0, -1.0,
                               1.0, -1.0 ];

 However, when I run it, the triangle disappears.

 According to OpenGL, glBufferData shows:  void glBufferData( 
 ignore, GLsizeiptr size, const GLvoid * data, ignore);

 So I thought the best solution would be to simply cast the 
 dynamic array to a pointer?

 So I tried:
 glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid 
 *) &verts, GL_STATIC_DRAW);
 and
 glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid 
 *) verts, GL_STATIC_DRAW);
 and
 glBufferData(GL_ARRAY_BUFFER, verts.sizeof, verts.ptr, 
 GL_STATIC_DRAW);
 and
 glBufferData(GL_ARRAY_BUFFER, verts.sizeof, cast(const GLvoid 
 *) verts.ptr, GL_STATIC_DRAW);
 and
 nothing but more blank screens.

 Any ideas?  Thanks.
sizeof on a slice doesn't do what you think it does, it returns the size of the actual slice object I believe.
May 03 2015
parent reply "WhatMeWorry" <kheaser gmail.com> writes:
Many thanks.  Just to recap, I got the code working with:

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

 sizeof on a slice doesn't do what you think it does, it returns 
 the size of the actual slice object I believe.
I have to admit that I didn't understand the above sentence until I did the following. Since this is a learning forum, I'll post all the details for other learners. GLfloat[6] staticVerts = [ 0.0, 1.0, -1.0, -1.0, 1.0, -1.0 ]; GLfloat[] dynamicVerts = [ 0.0, 1.0, -1.0, -1.0, 1.0, -1.0 ]; staticVerts.length (num elements) = 6 staticVerts.sizeof (num bytes) = 24 before glBufferData Press any key to continue . . . dynamicVerts.length (num elements) = 6 dynamicVerts.sizeof (num bytes) = 8 before glBufferData Press any key to continue . . . I see on the site under Array Properties Static array properties are: .sizeof Returns the array length multiplied by the number of bytes per array element. Dynamic array properties are: .sizeof Returns the size of the dynamic array reference, which is 8 in 32-bit builds and 16 on 64-bit builds. So that was my mistake. But just for arguments sake, wouldn't it have been better to define in dynamic array properties a .sizeofref and a .sizeof (which behaves like the static array.sizeof)?
May 04 2015
parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 05/04/2015 08:36 AM, WhatMeWorry wrote:

 But just for arguments sake, wouldn't it have
 been better to define in dynamic array properties a .sizeofref and a
 .sizeof (which behaves like the static array.sizeof)?
It would not work because 1) .sizeof means the size of the variable that a piece of code is dealing with, regardless of the size of data that it may own or manage. 2) It is and must be a compile-time property used in templates. Ali
May 04 2015