Derelict OpenGL basic program does not work but OpenGL does not say anything is wrong?

Bennet via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Feb 13 08:38:38 PST 2015


On Friday, 13 February 2015 at 09:18:29 UTC, Mike Parker wrote:
> On 2/13/2015 6:14 PM, Mike Parker wrote:
>> On 2/13/2015 12:46 PM, Bennet wrote:
>>> I've begun writing some basic OpenGL code using DerelictGL3 
>>> but I've hit
>>> a wall. I've managed to open a window (with DerelictSDL2) and 
>>> call basic
>>> OpenGL functions such as glClear(). Still I can not get a 
>>> basic triangle
>>> up and running. glError() does not return anything and when 
>>> compiling,
>>> linking, and validating the shaders I get no errors. My code 
>>> is located
>>> at: https://github.com/BennetLeff/PhySim
>>
>> As Nils said, there's no need to call DerelictGL3.load/reload 
>> all over
>> the place. The load method only need be called once to load 
>> the shared
>> library into memory and the reload method should be called 
>> every time
>> you change contexts. Since you are only using on context, then 
>> you only
>> need to call it once. display.d is a good place for it, but 
>> you can
>> delete all the calls the load/reload in sharer.d and mesh.d.
>>
>> That, of course, is not the cause of your problem. I haven't 
>> run your
>> code, but after a quick look through, I see you're doing some 
>> weird
>> stuff that simply isn't going to work.
>>
>> Here, vertices is a pointer to Vertex, which you've declared 
>> as a class
>> to wrap the struct type vec3 from gl3n. In app.d you've done 
>> this:
>>
>> ```
>> Vertex v1 = new Vertex(vec3(-0.5, -0.5, 0));
>> Vertex v2 = new Vertex(vec3(0, 0.5, 0));
>> Vertex v3 = new Vertex(vec3(0.5, -0.5, 0));
>>
>> Vertex* vertices = [v1, v2, v3].ptr;
>> ```
>>
>> First, that's a horrid way to declare an array. Second, it's 
>> an array of
>> class /references/. From line 24 of mesh.d:
>>
>> ```
>> glBufferData(GL_ARRAY_BUFFER, num_vertices * 
>> vertices[0].sizeof,
>> vertices, GL_STATIC_DRAW);
>> ```
>>
>> vertices[0].sizeof is giving you the size of a class reference.
>> Absolutely not what you want here. Compile and run the 
>> following to see
>> what I mean:
>>
>> ```
>> module sizes;
>>
>> class Foo {
>>     int x, y, z;
>> }
>>
>> struct SFoo {
>>     int x, y, z;
>> }
>>
>> void main() {
>>     import std.stdio : writeln;
>>
>>     auto foo = new Foo;
>>     writeln( "Foo Size: ", foo.sizeof );
>>
>>     SFoo sfoo;
>>     writeln( "SFoo Size: ", sfoo.sizeof );
>> }
>> ```
>>
>> If I were you, I'd drop the vertex class completely. The way 
>> you're
>> using it is pointless. Just use the vec3 type directly. It's a 
>> struct,
>> which means it's easy to reason about for this sort of work.
>>
>> ```
>> auto vertices = [ vec3(-0.5,-0.5,0), vec3( 0, 0.5, 0), vec3( 
>> 0.5, -0.5,
>> 0)];
>> auto mesh = new Mesh( vertices, 3 );
>> ////
>> class Mesh
>> {
>>     this(vec3[] vertices, int num_vertices)
>>     {
>>         glBufferData(GL_ARRAY_BUFFER, vertices.length, 
>> vertices.ptr,
>> GL_STATIC_DRAW);
>>     }
>> }
>> ```
>
> Sorry, that should be vertices.length * vec3.sizeof. And you 
> can eliminate the num_vertices parameter.
Thank you, this made my code run. I appreciate the help as 
obviously I am new to the D language.


More information about the Digitalmars-d-learn mailing list