Click here to Skip to main content
15,892,674 members
Articles / Desktop Programming / MFC

OpenGL MFC AppWizard

Rate me:
Please Sign up or sign in to vote.
4.79/5 (10 votes)
15 Jan 2009GPL36 min read 155.4K   5.4K   68  
An article showing how to make a Custom AppWizard for OpenGL applications in Visual Studio .NET 2008
// [!output PROJECT_NAME]View.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "[!output PROJECT_NAME].h"
#include "[!output PROJECT_NAME]View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// C[!output PROJECT_NAME]View

C[!output PROJECT_NAME]View::C[!output PROJECT_NAME]View()
{
}

C[!output PROJECT_NAME]View::~C[!output PROJECT_NAME]View()
{
}


BEGIN_MESSAGE_MAP(C[!output PROJECT_NAME]View, COpenGLView)
END_MESSAGE_MAP()



// C[!output PROJECT_NAME]View message handlers

BOOL C[!output PROJECT_NAME]View::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!CWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}

void C[!output PROJECT_NAME]View::DoOpenGLDraw()
{
[!if CHECKBOX_EXAMPLE_RENDERING]
	//	Here we'll do a little bit of example rendering, by drawing a cube.
	
	//	Set the clear colour to black and clear the colour buffer.
	glClearColor(0, 0, 0, 1);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	//	Look at the middle of the scene.
	glLoadIdentity();
	gluLookAt(-10, 10, 10, 0, 0, 0, 0, 1, 0);

	//	Set up some nice attributes for drawing the grid.
	glPushAttrib(GL_LINE_BIT | GL_ENABLE_BIT | GL_COLOR_BUFFER_BIT);
	glEnable(GL_LINE_SMOOTH);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDisable(GL_LIGHTING);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	glLineWidth(1.5f);

	//	Create the grid.

    glBegin(GL_LINES);
	
	for (int i = -10; i <= 10; i++)
    {
		glColor4f(0.2f, 0.2f, 0.2f, 0.8f);
        glVertex3f((float)i, 0, -10);
        glVertex3f((float)i, 0, 10);
        glVertex3f(-10, 0, (float)i);
        glVertex3f(10, 0, (float)i);
	}
	
	glEnd();

	//	Create the axies.
	glBegin(GL_LINES);

		glColor4f(1, 0, 0, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(3, 0, 0);
		glColor4f(0, 1, 0, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 3, 0);
		glColor4f(0, 0, 1, 1);
		glVertex3f(0, 0, 0);
		glVertex3f(0, 0, 3);

	glEnd();

	glPopAttrib();

	glFlush();
[!else]
	//	Do your OpenGL drawing here. Don't forget to call glFlush at the end!
[!endif]
}

void C[!output PROJECT_NAME]View::DoOpenGLResize(int nWidth, int nHeight)
{
	//	Create the viewport.
	glViewport(0, 0, nWidth, nHeight);

	//	Load the identity projection matrix.
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	//	Create a perspective viewport transformation.
	gluPerspective(45.0f, (GLfloat)nWidth / (GLfloat)nHeight, 0.1f, 100.0f);

	//	Go back to the modelview matrix.
	glMatrixMode(GL_MODELVIEW);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions