Ugh. I can't seem to get anything to work correctly lately.
Iv'e attempted to add mouse object picking, aided by an example from the OGL superbible (found by searching these forums). However, no matter where I click, it reads that I clicked the highest name ID (ex, if I have 3 objects, it always says I clicked object 3).
on rendering:
Code:
void prefab::renderprefabs()
{
for(int i=0; i<num_prefabs; i++)
{
//if(!_ceFrustum.CubeInFrustum(prefabP[i].pos.x, prefabP[i].pos.y, prefabP[i].pos.z, 1.0f))
//{
//i++; }
if(prefabP[i].type==0)
{
glPushMatrix();
glLoadName(i+1);
glMultMatrixf(&prefabP[i].matrix[0][0]);
drawsolidcube(&prefabP[i],1,1);
glPopMatrix();
}
}
}
picking code:
Code:
void processclicks(GLuint id)
{
if(id==0)
printf("miss\n");
else if(id>0)
printf("hit %d\n", id);
}
#define BUFFER_LENGTH 64
void ProcessSelection(int xPos, int yPos)
{
// Space for selection buffer
GLuint selectBuff[BUFFER_LENGTH];
// Hit counter and viewport storeage
GLint hits, viewport[4];
// Setup selection buffer
glSelectBuffer(BUFFER_LENGTH, selectBuff);
// Get the viewport
glGetIntegerv(GL_VIEWPORT, viewport);
// Switch to projection and save the matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
// Change render mode
glRenderMode(GL_SELECT);
// Establish new clipping volume to be unit cube around
// mouse cursor point (xPos, yPos) and extending two pixels
// in the vertical and horzontal direction
glLoadIdentity();
gluPickMatrix(xPos, viewport[3] - yPos, 2,2, viewport);
// Apply perspective matrix
gluPerspective(50.0f, fAspect, 0.01, -1.0);
// Draw the scene
display();
// Collect the hits
hits = glRenderMode(GL_RENDER);
// If a single hit occured, display the info.
if(hits == 1)
processclicks(selectBuff[3]);
// Restore the projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// Go back to modelview for normal rendering
glMatrixMode(GL_MODELVIEW);
}
void mouse(int button, int state, int x, int y)
{
if(button == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
ProcessSelection(x, y);
}
and before drawing I have
Code:
glInitNames();
glPushName(0);
The only lead I have to this, is that i'm probably just not doing it right ;p.
thanx
-psychopath