OpenGLEAN
OpenGLEAN User Support
Home page | Introduction | Documentation | Files | Examples | Proposals | Authors | Links


OpenGLEAN Development
Summary | CVS | (discussion via home page) | (contact via home page) | (suggestion box) | License | Todo | Bugs

progs/demos/joystick/joystick.c File Reference

Include dependency graph for progs/demos/joystick/joystick.c:


Detailed Description

This program is a joystick test program.

Author:
Copyright 2004-2005, the OpenGLEAN Project.

Copyright (C) 2004, the OpenGLUT project contributors.

openglean_joystick.png

OpenGLEAN Joystick Demonstration

#include <GL/openglean.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>

#define MAX_AXES 3

static float joystickAxis [MAX_AXES];
static int   joystickButtons;

/* GLUT callback Handlers */

static void resize (int width, int height)
{
    const double ar = width * 1.0 / height;
    const double sf = .8;

    glViewport (0, 0, width, height);

    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    if (ar > 1)
        glFrustum (-ar * sf, ar * sf, -sf, sf, 2, 8);
    else
        glFrustum (-sf, sf, -sf / ar, sf / ar, 2, 8);

    glMatrixMode (GL_MODELVIEW);
    glLoadIdentity ();
}

static void drawButton (int button, int down)
{
    glPushMatrix ();
    glTranslated (-.9 + .2 * button, .05, .8);
    glScaled (.16, .6, .16);
    if (down)
        glColor3d (1, 0, 0);
    else
        glColor3d (.4, .3, .3);
    glutSolidCube (1);
    glPopMatrix ();
}

static void display (void)
{
    glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glEnable (GL_LIGHTING);

    glPushMatrix ();
    glTranslated (0, -0.5, -4.5);
    glRotated (30, 1, 0, 0);

    glPushMatrix ();
    glScaled (2, .5, 2);
    glColor3d (.6, .6, .5);
    glutSolidCube (1);
    glPopMatrix ();

    glPushMatrix ();
    glRotated (-35 * joystickAxis [0], 0, 0, 1);
    glRotated ( 35 * joystickAxis [1], 1, 0, 0);
    glTranslated (0, 1, 0);
    glScaled (.5, 2, .5);
    if ((abs (joystickAxis [0]) < .001) || (abs (joystickAxis [1]) < .001))
        glColor3d (1, 0, 0);
    else
        glColor3d (.3, .3, .2);
    glutSolidCube (1);
    glPopMatrix ();

    glPushMatrix ();
    glTranslated (.9 * joystickAxis [2], 0, 1.1);
    glScaled (.2, .5, .2);
    glColor3d (.5, .9, .5);
    glutSolidCube (1);
    glPopMatrix ();

    drawButton (0, joystickButtons & 0x0001);
    drawButton (1, joystickButtons & 0x0002);
    drawButton (2, joystickButtons & 0x0004);
    drawButton (3, joystickButtons & 0x0008);
    drawButton (4, joystickButtons & 0x0010);
    drawButton (5, joystickButtons & 0x0020);
    drawButton (6, joystickButtons & 0x0040);
    drawButton (7, joystickButtons & 0x0080);
    drawButton (8, joystickButtons & 0x0100);
    drawButton (9, joystickButtons & 0x0200);

    glPopMatrix ();

    glutSwapBuffers ();
}

static void joystick (unsigned int buttons, int xaxis, int yaxis, int zaxis)
{
    joystickAxis [0] = xaxis / 1000.0;
    joystickAxis [1] = yaxis / 1000.0;
    joystickAxis [2] = zaxis / 1000.0;

    joystickButtons = buttons;

    glutPostRedisplay ();
}

static void key (unsigned char key, int x, int y)
{
    (void) x;                        /* XXX Make GCC happy */
    (void) y;
    switch (key)
    {
    case 27:
    case 'Q': case 'q':
        exit (0);

    default:
        break;
    }

    glutPostRedisplay ();
}

const GLfloat light_ambient [] = {0, 0, 0, 1};
const GLfloat light_diffuse [] = {1, 1, 1, 1};
const GLfloat light_specular [] = {1, 1, 1, 1};
const GLfloat light_position [] = {2, 5, 5, 0};

const GLfloat mat_ambient [] = {.7, .7, .7, 1};
const GLfloat mat_diffuse [] = {.8, .8, .8, 1};
const GLfloat mat_specular [] = {1, 1, 1, 1};
const GLfloat high_shininess [] = {100};

/* Program entry point */

int main (int argc, char **argv)
{
    int i;

    glutInitWindowSize (640, 480);
    glutInitWindowPosition (40, 40);
    glutInit (&argc, argv);
    glutInitDisplayMode (GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow ("OpenGLEAN Joystick");

    glutReshapeFunc (resize);
    glutDisplayFunc (display);
    glutJoystickFunc (joystick, 50);
    glutKeyboardFunc (key);

    glClearColor (1, 1, 1, 1);
    glEnable (GL_CULL_FACE);
    glCullFace (GL_BACK);

    glEnable (GL_DEPTH_TEST);
    glDepthFunc (GL_LESS);

    glEnable (GL_LIGHT0);
    glEnable (GL_NORMALIZE);
    glEnable (GL_COLOR_MATERIAL);

    glLightfv (GL_LIGHT0, GL_AMBIENT, light_ambient);
    glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse);
    glLightfv (GL_LIGHT0, GL_SPECULAR, light_specular);
    glLightfv (GL_LIGHT0, GL_POSITION, light_position);

    glMaterialfv (GL_FRONT, GL_AMBIENT, mat_ambient);
    glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
    glMaterialfv (GL_FRONT, GL_SPECULAR, mat_specular);
    glMaterialfv (GL_FRONT, GL_SHININESS, high_shininess);

    if (!glutDeviceGet (GLUT_HAS_JOYSTICK))
        printf ("Joystick not detected.\n");
    else
    {
        printf ("Joystick detected.\n");
        printf ("%d axes.\n", glutDeviceGet (GLUT_JOYSTICK_AXES));
        printf ("%d buttons.\n", glutDeviceGet (GLUT_JOYSTICK_BUTTONS));
        printf ("Poll rate: %d.\n", glutDeviceGet (GLUT_JOYSTICK_POLL_RATE));
        printf (
            "Joystick %savailable.\n",
            glutDeviceGet (GLUT_OWNS_JOYSTICK) ? "" : "un"
        );
    }

    for (i = 0; i < MAX_AXES; ++i)
        joystickAxis [i] = 0;

    joystickButtons = 0;

    glutMainLoop ();

    return EXIT_SUCCESS;
}




SourceForge.net Logo Supported in part by SourceForge.net.

Generated on Fri Sep 16 20:15:26 2005 for OpenGLEAN by doxygen 1.4.3
The OpenGLEAN project is hosted by olib.org and SourceForge.