OpenGL 3+ and SDL
#1 Members - Reputation: 392
Posted 17 December 2012 - 08:24 PM
#2 Members - Reputation: 1808
Posted 17 December 2012 - 08:46 PM
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.
#3 Members - Reputation: 392
Posted 18 December 2012 - 09:31 AM
#4 Members - Reputation: 674
Posted 18 December 2012 - 02:40 PM
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.
#5 Members - Reputation: 392
Posted 18 December 2012 - 04:13 PM
#6 Members - Reputation: 1808
Posted 18 December 2012 - 11:24 PM
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.
#7 Members - Reputation: 392
Posted 19 December 2012 - 09:19 AM
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")// namespaceusing namespace glm; // must be before std namespaceusing namespace std;// function prototypes//global variablesSDL_Window *MainWin;SDL_GLContext MainContext;SDL_Event event; // the event structure that will be usedbool quit = false; // make sure the program waits for a quitGLuint VBO; // vertex buffer objectGLuint 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?
#8 Members - Reputation: 1406
Posted 19 December 2012 - 09:35 AM
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.
#9 Members - Reputation: 392
Posted 19 December 2012 - 10:07 AM
glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
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?
#10 Members - Reputation: 1406
Posted 20 December 2012 - 01:51 AM
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?
#13 Members - Reputation: 392
Posted 20 December 2012 - 01:18 PM
#14 Members - Reputation: 392
Posted 21 December 2012 - 11:35 AM
EDIT: the project which works has /MD and still works in debug without errors or crashes, unlike the one where i used sdl
Edited by proanim, 21 December 2012 - 11:37 AM.






