• FEATURED

View more

View more

View more

$50 ### Image of the Day Submit IOTD | Top Screenshots ### The latest, straight to your Inbox. Subscribe to GameDev.net's newsletters to receive the latest updates and exclusive content. Sign up now ## OpenGL 3+ and SDL Old topic! Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic. 13 replies to this topic ### #1proanim Members Posted 17 December 2012 - 08:24 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. 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. ### #2Aldacron GDNet+ 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. ### #3proanim Members Posted 18 December 2012 - 09:31 AM 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? ### #4ic0de Members 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. Currently working on new DOS game, Chuck Jones: Space Cop of the Future, Check out my Dev Blog you know you program too much when you start ending sentences with semicolons; ### #5proanim Members Posted 18 December 2012 - 04:13 PM 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 . 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? ### #6Aldacron GDNet+ Posted 18 December 2012 - 11:24 PM 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. ### #7proanim Members Posted 19 December 2012 - 09:19 AM 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")// 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? ### #8BitMaster Members Posted 19 December 2012 - 09:35 AM 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. ### #9proanim Members Posted 19 December 2012 - 10:07 AM when debugging this line crashes 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?

### #10BitMaster  Members

Posted 20 December 2012 - 01:51 AM

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?

### #11proanim  Members

Posted 20 December 2012 - 07:55 AM

i did use /MD to eliminate 'conflicting libraries' problem. And i linked glm and glfw from tutorials with glew 1.8.0

### #12BitMaster  Members

Posted 20 December 2012 - 09:51 AM

Again, have you tried rebuilding it all? Have you changed the predefined Debug build of MSVC to use /MD? That will probably not be a good idea as the only change, use the predefined Release build instead.

### #13proanim  Members

Posted 20 December 2012 - 01:18 PM

With release build, /MD is default same libs and everything unchanged it compiles and runs without the crash and without 'vector out of bounds'. But shaders which are placed in the same directory as exe so it is correct file path still fails?

### #14proanim  Members

Posted 21 December 2012 - 11:35 AM

Ok i found my old project file in which i got the tutorials and shaders to work, turns out i setup the project exactly the same except i used glfw instead of sdl at the time. This is very irritating since i don't know why it only works with that project. Is there something in sdl or somewhere that can make these problems with shaders and all from above posts?

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.