www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Derelict OpenGL basic program does not work but OpenGL does not say

reply "Bennet" <bennetleff gmail.com> writes:
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
Feb 12 2015
next sibling parent "Nils Forsman" <forsmannils gmail.com> writes:
void main(){
     DerelictGL3.load();
     //create your sdl context, window hints,
     DerelictGL3.reload();
     //Do other stuff...
}

You got to have a valid context before you call reload. These two 
functions shall be called only once, I have the impression that 
you call them multiple times.
Feb 13 2015
prev sibling parent reply Mike Parker <aldacron gmail.com> writes:
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); } } ``` Of course, that assumes that vec3.sizeof is the same as float.sizeof*3. If not, you won't be able to pass the array pointer in directly and will have to take a different approach.
Feb 13 2015
parent reply Mike Parker <aldacron gmail.com> writes:
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.
Feb 13 2015
parent "Bennet" <bennetleff gmail.com> writes:
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.
Feb 13 2015