OpenGL 3+ and SDL

Started by
12 comments, last by proanim 11 years, 3 months ago
Is there a way to use SDL, GLFW and GLEW? It seems that all three are conflicting in some way. I am looking to initialize opengl 3.3 at least with glfw, opengl extensions with glew and handle input and message pump with sdl. It also seems that if glfw doesn't create a window sdl window creation fails, and if glfw does create a window than sdl also creates its window, and there is no effect from any of the gl functions like 'glClearColor' whatsoever.
Advertisement

Is there a way to use SDL, GLFW and GLEW? It seems that all three are conflicting in some way. I am looking to initialize opengl 3.3 at least with glfw, opengl extensions with glew and handle input and message pump with sdl.


I don't understand why you would want to do this. GLFW handles input events just fine. You gain nothing by attempting to do it. I strongly recommend you either drop SDL and use GLFW exlusively, or get the latest SDL2 snapshot, compile it, and use it instead. SDL2 supports OpenGL 3+ context creation.
Ok so i decided to use SDL2 and i have created a window with opengl context, everything nice and easy but, i can't get glew to intialize when using sdl2. I need glew for shaders there is no shader support is SDL2 - no glCreateShader and such. Any help?

Ok so i decided to use SDL2 and i have created a window with opengl context, everything nice and easy but, i can't get glew to intialize when using sdl2. I need glew for shaders there is no shader support is SDL2 - no glCreateShader and such. Any help?


when are you calling glewInit(), I'm using SDL2 and I call it after initializing my window and OpenGL context and it works just fine.
Ok no it doesn't crash when i call glewInit(); after i have setup window and opengl context, but i still can't get shaders to work. This really pisses me off big time, whenevery I want to test something it fails with shaders. I wanted to test SDL2 with this http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/ tutorial. It seems that it works ok on its own but when i want to use it's shader parts 'shader.hpp' and 'shader.cpp' to make the shaders work it fails with 'vector out of bounds' error angry.png . If i comment out the part that reports errors it still doesn't work. I don't have any idea how to fix this, any help?
A "vector out of bounds" error has to do with a std::vector being accessed with an invalid index. In the code from the tutorial you linked, the only use I see of std::vector is in creating a buffer for error messages from the shaders (a rather odd thing to do IMO). So my guess is that your shader is failing, the error path is being executed and there is a mistake with the buffer. Did you copy shader.cpp by hand, drop it in your project, or copy/paste?

Whatever the case, all anyone can do is guess without seeing *your* offending code and the exact error messages. So in the future, when you have errors like this, please include more information so people can help you more easily.
I have copy/pasted shader.hpp and shader.cpp files from tutorials and vertex and fragment shader are in the same directory as the executable.

My code looks like this:

base.h
[source lang="cpp"]// main header file
#ifndef _BASE_H_
#define _BASE_H_

// preprocessor directives
#define WIN32_LEAN_AND_MEAN

// included headers
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>

// OpenGL headers
#include "GL/glew.h"
#include "GL/glm/glm.hpp"
#include "GL/glm/gtc/matrix_transform.hpp"
#include <GL/gl.h>

// SDL headers
#include "SDL/SDL.h"

// project specific headers
#include "shader.hpp"

// included libraries
#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "SDL.lib")
#pragma comment(lib, "SDLmain.lib")

// namespace
using namespace glm; // must be before std namespace
using namespace std;

// function prototypes

//global variables
SDL_Window *MainWin;
SDL_GLContext MainContext;

SDL_Event event; // the event structure that will be used
bool quit = false; // make sure the program waits for a quit

GLuint VBO; // vertex buffer object
GLuint IBO; // index buffer object

#endif[/source]
main.cpp

[source lang="cpp"]#include "base.h"

int main(int argc, char* argv[])
{
// init SDL subsystems
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) == -1)
return 1;

// setup OpenGL version
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);

//setup stencil buffer
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);

// setup depth buffer
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

// create main window
MainWin = SDL_CreateWindow("TestApp",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);

MainContext = SDL_GL_CreateContext(MainWin); // attach OpenGL context to window

// init GLEW
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Failed to initialize GLEW\n");
return -1;
}

SDL_GL_SetSwapInterval(0); // vsync

// background color
// dark blue background
glClearColor(0.0f, 0.0f, 0.3f, 0.0f);

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

// create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");

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,
};

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

// while the user hasn't quit
while(quit == false)
{
// clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// use shader
glUseProgram(programID);

// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // From index 0 to 3 -> 1 triangle

glDisableVertexAttribArray(0);

SDL_GL_SwapWindow(MainWin);

// while there's an event to handle
while(SDL_PollEvent(&event))
{
// if the user has Xed out the window
if(event.type == SDL_QUIT)
{
// quit the program
quit = true;
}
}
}

// Close and destroy the window
SDL_DestroyWindow(MainWin);
// quit SDL
SDL_Quit();

return 0;
}[/source]
any ideas?
My suggestion would be to use the debugger to step into the call to LoadShaders, and then follow the code flow line by line in the debugger.

That said, I highly suspect your working directory is simply not what you expect it to be. But the bug in the shader error handling must be fixed anyway, so work on that first.
when debugging this line crashes

glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); huh.png

now I just tried to compile the actual tutoral and I am fairly sure I could run it, but now there is the same crash no matter what. And since there is a warning about conflicting libraries - when I sitch to multi threaded dll instead (multi threaded dll debug option) in linker configuration i get failure on comile time with

shader.obj : error LNK2019: unresolved external symbol __imp___CrtDbgReportW referenced in function "public: char & __thiscall std::vector<char,class std::allocator<char> >::operator[](unsigned int)" (??A?$vector@DV?$allocator@D@std@@@std@@QAEAADI@Z)

how should you compile this thing so it works? blink.png
If you get errors about "conflicting libraries" (you failed to post an accurate error message again, by the way) I assume that means something on the lines of MSVCRT? If so, it's no wonder things blow up right and left.

Everything you link together statically must use the same runtime (/MT, /MTd, /MD or /MDd). There are other ways to cause problems here but with MSVC, the choice of the right runtime for all libraries is the most common issue.

If switching to /MDd as you tried does cause problems at link time, have you tried to "Rebuild All" the project in question? What exactly are linking to, with which runtimes was that compiled?

This topic is closed to new replies.

Advertisement