NeHe OpenGL Lesson32 – Picking, Alpha Blending, Alpha Testing, Sorting

screen_shot17-300x225 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.

posted @   opencoder  阅读(372)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示