我尝试绘制对角线,但 OpenGL 将其显示为水平线,我不知道为什么。
这是我的代码:
设置 OpenGL:
if (!SetWindowPixelFormat(hDC))
return 0;
if (!CreateViewGLContext(hDC))
return 0;
BOOL SetWindowPixelFormat(HDC hDC)
{
int iBPP = GetDeviceCaps(hDC, BITSPIXEL) * GetDeviceCaps(hDC, PLANES);
ZeroMemory(&m_pfd, sizeof(PIXELFORMATDESCRIPTOR));
m_pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
m_pfd.nVersion = 1;
m_pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
m_pfd.iPixelType = PFD_TYPE_RGBA;
m_pfd.cColorBits = iBPP;
m_pfd.cDepthBits = 24;
m_pfd.iLayerType = PFD_MAIN_PLANE;
int pixelIdx = ChoosePixelFormat(hDC, &m_pfd);
if (pixelIdx <= 0)
return FALSE;
if (!SetPixelFormat(hDC, pixelIdx, &m_pfd))
return FALSE;
return TRUE;
}
设置平截头体:
void OnSize(UINT nType, int cx, int cy)
{
double fWidth = 35.209655761718750;
double fHeight = 22.964477539062500;
double xc = 506654.83659999998;
double yc = 7805600.7220999999;
SetFrustum(cx, cy, fWidth, fHeight, xc, yc);
}
void SetFrustum(int width, int height, double fWidth, double fHeight, double xc, double yc)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(xc - fWidth/2, xc + fWidth/2, yc - fHeight/2, yc + fHeight/2);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
在下面绘制 5 个点必须在对角线上,但 OpenGL 显示为水平线:
void OnDraw(CDC* pDC)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_POINTS);
glVertex3d(506655.5111, 7805600.4781, 0);
glVertex3d(506655.3184, 7805600.5478, 0);
glVertex3d(506655.2220, 7805600.5827, 0);
glVertex3d(506654.9330, 7805600.6872, 0);
glVertex3d(506654.8366, 7805600.7221, 0);
glEnd();
glFlush();
SwapBuffers(pDC->m_hDC);
}