对话框的OPENGL类不显示图像

mileir1 2011-02-25 10:49:16
写了一个基于对话框的OPENGL类。
可以渲染出黑色背景。但是图形无法显示。
不知道原因出在哪里了。

有人有空不。帮我看一下。多谢多谢。
QQ:21425004。
我将它发给你。
...全文
406 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
flydm 2013-07-18
  • 打赏
  • 举报
回复
是什么原因?
runer 2011-02-25
  • 打赏
  • 举报
回复
兄弟你的结贴率太低了
mileir1 2011-02-25
  • 打赏
  • 举报
回复
没问题了。已解决。
mileir1 2011-02-25
  • 打赏
  • 举报
回复
好的。
定义一个COpenGL类,继承CWnd类。
其主要消息处理函数为。


int COpenGL::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: 在此添加您专用的创建代码

/* EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &m_DMsaved);*/
GLuint PixelFormat; // Holds The Results After Searching For A Match
static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
m_DMsaved.dmBitsPerPel, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
if ( !( m_hdc = ::GetDC ( m_hWnd ) ) ) { // Did We Get A Device Context?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Create A GL Device Context." );
return FALSE;
}
if ( !( PixelFormat = ChoosePixelFormat ( m_hdc, &pfd ) ) ) { // Did Windows Find A Matching Pixel Format?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Find A Suitable PixelFormat." );
return FALSE;
}
if ( !SetPixelFormat ( m_hdc, PixelFormat, &pfd ) ){ // Are We Able To Set The Pixel Format?
KillGLWindow (); // Reset The Display
TRACE ( "Can't Set The PixelFormat." );
return FALSE;
}
if ( !( m_rc = wglCreateContext ( m_hdc ) ) ) { // Are We Able To Get A Rendering Context?
KillGLWindow (); // Reset The Display
TRACE( " Can't Create A GL Rendering Context." );
return FALSE;}
if ( !wglMakeCurrent ( m_hdc, m_rc ) ) { // Try To Activate The Rendering Context
KillGLWindow (); // Reset The Display
TRACE ( "Can't Activate The GL Rendering Context." );
return FALSE;
}
if ( !InitGL () ) { // Initialize Our Newly Created GL Window
KillGLWindow (); // Reset The Display
TRACE ( "Initialization Failed." );
return FALSE;
}

m_bInit = TRUE;
return 0;
}

void COpenGL::KillGLWindow()
{
if ( m_rc ) { // Do We Have A Rendering Context?
if ( !wglMakeCurrent ( NULL, NULL ) ) { // Are We Able To Release The DC And RC Contexts?
TRACE ( "Release Of DC And RC Failed." );
}
if ( !wglDeleteContext ( m_rc ) ) { // Are We Able To Delete The RC?
TRACE ( "Release Rendering Context Failed." );
}
m_rc = NULL; // Set RC To NULL
}
if ( m_hdc && !::ReleaseDC ( m_hWnd, m_hdc ) ) { // Are We Able To Release The DC
TRACE ( "Release Device Context Failed." );
m_hdc = NULL; // Set DC To NULL
}
if ( m_hWnd && !::DestroyWindow ( m_hWnd ) ) { // Are We Able To Destroy The Window?
TRACE( "Could Not Release m_hWnd." );
m_hWnd = NULL; // Set m_hWnd To NULL
}
}


void COpenGL::OnDestroy()
{
CWnd::OnDestroy();

// TODO: 在此处添加消息处理程序代码
if(m_hdc != 0)
{
::wglMakeCurrent(m_hdc,NULL);
m_hdc=0;
}
if(m_rc != 0)
{
::wglDeleteContext(m_rc);
m_rc=0;
}

}

void COpenGL::OnPaint()
{
// CPaintDC dc(this); // device context for painting
// TODO: 在此处添加消息处理程序代码
// 不为绘图消息调用 CWnd::OnPaint()

if(!m_bInit) return;
RenderGL();
SwapBuffers(m_hdc);
}

void COpenGL::RenderGL()
{
if(!m_bInit) return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0);

float x,y;
x = m_rect.right/2;
y = m_rect.bottom/2;
glBegin(GL_LINES);
glColor3f(0.0,1.0,0.0);
glVertex3f(x,y,0.0);
glVertex3f(x+10,y,0.0);
glVertex3f(x,y,0.0);
glVertex3f(x,y+10,0.0);
glEnd();

glLoadIdentity();
glFlush();
}

void COpenGL::Create(CRect rect, CWnd *parent)
{

if (m_bInit) return;
ASSERT(rect);
ASSERT(parent);
// CWnd* pMainWnd=new CWnd;
/*m_rect = rect;*/

m_rect.top = 0;
m_rect.left = 0;
m_rect.SetRect(CPoint(0,0),CPoint(rect.Width(),rect.Height()));
m_parent = parent;
CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS,NULL,
(HBRUSH)GetStockObject(WHITE_BRUSH),NULL);

this->CreateEx(0,className,"OpenGL",WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS| WS_CLIPCHILDREN
| CS_SAVEBITS | WS_OVERLAPPED,m_rect,m_parent,0);
this->ShowWindow(SW_SHOW);
//flag = FALSE;

}

int COpenGL::InitGL()
{
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping

return TRUE; // Initialization Went OK
}

void COpenGL::OnSize(UINT nType, int cx, int cy)
{
CWnd::OnSize(nType, cx, cy);

// TODO: 在此处添加消息处理程序代码
if ( cy==0) { // Prevent A Divide By Zero By
cy=1; // Making Height Equal One
}
glViewport(0,0,cx,cy); // Reset The Current Viewport
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
gluPerspective(45.0f,(GLfloat)cx/(GLfloat)cy,0.0001f,100.0f); // Calculate The Aspect Ratio Of The Window
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
}

显示结果为,背景渲染为黑色。
但是需要显示的图形无显示。
runer 2011-02-25
  • 打赏
  • 举报
回复
把你的关键代码贴出来吧

这样问题解决了,也能对别人有所帮助
mileir1 2011-02-25
  • 打赏
  • 举报
回复
这次一定按时结贴。
以前用的不熟练啊。

8,324

社区成员

发帖
与我相关
我的任务
社区描述
游戏开发相关内容讨论专区
社区管理员
  • 游戏开发
  • 呆呆敲代码的小Y
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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