Well i've been trying to build an easy 'picking' sample out of NeHe's tutorial 33&7&12.
I never programmend that selection thing before so i don't have clue what went wrong there. choose doesn't return the correct name, the rest of it is ok.
int DrawGLText(GLvoid)
{
GetCursorPos(&ptCursor);
ScreenToClient(hWnd, &ptCursor);
mouseX = ptCursor.x;
mouseY = ptCursor.y;
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f,0.0f,-1.0f); // Move One Unit Into The Screen
glColor3f(1.0f,1.0f,1.0f);
glRasterPos2f(-0.54f, 0.39f);
glPrint("X = %i Y = %i L= %i F= %i S=%i H=%i", mouseX,mouseY,light,filter,Global_Selections,Global_Hits); // Print GL Text To The Screen
return TRUE;
}
int DrawGLMap(GLvoid)
{
glLoadIdentity(); // Reset The View
glTranslatef(0.0f,0.0f,-20.0f);
glTranslatef(-6.0f,0.0f,0.0);
glBindTexture(GL_TEXTURE_2D, texture[filter]);
glColor3f(1.0f,1.0f,1.0f);
// float tex_x,tex_y;
for (int jj=1;jj<6;jj++)
{
glTranslatef(2.0f,0.0f,0.0);
glBegin(GL_QUADS);
// Front Face
glLoadName(jj);
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f( 0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(0.25f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(0.25f, 0.25f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f( 0.0f, 0.25f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
}
return TRUE;
}
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
// The Size Of The Viewport. [0] Is , [1] Is , [2] Is , [3] Is
GLint viewport[4];
// This Sets The Array 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) mouseX, (GLdouble) (viewport[3]-mouseY), 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
DrawGLMap(); // 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
}
}
Global_Selections = choose;
}
Global_Hits = hits;
}
int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
// Light&Texture OFF
glDisable(GL_LIGHT1);
if (light==1){glDisable(GL_LIGHTING);}
glDisable(GL_TEXTURE_2D);
DrawGLText();
// Light&Texture ON
glEnable(GL_LIGHT1);
if (light==1){glEnable(GL_LIGHTING);}
glEnable(GL_TEXTURE_2D);
DrawGLMap();
Selection();
return TRUE; // Keep Going
}
The whole source can be found here.
Edited by - Jonus on December 10, 2001 8:53:43 AM
Help a GLNewbie
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement