View Full Version : Where is mystake ?

11-24-2002, 07:06 AM
I used following function from nehe.gamedev.net , but it doesn't work, i drawed to scene one white quad, and it returned no hits, actually for this code it just closed form. Can you please tell my where could a mystake ?

void Selection(void) // This is where selection is done
GLuint buffer[512]; // Set up a selection buffer
GLint hits; // The number of objects that we selected

if (game) // Is game over?
return; // If so, don't bother checking for hits

PlaySound("data/shot.wav",NULL,SND_ASYNC); // Play gun shot sound

// The size of the viewport. [0] is <x>, [1] is <y>, [2] is <length>, [3] is <width>
GLint viewport[4];

// This sets the array <viewport> to the size and location of the screen relative to the window
glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, buffer); // Tell OpenGL to use our array for selection

// Puts OpenGL in selection mode. Nothing will be drawn. Object ID's and extents are stored in the buffer.
(void) glRenderMode(GL_SELECT);

glInitNames(); // Initializes the name stack
glPushName(0); // Push 0 (At least one entry) onto the stack

glMatrixMode(GL_PROJECTION); // Selects the projection matrix
glPushMatrix(); // Push the projection matrix
glLoadIdentity(); // Resets the matrix

// 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); // Select the modelview matrix
DrawTargets(); // Render the targets to the selection buffer
glMatrixMode(GL_PROJECTION); // Select the projection matrix
glPopMatrix(); // Pop the projection matrix
glMatrixMode(GL_MODELVIEW); // Select the modelview matrix
hits=glRenderMode(GL_RENDER); // Switch to render mode, find out how many
// Objects were drawn where the mouse was
if (hits > 0) // If there were more than 0 hits
int choose = buffer[3]; // Make our selection the first object
int depth = buffer[1]; // Store how far away it is

for (int loop = 1; loop < hits; loop++) // Loop through all the detected hits
// 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]; // Select the closer object
depth = buffer[loop*4+1]; // Store how far away it is

if (!object[choose].hit) // If the object hasn't already been hit
object[choose].hit=TRUE; // Mark the object as bening hit
score+=1; // Increase score
kills+=1; // Increase level kills
if (kills>level*5) // New level yet?
miss=0; // Misses reset back to zero
kills=0; // Reset level kills
level+=1; // Increase level
if (level>30) // Higher than 30?
level=30; // Set level to 30 (Are you a god?)