A question about selection mode

A question about selection mode

Post by Revelino P. F. Mateu » Mon, 12 Oct 1998 04:00:00


First, sorry for the very long letter.

Urgent! Can someone tell me why the following code doesn't work right i try
to select something. To me it seems that the translations
are not performed well when in selection mode?

#include <windows.h>            // Window defines
#include <gl\gl.h>              // OpenGL
#include <gl\glu.h>             // GLU library
#include <gl\glaux.h>   // AUX library

#define BALL 1
#define CUBE 2

#define BUFFER_SIZE 64

GLfloat fAspect;

static HINSTANCE hInstance;

// Window procedure
       UINT    message,
       WPARAM  wParam,
       LPARAM  lParam);

void SetDCPixelFormat(HDC hDC);
void Resize(GLsizei w, GLsizei h);
void Render(void);
void Selection(int xPos, int yPos);

// Process the selection.
void Selection(int xPos, int yPos)
 GLuint uiSelectionBuffer[BUFFER_SIZE];
 GLint ihits, iViewport[4];

 glSelectBuffer(BUFFER_SIZE, uiSelectionBuffer);

 glGetIntegerv(GL_VIEWPORT, iViewport);



 gluPickMatrix(xPos, yPos, 2,2, iViewport);

 gluPerspective(50.0f, fAspect, 10.0, 300.0);

 // Render the scene

 ihits = glRenderMode(GL_RENDER);

 // If a single hit occured, display the info.
 if(ihits == 1)
  MessageBox(NULL,"You've got a hit!",NULL,MB_OK | MB_ICONEXCLAMATION);



void Resize(GLsizei w, GLsizei h)
 // Prevent a divide by zero
 if(h == 0)
  h = 1;

    glViewport(0, 0, w, h);

 fAspect = (GLfloat)w/(GLfloat)h;

 gluPerspective(50.0f, fAspect, 10.0, 300.0);

 // Reseting the Modelview matrix


// Called to draw scene
void Render(void)
 // Clear the window and the depth buffer


 // Viewing transformation
 glTranslatef(0.0f, 0.0f, -50.0f);

 // Initialize the names stack

 // Draw the ball
 glTranslatef(-20.0f, 0.0f, 0.0f);
 glColor3f(0.8f, 0.8f, 0.0f);

 // Draw the cube
 glTranslatef(15.0f, 10.0f, 0.0f);



// Select the pixel format for a given device context
void SetDCPixelFormat(HDC hDC)
 int nPixelFormat;

  sizeof(PIXELFORMATDESCRIPTOR),  // Size of this structure
  1,                                                              // Version
of this structure
  PFD_DRAW_TO_WINDOW |                    // Draw to Window (not to bitmap)
  PFD_SUPPORT_OPENGL |     // Support OpenGL calls in window
  PFD_DOUBLEBUFFER,                       // Double buffered
  PFD_TYPE_RGBA,                          // RGBA Color mode
  24,                                     // Want 24bit color
  0,0,0,0,0,0,                            // Not used to select mode
  0,0,                                    // Not used to select mode
  0,0,0,0,0,                              // Not used to select mode
  32,                                     // Size of depth buffer
  0,                                      // Not used to select mode
  0,                                      // Not used to select mode
  PFD_MAIN_PLANE,                         // Draw in main plane
  0,                                      // Not used to select mode
  0,0,0 };                                // Not used to select mode

 // Choose a pixel format that best matches that described in pfd
 nPixelFormat = ChoosePixelFormat(hDC, &pfd);

 // Set the pixel format for the device context
 SetPixelFormat(hDC, nPixelFormat, &pfd);


int WINAPI WinMain( HINSTANCE hInst,HINSTANCE hPrevInstance,
     LPSTR lpCmdLine,int nCmdShow)
 static char szAppName[]="Picking";
 MSG             msg;
 WNDCLASS        wc;
 HWND            hWnd;

 wc.style                = CS_HREDRAW | CS_VREDRAW;
 wc.lpfnWndProc          = WndProc;
 wc.cbClsExtra           = 0;
 wc.cbWndExtra           = 0;
 wc.hInstance            = hInstance;
 wc.hIcon                = NULL;
 wc.hCursor              = LoadCursor(NULL, IDC_ARROW);
 wc.hbrBackground        = NULL;
 wc.lpszMenuName         = NULL;
 wc.lpszClassName        = szAppName;

 if(RegisterClass(&wc) == 0)
  return FALSE;

 // Create the main application window
 hWnd = CreateWindow(
    "It doesn't work",
    50, 50,
    500, 500,


 while( GetMessage(&msg, NULL, 0, 0))
 return msg.wParam;


       WPARAM wParam, LPARAM  lParam)
 static HDC hDC;
 static HGLRC hRC;

 switch (message)
 case WM_CREATE:
  hDC = GetDC(hWnd);

  // Select the pixel format

  // Create the rendering context and make it current
  hRC = wglCreateContext(hDC);
  wglMakeCurrent(hDC, hRC);
  glClearColor(0.2f, 0.2f, 0.2f, 1.0f );

  return 0;

 // Window is resized.
 case WM_SIZE:
  // Call our function which modifies the clipping
  // volume and viewport
  Resize(LOWORD(lParam), HIWORD(lParam));
  return 0;

 // The painting function.  This message sent by Windows
 // whenever the screen needs updating.
 case WM_PAINT:
   // Call OpenGL drawing code


   // Validate the newly painted client area
  return 0;

   int xPos = LOWORD(lParam);
   int yPos = HIWORD(lParam);

   // Render in selection mode and display results
   Selection(xPos, yPos);
  return 0;

 // Window is being destroyed, cleanup
  // Deselect the current rendering context and delete it

  // Tell the application to terminate after the window
  // is gone.
  return 0;
 return DefWindowProc(hWnd, message, wParam, lParam);



1. Modeler Suggestion/Question--selection data transfer between modes


As for #1 with one or more polygons selected you can use select
connected "]" and all polygons that are connected to it will be selected
or select by surface name.

As for #2 select the polygons you want and hit hide unselected, after
that everything that is hidden is also hidden from the stats panels so
you can then just select all points. This will not work if you want to
select a bunch of points then select the polygons that are connected to
since points can exist without polygons but not vis versa and the
polygons will be hidden.

Frank D. Cocke
The Lightray Factory, 3D animation & Graphics


2. Macromedia Director

3. Newbie question : MODE X / MODE 13

4. Word 7 file

5. gl selection mode

6. R&D Company Seeks Advice from Graphic Artists

7. selection mode

8. How do you draw a transparent image using GDI only?

9. Backfacing polygon in selection mode

10. selection Mode in OpenGL

11. Problem with OpenGL selection mode under MS Win using MFC

12. Problem with Selections in 16bit color mode