A question about selection mode

A question about selection mode

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



Hi,

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
LRESULT CALLBACK WndProc(   HWND    hWnd,
       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);

 glMatrixMode(GL_PROJECTION);
 glPushMatrix();

 glRenderMode(GL_SELECT);

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

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

 // Render the scene
 Render();

 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);

 glMatrixMode(GL_PROJECTION);
 glPopMatrix();
 glMatrixMode(GL_MODELVIEW);

}

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;
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

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

 // Reseting the Modelview matrix
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

}

// Called to draw scene
void Render(void)
{
 // Clear the window and the depth buffer
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

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

 // Initialize the names stack
 glInitNames();
 glPushName(0);

 // Draw the ball
 glPushMatrix();
 glRotatef(10.0f,1.0f,0.0f,0.0f);
 glRotatef(35.0f,0.0f,0.0f,1.0f);
 glTranslatef(-20.0f, 0.0f, 0.0f);
 glColor3f(0.8f, 0.8f, 0.0f);
 glLoadName(BALL);
 auxSolidSphere(10.0f);
 glPopMatrix();

 // Draw the cube
 glTranslatef(15.0f, 10.0f, 0.0f);
 glColor3f(0.0f,1.0f,1.0f);
 glLoadName(CUBE);
 auxSolidCube(10.0f);

 glFlush();

}

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

 static PIXELFORMATDESCRIPTOR pfd = {
  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(
    szAppName,
    "It doesn't work",
    WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
    50, 50,
    500, 500,
    NULL,
    NULL,
    hInstance,
    NULL);

 ShowWindow(hWnd,SW_SHOW);
 UpdateWindow(hWnd);

 while( GetMessage(&msg, NULL, 0, 0))
 {
  TranslateMessage(&msg);
  DispatchMessage(&msg);
 }
 return msg.wParam;

}

LRESULT CALLBACK WndProc( HWND hWnd, UINT message,
       WPARAM wParam, LPARAM  lParam)
{
 static HDC hDC;
 static HGLRC hRC;

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

  // Select the pixel format
  SetDCPixelFormat(hDC);

  // 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
   Render();

   SwapBuffers(hDC);

   // Validate the newly painted client area
   ValidateRect(hWnd,NULL);
  }
  return 0;

 case WM_LBUTTONDOWN:
  {
   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
 case WM_DESTROY:
  // Deselect the current rendering context and delete it
  wglMakeCurrent(hDC,NULL);
  wglDeleteContext(hRC);

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

}

 
 
 

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

(snip)

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
them
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

HTTP://www.netdoor.com/com/lightray

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