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.

转载于:https://www.cnblogs.com/open-coder/archive/2012/08/23/2651848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值