After finishing the basic classes for an ingame-GUI-system, I started looking at how I want to multi-thread my program. In general with older OpenGL specifications, it is not easily possible to parallelise the drawing process, but there are certain things, that can be done.
The OpenGL context is bound to a single thread, so the context is inaccessible for other threads, but it is possible to access data at the same time, if for example VBO-data is mapped, this data can be accessed from other threads (useful for optimising CPU-side skinning). Another possibility is to create another context and let the contexts share ressources.
Anyway in the end the best thing is to simply have a drawing thread and handle all the other stuff (like physics, networking etc.) in other threads, so the drawing thread is simply drawing the last state, while the other threads are calculating the next state of the game.
#Parallel OpenGL FAQ
#Intel: Designing the Framework of a Parallel Game Engine
Code-Highlight
Showing posts with label opengl. Show all posts
Showing posts with label opengl. Show all posts
Thursday, 29 March 2012
Saturday, 4 February 2012
#some tutorial on FBOs
The final render target of OpenGL is a framebuffer. External framebuffers can be used to render to textures. OpenGL provides an interface to create these framebuffers, also known as Frame Buffer Objects (FBO).
To illustrate how FBOs are used and what they can do, here is an approach to implement rendered textures:
You start by creating the texture and setting the parameters of the texture.
GLuint texture = 0; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, 0);
This is straight-forward and similar to the usual way, you set up a texture using OpenGL, but something people don't notice is, that the last line, which is unbinding the texture is really important!
To be able to attach a texture to an FBO it needs to be unbinded at that time, as well as when something is rendered into the FBO.
The next step is to setup a depthbuffer for depth-testing, may wonder at this point why setup the texture and depthbuffer before the FBO, there is no real reason, but what is important is that textures, buffers attached to a FBO have the same size as the FBO!
glGenRenderbuffersEXT(1, &depth); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, depth); glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, width, height); glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
Again it is important to unbind before attaching something to the FBO.
Now finally create the FBO:
glGenFramebuffersEXT(1, &fbo); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, texture, 0); glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER_EXT, depth);
After creating the FBO the only last thing left to do is, checking if everything worked fine and then unbind the framebuffer:GLenum err = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (GL_FRAMEBUFFER_COMPLETE != err) std::cout << glewGetErrorString(err) << endl; glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
The depth-buffer is optional for a rendered texture, but it is quite useful to know about it for other applications. Now everything is setup to render to the texture simply bind the fbo and then render what should be in the texture, but it is important, that the viewport is not bigger than the buffer, so if you want to render to the full size of the texture you would do it like this:
glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fbo); glPushAttrib(GL_VIEWPORT_BIT); glViewport(0,0,width,height); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // RENDER STUFF IN THE TEXTURE... glPopAttrib(); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
If you want to use the texture now you can do it the usual way, nothing special about that...
Some more references:
#more infos and speccs on FBOs
#interesting question on how to utilize FBOs
Labels:
fbo,
framebufferobject,
opengl,
tutorial
Monday, 23 January 2012
#some project
After the x-mas break and the coursework time I started a new small personal project again. My personal goal now is to build a simple framework I can work with for further coursework or small games.
I decided to use the SFML-library for cross-platform window creation and input handling et cetera and use glew for drawing. Both libraries integrate seemlessly and SFML even allows a lightweight window creation perfectly suitable for game purposes.
The makefile I wrote is currently just working on OSX, but other operating system will be added in the future. The cross-platform capabilities are available and will be used later on.
The major concern I have at the moment is that OSX Lion is still not supporting a newer version of OpenGL and I would love to work with 3.3+ (and the new GLSL version) but unfortunately I have to live with that.
I will update on my progress later and also publish my git repository. After finishing the basic framework I am planning to implement an skip-quadtree based event system to do some research on event systems suitable for big interactive game-worlds (e.g. MMOs).
I decided to use the SFML-library for cross-platform window creation and input handling et cetera and use glew for drawing. Both libraries integrate seemlessly and SFML even allows a lightweight window creation perfectly suitable for game purposes.
The makefile I wrote is currently just working on OSX, but other operating system will be added in the future. The cross-platform capabilities are available and will be used later on.
The major concern I have at the moment is that OSX Lion is still not supporting a newer version of OpenGL and I would love to work with 3.3+ (and the new GLSL version) but unfortunately I have to live with that.
I will update on my progress later and also publish my git repository. After finishing the basic framework I am planning to implement an skip-quadtree based event system to do some research on event systems suitable for big interactive game-worlds (e.g. MMOs).
Subscribe to:
Posts (Atom)