Derelict, SDL, and OpenGL3: Triangle Tribulations

BBasile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 11 22:34:18 PDT 2015


On Wednesday, 12 August 2015 at 03:32:47 UTC, DarthCthulhu wrote:
> So I decided to try some OGL3 stuff in D utilizing the Derelict 
> bindings and SDL. Creating an SDL-OGL window worked fine, but 
> I'm having trouble with doing the most basic thing of rendering 
> a triangle. I get the window just fine and the screen is being 
> properly cleared and buffered, but no triangle.
>
> So, any ideas what I'm doing wrong?

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.


More information about the Digitalmars-d-learn mailing list