win32 window in vtk

        In the option of creating a new win32 project with VS ide, one is called "Win32 console application" and the other is called "Win32 application". The details of the difference are skipped here. The most significant thing is that the win32 console has no GUI window. , only a black box for the command line pops up; while the win32 application has a GUI window, but no black box.

        but! For example, in the following demo, there are all command line black windows and GUI display windows in vtk example. All are win32 console projects. Where does the GUI display window come from?


int main(int , char *[])
{
    //↓↓↓↓↓↓↓↓↓↓
    double p0[3] = {0.0, 0.0, 0.0};
    double p1[3] = {1.0, 0.0, 0.0};
    double p2[3] = {1.0, 1.0, 0.0};
    double p3[3] = {0.0, 1.0, 0.0};

    // Add the points to a vtkPoints object
    vtkPoints *points =  vtkPoints::New();
    ... ... ... ...
    //↑↑↑↑↑The above are all data structures, and there is no OPENGL related drawing content.↑↑↑↑

    // Setup actor and mapper
    // The reflection mechanism is automatically reflected to vtkOpenGLPolyDataMapper under win32, here is the traditional OPENGL code
    vtkPolyDataMapper*mapper =     vtkPolyDataMapper::New();
    mapper->SetInput(polydata);

    vtkActor*actor =       vtkActor::New();
    actor->SetMapper(mapper);

    // Setup render window, renderer, and interactor
    vtkRenderer*renderer =
        vtkRenderer::New();
    vtkRenderWindow*renderWindow =
        vtkRenderWindow::New();
    renderWindow->SetWindowName("Quad");
    renderWindow->AddRenderer(renderer);
    vtkRenderWindowInteractor*renderWindowInteractor =
        vtkRenderWindowInteractor::New();
    renderWindowInteractor->SetRenderWindow(renderWindow);

    renderer->AddActor(actor);
    renderWindow->Render();
    renderWindowInteractor->Start();

    return EXIT_SUCCESS;
}

        At first, I always took it for granted, because the bottom layer of VTK is OpenGL, of course, it is the OpenGL window. It looks like an OpenGL window. All OpenGL tutorials on the Internet look like this. As I slowly learned about OpenGL, I found that I was once again deceived by appearances. The truth was not what I saw, it was just a long image. . . Any program that uses OpenGL display will be bound to another framework containing UI, similar to GLUT, Qt, MFC, FLTK, OpenGL itself cannot be displayed! ! ! A UI must be bound! ! ! A UI must be bound! ! ! A UI must be bound! ! ! NND, I'm stupid, I have been fooled for so many years, I always think that OpenGL can draw windows by itself.

        OpenGL is only responsible for drawing the scene, and is not responsible for the window, the window is another frame. To see an OpenGL scene on the desktop, it must be bound to a framework that draws UI!

        Let's see why the vtk example can pop up a window + command line under a Win32 console application:

     Find the class vtkWin32OpenGLRenderWindow, and you can know the function of this class according to the name: vtk + Win32 + OpenGL + RenderWindow, which can be understood as vtk binds OpenGL to RenderWindow under win32!

// Initialize the window for rendering.
void vtkWin32OpenGLRenderWindow::WindowInitialize (void)
{
  int x, y, width, height;
  GLenum type;
  static int count = 1;
  char * windowName;
  
  x = ((this->Position[0] >= 0) ? this->Position[0] : 5);
  y = ((this->Position[1] >= 0) ? this->Position[1] : 5);
  width = ((this->Size[0] > 0) ? this->Size[0] : 300);
  height = ((this->Size[1] > 0) ? this->Size[1] : 300);

  // create our own window if not already set
    if (!this->WindowId)
      {
      WNDCLASS wndClass;
      
      int len = strlen( "Visualization Toolkit - Win32OpenGL #")
	+ (int)ceil( (double) log10( (double)(count+1) ) )
	+ 1;
      windowName = new char [ len ];
      sprintf(windowName,"Visualization Toolkit - Win32OpenGL #%i",count++);
      this-> SetWindowName (windowName);
      delete [] windowName;
      
      // has the class been registered ?
      if (!GetClassInfo(this->ApplicationInstance,"vtkOpenGL",&wndClass))
        {
        wndClass.style = CS_HREDRAW | CS_VREDRAW;
        wndClass.lpfnWndProc = vtkWin32OpenGLRenderWindow::WndProc;
        wndClass.cbClsExtra = 0;
        wndClass.hInstance = this->ApplicationInstance;
        wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        wndClass.lpszMenuName = NULL;
        wndClass.lpszClassName = "vtkOpenGL";
        // vtk doesn't use these extra 4 bytes, but app writers
        // may want them, so we provide them.
        wndClass.cbWndExtra = 4;
        RegisterClass(&wndClass);
        }
      
      // use real mutex
      vtkWin32OpenGLRenderWindow::WindowMutex->Lock();
      if (vtkWin32OpenGLRenderWindow::TempPointerToThis)
        {
        vtkErrorMacro("Two windows being created at the same time");
        }
      vtkWin32OpenGLRenderWindow::TempPointerToThis = this;
      /* create window */
      if (this->ParentId)
        {
        this->WindowId = CreateWindow(
          "vtkOpenGL", this->WindowName,
          WS_CHILD | WS_CLIPCHILDREN / * | WS_CLIPSIBLINGS * /,
          x, y, width, height,
          this->ParentId, NULL, this->ApplicationInstance, NULL);
        }
      else
        {
        this->WindowId = CreateWindow(
	  "vtkOpenGL", this->WindowName,
	  WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN / * | WS_CLIPSIBLINGS * /,
	  x,y, width+2*GetSystemMetrics(SM_CXFRAME),
	  height+2*GetSystemMetrics(SM_CYFRAME) +GetSystemMetrics(SM_CYCAPTION),
	  NULL, NULL, this->ApplicationInstance, NULL);
        }
      vtkWin32OpenGLRenderWindow::TempPointerToThis = NULL;
      vtkWin32OpenGLRenderWindow::WindowMutex->Unlock();
      if (!this->WindowId)
        {
        vtkErrorMacro("Could not create window, error:  " << GetLastError());
        return;
        }
      // extract the create info
      
      /* display window */
      ShowWindow(this->WindowId, SW_SHOW);
      //UpdateWindow(this->WindowId);
      this->OwnWindow = 1;
      }
    else
      {
      SetWindowLong(this->WindowId,GWL_USERDATA,(LONG)this);
      this->DeviceContext = GetDC(this->WindowId);
      if (this->StereoCapableWindow)
	{
	this->SetupPixelFormat(this->DeviceContext, PFD_SUPPORT_OPENGL |
			       PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER |
			       PFD_STEREO, this->GetDebug(), 32, 32);
	}
      else
	{
	this->SetupPixelFormat(this->DeviceContext, PFD_SUPPORT_OPENGL |
			       PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER,
			       this->GetDebug(), 32, 32);
	}
      this->SetupPalette(this->DeviceContext);
      this->ContextId = wglCreateContext(this->DeviceContext);
      wglMakeCurrent(this->DeviceContext, this->ContextId);
      this->OpenGLInit();
      }
    this->Mapped = 1;
    }	
  else
    {
    wglMakeCurrent(this->DeviceContext, this->ContextId); // hsr
    this->OpenGLInit();
    }

  // set the DPI
  this->SetDPI(GetDeviceCaps(this->DeviceContext, LOGPIXELSY));
}

        The above code is the method to create a win32 window, which can be clearly seen 

1、WNDCLASSCreateWindow,SetupPixelFormat,

The interface function of the window win32 application window is also a common standard method.

2、wglCreateContext,wglMakeCurrent。

Call the gl interface in the windows system.

3. OpenGLInit initialization function

void vtkWin32OpenGLRenderWindow::OpenGLInit()
{
  glMatrixMode( GL_MODELVIEW );
  glDepthFunc (GL_LEQUAL);
  glEnable( GL_DEPTH_TEST );
  glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
.......
}

        The explanation here should be relatively clear. In the "Win32 console application", vtk calls the interface of the win32 application window window, draws a UI by itself, and then binds OpenGL to it.


Looks like I haven't blogged in a while~

See through the rules of the game and return to core values.

"If I were given an hour to answer a question that would determine my life or death, I would spend 55 minutes trying to figure out what the question was asking."

                                                                                                                                    ---Einstein?


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324458439&siteId=291194637