Click here to Skip to main content
15,884,099 members
Articles / Multimedia / OpenGL

OpenGL Win32 AppWizard

Rate me:
Please Sign up or sign in to vote.
4.97/5 (45 votes)
19 May 20033 min read 485.9K   22.7K   115  
This Custom AppWizard for VC++ 6.0 or VC++.NET creates an OpenGL enabled Win32 application suitable for demos and simple games.
// MainWnd.cpp: implementation of the CMainWnd class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MainWnd.h"
$$IF(opengl)
$$IF(fps)
#include <stdio.h>
$$ENDIF
$$ENDIF

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
$$IF(opengl)
CMainWnd::CMainWnd():
	m_hWnd(NULL),
$$IF(sample)
	m_fAngle(0.0f),
$$ENDIF
	m_hRC(NULL)
{
}
$$ELSE
CMainWnd::CMainWnd():
	m_hWnd(NULL)
{
}
$$ENDIF

CMainWnd::~CMainWnd()
{
}

$$IF(opengl)
$$IF(sample)
//
// GLCube()
// Renders a cube.
//
void GLCube(GLfloat x1, GLfloat y1, GLfloat z1, GLfloat x2, GLfloat y2, GLfloat z2)
{
	glBegin(GL_POLYGON);
	glNormal3f(0.0f, 0.0f, 1.0f);
	glVertex3f(x2, y2, z2);
	glVertex3f(x1, y2, z2);
	glVertex3f(x1, y1, z2);
	glVertex3f(x2, y1, z2);
	glEnd();

	glBegin(GL_POLYGON);
	glNormal3f(0.0f, 0.0f, -1.0f);
	glVertex3f(x2, y2, z1);
	glVertex3f(x2, y1, z1);
	glVertex3f(x1, y1, z1);
	glVertex3f(x1, y2, z1);
	glEnd();

	glBegin(GL_POLYGON);
	glNormal3f(-1.0f, 0.0f, 0.0f);
	glVertex3f(x1, y2, z2);
	glVertex3f(x1, y2, z1);
	glVertex3f(x1, y1, z1);
	glVertex3f(x1, y1, z2);
	glEnd();

	glBegin(GL_POLYGON);
	glNormal3f(1.0f, 0.0f, 0.0f);
	glVertex3f(x2, y2, z2);
	glVertex3f(x2, y1, z2);
	glVertex3f(x2, y1, z1);
	glVertex3f(x2, y2, z1);
	glEnd();

	glBegin(GL_POLYGON);
	glNormal3f(0.0f, 1.0f, 0.0f);
	glVertex3f(x1, y2, z1);
	glVertex3f(x1, y2, z2);
	glVertex3f(x2, y2, z2);
	glVertex3f(x2, y2, z1);
	glEnd();

	glBegin(GL_POLYGON);
	glNormal3f(0.0f, -1.0f, 0.0f);
	glVertex3f(x1, y1, z1);
	glVertex3f(x2, y1, z1);
	glVertex3f(x2, y1, z2);
	glVertex3f(x1, y1, z2);
	glEnd();
}

$$ENDIF
//
// InitScene()
// Called when the OpenGL RC has been created. Sets the initial state for
// the OpenGL scene.
//
void CMainWnd::InitScene()
{
	glClearColor($$back_r$$f, $$back_g$$f, $$back_b$$f, 1.0f); //Background color

$$IF(sample)
	// TODO: Replace the following sample code with your initialization code.

	// Activate lighting and a light source
  glEnable(GL_LIGHT0);
	glEnable(GL_LIGHTING);
  glEnable(GL_DEPTH_TEST);

	// Define material parameters
	static GLfloat glfMatAmbient[] = {0.000f, 0.450f, 1.000f, 1.0f};
	static GLfloat glfMatDiffuse[] = {0.000f, 0.000f, 0.580f, 1.0f};
	static GLfloat glfMatSpecular[]= {1.000f, 1.000f, 1.000f, 1.0f};
	static GLfloat glfMatEmission[]= {0.000f, 0.000f, 0.000f, 1.0f};
	static GLfloat fShininess = 128.000f;

	// Set material parameters
	glMaterialfv(GL_FRONT, GL_AMBIENT,  glfMatAmbient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE,  glfMatDiffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, glfMatSpecular);
	glMaterialfv(GL_FRONT, GL_EMISSION, glfMatEmission);
	glMaterialf(GL_FRONT, GL_SHININESS, fShininess);
$$ELSE
	// TODO: Add scene initialization code
$$ENDIF
}

//
// KillScene()
// Called when the OpenGL RC is about to be destroyed. 
//
void CMainWnd::KillScene()
{
	// TODO: Use KillScene to free resources.
}

//
// DrawScene()
// Called each time the OpenGL scene has to be drawn.
//
void CMainWnd::DrawScene()
{
$$IF(sample)
	// TODO: Replace the following sample code with your code to draw the scene.

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
  glLoadIdentity(); // Load identity matrix

	// Add a light source
	GLfloat glfLight[] = {-4.0f, 4.0f, 4.0f, 0.0f};
	glLightfv(GL_LIGHT0, GL_POSITION, glfLight);
 
	// Position and rotate the camera
  glTranslatef(0.0f, 0.0f, -5.0f);	
	glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
	glRotatef(m_fAngle, 0.0f, 1.0f, 0.0f);

	// Draw a cube
	static GLfloat r = .7f;
	GLCube(-r, -r, -r, r, r, r);

  glFlush();
$$ELSE
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear buffers
  glLoadIdentity(); // Load identity matrix

	// TODO: Render OpenGL scene

  glFlush();
$$ENDIF
}

//
// Tick()
// The applications "heartbeat" function. Called before another frame needs
// to be drawn. Use this function to calculate new positions for objects in 
// the scene, for instance. Set bRedrawScene = FALSE if the scene should not be 
// redrawn.
//
void CMainWnd::Tick(BOOL &bRedrawScene)
{
$$IF(sample)
	m_fAngle+= 1; // Add some rotation to the cube
$$ENDIF
$$IF(fps)	
$$IF(winfull)
#ifndef FULLSCREEN
	UpdateFPS();
#endif
$$ELSE
	UpdateFPS();
$$ENDIF
$$ENDIF
}

$$IF(fps)
//
// UpdateFPS()
// Calculates FPS and shows the result in the window caption on regular
// intervals. Needs to be called every frame update.
//
void CMainWnd::UpdateFPS()
{
	const int FRAMEINTERVAL = 1000;            // Show FPS every 1000th milliseconds
	static DWORD nFrames = 0;                  // Number of frames since last update
	static DWORD nLastTick = GetTickCount();   // Time of last update
	DWORD nTick = GetTickCount();              // Current time
	if(nTick-nLastTick>=FRAMEINTERVAL)
	{	
		float fFPS = 1000.0f*(GLfloat)nFrames/(GLfloat)(nTick-nLastTick);
		nLastTick = nTick;
		nFrames = 0;
		char szFPS[256];
		sprintf(szFPS, "%.1f FPS", fFPS);
		SetWindowText(m_hWnd, szFPS);
	}
	nFrames++;
}
$$ENDIF
$$ENDIF

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Sweden Sweden
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions