2015-06-13 45 views
0

我有使用OpenGL在派生的CWnd类的设备上下文中绘制矩形的代码。有人可以告诉我为什么矩形没有在我的控件的左上角位置绘制? OpenGL用深蓝填充整个控件,为什么我的矩形不能从相同的y位置绘制? x位置很好,但y位移。MFC中的OpenGL在错误的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; 
} 

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

enter image description here

回答

1

我发现,通过设置我的控件的大小,以便它是一样的视口的大小,它绘制矩形在正确的左上角位置。