Derelict OpenGL basic program does not work but OpenGL does not say anything is wrong?
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Feb 13 01:14:25 PST 2015
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.
More information about the Digitalmars-d-learn
mailing list