OpenGL Selection Using Unique Color IDs
来源:互联网 发布:国内域名需要备案么 编辑:程序博客网 时间:2024/05/18 12:43
Introduction
There are a number of ways object selection can be performed. Using OpenGL you can use the special Selection Buffer, which allows you to select objects in the scene having unique ID's preassigned to them. A tutorial demonstrating this method can be found here: OpenGL:Tutorials:Picking
But there is another method. This method can not only be applied to OpenGL apps, but to DirectX apps as well. For the sake of this tutorial, the code given below will be using OpenGL.
How It Works
Essentially the way this method works, is that each object in our world is assigned a unique color. Since we're using a 24bit color scheme, this means we can have A LOT of objects with unique colors. If we wish to find out which object the user has clicked, we simply do the following:
- Render the scene with textures, lighting, fog turned off
- Render each object using its unique color
- Read back data from the color buffer at the current mouse position
- Search through the list of objects looking for a color ID match
- If we find one, we have a selection
This is extremely simple to implement and is not API dependent.
So the first thing we must do is declare a base class for every object in our world. This base class must take care of initializing every object's colorID. It does this by declaring a static variable in the class, gColorID[3], and setting it to the color black, (0, 0, 0). From there, each time an object is created, gColorID is incremented. The first component, or red color channel, is incremented first, and when it reaches a value of 255, it is reset back to 0 and the second component, or green channel, is incremented. The same goes for the third, or blue channel.
Now every class we declare in our game or app should be derived from this base class. So suppose we wish to declare a Scene Object class. This class should be derived from the Base Object class and, depending on our implementation, you can simply insert a function to render the object only using its colorID. Hence in the example code below the only function of the function Picking() is to render the scene object with a solid color, specifically the object's colorID color.
Now as stated before we need to turn off texturing, lighting and fog before doing any of this. We also probably only want to perform object selection when a mouse button is pressed. So in the event of a mouse button being pressed, we render every object in the scene to the color buffer, read back the color information and search through our object list.
And that's it! Its extremely fast, depending on how many objects you have in the scene. It is API independent and is fairly straightward to implement. If for some crazy reason you feel that only using 24bits is not enough for how many objects you have in your world, then simply add an alpha channel to each object's colorID. Then when calling glReadPixels, you would change GL_RGB to GL_RGBA.
Also, some of you may be wondering why for the y - coordinate of glReadPixels, we passed in
This is because of the way OpenGL sets up the viewport, specially the lower left is the origin. Since in Windows the upper left is the origin, we must subtract the y mouse coordinate from the height of the viewport to get the correct OpenGL y coordinate.
To see this technique in action you can run the terrain editing software, Freeworld3D. This software uses this technique, not only for object selection, but also for Axis selection when translating, rotating and scaling objects. This is a very powerful and flexible way of handling object selection.
- OpenGL Selection Using Unique Color IDs
- 用唯一的颜色id编号实现OpenGL选择功能(OpenGL Selection Using Unique Color IDs )
- Runtime ROI Selection using Mouse
- Multiple selection using DropDownList in DataGrid
- Terrain Selection using a Mesh Decal
- Learning OpenGL : (5) Using OpenGL
- 颜色选择 Color Selection@GTK+ 2.0 中文教程连载
- Using OpenGL with MFC
- Particle Engine Using OpenGL
- Shadow Mapping Using OpenGL
- Creating Anaglyphs using OpenGL
- Using OpenGL with MFC
- developing OpenGL using makefile
- Using Profile:Java Color Scheme to set Forms Color
- Using color in Android, by XML
- Selection
- Selection
- :: selection
- IEEE802.3ah协议学习(3)——MPCP多点控制协议
- Installing PHP 5 on IIS in 5 simple steps
- 关于如何将java.util.Date转化为java.sql.Date问题
- 十二年,弹指一挥间
- 谷歌博客蔽屏了
- OpenGL Selection Using Unique Color IDs
- Windows系统编程学习笔记-堆管理
- 获取鼠标位置的三维坐标
- OpenCV学习——改变图片大小
- OCCI在VC下的编译问题
- net use命令详解
- 解决WinForm中ComboBox没有value值的bug
- NET USE 命令详解 稍细
- avi wav bmp2avi 杂乱 (只是在这里放一下)