Derelict, SDL, and OpenGL3: Triangle Tribulations
Mike Parker via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Aug 11 22:46:26 PDT 2015
On Wednesday, 12 August 2015 at 05:34:22 UTC, BBasile wrote:
> For me the following code works:
>
> ---
> import derelict.sdl2.sdl;
> import derelict.opengl3.gl3;
> import derelict.opengl3.gl;
>
> import std.stdio;
>
> static this()
> {
> DerelictGL3.load;
> DerelictGL.load;
> DerelictSDL2.load;
> SDL_Init(SDL_INIT_VIDEO);
> }
>
> static ~this()
> {
> SDL_Quit();
> DerelictGL3.unload;
> DerelictSDL2.unload;
> }
>
> GLuint initVAO () {
>
> // An array of 3 vectors which represents 3 vertices
> static const GLfloat[] g_vertex_buffer_data = [
> -1.0f, -1.0f, 0.0f,
> 1.0f, -1.0f, 0.0f,
> 0.0f, 1.0f, 0.0f,
> ];
>
> // This will identify our vertex buffer
> GLuint vertexbuffer;
>
> // Generate 1 buffer, put the resulting identifier in
> vertexbuffer
> glGenBuffers(1, &vertexbuffer);
>
> // The following commands will talk about our 'vertexbuffer'
> buffer
> glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
>
> // Give our vertices to OpenGL.
> glBufferData(GL_ARRAY_BUFFER, g_vertex_buffer_data.length *
> GL_FLOAT.sizeof, g_vertex_buffer_data.ptr, GL_STATIC_DRAW);
>
> glEnableVertexAttribArray(0);
> glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, null);
> glBindBuffer(GL_ARRAY_BUFFER, 0);
>
> return vertexbuffer;
> }
>
> void main(string[] args)
> {
>
> auto flags = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
> SDL_WINDOW_RESIZABLE;
> auto win = SDL_CreateWindow( null, 50, 50, 800, 600, flags);
> auto ctxt = SDL_GL_CreateContext(win);
>
> DerelictGL3.reload;
>
> GLuint vertexbuffer = initVAO();
>
> SDL_Event ev;
> while (true)
> {
> if (SDL_WaitEvent(&ev))
> {
> glClear(GL_COLOR_BUFFER_BIT);
>
> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
>
> glBindVertexArray(vertexbuffer);
>
> // Draw the triangle
> glDrawArrays(GL_TRIANGLES, 0, 3);
>
> glDisableVertexAttribArray(vertexbuffer);
> glBindVertexArray(0);
>
> SDL_GL_SwapWindow(SDL_GL_GetCurrentWindow());
> }
> if (ev.type == SDL_QUIT)
> break;
> }
>
> SDL_DestroyWindow(win);
> SDL_GL_DeleteContext(ctxt);
> }
> ---
>
> It looks like it's the window/context creation that fail for
> you because the OpenGL code is 100% the same.
It seems to me that your driver is doing things it isn't actually
supposed to do. This code is binding a vertex buffer object with
a function which is supposed to bind a vertex array object. The
only reason the vbo is bound at all is because of the call to
glBindBuffer in the misnamed initVAO -- a function which never
even initializes a vao. The spec actually requires a vao to be
created and a shader program to be bound, so I would expect a
conforming driver to show nothing.
I expect a couple of calls to glError will not come up empty.
More information about the Digitalmars-d-learn
mailing list