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;
}
這裏是正在發生的事情的圖像。紅色和黃色的矩形應該在左上角。