Archived

This topic is now archived and is closed to further replies.

Help a GLNewbie

This topic is 5848 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
I i found the problem - it was the namestack. That 'thing' seems to be a bit bugy i must do a glBegin(GL_LINES);glEnd(); after glLoadName(BLAH) to get my progy run smooth :rolleyes: d'oh!

Its done wrong in the NeHe sample 33 too btw.

Edited by - Jonus on December 10, 2001 8:55:46 AM

Share this post


Link to post
Share on other sites