【问题标题】:OpenGL in MFC Control painting at wrong Y positionMFC控件中的OpenGL在错误的Y位置绘制
【发布时间】:2015-06-13 00:26:41
【问题描述】:

我的代码使用 OpenGL 在派生的 CWnd 类的设备上下文中绘制矩形。有人可以告诉我为什么没有在我的控件的左上角绘制矩形吗? OpenGL 用深蓝色填充整个控件,那么为什么我的矩形不从相同的 y 位置绘制? x 位置很好,但 y 偏移了。

void CMyImage::OnPaint()
{
  CPaintDC dc( this ); // device context for painting
  HDC hdc;

  hdc = ::GetDC(m_hWnd);

  MySetPixelFormat( hdc );

  HGLRC hglrc;

  glClearColor(0,0,0,0);
  glColor3f(1, 1, 1);

  hglrc = wglCreateContext( hdc );

  if( hglrc )
  { 
    // try to make it the thread's current rendering context
    if( wglMakeCurrent( hdc, hglrc ) )
    {
      glViewport( 0, 0, m_ScreenWidth, m_ScreenHeight );                    // Reset The Current Viewport

        glMatrixMode( GL_PROJECTION );                      // Select The Projection Matrix
        glLoadIdentity();                           // Reset The Projection Matrix

      glOrtho( 0.0f, m_ScreenWidth, m_ScreenHeight, 0.0f, -1.0f, 1.0f );

        glMatrixMode( GL_MODELVIEW );                       // Select The Modelview Matrix
        glLoadIdentity();                           // Reset The Modelview Matrix
      glTranslatef(0.5, 0.5, 0);

      // Dark blue..
      glClearColor( 0.0f, 0.0f, 0.3f, 1.0f );

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );           

      glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
      glAlphaFunc( GL_GREATER, 0.1f );

      glEnable(GL_BLEND);
      glEnable(GL_ALPHA_TEST);

      glBindTexture( GL_TEXTURE_2D, 0 );

      glColor4f( 1.0f, 1.0f, 0, 1 );
      DrawFilledRectangle( 0, 0, 320, 200 );

      glColor4f( 1.0f, 0, 0, 1 );
      DrawFilledRectangle( 0, 0, 50, 50 );

...

void DrawFilledRectangle( int nLeft, int nTop, int nWidth, int nHeight )
{
  // Undo the glTranslatef( 0.5, 0.5 ) thats needed for drawing lines..
  glLoadIdentity();

  float fLeft   = (float) nLeft;
  float fTop    = (float) nTop;

  float fRight  = fLeft + (float) nWidth;
  float fBottom = fTop  + (float) nHeight;

  GLfloat avRect[ 18 ];
  GLfloat *p = avRect;    

  p = AddVertex3( p, fLeft,  fTop );
  p = AddVertex3( p, fRight, fTop );
  p = AddVertex3( p, fRight, fBottom );

  p = AddVertex3( p, fLeft,  fTop );
  p = AddVertex3( p, fLeft,  fBottom );
  p = AddVertex3( p, fRight, fBottom );

  glEnableClientState( GL_VERTEX_ARRAY );
  glEnableClientState( GL_NORMAL_ARRAY );

  glVertexPointer( 3, GL_FLOAT, 0, avRect );

  glDrawArrays( GL_TRIANGLES, 0, 6 );

  glDisableClientState( GL_VERTEX_ARRAY );
}

GLfloat *AddVertex3( GLfloat *pVertices, float fX, float fY )
{
  GLfloat *p = pVertices;

  *p = fX;   p ++;
  *p = fY;   p ++;
  *p = 0.0f; p ++;

  return p;
}

这是正在发生的事情的图像。红色和黄色矩形都应该在左上角。

【问题讨论】:

    标签: opengl mfc


    【解决方案1】:

    我发现通过将控件的大小设置为与视口的大小相同,它可以在正确的左上角位置绘制矩形。

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-30
      • 1970-01-01
      相关资源
      最近更新 更多