Code-Highlight

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

Tuesday, 24 January 2012

#some research on quadtrees

I just did some research on QuadTrees, because I am thinking about using them for an efficient in-game event system, that would be suitable for big MMOs to be able to process the vast amount of events on the server-side.

The most interesting advanced QuadTree implementation I came across is a so called Skip-QuadTree, which has several advantages, but do the reading yourself if you are interested:

#Skip-QuadTree publication
#Presentation of the same group
#Another paper dealing with quadtree and more
#Presentation of quadtree utilized in distribution system

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).

Wednesday, 14 December 2011

#some review of skyrim and some research on water

I am wasn't really posting anything. Right now I am focusing on finishing the coursework for this semester, where some informations and git repositories are coming up.

Anyway in my spare time I started playing Skyrim.

Skyrim is brilliant!

Gameplaywise it's still the same, but if the gameplay is fine why do big changes. For me as a hopefully soon to be graphics programmer some of the graphics are pretty impressive.
First of the general graphics are good. As far as I know they were sticking to DX9, which is quite interesting. They did an awesome job on the terrain engine, which has great viewing range and great look. 

The nicest details although in my opinion are the water effects:
1. Water dropping from the ceiling
2. Flowing rivers (although not as nice as the FlowMaps presented by Valve at SIGGRAPH)
3. Waterfalls, White-Water-Effects
All this supports the natural look of skyrim and underlines the great work they did on the detailed world.

So there is loads of material to get an impression of Skyrim effects on the internet, but to save you some time, here are some links on flow maps. 

Monday, 14 November 2011

#some research on pathfinding

The first keyword that comes to mind thinking about pathfinding, is the A* algorithm. The A* is a fast node based approach to compute the shortest way from start to finish over a bunch of nodes, because it's a geometrical simplified approach it's fast as well. 

Also it's scalable:
More nodes, better results, weak performance...
Less nodes, bad results, fast performance... 

In this case bad results mean for example unnecessary edges in the path. Also in general the computed way looks quite unnatural, so to use smooth algorithms is quite common or algorithm to generate curves instead of sharp edges.

To get a better insight I found following useful references:

#some vision on the internet

After the announcements of Adobe it seems everything will change. And let's just hope it will be a good change, but what will be the replacement. If I think about the future of the internet in general, I think about a cloud-based world, where Application and the User are within this system. 


The future me could go on a website, but also start an app or open an ebook. So the adresses will be way more flexible in the future.
I open up an adress like "site://amazon.co.uk" and look for a book. So after reading some good reviews, I want to have a look in an example chapter, so I click on a link and the address "book://amazon.co.uk/somebook" opens up. My modern browser directly opens it with my favorite reader implementation.

This is not the only thing I could do, this would be a really simple example, because the apps will be capable of way more. For example I purchased photoshop from adobe on their website, so a licence for this app gets added to my "Online-ID". Before I had this licence, when I open up the address "app://photoshop.en", I could only access a demo or trial or had a verify full licence button, but now with the licence the proper photoshop opens like a normal program in the past. The webapp is cached after the first run, so I don't have to bother about loading times in the future. If there is an update I would just notice a minimum increment of the loading time (applying some updates to the cache).
For easy access I create an icon for the photoshop app in my dashbar at the bottom of the screen.

The big advantage for the industry would be the improved distribution and also a platform independent system. For the capitalist it would be a way to get a safe licensing system.

A way to realise that in the future would be something like google's Native Client or HTML5, but there will be more changes. The modern browser may merge web-applications, local applications, local filesystem and also websites -  everything available through one GUI. so an webapp will be in a window -  Instead of a bar at the bottom in windows, we have something similar, some tabs at the top and an adress.

A way to make this possible, would be to use mono/dotnetfx to compile to CLR. So the browser requests an application using a new protocol and gets the CLR to compile it locally for the specific architecture. Simple as that it would be possible to change how the hole internet would feel.

Unfortunately humans don't like change, so it will probably a while 'til big changes come...

#some hint on how to start optimising

Just decided to start a blog... to somehow keep track about what I am doing.

For game developers the final program must be optimised to every bit - to achieve that, the developers need a lot of in-depth knowledge of the architecture, they are developing for.
I found it quite hard to find a good piece of information about how to achieve this level of optimisation,
but I found a really good ressource in the end.

There is a swedish professor, who published really good tutorials about optimising. He discribes the main architectures in detail and the several operation systems in the first part. This is also the most important part, because modern compilers are good at optimising, so there is no general need to optimise it yourself on an assembly level.
For some people like myself, it's not enough to not know if I could still squeeze out another 5% performance, so I am glad that this swedish professor felt the same.

So for everyone who wants some in-depth knowledge about these topics, here you go.