glGenFramebuffers makes program fail to run - opengl

I have reinstalled devcpp 4.9.9.2 on windows xp virtualbox and installed glut and glew.
My original program just used glut, and shows some spheres bouncing around a room. My problem is that once I add in the line
glGenFramebuffers(1, &myBuffer);
my program fails to run. It compiles just fine. But when I run it says "Ass1.exe has encountered a problem and needs to close. We are sorry for the inconvenience.".
If I comment out this line then it works just fine, with balls bouncing around. The glGenFramebuffers is at the bottom of my setup method.
Here is a link to my code. https://dl.dropboxusercontent.com/u/13330596/Exercise1.cpp
This is the code just before I now call glewInit();
// Initialize GLUT.
glutInit(&argc, argv);
// Set display mode with an RGB colour buffer, double buffering and a depth buffer..
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
// Set OpenGL window size
glutInitWindowSize(1000, 1000);
// Set position of OpenGL window upper-left corner
glutInitWindowPosition(100, 100);
// Create OpenGL window with title
glutCreateWindow("Dissertation");
glewInit();

You must call glewInit(); before you can use extended functionality. Probably you didn't so the functions pointers are still null pointer. glewInit must be called after a context has been created and bound. In the case of using GLUT this is right after glutCreateWindow(…);

Related

OpenGL depth test doesn't work on some computers

My first question here. In my program depth testing works properly on some computers, but it doesn't work on others, objects that are located farther away cover those which are located closer. I called glEnable(GL_DEPTH_TEST); and tried to call glDepthFunc(GL_LESS); and as I said, everything works properly on some computers, but the same program doesn't work properly on other computers. How can it be fixed?
Edit: Problem solved. Added these lines before calling
al_create_display(); and everything works
al_set_new_display_option( ALLEGRO_COLOR_SIZE, 32, ALLEGRO_REQUIRE);
al_set_new_display_option( ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE);
al_set_new_display_option( ALLEGRO_STENCIL_SIZE, 8, ALLEGRO_REQUIRE);
al_set_new_display_option( ALLEGRO_AUX_BUFFERS, 0, ALLEGRO_REQUIRE);
al_set_new_display_option( ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST);
In addition to activating the Depth Test (glEnable(GL_DEPTH_TEST)), it is important that the current framebuffer has a depth buffer.
The default framebuffer is created at the time the OpenGL Context is constructed. The creation of the OpenGL context depends on the OS and windowing library (e.g. GLFW, SDL, SFML). Whether a depth buffer is created by default often depends on the system. In general, window libraries provide additional options for explicitly specifying a depth buffer when generating the OpenGL window:
For instance:
GLFW - Framebuffer related hints
glfwWindowHint(GLFW_DEPTH_BITS, 24);
// [...]
GLFWwindow *wnd = glfwCreateWindow(800, 600, "OpenGL window", nullptr, nullptr);
SDL - Using OpenGL With SDL
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
// [...]
SDL_SetVideoMode(800, 600, bpp, flags);
SFML - Using OpenGL in a SFML window
sf::ContextSettings settings;
settings.depthBits = 24;
// [...]
sf::Window window(sf::VideoMode(800, 600), "OpenGL window", sf::Style::Default, settings);
GLUT - glutInitDisplayMode
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
glutInitWindowSize(800, 600);

Freeglut error: ERROR: No display callback registered for window 1 when destroyed a window and created a new window

I want to create the opengGL context using freeglut. I will first decide the which context to by checking the supporting version using glew and some other parameters. I know for glew to work, it needs a opengl context. So I first create a context using glutCreateWindow, then check the supported version and then set the version required using the glutInitContextVersion() and destroy the previous window using glutDestroyWindow and recreate the new window by using glutCreateWindow. I get this error Freeglut error: ERROR: No display callback registered for window 1 (I checked 1 is ID for my previous window which I destroyed) . Following is my code
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(900, 600);
int winID = glutCreateWindow("Rotating Cube"); //winID is 1 here
glewExperimental = GL_TRUE;
glewInit();
//I decide the context on some other parameters also except the supported version reported by glew.
//I have tested this with opengl 3.2 core profile as well.
//This is not working even if I forcefully set the opengl version to 2.0
if (glewIsSupported("GL_VERSION_3_1"))
{
glutInitContextVersion (3, 1);
glutInitContextFlags (GLUT_FORWARD_COMPATIBLE);
//glutInitContextVersion (3, 2);
//glutInitContextFlags (GLUT_CORE_PROFILE);
}
else if (glewIsSupported("GL_VERSION_2_0"))
{
glutInitContextVersion (2, 0);
}
glutDestroyWindow(winID);
winID = glutCreateWindow("Rotating Cube"); //winID is 2 here
glutSetWindow(winID);
glutDisplayFunc(RenderScene);
glutIdleFunc(RenderScene);
glutReshapeFunc(ChangeSize);
glutKeyboardFunc(ProcessNormalKeys);
glutSpecialFunc(ProcessSpecialKeys);
glutMainLoop();
I think I need to do this as a openGL context is always required for glew to work. I already tried setting the display function on first window as well (though I know I am going to destroy it) but that also didn't work. I am setting the current window to new window and then calling the glutMainLoop. So I think this should work
According to answer by rhashimoto, I tried to put the destroy command at different positions
if (g_winID>=0)
{
glutDestroyWindow(g_winID);
g_winID = -1;
}
I put the destroy command at begining of reshape callback
ERROR: No display callback registered for window 1
The I put the destroy command at beginning of display function
ERROR: Function <glutSwapBuffers> called with no current window defined.
If I put this at end of Display callback, it does not give error but the displayed scene is not correct. Somethings are missing from the scene
So is there some specific callback function I need to put this display command? I dont think there is any destroy callback I can set. yes there is glutCloseFunc but I think that is meant to be called when window is being destroyed that is when glutDestroyWindow has been called on window
I think you can argue that this is a FreeGLUT bug, either in the implementation or the documentation. It looks like glutDestroyWindow() needs to be called from a GLUT callback to work properly.
glutDestroyWindow() mainly puts the window on a list to be destroyed, as well as clearing all callbacks (except a destroy callback). This is probably why setting the display function didn't work for you - it was removed when you called glutDestroyWindow().
Windows are actually destroyed at the end of each main loop. So on the first time through the loop, your window still exists. The fact that it has no display callback makes GLUT unhappy.
The best workaround is probably to arrange to call glutDestroyWindow() only via one of the GLUT callbacks. I don't know if this will make the window briefly flash on the screen. My guess is it won't, but it might depend on the platform.

activate quad buffered stereo with sfml or openGL

My program works perfectly fine in a normal 3D with one buffer, it is coded with SFML window management.
I would like to add quad buffered stereo, therefore i changed my drawing code to the following :
glDrawBuffer(GL_BACK_LEFT);
camera->OnMouseMotion(sf::Vector2i(-1,0));
for (auto i = objects->cbegin(); i != objects->cend(); ++i)
(*i)->draw(camera);
glFlush();
glDrawBuffer(GL_BACK_RIGHT);
camera->OnMouseMotion(sf::Vector2i(2,0));
for (auto i = objects->cbegin(); i != objects->cend(); ++i)
(*i)->draw(camera);
glFlush();
camera->OnMouseMotion(sf::Vector2i(-1,0));
Notice that my camera changed are not perfectly right and i know i will have to change these, right now i am focusing on displaying an image just using quad buffered stereo. I noticed in all examples of programs using this stereo that they were initialising the window with something like this :
type = GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STEREO;
glutInitDisplayMode(type);
Using SFML, such function isn't available, my questions are :
Can i use a low-level openGL function to achieve the same result ? Can i use another window managing library with SFML ? Should i forget SFML for my program and completly change it to another one ?
SFML doesn't have an initialisation function, it creates openGL context automatically, two things you can do :
Modify SFML sources you are using, and try to add somewhere in the window creation function your parameter
change your display library to create the openGL context, however you may or not keep SFML 2D drawing functions, i am not sure these are gonna work if you create your context with glut for example.
EDIT : after a small check, you cannot create a context with another library and still use SFML for drawing simple 2D forms, i am afraid you are forced to let SFML go.

Fullscreening a window in SDL2 with openGL

My program starts with a loading window while it is compiling shaders, loading textures etc. I then want to be able to launch a fullscreen application and use these resources. My understanding is that the openGL context must be the same before and after. I tried two methods for this: first of all I tried making a second window which was fullscreen, and used the SDL_GL_makecurrent command on this window to 'transfer' the context across (couldn't find where I read about this method), and secondly tried just fullscreening the loading window. Both of these methods resulted in the loading screen being moved to the top left corner of the screen. However opengl commands no longer ran properly in fullscreen, including clearing buffers which meant that the window contained the contents of my desktop/background applications.
Is there a proper way of doing this? Or is this a strange bug in sdl/opengl drivers?
Code to fullscreen original window:
//opengl commands work fine up to here
//now to fullscreen
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL_SetWindowSize(window, 1366, 768); //tried this on either side of line above and without either line
glViewport(0, 0, 1366, 768); //update viewport
glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);
//window should be whited, other draw commands tried and all fail or distort
SDL_GL_SwapWindow(window);
Creating a new window and using previous context:
//Fine up to here
window2 = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1366, 768, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_SHOWN);
SDL_GL_MakeCurrent(window2, glContext); //created with SDL_GL_CreateContext(oldwindow);
//draw commands dont work
PS: running ubuntu
Update: In the second code, reusing the context in a new window, it returns an error saying 'invalid window' when it fails, which is most of the time but not always. When it fails, the screen ends up completely corrupted(black with weird white squares and patterns), ending the program will not clear the screen of this (although screenshots are perfectly fine?), but it can be restored by ctrl+f1 to terminal then ctrl+f7 back
I dont really know if its a bug. I experienced the same issue with sdl2 and opengl.
Create an regular window
attach to opengl context.
fullscreen
BOOM. black screen and crashed window.
I only noticed that issue in ubuntu.
Trought some tests i found a quick way to fix it:
Uint32 flags = 0;
flags |= SDL_WINDOW_RESIZABLE;
//bla bla bla your tags
flags |= SDL_WINDOW_OPENGL;
m_window = SDL_CreateWindow( "hello gl", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, m_screen.x, m_screen.y,flags);
m_glContext = SDL_GL_CreateContext(m_window);
//Set right the way the screen to fullscrene false
SDL_SetWindowFullscreen(m_window, SDL_FALSE);
Now the fullscreen seems to work without problem.

closing multiple GLUT windows independently

In my GLUT program i have created two windows. when i try to close one window entire program shuts down. can anyone tell how to avoid it.
My code to create window is as follows
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(ww,wh);
window1 = glutCreateWindow("sample");
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
glutDisplayFunc(renderScene);
window3 = glutCreateWindow("sample2");
glutReshapeFunc(changeSize);
glutDisplayFunc(renderScene2);
glutIdleFunc(renderScene2);
glutMouseFunc(mouse);
glutPostRedisplay();
glutMainLoop();
return 1;
}
I don't recall GLUT being able to close windows independently in the same thread, like that. As you only use a single glutMainLoop() call to get them going, thereby when the main loop is killed for one window, it's killed for all of them.
You could try creating the windows in their own Thread. That might work, but I'm not entirely sure.
Something else you could try, is to use GLFW instead, using GLFW, you need to create the main loop etc, yourself. Bottom line, that's much easier to use and it gives you a lot more control, over your OpenGL programs. Also if you don't already have it, you should get something like GLEW.
You've just left the capabilities of GLUT with this demand. But you're lucky: GLUT =/= OpenGL and there are many other frameworks that will satisfy your needs. How about you take a look at Qt, which offers you not only a runtime environment and a OpenGL widget, but also a large set of widgets to draw UI elements with.