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

本文详细介绍了3D游戏开发中关键的技术实现,包括使用OpenGL进行屏幕鼠标拾取对象,实现Alpha混合与测试以处理透明度,以及通过自定义比较函数对渲染对象进行排序的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值