Derelict, SDL, and OpenGL3: Triangle Tribulations

DarthCthulhu via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 11 20:32:46 PDT 2015


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.

Code:

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 title = "Hello, world! With SDL2 OpenGL3.3!";
	writefln(title);

	// Create a new OGL Window
	Window win = new SDL_OGL_Window(title);

	GLuint vertexbuffer = initVAO();

	// Main loop flag
	bool quit = false;

	//Event handler
	SDL_Event e;

	glClearColor(0,0,0.4,0);

	do {
		
		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(win.window);

		// Handle events on queue
		while( SDL_PollEvent( &e ) != 0 ) {
			//User requests quit
			if( e.type == SDL_QUIT ) {
				quit = true;
			}
		}

		const Uint8* currentKeyStates = SDL_GetKeyboardState( null );

		// Escape to quit as well.
		if( currentKeyStates[ SDL_SCANCODE_ESCAPE ] ) {
			quit = true;;
		}

	} while ( !quit );

	glDeleteBuffers(1, &vertexbuffer);

}

The SDL_OGL_Window is an object that creates an SDL window and 
binds a OpenGL context to it; I don't think it's relevant for 
what's going on, but if anyone thinks it might be the culprit, 
I'll post it.

So, any ideas what I'm doing wrong?


More information about the Digitalmars-d-learn mailing list