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 @ 2012-08-23 09:11  opencoder  阅读(365)  评论(0编辑  收藏  举报