Hi, I’m using VS10 and OpenGL Wizard from here:
http://www.codeproject.com/KB/openGL/opengl_custom_wizard.aspx
And when i choose in Document Type: Multi Document Interface this wizard still create SDI project.
Any chance for MDI App with Opengl?
When i try copy-paste to a blank MDI project I still have white window.
Should I use OnDraw or OnPaint?
Link to my project: http://kmr.glt.pl/midopengl.rar
View:
// midopenglView.cpp : implementation of the CmidopenglView class
//
#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "midopengl.h"
#endif
#include "midopenglDoc.h"
#include "midopenglView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CmidopenglView
IMPLEMENT_DYNCREATE(CmidopenglView, CView)
BEGIN_MESSAGE_MAP(CmidopenglView, CView)
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CmidopenglView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
END_MESSAGE_MAP()
// CmidopenglView construction/destruction
CmidopenglView::CmidopenglView()
{
// TODO: add construction code here
}
CmidopenglView::~CmidopenglView()
{
}
BOOL CmidopenglView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CmidopenglView drawing
void CmidopenglView::OnDraw(CDC* pDC)
{
CmidopenglDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
OnPaint();
// TODO: add draw code for native data here
}
// CmidopenglView printing
void CmidopenglView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CmidopenglView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CmidopenglView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CmidopenglView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
void CmidopenglView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CmidopenglView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CmidopenglView diagnostics
#ifdef _DEBUG
void CmidopenglView::AssertValid() const
{
CView::AssertValid();
}
void CmidopenglView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CmidopenglDoc* CmidopenglView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CmidopenglDoc)));
return (CmidopenglDoc*)m_pDocument;
}
#endif //_DEBUG
// CmidopenglView message handlers
void CmidopenglView::SetDCPixelFormat(HDC hDC)
{
int nPixelFormat;
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
16, // Want 16 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
24, // Size of depth buffer
0, // Not used to select mode
PFD_MAIN_PLANE, // Draw in main plane
0, // Not used to select mode
0, // Not used to select mode
0,0,0 }; // Not used to select mode
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
void CmidopenglView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
width=cx;
height=cy;
if(cx && cy)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
InitProjection();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, cx, cy);
}
}
void CmidopenglView::OnLButtonDown(UINT nFlags, CPoint point)
{
//CMainFrame* m_pFrame = (CMainFrame*)AfxGetMainWnd();
//m_lbutton = TRUE;
//m_prevpoint.x=point.x;
//m_prevpoint.y=point.y;
//CView::OnLButtonDown(nFlags, point);
}
void CmidopenglView::OnPaint()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
InitProjection();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GetDocument()->DrawTriangles();
glColor4f(0.6f,0.8f,0.6f,0.6f);
glScalef(1.5,1.5,1.5);
glRotatef(m_spinx, 0.0f, 1.0f, 0.0f); // OnMouseMove
glRotatef(m_spiny-90.0f, 1.0f, 0.0f, 0.0f);
GetDocument()->DrawQuads();
glFlush();
SwapBuffers(hDC);
ValidateRect(NULL);
}
void CmidopenglView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_lbutton = FALSE; // if left mouse button up, flag is reset
CView::OnLButtonUp(nFlags, point);
}
void CmidopenglView::OnMouseMove(UINT nFlags, CPoint point)
{
if(m_lbutton) // mouse pointer
{
m_spinx += (float)point.x-(float)m_prevpoint.x;
m_spiny += (float)point.y-(float)m_prevpoint.y;
m_prevpoint.x = point.x;
m_prevpoint.y = point.y;
InvalidateRect(NULL, FALSE);
}
CView::OnMouseMove(nFlags, point);
}
BOOL CmidopenglView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
int CmidopenglView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
hDC = ::GetDC(m_hWnd); // OpenGL rendering context¸
SetDCPixelFormat(hDC);
hRC = wglCreateContext(hDC);
hRCChild1 = wglCreateContext(hDC); // first child context
hRCChild2 = wglCreateContext(hDC); // second child context
wglShareLists(hRCChild1, hRC); //Share Context between child-context and parent-context
wglMakeCurrent(hDC, hRC);
GetDocument()->InitDocTexture();
wglMakeCurrent(hDC, hRCChild1);
//SwapBuffers(hDCAnother);
InitOpenGL(); // Load lights, fog, alpha and others cool stuff ;)
return 0;
}
void CmidopenglView::OnChangeSize(int cx, int cy)
{
width=cx;
height=cy;
SetWindowPos( &wndTop, 0,0,cx,cy,SWP_NOMOVE);
}
void CmidopenglView::InitOpenGL(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
InitLight();
//GetDocument()->InitDocTexture(); // without sharing context UNcomment this to load texture on model
InitAlphaTest();
InitFog();
InitShadingModel();
glMatrixMode(GL_PROJECTION); // projection matrix
glLoadIdentity();
InitProjection();
glMatrixMode(GL_MODELVIEW); // modelview matrix
glLoadIdentity();
}
// Projection Support
void CmidopenglView::InitProjection()
{
gluPerspective( 45.0f, (float)width/height, 2.0f, 100.0f );
gluLookAt( 2.0f, 4.0f, 17.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f );
}
void CmidopenglView::OnDestroy()
{
CView::OnDestroy();
wglMakeCurrent(NULL,NULL); // OpenGL rendering context¸
wglDeleteContext(hRC);
wglDeleteContext(hRCChild1);
::ReleaseDC( m_hWnd, hDC );
}
void CmidopenglView::InitShadingModel()
{
glShadeModel(GL_SMOOTH); // Smooth shading
}
void CmidopenglView::InitAlphaTest()
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
}
void CmidopenglView::InitLight()
{
GLfloat lightposition[4] = {10.0f, 10.0f, 10.0f, 1.0f};
GLfloat globalambient[4] = {0.3f, 0.3f, 0.3f, 1.0f}; // Global Ambient
GLfloat lightcolor[4] = {0.9f, 0.9f, 0.9f, 1.0f};
GLfloat matambient[4] = {0.1f, 0.1f, 0.1f, 0.6f}; // Mat Ambient Array
GLfloat matdiffuse[4] = {0.6f, 0.8f, 0.6f, 0.6f};
GLfloat matspecular[4] = {0.9f, 0.9f, 0.9f, 0.7f};
GLbyte shine = 40;
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightposition);
glLightfv(GL_LIGHT0, GL_AMBIENT_AND_DIFFUSE, lightcolor);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globalambient);
glMaterialfv(GL_FRONT, GL_AMBIENT, matambient); // Ambient color
glMaterialfv(GL_FRONT, GL_DIFFUSE, matdiffuse); // diffuse color
glMaterialfv(GL_FRONT, GL_SPECULAR, matspecular); // spectular color
glMaterialf(GL_FRONT, GL_SHININESS, shine); // shininess
}
void CmidopenglView::InitFog()
{
GLfloat fogcolor[4] = {0.5f, 0.5f, 0.5f, 1.0f};
glFogi(GL_FOG_MODE, GL_EXP);
glFogfv(GL_FOG_COLOR, fogcolor);
glFogf(GL_FOG_DENSITY, 0.25f);
glHint(GL_FOG_HINT, GL_DONT_CARE);
glFogf(GL_FOG_START, 1.0f);
glFogf(GL_FOG_END, 50.0f);
glEnable(GL_FOG);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
}