[fpc-pascal] OpenGL and SDL frustrations

Ryan Joseph ryan at thealchemistguild.com
Wed May 24 05:13:46 CEST 2017


I’ve wasted so much time on this I just need to stop and ask for help already. Over the last few years I’ve been picking away at OpenGL and only ever got the legacy 2.x API to work which has been fine for making 2D games but I wanted to at least learn the modern API and shaders.

Here’s a snippet I cobbled together from a test program. It merely wants to draw a triangle. The actual OpenGL is from a tutorial and I don’t see how anything could be simpler than this. The only thing curious to me is why I can’t load the version of OpenGL I want from SDL. If I attempt to load 4.0 (with SDL_GL_SetAttribute) GL_VERSION returns null but if I load 3.3 it works and GL_VERSION returns 4.1 anyways (I’m a Mac from 2015).

I removed the error handling code here but I don’t get any errors, just a black screen. Despite the tutorial I was following not using a shader they still got a white triangle (I added a shader also but it still didn’t work so I removed it to reduce complexity). The only thing I can think of is they were using GLEW and it did some init that SDL isn’t doing but I doubt it. This is barebones simple OpenGL here.

Any ideas on what’s wrong with this? I don’t care about this example in particular I just want to draw ANYTHING using OpenGL 3.3 so I can move forward.

=====

type
	TVec2 = record
		x, y: GLfloat;
	end;

var
	verts: array[0..2] of TVec2;
	bufferID: GLuint;	
	vao: GLuint;
	
// run sdl window
if SDL_Init(SDL_INIT_VIDEO) < 0 then
	Fatal('SDL could not initialize! '+SDL_GetError);

// load function pointers
if Load_GL_VERSION_3_3 = false then
	Fatal('OpenGL is not loaded');

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
// NOTE: 4.0 doesn't work, GL_VERSION return null
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);	
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
		
// create window	
window := SDL_CreateWindow('SDL Tutorial', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN + SDL_WINDOW_OPENGL);
context := SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
SDL_GL_SetSwapInterval(1);

writeln('Vendor: ', glGetString(GL_VENDOR));
writeln('OpenGL Version: ', glGetString(GL_VERSION));
writeln('GLSL Version: ', glGetString(GL_SHADING_LANGUAGE_VERSION));

// NOTE: despite SDL_GL_SetAttribute asking for 3.3 and 4.0 failing GL_VERSION still returns 4.1
// Vendor: Intel Inc.
// OpenGL Version: 4.1 INTEL-10.25.13
// GLSL Version: 4.10 

// setup
glViewPort(0, 0, width, height);

// simplest example possible
// https://www.youtube.com/watch?v=Dyue3MzJDss
verts[0].x := 0.0;
verts[0].y := 1.0; 
verts[1].x := -1.0;
verts[1].y := -1.0; 
verts[2].x := 1.0;
verts[2].y := -1.0;

// NOTE: glEnableVertexAttribArray returns an error if I don't bind a vao
glGenVertexArrays(1, @vao);
glBindVertexArray(vao);

// gen array buffer and bind to verts
glGenBuffers(1, @bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), @verts, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(TVec2), nil);

// draw loop
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 3);
SDL_GL_SwapWindow(window);
...


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list