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

📄 f16.c

📁 F16的pengl程序
💻 C
📖 第 1 页 / 共 2 页
字号:

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

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

    glEndList();

    gluDeleteQuadric(quadric);
    gluDeleteTess(tess);
    }


/*
 * 'glu_vertex()' - Set a vertex.
 */

void APIENTRY
glu_vertex(GLdouble *xyz)  /* I - XYZ location + ST texture coordinate */
    {
    glTexCoord2dv(xyz + 3);
    glVertex3dv(xyz);
    }


/*
 * 'GetClock()' - Return an increasing clock time in seconds...
 */

double /* O - Time in seconds */
GetClock(void)
    {
#ifdef WIN32
    SYSTEMTIME t;      /* Current time of day */

    GetSystemTime(&t);
    return (((t.wHour * 60.0) + t.wMinute) * 60 + t.wSecond +
            t.wMilliseconds * 0.001);
#else /* UNIX */
    struct timeval t; /* Current time of day */

    gettimeofday(&t, NULL);
    return ((double)t.tv_sec + 0.001 * (double)t.tv_usec);
#endif /* WIN32 */
    }


/*
 * 'Idle()' - Move the viewer and redraw...
 */

void
Idle(void)
    {
    int     i, j;         /* Column and row in terrain */
    GLfloat movex, movey; /* Scaled mouse movement */
    double  curtime;      /* Current time in milliseconds */
    GLfloat distance;     /* Distance to move */

    /* Get the current system time to figure out how far to move. */
    curtime  = GetClock();
    distance = curtime - LastTime;
    LastTime = curtime;

    /*
     * See how far the mouse pointer is from the 'center' (click)
     * position.
     */

    movex = distance * 0.2 * (MouseX - MouseStartX);
    movey = distance * 0.2 * (MouseY - MouseStartY);

    /*
     * Adjust roll, pitch, and heading according to the current
     * mouse inputs and orientation.
     */

    Orientation[0] += movey * cos(Orientation[2] * M_PI / 180.0);
    Orientation[1] += movey * sin(Orientation[2] * M_PI / 180.0);
    Orientation[2] += movex;

    if (Orientation[0] < -90.0)
        {
        Orientation[0] = -180.0 - Orientation[0];
	Orientation[2] += 180.0;
	}
    else if (Orientation[0] > 90.0)
        {
        Orientation[0] = 180.0 - Orientation[0];
	Orientation[2] -= 180.0;
	}

    if (Orientation[1] < 0.0)
        Orientation[1] += 360.0;
    else if (Orientation[1] > 360.0)
        Orientation[1] -= 360.0;

    if (Orientation[2] < -180.0)
        Orientation[2] += 360.0;
    else if (Orientation[2] > 180.0)
        Orientation[2] -= 360.0;

    glutPostRedisplay();
    }


/*
 * 'Joystick6()' - Handle joystick movement.
 */

void
Joystick(unsigned state,       /* I - Button state */
         int      x,           /* I - X position (-1000 to 1000) */
 	 int      y,           /* I - Y position (-1000 to 1000) */
	 int      z)           /* I - Z position (-1000 to 1000) */
    {
    static int last_state = 0; /* Last button state */

    if (last_state != state)
        {
	/* Button changed state; see what the new state is... */
        MouseStartX = MouseX = x / 2;
	MouseStartY = MouseY = y / 2;

	if (state && !last_state)
            {
    	    /* Start flying */
	    LastTime    = GetClock();
            glutIdleFunc(Idle);
	    }
            else if (!state && last_state)
            {
	    /* Stop flying */
            glutIdleFunc((void (*)(void))0);
	    glutPostRedisplay();
	    }

	last_state = state;
	}
    else if (state)
        {
        /* Update the joystick/mouse position */
	MouseX = x / 2;
        MouseY = y / 2;
	}
    }


/*
 * 'Keyboard()' - Handle key presses...
 */

void
Keyboard(unsigned char key, /* I - Key that was pressed */
         int           x,   /* I - Mouse X position */
	 int           y)   /* I - Mouse Y position */
    {
    switch (key)
        {
	case 0x1b :
	    exit(0);
	    break;
	case 't' :
	    UseTexturing = !UseTexturing;
	    break;
	}

    glutPostRedisplay();
    }


/*
 * 'Motion()' - Handle mouse pointer motion.
 */

void
Motion(int x, /* I - Current mouse X position */
       int y) /* I - Current mouse Y position */
    {
    MouseX = x;
    MouseY = y;
    }


/*
 * 'Mouse()' - Handle mouse button events.
 */

void
Mouse(int button, /* I - Button that changed */
      int state,  /* I - Current button states */
      int x,      /* I - Current mouse X position */
      int y)      /* I - Current mouse Y position */
    {
    MouseStartX = MouseX = x;
    MouseStartY = MouseY = y;

    if (state == GLUT_DOWN)
        {
	/* Start flying */
	LastTime    = GetClock();
        glutIdleFunc(Idle);
	}
    else
        {
	/* Stop flying */
        glutIdleFunc((void (*)(void))0);
	glutPostRedisplay();
	}
    }


/*
 * 'Redraw()' - Redraw the window...
 */

void
Redraw(void)
    {
    GLfloat        pitch, roll; /* Pitch and roll control values */
    static GLfloat sunpos[4] = { 0.7071, 0.7071, 0.0, 0.0 };
    static GLfloat suncolor[4] = { 0.5, 0.5, 0.4, 1.0 };
    static GLfloat sunambient[4] = { 0.5, 0.5, 0.4, 1.0 };

    /* Clear the window to light blue... */
    glClearColor(0.75, 0.75, 1.0, 1.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    /* Setup viewing transformations for the current orientation... */
    glPushMatrix();
    glTranslatef(0.0, 0.0, -15.0);
    glRotatef(Orientation[1], 0.0, -1.0, 0.0);
    glRotatef(Orientation[0], 1.0, 0.0, 0.0);
    glRotatef(Orientation[2], 0.0, 0.0, -1.0);

    /* Setup lighting if needed... */
    glEnable(GL_LIGHTING);
    glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, sunambient);

    glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT);
    glEnable(GL_COLOR_MATERIAL);

    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, sunpos);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, suncolor);
    glLightfv(GL_LIGHT0, GL_AMBIENT, sunambient);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    if (UseTexturing)
	glEnable(GL_TEXTURE_2D);
    else
        glDisable(GL_TEXTURE_2D);

    // Draw the main body
    glCallList(F16Body);

    // Draw the rollerons...
    pitch = 0.1 * (MouseY - MouseStartY);
    roll  = 0.1 * (MouseX - MouseStartX);

    if (UseTexturing)
	glEnable(GL_TEXTURE_2D);

    // Left rolleron
    glPushMatrix();
        glTranslatef(0.0, 0.0, 2.0);
	glRotatef(roll - pitch, 1.0, 0.0, 0.0);
	glTranslatef(0.0, 0.0, -2.0);
	glCallList(F16Rolleron[0]);
    glPopMatrix();

    // Right rolleron
    glPushMatrix();
        glTranslatef(0.0, 0.0, 2.0);
	glRotatef(roll + pitch, -1.0, 0.0, 0.0);
	glTranslatef(0.0, 0.0, -2.0);
	glCallList(F16Rolleron[1]);
    glPopMatrix();

    glPopMatrix();

    /* Finish up */
    glutSwapBuffers();
    }


/*
 * '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;

    /* Reset the viewport... */
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(22.5, (float)width / (float)height, 0.1, 20.0);
    glMatrixMode(GL_MODELVIEW);
    }

⌨️ 快捷键说明

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