This sample shows us a small proto-type how to write a game with OpenGL. This is the first lesson that abandon using hard code game world. It seems like a engine, a small one. What is an engine? An engine just define a group of data structures and data manager.
In this lesson, the game world saved as a text file, also human readable. The program will load and parse this text format game world. Parsing string and text, this is a bit time wasting. A better method is to binarize those data as disc file. At the game running time, load those data with a chunk of memory, then you could use it directly without eat one char by one char to get what are reading. Well, this will get into the engine asset pipeline and game resource management, data compress, data de-compress and so on. That is a big topic beyond this lesson.
OpenGL Texture Object
Texture object: In OpenGL, we could set the texture filter mode, address mode for each texture object. Just as the following code, those settings go along with OpenGL texture objects.
// Create Nearest Filtered Texture glBindTexture(GL_TEXTURE_2D, texture[0]); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
When you want to use this texture object, what you need to do is like this:
glBindTexture(GL_TEXTURE_2D, texture[filter]);
This is very different from the D3D texture filter mode set up. In D3D, you need to call SetSamplerState() to archieve to do that. What you should do is set the current d3d texture to this sample slot, then you set the filter mode, address for the current sample slot.
OpenGL Texture Sample Slots
Here comes another questions, how about texture sample slots in OpenGL? By default, we use the texture sample slot 0 for using, if we want to use multiple texure slots at the same time, you should write some code like this:
glActiveTexture(GL_TEXTURE0); // bind the first texture handle to this slot glBindTexture(GL_TEXTURE_2D, textureAId); // activate the second texture slot glActiveTexture(GL_TEXTURE0 + textureNumber); // bind the first texture handle to this slot glBindTexture(GL_TEXTURE_2D, textureBId);
You active the current texture sample slot, then set it. When you do not need this texture slot, you could use glBindTexture(.., NULL) to unbind the current texture unit.
Maximum Texture Sample Number
There is a maximum number of texture units that can be simultaneously supported on graphics hardware. Usually this is either 8 and 16 textures that can be used in one shader. We can find out exactly how many with in OpenGL:
int maxTextures = glGet(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS);
For more details about how to implement this demo, you could refer to NeHe OpenGL lesson 10.
The full source code could be found here.