Hey everyone, I’m new to OpenGL but loving it so far. Unfortunately though I can’t seem to get picking to work properly. It loads up just fine and displays everything properly but when I go to select something it always has hits in it no matter where I am in the window and they are not valid hits. I have tried everything I can think of and in all sorts of ways. The latest attempt and the relevant code is below, thanks in advance for the help.
[b]
- (void)mouseMoved:(NSEvent *)theEvent
{
if(gZoom == NO && gPan == NO && gTrackball == NO && gNotes == NO)
{
int x, y;
NSPoint location = [self convertPoint:[theEvent locationInWindow] fromView:nil];
x = location.x;
y = location.y;
GLfloat fAspect; // Screen aspect ratio
// Space for selection buffer
GLuint selectBuff[512];
// Hit counter and viewport storeage
GLint hits, viewport[4];
GLdouble proj[16];
// Setup selection buffer
glSelectBuffer(512, selectBuff);
// Change render mode
glRenderMode(GL_SELECT);
// Initialize names
glInitNames();
// Switch to projection and save the matrix
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
// Obtain current projection matrix
glGetDoublev(GL_PROJECTION_MATRIX, proj);
// Get the viewport
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix(x, viewport[3] - y, 2,2, viewport);
// Apply perspective matrix
fAspect = (float)[self bounds].size.width / (float)[self bounds].size.height;
gluPerspective(35.0f, fAspect, 0.1f, 1000.0f);
glPopMatrix ();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// Clear the window with current clearing color
[self drawObjects];
glPopMatrix();
// Collect the hits
hits = glRenderMode(GL_RENDER);
// If a single hit occured, display the info.
if(hits == 1)
{
// does picking functions here
}
else
{
// does nothing
}
}
}
[/b]
[/b][/QUOTE]
The program draws the objects as follows:
[b]
-
(void)drawRect:(NSRect)bounds
{
// Draw the objects
GLint zeroOpacity = 0;
[[self openGLContext] setValues:&zeroOpacity forParameter:NSOpenGLCPSurfaceOpacity];
[[self window] setOpaque:NO];
[self resize];
[self renderObjects];
glFlush();
} -
(void)renderObjects
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glShadeModel(GL_SMOOTH);
glEnable(GL_NORMALIZE);// Save the matrix state
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();// Camera
glTranslatef(xTrans, yTrans, zTrans);
if(scribeViewInfo == self && gTrackBallRotation[0] != 0.0f) // User is rotating the camera with the mouse
glRotatef(gTrackBallRotation[0], -gTrackBallRotation[1], gTrackBallRotation[2], -gTrackBallRotation[3]);
else // User is not rotating camera so use the standard world rotation
glRotatef(worldRotation[0], -worldRotation[1], worldRotation[2], -worldRotation[3]);
gluQuadricNormals(gluNewQuadric(), GLU_SMOOTH);// Light values and coordinates
GLfloat whiteLight[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat sourceLight[] = {0.25f, 0.25f, 0.25f, 1.0f};
GLfloat diffuseLight[] = {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat lightPos[] = {0.0f, -20.0f, 40.0f, 0.0f};glEnable(GL_DEPTH_TEST); // Hidden surface removal
glFrontFace(GL_CCW); // Counter clock-wise polygons face out// Enable lighting
glEnable(GL_LIGHTING);// Setup and enable light
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, whiteLight);
glLightfv(GL_LIGHT0, GL_AMBIENT, sourceLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_LIGHT0);// Enable color tracking
glEnable(GL_COLOR_MATERIAL);// Set Material properties to follow glColor values
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);[self drawObjects];
} -
(void)drawObjects
{
glPushMatrix();
// Objects are all drawn with their own glPushMatrix and glPopMatrix
glPopMatrix();
[/b]
[/b][/QUOTE]