Using OpenGL in Visual C++: Part I #1 Jul 14th, 2004
Here is the example i also used to implement an opengl window ready for painting. But the problem is i still have flicker. How can i get rid of it ? pls help me...
here is my code :
// DiplomaView.cpp : implementation of the CDiplomaView class
//
#include "stdafx.h"
#include "Diploma.h"
#include "DiplomaDoc.h"
#include "DiplomaView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDiplomaView
IMPLEMENT_DYNCREATE(CDiplomaView, CView)
BEGIN_MESSAGE_MAP(CDiplomaView, CView)
ON_WM_CREATE()
ON_WM_DESTROY()
ON_WM_SIZE()
ON_WM_PAINT()
ON_WM_MOVE()
ON_WM_SHOWWINDOW()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//{{AFX_MSG_MAP(CDiplomaView)
ON_COMMAND(ID_CUB, OnCub)
ON_COMMAND(ID_CON, OnCon)
ON_COMMAND(ID_CILINDRU, OnCilindru)
ON_COMMAND(ID_PIRAMIDA, OnPiramida)
ON_COMMAND(ID_SFERA, OnSfera)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDiplomaView construction/destruction
CDiplomaView::CDiplomaView()
{
// TODO: add construction code here
m_hGLContext = NULL;
m_GLPixelIndex = 0;
}
CDiplomaView::~CDiplomaView()
{
}
BOOL CDiplomaView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDiplomaView drawing
void CDiplomaView::OnDraw(CDC* pDC)
{
CDiplomaDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CDiplomaView diagnostics
#ifdef _DEBUG
void CDiplomaView::AssertValid() const
{
CView::AssertValid();
}
void CDiplomaView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDiplomaDoc* CDiplomaView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDiplomaDoc)));
return (CDiplomaDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDiplomaView message handlers
BOOL CDiplomaView::SetWindowPixelFormat(HDC hDC)
{
PIXELFORMATDESCRIPTOR pixelDesc;
pixelDesc.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixelDesc.nVersion = 1;
pixelDesc.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER |
PFD_STEREO_DONTCARE;
pixelDesc.iPixelType = PFD_TYPE_RGBA;
pixelDesc.cColorBits = 24;
pixelDesc.cRedBits = 0;
pixelDesc.cRedShift = 0;
pixelDesc.cGreenBits = 0;
pixelDesc.cGreenShift = 0;
pixelDesc.cBlueBits = 0;
pixelDesc.cBlueShift = 0;
pixelDesc.cAlphaBits = 0;
pixelDesc.cAlphaShift = 0;
pixelDesc.cAccumBits = 0;
pixelDesc.cAccumRedBits = 0;
pixelDesc.cAccumGreenBits = 0;
pixelDesc.cAccumBlueBits = 0;
pixelDesc.cAccumAlphaBits = 0;
pixelDesc.cDepthBits =32;
pixelDesc.cStencilBits = 0;
pixelDesc.cAuxBuffers = 0;
pixelDesc.iLayerType = PFD_MAIN_PLANE;
pixelDesc.bReserved = 0;
pixelDesc.dwLayerMask = 0;
pixelDesc.dwVisibleMask = 0;
pixelDesc.dwDamageMask = 0;
m_GLPixelIndex = ChoosePixelFormat( hDC, &pixelDesc);
if (m_GLPixelIndex==0) // Let's choose a default index.
{
m_GLPixelIndex = 1;
MessageBox("ChoosePixelFormat failed");
if (DescribePixelFormat(hDC, m_GLPixelIndex,
sizeof(PIXELFORMATDESCRIPTOR), &pixelDesc)==0)
{
return FALSE;
}
}
if (SetPixelFormat( hDC, m_GLPixelIndex, &pixelDesc)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CDiplomaView::CreateViewGLContext(HDC hDC)
{
m_hGLContext = wglCreateContext(hDC);
if (m_hGLContext == NULL)
{
return FALSE;
}
if (wglMakeCurrent(hDC, m_hGLContext)==FALSE)
{
return FALSE;
}
return TRUE;
}
void CDiplomaView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
GLsizei width, height;
GLdouble aspect;
width = cx;
height = cy;
if (cy==0)
aspect = (GLdouble)width;
else
aspect = (GLdouble)width/(GLdouble)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, aspect, 1, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDrawBuffer(GL_BACK);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
desenarecub=false;
desenarecon=false;
desenarecilindru=false;
desenarepiramida=false;
desenaresfera=false;
// TODO: Add your message handler code here
}
int CDiplomaView::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;
// TODO: Add your specialized creation code here
}
void CDiplomaView::OnDestroy()
{
CView::OnDestroy();
if(wglGetCurrentContext()!=NULL)
{
// make the rendering context not current
wglMakeCurrent(NULL, NULL) ;
}
if (m_hGLContext!=NULL)
{
wglDeleteContext(m_hGLContext);
m_hGLContext = NULL;
}
// Now the associated DC can be released.
CView::OnDestroy();
// TODO: Add your message handler code here
}
void CDiplomaView::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDiplomaDoc* pDoc = GetDocument();
if (desenarecub)
{
pDoc->RenderSceneCub();
}
if (desenarecilindru)
{
pDoc->RenderSceneCilindru(raza1cilindru,raza2cilindru,inaltimecilindru);
}
if (desenarecon)
{
pDoc->RenderSceneCon(razacon,inaltimecon);
}
if (desenarepiramida)
{
pDoc->RenderScenePiramida();
}
if (desenaresfera)
{
pDoc->RenderSceneSfera(razasfera);
}
SwapBuffers(dc.m_ps.hdc);
// Do not call CView::OnPaint() for painting messages
}
void CDiplomaView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_LeftButtonDown = TRUE;
m_LeftDownPos = point;
CView::OnLButtonDown(nFlags, point);
}
void CDiplomaView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_LeftButtonDown = FALSE;
CView::OnLButtonUp(nFlags, point);
}
void CDiplomaView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_LeftButtonDown)
{
CDiplomaDoc* pDoc = GetDocument();
CSize rotate = m_LeftDownPos - point;
m_LeftDownPos = point;
pDoc->m_xRotate += rotate.cx/3;
pDoc->m_yRotate += rotate.cy/3;
InvalidateRect(NULL);
}
CView::OnMouseMove(nFlags, point);
}
void CDiplomaView::OnCub()
{
// TODO: Add your command handler code here
// device context for painting
repeta :
if(dlgCub.DoModal()==IDOK)
{
laturacub = (float) dlgCub.m_iLat;
if ((laturacub<=0) || (laturacub>10))
{
MessageBox("Introduceti o valoare intre 0 si 10","Eroare");
goto repeta;
}
desenarecub = true;
OnPaint();
}
}
void CDiplomaView::OnCon()
{
// TODO: Add your command handler code here
// int raza,inaltimea;
repeta :
if(dlgCon.DoModal()==IDOK)
{
razacon = (float) dlgCon.m_iR1;
inaltimecon = (float) dlgCon.m_iH;
if ((razacon<=0) || (razacon>10) || (inaltimecon<=0) || (inaltimecon>10))
{
MessageBox("Introduceti o valoare intre 0 si 10","Eroare");
goto repeta;
}
desenarecon = true;
OnPaint();
}
}
void CDiplomaView::OnCilindru()
{
// TODO: Add your command handler code here
repeta :
if(dlgCilindru.DoModal()==IDOK)
{
raza1cilindru = (float) dlgCilindru.m_iR1;
raza2cilindru = (float) dlgCilindru.m_iR2;
inaltimecilindru = (float) dlgCilindru.m_iH;
if ((raza1cilindru<=0) || (raza1cilindru>10) ||(raza2cilindru<=0) || (raza2cilindru>10))
{
MessageBox("Introduceti o valoare intre 0 si 10","Eroare");
goto repeta;
}
desenarecilindru = true;
OnPaint();
}
}
void CDiplomaView::OnPiramida()
{
// TODO: Add your command handler code here
repeta :
if(dlgPiramida.DoModal()==IDOK)
{
muchiepiramida = (float) dlgPiramida.m_iMuchia;
inaltimepiramida = (float) dlgPiramida.m_iH;
if ((muchiepiramida<=0) || (muchiepiramida>10) ||(inaltimepiramida<=0) || (inaltimepiramida>10))
{
MessageBox("Introduceti o valoare intre 0 si 10","Eroare");
goto repeta;
}
desenarepiramida=true;
OnPaint();
}
}
void CDiplomaView::OnSfera()
{
// TODO: Add your command handler code here
repeta :
if(dlgSfera.DoModal()==IDOK)
{
razasfera = (float) dlgSfera.m_iRaza;
if ((razasfera<=0) || (razasfera>10))
{
MessageBox("Introduceti o valoare intre 0 si 10","Eroare");
goto repeta;
}
desenaresfera=true;
OnPaint();
}
}
of course, in the document part i did all the drawing... why do i have the flicker ?:(
what can i do ?