NeHe OpenGL Lesson32 – Picking, Alpha Blending, Alpha Testing, Sorting
2012-08-23 09:11
302 查看
This simple game shows us how to do something funny that already detailed as in the title.
1) Screen mouse picking: We could emit a 3D ray from the current viewer position and do intersection test for this ray and those pick-able objects. With this method, we could pick the objects that we want on the screen mouse cursor. There is one demo under the DirectX install directory does that, some triangles will be picked by a 3D ray. But here we do picking in a different manner, that support by OpenGL API it self. A selection mode that very similar to the render mode was created to support this feature. Set up the selection buffer first, switch to selection rendering mode, set up the pick-able matrix with gluPickMatrix, then draw the objects normally. The object draw process is almost the same as the normal draw process, the only difference is that you need to set a special label for your current objects with glLoadName() function. This label could be used to figure out which object is picked. The source code as following:
glGetIntegerv(GL_VIEWPORT, viewport); glSelectBuffer(512, buffer); // Tell OpenGL To Use Our Array For Selection glRenderMode(GL_SELECT); glInitNames(); glPushName(0); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); // This Creates A Matrix That Will Zoom Up To A Small Portion Of The Screen, Where The Mouse Is. gluPickMatrix((GLdouble) mouse_x, (GLdouble) (viewport[3]-mouse_y), 1.0f, 1.0f, viewport); // Apply The Perspective Matrix gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f); glMatrixMode(GL_MODELVIEW); foreach (render_object ) { glPushName(render_object_lable_id); render_object.draw(); } glMatrixMode(GL_PROJECTION); glPopMatrix(); glMatrixMode(GL_MODELVIEW); hits=glRenderMode(GL_RENDER); if (hits > 0) { int choose = buffer[3]; int depth = buffer[1]; for (int loop = 1; loop < hits; loop++) { // If This Object Is Closer To Us Than The One We Have Selected if (buffer[loop*4+1] < GLuint(depth)) { choose = buffer[loop*4+3]; depth = buffer[loop*4+1]; } } }
2) Alpha blending, Alpha testing: we usually create one texture with alpha channel. These two topic already detailed in the previous lessons, no need to repeat it again and again.
3) Sorting: this method used here is very similar to std::vector sort. But here, you do not need to create another array wrapper, but work with native element array block memory address. The source code just as following:
typedef int (*compfn)(const void*, const void*); int Compare(struct objects *elem1, struct objects *elem2) { if ( elem1->distance < elem2->distance) return -1; else if (elem1->distance > elem2->distance) return 1; else return 0; } ... qsort((void *) &object, level, sizeof(struct objects), (compfn)Compare );
The full source code could be found here.
相关文章推荐
- 软件工程&ndash;Alpha Release and team testing
- NeHe OpenGL Lesson24 – Tokens, Extensions, Scissor Testing And TGA Loading
- 软件工程–Alpha Release and team testing
- NeHe OpenGL Lesson31 – Model Loading
- 【Unity Shaders】Alpha Test和Alpha Blending
- NeHe OpenGL Lesson15 – Texture Mapped Outline Fonts
- NeHe OpenGL Lesson30 – Collision Detection
- NeHe OpenGL Lesson14 – Outline Fonts
- NeHe OpenGL Lesson29 – Blitter Function, RAW Texture Loading
- NeHe OpenGL Lesson13 – Bitmap Fonts
- 【Unity Shaders】Alpha Test和Alpha Blending
- NeHe OpenGL Lesson44 - 3D Lens Flare With Occlusion Testing
- NeHe OpenGL Lesson28 – Bezier Patches(贝叶茨曲面) / Fullscreen Fix
- NeHe OpenGL Lesson12 – Display Lists
- 【Unity Shaders】Alpha Test和Alpha Blending
- NeHe OpenGL Lesson27 – Volume Shadows(体积阴影)
- NeHe OpenGL Lesson11 – Flag Effect (Waving Texture)
- Google Play Alpha/beta testing.
- NeHe OpenGL Lesson26 – Clipping & Reflections Using The Stencil Buffer
- NeHe OpenGL Lesson 10 – Loading And Moving Through A 3D World