我有使用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;
}
这是一张正在发生的事情的图像。红色和黄色矩形都应该在左上方。
答案 0 :(得分:1)
我发现通过设置控件的大小使其与视口的大小相同,它会在正确的左上角位置绘制矩形。