MFC点击按钮显示OPenGL图像未遂。。。大家帮我看看啊~

cengyunmuxue 2012-04-13 11:41:57
本来应该是点击按钮显示图像的,但是点击以后什么也没有,大神们帮我看看问题出在哪里,小女子不胜感激啊。。。
我为OPenGL单独添加了一个类CGLWindow,CGLPointDlg来控制。代码如下。。。小白一枚,大家慎拍砖啊。。。

BOOL CGLPointDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// Add "About..." menu item to system menu.

// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon


HDC hDC = ::GetDC(this->m_hWnd);

PIXELFORMATDESCRIPTOR pfd;
// Choose a pixel format that best matches that described in pfd
int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
DescribePixelFormat(hDC, nPixelFormat, pfd.nSize, &pfd);

// Set the pixel format for the device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
::ReleaseDC(this->m_hWnd, hDC);
WNDCLASS wndClass;

wndClass.style = CS_OWNDC;
wndClass.lpfnWndProc = AfxWndProc;
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = AfxGetInstanceHandle();
wndClass.hIcon = NULL;
wndClass.hCursor = NULL;
wndClass.hbrBackground = NULL;
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = "OpenGLClass";

// Register the window class
AfxRegisterClass(&wndClass);

CRect glRect;
CRect dlgRect;
CRect clRect;
GetWindowRect(&dlgRect);
GetClientRect(&clRect);
GetDlgItem(IDC_TEST_FRAME)->GetWindowRect(&glRect);
newRect.top = glRect.top - dlgRect.top - (dlgRect.Height() - clRect.Height()) +20;
newRect.bottom = glRect.bottom - dlgRect.top - (dlgRect.Height() - clRect.Height()) -3;
newRect.left = glRect.left - dlgRect.left - (dlgRect.Width() - clRect.Width()) +9;
newRect.right = glRect.right - dlgRect.left - (dlgRect.Width() - clRect.Width()) -1 ;
return TRUE; // return TRUE unless you set the focus to a control
}


void CGLPointDlg::OnDisplayBT()
{
// TODO: Add your control notification handler code here
// HDC hDC = ::GetDC(this->m_hWnd);
int i = 0,j = 0;

for(i=0;i<204;i++){
for(j=0;j<204;j++){
DMatrix[204*i+j] = abs(rand())/1000;}}

m_glWnd.DestroyWindow();
MessageBox("here we'll launch.",NULL,MB_OK);
m_glWnd.Create("OpenGLClass",NULL,WS_CHILD | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
newRect,this,110,NULL);
m_glWnd.Invalidate(FALSE);
// ::ReleaseDC(this->m_hWnd, hDC);
}
float CGLPointDlg::MatMax(float* Mat) //求最大值
//float MatMax(float* Mat)
{
int i=0;
float Max=Mat[0];
for(i=0;i<204*204;i++) if(Max<Mat[i]) Max = Mat[i];
return Max;
}



下面是类CGLWindow代码

// GLWindow.cpp : implementation file
//

#include "stdafx.h"
#include "GLPoint.h"
#include "GLWindow.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

extern float DMatrix[204*204]; //没办法的办法
/////////////////////////////////////////////////////////////////////////////
// CGLWindow

CGLWindow::CGLWindow()
{
m_hRC = NULL;
m_hDC = NULL;
m_nPixelFormat;
}

CGLWindow::~CGLWindow()
{
}


BEGIN_MESSAGE_MAP(CGLWindow, CWnd)
//{{AFX_MSG_MAP(CGLWindow)
ON_WM_PAINT()
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CGLWindow message handlers
void CGLWindow::OnPaint() //绘制
{
static float fX = 0.0f;
static float fY = 0.0f;
// MessageBox("begain to paint GLWND.",NULL,MB_OK);
if(m_hRC != NULL)
{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//fX += 0.015f;
//fY += 0.01f;
fX += 1.0f;
fY += 1.0f;

if(fX > 360.0f)
fX = 0.015f;

if(fY > 360.0f)
fY = 0.01f;

glPushMatrix();
glRotatef(fX, 1.0f, 0.0f, 0.0f);
glRotatef(fY, 0.0f, 1.0f, 0.0f);

float fSize = 20.0f;
int x_row, y_row, m_nomb = 0;
GLfloat dmax;
int i=0;
dmax =DMatrix[0];
for(i=0;i<204*204;i++) if(dmax<DMatrix[i]) dmax = DMatrix[i];

glBegin(GL_POINTS);
for(m_nomb = 0; m_nomb < 204*204; m_nomb++)
{
if(DMatrix[m_nomb]/dmax>0.5)
{
glColor3f((GLfloat)(DMatrix[m_nomb]/dmax),
(GLfloat)(1-DMatrix[m_nomb]/dmax),
0.0f);
glVertex3f((GLfloat)(x_row = m_nomb%204) , (GLfloat)(y_row = (int)m_nomb/204) ,
(GLfloat)(DMatrix[m_nomb]));
}
else
{
glColor3f(0.0f,
(GLfloat)(DMatrix[m_nomb]/dmax),
(GLfloat)(1-DMatrix[m_nomb]/dmax));
glVertex3f((GLfloat)(x_row = m_nomb%204) , (GLfloat)(y_row = (int)m_nomb/204) ,
(GLfloat)DMatrix[m_nomb]);
}
}

glEnd();

glPopMatrix();

GLint eBuffer;
glGetIntegerv(GL_DRAW_BUFFER, &eBuffer);

if(eBuffer == GL_FRONT)
glFinish();
else
SwapBuffers(m_hDC);
}
else
{
MessageBox("Fail to paint",NULL,MB_OK);
}

ValidateRect(NULL);
}

int CGLWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate(lpCreateStruct) == -1)
return -1;
MessageBox("we create the GL WND",NULL,MB_OK);
// Get the device context
m_hDC = ::GetDC(this->m_hWnd);
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
1, // Version of this structure
PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
PFD_DOUBLEBUFFER, // Double buffered mode
PFD_TYPE_RGBA, // RGBA Color mode
32, // Want 32 bit color
0,0,0,0,0,0, // Not used to select mode
0,0, // Not used to select mode
0,0,0,0,0, // Not used to select mode
16, // Size of depth buffer
0, // Not used to select mode
0, // Not used to select mode
0, // Not used to select mode
0, // Not used to select mode
0,0,0 }; // Not used to select mode


// Choose a pixel format that best matches that described in pfd
m_nPixelFormat = ChoosePixelFormat(m_hDC, &pfd);
// Set the Pixel Format
SetPixelFormat(m_hDC, m_nPixelFormat, NULL);

// Create the rendering context and make it current
m_hRC = wglCreateContext(m_hDC);

if(m_hRC != NULL)
wglMakeCurrent(m_hDC, m_hRC);

// Setup the context
if(m_hRC != NULL)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);

}

SetTimer(101, 30, NULL);

return 0;
}

void CGLWindow::OnDestroy()
{
KillTimer(101);

CWnd::OnDestroy();

if(m_hRC)
{
wglMakeCurrent(m_hDC, NULL);
wglDeleteContext(m_hRC);
m_hRC = NULL;
}

::ReleaseDC(this->m_hWnd, m_hDC);
m_hDC = NULL;
}

void CGLWindow::OnSize(UINT nType, int w, int h)
{
CWnd::OnSize(nType, w, h);
MessageBox("change the GLWND size.",NULL,MB_OK);
GLfloat nRange = 100.0f;
GLfloat fAspect;

// Prevent a divide by zero
if(h == 0)
h = 1;

fAspect = (GLfloat)w/(GLfloat)h;

// Set Viewport to window dimensions
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);

// Reset coordinate system
glLoadIdentity();

// Setup perspective for viewing
gluPerspective(17.5f,fAspect,60,300);

// Viewing transformation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -250.0f);
}

void CGLWindow::OnTimer(UINT nIDEvent)
{
InvalidateRect(NULL);
CWnd::OnTimer(nIDEvent);
}
...全文
161 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
cengyunmuxue 2012-04-17
  • 打赏
  • 举报
回复
已解决 结帖啦~
向立天 2012-04-16
  • 打赏
  • 举报
回复
cengyunmuxue 2012-04-13
  • 打赏
  • 举报
回复
有人么 自己顶起
cengyunmuxue 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
我有个“DynViews.zip”可参考(0)分。
http://download.csdn.net/detail/schlafenhamster/4206078
[/Quote]
好滴 偶先去瞧瞧 不懂的可以问你么?
schlafenhamster 2012-04-13
  • 打赏
  • 举报
回复
我有个“DynViews.zip”可参考(0)分。
http://download.csdn.net/detail/schlafenhamster/4206078
cengyunmuxue 2012-04-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
未遂这字眼看起来有点。。。容易让人联想到弓虽x未遂。。。
开个玩笑,楼主你下了nehe的opengl教程来看没?
跟着他的例子一课一课学,很快就能上手了
[/Quote]
我看了OPENGL super bible 我这现在是不知道问题出在哪儿了 编译也没有错误 单步也没看出问题来 好纠结>_< 你说的那个教程我找找先看一下 谢谢啦
三条猫 2012-04-13
  • 打赏
  • 举报
回复
未遂这字眼看起来有点。。。容易让人联想到弓虽x未遂。。。
开个玩笑,楼主你下了nehe的opengl教程来看没?
跟着他的例子一课一课学,很快就能上手了

19,473

社区成员

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

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