65.9K
CodeProject is changing. Read more.
Home

A Cessna Skyhawk Skeleton for Further Development in OpenGL (GLUT)

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.07/5 (10 votes)

May 28, 2010

CPOL

2 min read

viewsIcon

39914

downloadIcon

2373

A Cessna Skyhawk Skeleton for further development in OpenGL (GLUT) using a Win32 Console

Introduction

I would hate to see this code go lost. The code in question is a flexible skeleton for a Cessna Skyhawk simulation, but can be developed into a fullfledged flight simulator.

It uses the OpenGL (GLUT) API so if you do not already have GLUT installed on your system, visit http://www.xmission.com/~nate/glut.html to download the latest GLUT binaries.

How to Install glut

  • Unpack the contents of the archive glut-3.7.6-bin.zip
  • Copy glut.lib to the \Microsoft Visual Studio\VCxx\Lib directory
  • Copy glut.h to the \Microsoft Visual Studio\VCxx\Include\GL directory
  • Copy glut32.dll to the \WINDOWS\system32 directory

The project files are:

cessna.cpp The main file
cessna.h The cessna header file
draw.h The draw header file
glm.h The glm.h header file
globals.h The globals header file
input.h The input header file
materials.h The materials header file
polygons.h The polygons header file
render.h The render header file
splash.h The splash header file
vertices.h The vertices input header file

The Controls

The Cessna Skyhawk is controlled by either keyboard or mouse.
The Keyboard controls are:

RIGHT KEY or C roll right
LEFT KEY or Z roll left
UP KEY or S pitch down
DOWN KEY or X pitch up
1 External view from behind, follow aircraft
2 External view from left side
3 External view from right side
4 Internal view from cockpit
+ Increase propeller speed
- Decrease propeller speed

The key() Function

The key() function is a callback for when the key is pressed and released within the window.

void key(unsigned char k, int xx, int yy)
{
	kbCode=k;
	switch(k)
	{
		case 27:
		  exit(0);
		break;

		case 'c':
			theta[2] += 2.0; // roll right on c
		break;

		case 'z':
			theta[2] -= 2.0; // roll left on z
		break;
		case 's':
			theta[0] += 2.0; // pitch down on s
			updownspeed -= 1;
		break;

		case 'x':
			theta[0] -= 2.0; // pitch up on X
			updownspeed += 1;
		break;

		case 'r':
			reset();
		break;

		case 'q':
			exit(1);
		break;

		case '+':
			if (propvar < 2) propvar = 2; // sets propvar to 2 from 0
			propvar +=10; // adds 10

		break;

		case '-':
			propvar -=10; // decrease speed by 10
			if (propvar < 0){propvar = 0; 	reset();   }
		break;

		case '1':
			floatcamera = 0;
			insidecamera = 0;
			eyex = 0.0;
			eyey = 110.0;
			eyez = -550.0;
			atx = 0.0;
			aty = 0.0;
			atz = 0.0;
			upx = 0.0;
			upy = 1.0;
			upz = 0.0;

		break;

		case '2':
			floatcamera = 0;
			insidecamera = 0;
			eyex = 500.0;
			eyey = 1.0;
			eyez = 0.0;
			atx = 0.0;
			aty = 0.0;
			atz = 0.0;
			upy = 1.0;
			upz = 0.0;

		break;

		case '3':
			floatcamera = 0;
			insidecamera = 0;
			eyex = -500.0;
			eyey = 1.0;
			eyez = 0.0;
			atx = 0.0;
			aty = 0.0;
			atz = 0.0;
			upy = 1.0;
			upz = 0.0;

		break;

		case '4':
			insidecamera = 1;
			eyex = 0.0;
			eyey = 22.0;
			eyez = 35.0;
			atx = 0.0;
			aty = 22.0;
			atz = 100.0;
			upy = 1.0;
			upz = 0.0;

		break;

		case '5':
			floatcamera = 1;
		break;

		case '6':
			floatcamera = 0;
			insidecamera = 0;
			eyex = 0.0;
			eyey = 500.0;
			eyez = -650.0;
			atx = 0.0;
			aty = 0.0;
			atz = 0.0;
			upy = 1.0;
			upz = 0.0;

		break;

		case 'j':
		   eyex -= 10;
		break;
		case 'l':
			eyex += 10;
		break;
		case 'i':
			eyey += 10;
		break;
		case 'k':
			eyey -= 10;
		break;
		case 'u':
			eyez += 10;
		break;

		case 'n':
			atx += 10;
		break;

		case 'm':
			atx -= 10;
		break;


	}//switch

	// checks to see if the plane has exceeded maximum pitch or roll
	if( theta[0] > 360.0 ) theta[0] -= 360.0;
	if( theta[0] < -360.0 ) theta[0] += 360.0;
	if( theta[2] > 360.0 ) theta[2] -= 360.0;
	if( theta[2] < -360.0 ) theta[2] += 360.0;

	glutPostRedisplay();
} 

The mouse controls are:

MOUSE MOVE RIGHT roll right
MOUSE MOVE LEFT roll left
MOUSE MOVE UP pitch down
MOUSE MOVE DOWN pitch up

The motion() Function

The motion() function is a callback for when the mouse moves within the window while no mouse buttons are pressed.

int mouseX, mouseY;		// mouse coordinates
int oldMouseX, oldMouseY;
void motion(int x, int y)
{
	oldMouseX = mouseX;
	oldMouseY = mouseY;
	mouseX = x;
	mouseY = y;

  if (speed>0)
  {
		if ((mouseX - oldMouseX) > 0) theta[2] += 4.0;	// mouse moved 
								// to the right
		else if ((mouseX - oldMouseX) < 0) theta[2] -= 4.0;	// mouse moved 
								// to the left
		if ((mouseY - oldMouseY) < 0)
		{
			theta[0] += 3.0; // pitch down on s
			updownspeed -= 1;

		}
		else if ((mouseY - oldMouseY) > 0) 	// mouse moved to the left
		{
			theta[0] -= 3.0; // pitch up on X
			updownspeed += 1;
		}
     }
} 

This is only a skeleton for a Cessna Skyhawk simulation, but can be developed into a fullfledged flight simulator.