NeHe OpenGL Lesson 10 – Loading And Moving Through A 3D World
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了