Hi thank you very much for your help. I manage to run OpenGL in My dialog, as you told me .
This is running ok in the SDI (I checked and on mouse move, mouse click is ok, is entering to the functions). But In the dialog, only mouse move works, mouse click is not working (he is not entering to the functions). When I do click inside the dialog I get
" Debug Assertion Failed
File:viewcore.cpp
Line: 249
"
Any help will be really good. Thank you again
This is how I add it to the dialog
BOOL CAboutDlg::OnInitDialog()
{ CDialog::OnInitDialog();
CRect rect;
GetDlgItem(IDC_OPENGL)->GetWindowRect(rect);
z0Lv_openGL = new COpenGLView();
ScreenToClient(rect);
z0Lv_openGL->oglCreate(rect, this);
return TRUE;
}
This is my is COpenGLView.h
#include
#include "MyView.h"
class COpenGLView : public CView
{
public:
COpenGLView();
virtual ~COpenGLView();
void oglCreate(CRect rect, CWnd *parent);
protected:
DECLARE_DYNCREATE(COpenGLView)
protected:
CDC* m_pDC;
HGLRC m_hRC;
GLUquadricObj* m_quadric;
double m_rotation[3];
public:
protected:
virtual void OnDraw(CDC* pDC);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
void SetupPixelFormat();
void InitGL();
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnDestroy();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
My COpenGLView.cpp
IMPLEMENT_DYNCREATE(COpenGLView, CView)
COpenGLView::COpenGLView()
{
}
COpenGLView::~COpenGLView()
{
}
BEGIN_MESSAGE_MAP(COpenGLView, CView)
ON_WM_CREATE()
ON_WM_ERASEBKGND()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_TIMER()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
void COpenGLView::OnDraw(CDC* pDC)
{static double dLv_d=0.5;
dLv_d =dLv_d+1.0;
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glRotated(m_rotation[0], 1.0, 0.0, 0.0);
glRotated(m_rotation[1], 0.0, 1.0, 0.0);
glRotated(m_rotation[2], 0.0, 0.0, 1.0);
glPushMatrix();
glColor3f(1.0f, 0.1f, 0.5f);
gluCylinder(m_quadric, 1.0, 1.0, 1.0, 16, 16);
gluDisk(m_quadric, 0.0, 1.0, 16, 1);
glTranslated(dLv_d, 0.0, 1.0);
gluDisk(m_quadric, 0.0, 1.0, 16, 1);
glPopMatrix();
SwapBuffers(m_pDC->GetSafeHdc());
}
#ifdef _DEBUG
void COpenGLView::AssertValid() const
{
CView::AssertValid();
}
void COpenGLView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
int COpenGLView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
InitGL();
return 0;
}
BOOL COpenGLView::PreCreateWindow(CREATESTRUCT& cs)
{
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
void COpenGLView::InitGL()
{
m_pDC = new CClientDC(this);
SetupPixelFormat();
m_hRC = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat color[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat lightPos[4] = {3.0f, 3.0f, 3.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, color);
glLightfv(GL_LIGHT0, GL_DIFFUSE, color);
glLightfv(GL_LIGHT0, GL_SPECULAR, color);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
m_quadric = gluNewQuadric();
for(int i=0; i<3; i++) {
m_rotation[i] = (((double) rand() / (double) RAND_MAX) * 2.0) - 1.0;
}
}
void COpenGLView::SetupPixelFormat()
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0,
};
int pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd);
SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd);
}
BOOL COpenGLView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void COpenGLView::OnDestroy()
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(m_hRC);
if(m_pDC) delete m_pDC;
gluDeleteQuadric(m_quadric);
CView::OnDestroy();
}
void COpenGLView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
wglMakeCurrent(m_pDC->GetSafeHdc(), m_hRC);
glViewport(0, 0, cx, cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double) cx / (double) cy, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void COpenGLView::OnTimer(UINT nIDEvent)
{
Invalidate();
CView::OnTimer(nIDEvent);
}
void COpenGLView::oglCreate(CRect rect, CWnd *parent)
{
CString className = AfxRegisterWndClass(CS_HREDRAW |
CS_VREDRAW | CS_OWNDC, NULL,
(HBRUSH)GetStockObject(BLACK_BRUSH), NULL);
CreateEx(0, className, "OpenGL", WS_CHILD | WS_VISIBLE |
WS_CLIPSIBLINGS | WS_CLIPCHILDREN, rect, parent, 0);
}
void COpenGLView::OnMouseMove(UINT nFlags, CPoint point)
{
Invalidate();
CView::OnMouseMove(nFlags, point);
}
void COpenGLView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
CView::OnLButtonDblClk(nFlags, point);
}
void COpenGLView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
}
void COpenGLView::OnLButtonUp(UINT nFlags, CPoint point)
{
CView::OnLButtonUp(nFlags, point);
}
|