Click here to Skip to main content
Click here to Skip to main content

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

, 30 May 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .
 
I hold lectures in programming. I have also coached students in C++, Java and Visual basic.
 
In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

Comments and Discussions

 
GeneralMy vote of 1 Pinmembereds2-Jun-10 9:47 
GeneralRe: My vote of 1 PinmemberJulian Nicholls3-Jun-10 6:44 
GeneralNice Work PinmemberAli BaderEddin29-May-10 17:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid