Need help rendering with opengl


#1

I would like to start using opengl for rendering instead of SDL’s built in graphics library, but I cannot seem to get opengl to display anything to the window. I am compiling on windows 10 using the msvc compiler through msvc. My code is below, in it I call SDL to dynamically link with opengl, load a couple of procedures from opengl, and then call them in an attempt to fill the screen with color. Right now the program displays an empty black window. No error conditions are hit, the program runs as if it is working properly, and according to my debugger, the addresses of opengl procedures are being correctly loaded.

Is anyone able to see what I am doing wrong here?

My guess is that I am somehow calling into opengl incorrectly with bad arguments. However I haven’t been able to find any information or example code on how to do this correctly that uses only dynamic linking.

#include <stdio.h>
#include "SDL.h"
#undef main

typedef void (*glClearColor_t)(float, float, float, float);
static glClearColor_t glClearColor;
typedef void (*glClear_t)(int);
static glClear_t glClear;
typedef void (*glViewport_t)(int, int, int, int);
static glViewport_t glViewport;

void load_opengl_procs() {
	#define load_gl_proc(name) name = ((name ## _t)SDL_GL_GetProcAddress(#name))
	load_gl_proc(glClearColor);
	load_gl_proc(glClear);
	load_gl_proc(glViewport);
}

int main(int argc, char** argv) {
	if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		printf("Could not initialize SDL: %s.\n", SDL_GetError());
		return -1;
	}

	SDL_GL_LoadLibrary(NULL);

	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	auto window_options = SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN;//|SDL_WINDOW_RESIZABLE;
	SDL_Window* window = SDL_CreateWindow("life", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1800, 1000, window_options);
	if(!window) {
		printf("Could not create window: %s.\n", SDL_GetError());
		SDL_Quit();
		return -1;
	}
	SDL_GLContext glcontext = SDL_GL_CreateContext(window);
	if(!glcontext) {
		printf("Could not create gl context: %s.\n", SDL_GetError());
		SDL_Quit();
		return -1;
	}
	if(SDL_GL_MakeCurrent(window, glcontext) < 0) {
		printf("Could not initialize gl context: %s.\n", SDL_GetError());
		SDL_GL_DeleteContext(glcontext);
		SDL_Quit();
		return -1;
	}
	load_opengl_procs();

	bool is_game_running = 1;
	while(is_game_running) {
		SDL_Event event;
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT) {
				is_game_running = 0;
				break;
			}
		}
		glViewport(0, 0, 1800, 1000);
		glClearColor(.99, .2, .99, .5);
		glClear(0xFFFFFFFF);
		SDL_GL_SwapWindow(window);
	}
	SDL_GL_DeleteContext(glcontext);
	SDL_Quit();
	return 0;
}

I went with dynamic linking since this seems to be the most platform independent way to use opengl with sdl.
I had to use 0xFFFFFFFF as my argument to glClear because I did not have the appropriate bitmask constants defined. I don’t like doing this, but I’ve tried using different literals and it does not fix the problem.
In addition to getting help displaying something to the screen, I would also like to know how I might get the definitions of the various opengl constants I might need into my source code in a platform independent way.


#2

It looks good for the most part. However the manual for glClear says you can’t set unknown bits: https://www.khronos.org/registry/OpenGL-Refpages/gl2.1/xhtml/glClear.xml. I know you said you tried it a different way but are you sure you used GL_COLOR_BUFFER_BIT? You can safely include <GL/gl.h> or whatever it is on your platform (eg <OpenGL/gl.h> on Mac).