Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / WinMobile

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

4.07/5 (11 votes)
30 May 2010CPOL2 min read 39.9K   2.4K  
A Cessna Skyhawk Skeleton for further development in OpenGL (GLUT) using a Win32 Console
Image 1

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.cppThe main file
cessna.hThe cessna header file
draw.hThe draw header file
glm.hThe glm.h header file
globals.hThe globals header file
input.hThe input header file
materials.hThe materials header file
polygons.hThe polygons header file
render.hThe render header file
splash.hThe splash header file
vertices.hThe vertices input header file

The Controls

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

RIGHT KEY or Croll right
LEFT KEY or Zroll left
UP KEY or Spitch down
DOWN KEY or Xpitch up
1External view from behind, follow aircraft
2External view from left side
3External view from right side
4Internal 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.

C++
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 LEFTroll left
MOUSE MOVE UPpitch down
MOUSE MOVE DOWNpitch up

The motion() Function

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

C++
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)