OpenGL画图,为什么不能显现图形?

xio 2005-03-07 03:09:44
请教高人,主要代码如下(只列出自己添加的代码,和修改后的成员函数):
class CGLSample3View : public CView
{
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);

protected:
DECLARE_MESSAGE_MAP()
BOOL SetWindowPixelFormat(HDC hDC);
int m_hGLPixelIndex;
public:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
protected:
BOOL CreateViewGLContext(HDC hDC);
HGLRC m_hGLContext;
public:
afx_msg void OnDestroy();
afx_msg void OnPaint();
};
//**********************
CGLSample3View::CGLSample3View()
: m_hGLPixelIndex(0)
{
m_hGLContext = NULL;
}
BOOL CGLSample3View::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
return CView::PreCreateWindow(cs);
}
BOOL CGLSample3View::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_DRAW_TO_BITMAP |
PFD_SUPPORT_GDI |
PFD_SUPPORT_OPENGL |
PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 32;
pixelDesc.cRedBits = 8;
pixelDesc.cRedShift = 16;
pixelDesc.cGreenBits = 8;
pixelDesc.cGreenShift = 8;
pixelDesc.cBlueBits = 8;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 64;
pixelDesc.cAccumRedBits = 16;
pixelDesc.cAccumGreenBits = 16;
pixelDesc.cAccumBlueBits = 16;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits = 32;
pixelDesc.cStencilBits = 8;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;

m_hGLPixelIndex = ChoosePixelFormat(hDC, &pixelDesc);
if(m_hGLPixelIndex == 0)
{
m_hGLPixelIndex = 1;
if(DescribePixelFormat(hDC, m_hGLPixelIndex, sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc) == 0)
{
return FALSE;
}
}
if(SetPixelFormat(hDC, m_hGLPixelIndex, &pixelDesc) == FALSE)
{
return FALSE;
}
return TRUE;
}

BOOL CGLSample3View::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if(m_hGLContext == NULL)
return FALSE;
if(wglMakeCurrent(hDC, m_hGLContext) == FALSE)
return FALSE;
return TRUE;
}

int CGLSample3View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
HWND hWnd = GetSafeHwnd();
HDC hDC = ::GetDC(hWnd);
if(SetWindowPixelFormat(hDC) == FALSE)
return 0;
if(CreateViewGLContext(hDC) == FALSE) //使图形操作描述表有用
return 0;

return 0;
}



void CGLSample3View::OnDestroy()
{
CView::OnDestroy();
if(wglGetCurrentContext() != NULL)
wglMakeCurrent(NULL, NULL);
if(m_hGLContext != NULL)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
}

void CGLSample3View::OnPaint()
{
CPaintDC dc(this); // device context for painting
CGLSample3Doc *pDoc = GetDocument();
pDoc->RenderScene();
}
...全文
68 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenbuaa 2005-03-10
  • 打赏
  • 举报
回复
我只用过directx,没用过opengl。

但是,我看你得source根本就没有把buffer里面的内容给render出来,哪里会有图形呢??
syy64 2005-03-10
  • 打赏
  • 举报
回复
1、加上SwapBuffers(hdc)函数试试;
2、显示列表建的时候放在产生新文档里不合适。
xio 2005-03-09
  • 打赏
  • 举报
回复
为什么没有人指点呢?
xio 2005-03-07
  • 打赏
  • 举报
回复
class CGLSample3Doc : public CDocument
{
public:
enum GLDisplayListNames
{
ArmPart = 1
};
public:
void RenderScene(void);
double m_transY;
double m_transX;
double m_angle1;
double m_angle2;
};
//********************************
CGLSample3Doc::CGLSample3Doc()
: m_transY(100)
, m_transX(100)
, m_angle1(15)
, m_angle2(15)
{
}
BOOL CGLSample3Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
glNewList(ArmPart, GL_COMPILE_AND_EXECUTE);
glBegin(GL_POLYGON);
glVertex2f(-10.0f, 10.0f);
glVertex2f(-10.0f, -10.0f);
glVertex2f(100.0f, -10.0f);
glVertex2f(100.0f, 10.0f);
glEnd();
glEndList();

return TRUE;
}
void CGLSample3Doc::RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);

glPushMatrix();
glTranslated(m_transX, m_transY, 0);
glRotated(m_angle1, 0, 0, 1);

glPushMatrix();
glTranslated(90, 0, 0);
glRotated(m_angle2, 0, 0, 1);
glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glCallList(ArmPart);
glPopMatrix();

glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
glCallList(ArmPart);
glPopMatrix();
glFlush();
}

19,468

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 图形处理/算法
社区管理员
  • 图形处理/算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧