flightsim.c
来自「飞机飞行界面」· C语言 代码 · 共 1,162 行 · 第 1/3 页
C
1,162 行
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
for (i = 0, tp = Terrain[0]; i < (TERRAIN_COUNT - 1); i ++)
{
glBegin(GL_TRIANGLE_STRIP);
for (j = 0; j < TERRAIN_COUNT; j ++, tp ++)
{
glNormal3fv(tp[0].n);
glVertex3fv(tp[0].v);
glNormal3fv(tp[TERRAIN_COUNT].n);
glVertex3fv(tp[TERRAIN_COUNT].v);
}
glEnd();
}
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
}
else
{
glBegin(GL_QUADS);
glNormal3f(0.0, 1.0, 0.0);
glVertex3f(-0.5 * TERRAIN_SIZE, 0.0, -0.5 * TERRAIN_SIZE);
glVertex3f( 0.5 * TERRAIN_SIZE, 0.0, -0.5 * TERRAIN_SIZE);
glVertex3f( 0.5 * TERRAIN_SIZE, 0.0, 0.5 * TERRAIN_SIZE);
glVertex3f(-0.5 * TERRAIN_SIZE, 0.0, 0.5 * TERRAIN_SIZE);
glEnd();
}
glDisable(GL_TEXTURE_GEN_S);
glDisable(GL_TEXTURE_GEN_T);
if (ShowWater && ShowTerrain)
{
glEnable(GL_DEPTH_TEST);
glColor4f(0.0, 0.0, 0.25, 0.75);
for (i = 0; i < (TERRAIN_COUNT - 1); i ++)
{
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0.0, 1.0, 0.0);
for (j = 0; j < TERRAIN_COUNT; j ++)
{
glTexCoord2i(i, j);
glVertex3f(i * TERRAIN_SPACING - 0.5 * TERRAIN_SIZE, 0.0,
0.5 * TERRAIN_SIZE - j * TERRAIN_SPACING);
glTexCoord2i(i + 1, j);
glVertex3f((i + 1) * TERRAIN_SPACING - 0.5 * TERRAIN_SIZE, 0.0,
0.5 * TERRAIN_SIZE - j * TERRAIN_SPACING);
}
glEnd();
}
glDisable(GL_DEPTH_TEST);
}
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
/* Figure out the sizes of the controls... */
isize = Height / 4;
if ((19 * isize / 4) > Width)
isize = Width / 5;
ix = (Width - 19 * isize / 4) / 2;
iy = (Height / 2 - 3 * isize / 2) / 2;
/* Redraw the FPS and instruments... */
glViewport(0, 0, Width, Height);
glDisable(GL_SCISSOR_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)Width, 0.0f, (float)Height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
/* Show the cockpit outline */
glColor3f(0.2f, 0.2f, 0.21f);
glBegin(GL_TRIANGLE_FAN);
glVertex2i(Width / 2, 3 * Height / 8);
for (theta = 0.0; theta < (M_PI + M_PI / 20.0); theta += M_PI / 20.0)
glVertex2f(Width / 2 - Width / 2 * cos(theta),
3 * Height / 8 + Height / 8 * sin(theta));
glEnd();
/* Show the frames-per-second in the upper window... */
glColor3f(1.0f, 1.0f, 1.0f);
glRasterPos2i(Width / 2, 7 * Height / 16);
FontPrintf(TextFont, 0, "FPS = %d", FPS);
if (RedrawAll)
{
glColor3f(0.2f, 0.2f, 0.21f);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(Width, 0);
glVertex2i(Width, 3 * Height / 8);
glVertex2i(0, 3 * Height / 8);
glEnd();
}
if (RedrawAirspeed || RedrawAll)
{
/*
* Redraw the airspeed indicator...
*/
Airspeed = Velocity;
draw_instrument(ix, iy, isize);
glColor3f(0.0f, 0.0f, 0.0f);
glRasterPos2i(ix + isize * 0.5f, iy + isize * 0.3f);
FontPrintf(TextFont, 0, "KNOTS");
glBegin(GL_LINES);
for (i = 0; i < 200; i += 10)
{
theta = i * M_PI * 2.0f / 200.0f;
st = sin(theta);
ct = cos(theta);
glVertex2f(ix + isize * 0.5 + st * 0.3f * isize,
iy + isize * 0.5f + ct * 0.3f * isize);
glVertex2f(ix + isize * 0.5 + st * 0.4f * isize,
iy + isize * 0.5f + ct * 0.4f * isize);
}
glEnd();
glRasterPos2f(ix + isize * 0.5f, iy + isize * 0.2f + 2);
FontPrintf(TextFont, 0, "100");
glRasterPos2f(ix + isize * 0.2f, iy + isize * 0.5f - 4);
FontPrintf(TextFont, 1, "150");
glRasterPos2f(ix + isize * 0.5f, iy + isize * 0.8f - 10);
FontPrintf(TextFont, 0, "0");
glRasterPos2f(ix + isize * 0.8f, iy + isize * 0.5f - 4);
FontPrintf(TextFont, -1, "50");
glPushMatrix();
glTranslatef(ix + isize * 0.5f, iy + isize * 0.5f, 0.0);
glRotatef((Airspeed * 3600.0f / 1852.0f) * 360.0f / 200.0f,
0.0f, 0.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(-0.05f * isize, 0.0f);
glVertex2f( 0.05f * isize, 0.0f);
glVertex2f( 0.0f, 0.35f * isize);
glEnd();
glPopMatrix();
}
ix += 5 * isize / 4;
iy += isize / 4;
if (RedrawAltimeter || RedrawAll)
{
/*
* Redraw the altimeter...
*/
Altimeter = Position[1];
draw_instrument(ix, iy, isize);
glColor3f(0.0f, 0.0f, 0.0f);
glRasterPos2i(ix + isize * 0.5f, iy + isize * 0.5f);
glBitmap(0, 0, 0, 0, 0, -5, NULL);
FontPrintf(TextFont, 0, "%05d FT", (int)(Altimeter * 3.28));
glBegin(GL_LINES);
for (i = 0; i < 100; i += 10)
{
theta = i * M_PI * 2.0f / 100.0f;
st = sin(theta);
ct = cos(theta);
glVertex2f(ix + isize * 0.5 + st * 0.3f * isize,
iy + isize * 0.5f + ct * 0.3f * isize);
glVertex2f(ix + isize * 0.5 + st * 0.4f * isize,
iy + isize * 0.5f + ct * 0.4f * isize);
}
glEnd();
glRasterPos2f(ix + isize * 0.5f, iy + isize * 0.8f - 10);
FontPrintf(TextFont, 0, "0");
glPushMatrix();
glTranslatef(ix + isize * 0.5f, iy + isize * 0.5f, 0.0);
glRotatef(fmod(Altimeter * 0.0328, 1.0) * 360.0f,
0.0f, 0.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(-0.05f * isize, 0.0f);
glVertex2f( 0.05f * isize, 0.0f);
glVertex2f( 0.0f, 0.35f * isize);
glEnd();
glPopMatrix();
}
ix += 5 * isize / 4;
if (RedrawHorizon || RedrawAll)
{
/*
* Redraw the artificial horizon; we draw a 2D horizon which isn't
* perfect but illustrates the point...
*/
float start, end; /* Start and end angles of horizon */
Horizon[0] = Orientation[0];
Horizon[1] = Orientation[2];
draw_instrument(ix, iy, isize);
start = M_PI * (90.0f + Horizon[0] - Horizon[1]) / 180.0f;
end = M_PI * (270.0f - Horizon[0] - Horizon[1]) / 180.0f;
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
for (theta = start; theta < end; theta += M_PI / 18)
glVertex2f(ix + isize * 0.5f + sin(theta) * isize * 0.4f,
iy + isize * 0.5f + cos(theta) * isize * 0.4f);
glVertex2f(ix + isize * 0.5f + sin(end) * isize * 0.4f,
iy + isize * 0.5f + cos(end) * isize * 0.4f);
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(ix + 0.2f * isize, iy + 0.5f * isize);
glVertex2f(ix + 0.4f * isize, iy + 0.5f * isize);
glVertex2f(ix + 0.6f * isize, iy + 0.5f * isize);
glVertex2f(ix + 0.8f * isize, iy + 0.5f * isize);
glEnd();
}
ix += 5 * isize / 4;
iy -= isize / 4;
if (RedrawCompass || RedrawAll)
{
/*
* Redraw the compass...
*/
Compass = Orientation[1];
draw_instrument(ix, iy, isize);
glPushMatrix();
glTranslatef(ix + isize * 0.5f, iy + isize * 0.5f, 0.0);
glRotatef(Compass, 0.0f, 0.0f, 1.0f);
glColor3f(0.0f, 0.0f, 0.0f);
glRasterPos2f(0.0f, isize * 0.3f);
glBitmap(0, 0, 0, 0, 0, -5, NULL);
FontPrintf(TextFont, 0, "N");
glRasterPos2f(0.0f, -isize * 0.3f);
glBitmap(0, 0, 0, 0, 0, -5, NULL);
FontPrintf(TextFont, 0, "S");
glRasterPos2f(isize * 0.3f, 0.0f);
glBitmap(0, 0, 0, 0, 0, -5, NULL);
FontPrintf(TextFont, 0, "E");
glRasterPos2f(-isize * 0.3f, 0.0f);
glBitmap(0, 0, 0, 0, 0, -5, NULL);
FontPrintf(TextFont, 0, "W");
glBegin(GL_LINES);
for (i = 0; i < 360; i += 10)
if (i % 90)
{
theta = i * M_PI / 180.0f;
st = sin(theta);
ct = cos(theta);
glVertex2f(st * 0.3f * isize, ct * 0.3f * isize);
glVertex2f(st * 0.4f * isize, ct * 0.4f * isize);
}
glEnd();
glRotatef(Compass, 0.0f, 0.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glVertex2f(0.0f, 0.3f * isize);
glVertex2f(-0.05f * isize, 0.1f * isize);
glVertex2f(0.05f * isize, 0.1f * isize);
glEnd();
glRasterPos2i(0, 0);
FontPrintf(TextFont, 0, "%03d", (int)Compass);
glPopMatrix();
}
ix -= 15 * isize / 4;
if (!RedrawAll && UseSwapHint)
{
/* Tell windows just to swap the top */
(*glAddSwapHintRectWIN)(0, 3 * Height / 8, Width,
Height - 3 * Height / 8);
/* and any instruments we've changed... */
if (RedrawAirspeed)
(*glAddSwapHintRectWIN)(ix, iy, isize, isize);
if (RedrawAltimeter)
(*glAddSwapHintRectWIN)(ix + 5 * isize / 4, iy + isize / 4, isize, isize);
if (RedrawHorizon)
(*glAddSwapHintRectWIN)(ix + 10 * isize / 4, iy + isize / 4, isize, isize);
if (RedrawCompass)
(*glAddSwapHintRectWIN)(ix + 15 * isize / 4, iy, isize, isize);
}
/* Finish up */
glutSwapBuffers();
RedrawAll = 0;
RedrawAirspeed = 0;
RedrawAltimeter = 0;
RedrawCompass = 0;
RedrawHorizon = 0;
}
/*
* 'Resize()' - Resize the window...
*/
void
Resize(int width, /* I - Width of window */
int height) /* I - Height of window */
{
/* Save the new width and height */
Width = width;
Height = height;
/* Force the whole window to be redrawn... */
RedrawAll = 1;
}
/*
* 'Special()' - Handle special keys like left, right, up, and down.
*/
void
Special(int key, /* I - Key that was pressed */
int x, /* I - X location of the mouse pointer */
int y) /* I - Y location of the mouse pointer */
{
switch (key)
{
case GLUT_KEY_UP :
ViewAngle = 0;
glutPostRedisplay();
break;
case GLUT_KEY_DOWN :
ViewAngle = 180;
glutPostRedisplay();
break;
case GLUT_KEY_LEFT :
ViewAngle = (ViewAngle + 315) % 360;
glutPostRedisplay();
break;
case GLUT_KEY_RIGHT :
ViewAngle = (ViewAngle + 45) % 360;
glutPostRedisplay();
break;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?