⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f16.c

📁 F16的pengl程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 * F-16 program from Chapter 12.
 *
 * Written by Michael Sweet
 */

#include <GL/glut.h>
#include <math.h>
#ifndef M_PI
#  define M_PI	(double)3.14159265358979323846
#endif /* !M_PI */
#include "texture.h"


/*
 * Globals...
 */

int        Width;             /* Width of window */
int        Height;            /* Height of window */
GLuint     F16Body,           /* F-16 body */
           F16Rolleron[2];    /* F-16 rollerons */
GLuint     F16Texture[2];     /* Camoflage texture objects */
double     LastTime;          /* Last update time */
int	   MouseStartX;       /* Initial mouse X position */
int        MouseStartY;       /* Initial mouse Y position */
int	   MouseX;            /* Mouse X position */
int        MouseY;            /* Mouse Y position */
int        UseTexturing = 1;  /* Use texture mapping? */
GLfloat	   Orientation[3] =   /* Orientation of viewer */
    {
    15.0, 45.0, 30.0
    };


/*
 * Functions...
 */

void   BuildF16(void);
void   APIENTRY glu_vertex(GLdouble *xyz);
double GetClock(void);
void   Idle(void);
void   Keyboard(unsigned char key, int x, int y);
void   Joystick(unsigned state, int x, int y, int z);
void   Motion(int x, int y);
void   Mouse(int button, int state, int x, int y);
void   Redraw(void);
void   Resize(int width, int height);


/*
 * 'main()' - Open a window and display a sphere and cube.
 */

int                /* O - Exit status */
main(int  argc,    /* I - Number of command-line arguments */
     char *argv[]) /* I - Command-line arguments */
    {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(792, 573);
    glutCreateWindow("F-16 Using Quadrics and GLU Polygons");

    glutDisplayFunc(Redraw);
    glutKeyboardFunc(Keyboard);
    glutMouseFunc(Mouse);
    glutMotionFunc(Motion);
    glutReshapeFunc(Resize);
    if (glutDeviceGet(GLUT_HAS_JOYSTICK))
        glutJoystickFunc(Joystick, 100);

    BuildF16();

    glutMainLoop();
    return (0);
    }


/*
 * 'BuildF16()' - Build the F-16 model.
 */

void
BuildF16(void)
    {
    int                i;           /* Looping var */
    GLUquadric         *quadric;    /* Quadric object */
#ifdef GLU_VERSION_1_2
    GLUtesselator      *tess;       /* Tesselator object */
#else
    GLUtriangulatorObj *tess;
#endif /* GLU_VERSION_1_2 */
    static GLdouble    wing[][5] =  /* Main wing points */
        {
	/* { x, y, z, s, t } */
	{  0.25, 0.0, -1.0,  0.125, 0.0 },
	{  0.45, 0.0,  0.0,  0.25,  0.4 },
	{  1.65, 0.0,  0.8,  1.0,   0.8 },
	{  1.65, 0.0,  1.2,  1.0,   1.0 },
	{  0.35, 0.0,  1.2,  0.15,  1.0 },
	{  0.35, 0.0,  2.4,  0.15,  2.0 },
	{  0.25, 0.0,  2.4,  0.125, 2.0 },
	{  0.25, 0.0,  2.0,  0.125, 1.5 },
	{ -0.25, 0.0,  2.0, -0.125, 1.5 },
	{ -0.25, 0.0,  2.4, -0.125, 2.0 },
	{ -0.35, 0.0,  2.4, -0.15,  2.0 },
	{ -0.35, 0.0,  1.2, -0.15,  1.0 },
	{ -1.65, 0.0,  1.2, -1.0,   1.0 },
	{ -1.65, 0.0,  0.8, -1.0,   0.8 },
	{ -0.45, 0.0,  0.0, -0.25,  0.4 },
	{ -0.25, 0.0, -1.0, -0.125, 0.0 }
        };
    static GLdouble    tail[][5] =      /* Tail points */
        {
	/* { x, y, z, s, t } */
	{ 0.0, 0.24, 0.5, 1.5, 0.0 },
	{ 0.0, 0.4,  1.1, 1.2, 0.1 },
	{ 0.0, 1.0,  2.0, 0.4, 1.0 },
	{ 0.0, 1.0,  2.4, 0.05, 1.0 },
	{ 0.0, 0.4,  2.1, 0.2, 0.1 },
	{ 0.0, 0.24, 2.1, 0.2, 0.0 }
	};
    static GLdouble    left_fin[][5] =  /* Left fin points */
        {
	/* { x, y, z, s, t } */
	{ -0.1,  -0.1, 1.1, 0.0, 0.0 },
	{ -0.25, -0.3, 1.2, 1.0, 0.0 },
	{ -0.25, -0.3, 1.5, 1.0, 1.0 },
	{ -0.1,  -0.1, 1.5, 1.0, 0.0 }
	};
    static GLdouble    right_fin[][5] = /* Right fin points */
        {
	/* { x, y, z, s, t } */
	{ 0.1,  -0.1, 1.1, 0.0, 0.0 },
	{ 0.25, -0.3, 1.2, 1.0, 0.0 },
	{ 0.25, -0.3, 1.5, 1.0, 1.0 },
	{ 0.1,  -0.1, 1.5, 1.0, 0.0 }
	};
    static GLdouble    left_rolleron[][5] =  /* Left rolleron points */
        {
	/* { x, y, z, s, t } */
	{ -0.35, 0.0, 1.6, 0.0, 0.0 },
	{ -0.85, 0.0, 2.1, 1.0, 0.5 },
	{ -0.85, 0.0, 2.4, 1.0, 1.0 },
	{ -0.35, 0.0, 2.4, 0.0, 1.0 }
	};
    static GLdouble    right_rolleron[][5] = /* Right rolleron points */
        {
	/* { x, y, z, s, t } */
	{ 0.35, 0.0, 1.6, 0.0, 0.0 },
	{ 0.85, 0.0, 2.1, 1.0, 0.5 },
	{ 0.85, 0.0, 2.4, 1.0, 1.0 },
	{ 0.35, 0.0, 2.4, 0.0, 1.0 }
	};

    /* Load the texture images */
    F16Texture[0] = TextureLoad("camoflage.bmp", GL_FALSE, GL_NEAREST,
                                GL_NEAREST, GL_REPEAT);
    F16Texture[1] = TextureLoad("tail.bmp", GL_FALSE, GL_NEAREST,
                                GL_NEAREST, GL_REPEAT);

    /* Then build the F-16 body */
    F16Body = glGenLists(1);
    glNewList(F16Body, GL_COMPILE);
    glBindTexture(GL_TEXTURE_2D, F16Texture[0]);

    tess = gluNewTess();
    gluTessCallback(tess, GLU_TESS_BEGIN, glBegin);
    gluTessCallback(tess, GLU_TESS_END, glEnd);
    gluTessCallback(tess, GLU_TESS_VERTEX, (void (CALLBACK *)())glu_vertex);

    quadric = gluNewQuadric();
    gluQuadricTexture(quadric, GL_TRUE);

    /* Main fuselage */
    glColor3f(0.8, 0.8, 0.8);
    glPushMatrix();
        glTranslatef(0.0, 0.0, -1.5);
	gluCylinder(quadric, 0.25, 0.25, 3.5, 20, 2);
    glPopMatrix();

    /* Nose */
    glColor3f(0.8, 0.8, 0.8);
    glPushMatrix();
        glTranslatef(0.0, 0.0, -2.5);
	gluCylinder(quadric, 0.0, 0.25, 1.0, 20, 2);
    glPopMatrix();

    /* Main wing */
    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(0.0, 1.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

    for (i = 0; i < 16; i ++)
        gluTessVertex(tess, wing[i], wing[i]);

#ifdef GL_VERSION_1_2
    gluTessEndContour(tess);
#endif /* GL_VERSION_1_2 */
    gluTessEndPolygon(tess);

    /* Fins */
    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(-1.0, 0.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

    for (i = 0; i < 4; i ++)
        gluTessVertex(tess, left_fin[i], left_fin[i]);

#ifdef GL_VERSION_1_2
    gluTessEndContour(tess);
#endif /* GL_VERSION_1_2 */
    gluTessEndPolygon(tess);

    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(1.0, 0.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

    for (i = 0; i < 4; i ++)
        gluTessVertex(tess, right_fin[i], right_fin[i]);

#ifdef GL_VERSION_1_2
    gluTessEndContour(tess);
#endif /* GL_VERSION_1_2 */
    gluTessEndPolygon(tess);

    /* Tail */
    glBindTexture(GL_TEXTURE_2D, F16Texture[1]);
    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(1.0, 0.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

    for (i = 0; i < 6; i ++)
        gluTessVertex(tess, tail[i], tail[i]);

#ifdef GL_VERSION_1_2
    gluTessEndContour(tess);
#endif /* GL_VERSION_1_2 */
    gluTessEndPolygon(tess);

    /* Don't texture any more of the body... */
    glDisable(GL_TEXTURE_2D);

    /* Canopy */
    glPushMatrix();
        glColor4f(0.5, 0.5, 1.0, 0.75);
        glTranslatef(0.0, 0.2, -1.0);
	glScalef(1.0, 1.0, 0.65 / 0.15); 
	gluSphere(quadric, 0.15, 6, 12);
    glPopMatrix();

    /* Engine */
    glPushMatrix();
        /* Cowling */
        glColor3f(0.1, 0.1, 0.1);
        glTranslatef(0.0, 0.0, 2.0);
	gluCylinder(quadric, 0.25, 0.15, 0.5, 20, 2);
	gluDisk(quadric, 0.0, 0.25, 20, 2);

        /* Exhaust */
	glPushAttrib(GL_LIGHTING_BIT);
	glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
        glColor3f(0.5, 0.5, 1.0);
	gluCylinder(quadric, 0.2, 0.0, 0.3, 10, 2);
        glColor4f(0.25, 0.25, 1.0, 0.75);
	gluCylinder(quadric, 0.2, 0.1, 0.4, 10, 2);
	glPopAttrib();
    glPopMatrix();

    glEndList();

    /* Now the left rolleron */
    F16Rolleron[0] = glGenLists(1);
    glNewList(F16Rolleron[0], GL_COMPILE);

    glBindTexture(GL_TEXTURE_2D, F16Texture[0]);
    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(0.0, 1.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

    for (i = 0; i < 4; i ++)
        gluTessVertex(tess, left_rolleron[i], left_rolleron[i]);

#ifdef GL_VERSION_1_2
    gluTessEndContour(tess);
#endif /* GL_VERSION_1_2 */
    gluTessEndPolygon(tess);

    glEndList();

    /* And the right rolleron */
    F16Rolleron[1] = glGenLists(1);
    glNewList(F16Rolleron[1], GL_COMPILE);

    glBindTexture(GL_TEXTURE_2D, F16Texture[0]);
    glColor3f(0.8, 0.8, 0.8);
    glNormal3f(0.0, 1.0, 0.0);
    gluTessBeginPolygon(tess, NULL);
#ifdef GL_VERSION_1_2
    gluTessBeginContour(tess);
#endif /* GL_VERSION_1_2 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -