2009-11-06 15:26:16 +02:00
|
|
|
/* Spaceball demo
|
|
|
|
*
|
|
|
|
* Written by John Tsiombikas <nuclear@member.fsf.org>
|
|
|
|
* (converted from the libspnav cube example)
|
|
|
|
*
|
|
|
|
* Use the spaceball to move and rotate the colored cube.
|
|
|
|
* Pressing any button will reset the cube at its original location.
|
|
|
|
*
|
|
|
|
* Press escape or q to exit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <GL/freeglut.h>
|
|
|
|
#include "vmath.h"
|
|
|
|
|
2009-11-11 03:21:41 +02:00
|
|
|
#ifndef M_PI
|
|
|
|
#define M_PI 3.14159265358979323846264338327950
|
|
|
|
#endif
|
|
|
|
|
2009-11-06 15:26:16 +02:00
|
|
|
void draw_cube(void);
|
|
|
|
|
|
|
|
/* callbacks */
|
|
|
|
void disp(void);
|
|
|
|
void reshape(int x, int y);
|
|
|
|
void keyb(unsigned char key, int x, int y);
|
2009-11-08 04:00:17 +02:00
|
|
|
void sbmot(int x, int y, int z); /* spaceball translation */
|
|
|
|
void sbrot(int x, int y, int z); /* spaceball rotation */
|
|
|
|
void sbbut(int bn, int state); /* spaceball button */
|
2009-11-06 15:26:16 +02:00
|
|
|
|
|
|
|
vec3_t pos = {0, 0, -6};
|
|
|
|
quat_t rot = {0, 0, 0, 1};
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
glutInit(&argc, argv);
|
|
|
|
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
|
|
|
glutCreateWindow("spaceball demo");
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glutDisplayFunc(disp);
|
|
|
|
glutReshapeFunc(reshape);
|
|
|
|
glutKeyboardFunc(keyb);
|
|
|
|
glutSpaceballMotionFunc(sbmot);
|
|
|
|
glutSpaceballRotateFunc(sbrot);
|
|
|
|
glutSpaceballButtonFunc(sbbut);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glEnable(GL_CULL_FACE);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glutMainLoop();
|
|
|
|
return 0;
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void disp(void)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
mat4_t xform;
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
quat_to_mat(xform, rot);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
glTranslatef(pos.x, pos.y, pos.z);
|
2009-11-11 03:21:41 +02:00
|
|
|
glMultMatrixf((float*)xform);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
draw_cube();
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glutSwapBuffers();
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void draw_cube(void)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
glBegin(GL_QUADS);
|
|
|
|
/* face +Z */
|
|
|
|
glNormal3f(0, 0, 1);
|
|
|
|
glColor3f(1, 0, 0);
|
|
|
|
glVertex3f(-1, -1, 1);
|
|
|
|
glVertex3f(1, -1, 1);
|
|
|
|
glVertex3f(1, 1, 1);
|
|
|
|
glVertex3f(-1, 1, 1);
|
|
|
|
/* face +X */
|
|
|
|
glNormal3f(1, 0, 0);
|
|
|
|
glColor3f(0, 1, 0);
|
|
|
|
glVertex3f(1, -1, 1);
|
|
|
|
glVertex3f(1, -1, -1);
|
|
|
|
glVertex3f(1, 1, -1);
|
|
|
|
glVertex3f(1, 1, 1);
|
|
|
|
/* face -Z */
|
|
|
|
glNormal3f(0, 0, -1);
|
|
|
|
glColor3f(0, 0, 1);
|
|
|
|
glVertex3f(1, -1, -1);
|
|
|
|
glVertex3f(-1, -1, -1);
|
|
|
|
glVertex3f(-1, 1, -1);
|
|
|
|
glVertex3f(1, 1, -1);
|
|
|
|
/* face -X */
|
|
|
|
glNormal3f(-1, 0, 0);
|
|
|
|
glColor3f(1, 1, 0);
|
|
|
|
glVertex3f(-1, -1, -1);
|
|
|
|
glVertex3f(-1, -1, 1);
|
|
|
|
glVertex3f(-1, 1, 1);
|
|
|
|
glVertex3f(-1, 1, -1);
|
|
|
|
/* face +Y */
|
|
|
|
glNormal3f(0, 1, 0);
|
|
|
|
glColor3f(0, 1, 1);
|
|
|
|
glVertex3f(-1, 1, 1);
|
|
|
|
glVertex3f(1, 1, 1);
|
|
|
|
glVertex3f(1, 1, -1);
|
|
|
|
glVertex3f(-1, 1, -1);
|
|
|
|
/* face -Y */
|
|
|
|
glNormal3f(0, -1, 0);
|
|
|
|
glColor3f(1, 0, 1);
|
|
|
|
glVertex3f(-1, -1, -1);
|
|
|
|
glVertex3f(1, -1, -1);
|
|
|
|
glVertex3f(1, -1, 1);
|
|
|
|
glVertex3f(-1, -1, 1);
|
|
|
|
glEnd();
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 45deg fov */
|
2009-11-08 04:00:17 +02:00
|
|
|
#define FOV (M_PI / 4.0)
|
2009-11-06 15:26:16 +02:00
|
|
|
|
|
|
|
void reshape(int x, int y)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
float aspect = (float)x / (float)y;
|
2009-11-11 03:21:41 +02:00
|
|
|
float halfy = (float)tan(FOV / 2.0);
|
2009-11-08 04:00:17 +02:00
|
|
|
float halfx = halfy * aspect;
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glViewport(0, 0, x, y);
|
2009-11-06 15:26:16 +02:00
|
|
|
|
2009-11-08 04:00:17 +02:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void keyb(unsigned char key, int x, int y)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
switch(key) {
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
case 27:
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
case ' ':
|
|
|
|
/* reset initial view */
|
|
|
|
pos = v3_cons(0, 0, -6);
|
|
|
|
rot = quat_cons(1, 0, 0, 0);
|
|
|
|
glutPostRedisplay();
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sbmot(int x, int y, int z)
|
|
|
|
{
|
2009-11-11 03:21:41 +02:00
|
|
|
pos.x += x * 0.001f;
|
|
|
|
pos.y += y * 0.001f;
|
|
|
|
pos.z -= z * 0.001f;
|
2009-11-08 04:00:17 +02:00
|
|
|
glutPostRedisplay();
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sbrot(int x, int y, int z)
|
|
|
|
{
|
2009-11-11 03:21:41 +02:00
|
|
|
float axis_len = (float)sqrt(x * x + y * y + z * z);
|
|
|
|
rot = quat_rotate(rot, axis_len * 0.001f, -x / axis_len, -y / axis_len, z / axis_len);
|
2009-11-08 04:00:17 +02:00
|
|
|
glutPostRedisplay();
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void sbbut(int bn, int state)
|
|
|
|
{
|
2009-11-08 04:00:17 +02:00
|
|
|
if(state == GLUT_DOWN) {
|
|
|
|
pos = v3_cons(0, 0, -6);
|
|
|
|
rot = quat_cons(1, 0, 0, 0);
|
|
|
|
glutPostRedisplay();
|
|
|
|
}
|
2009-11-06 15:26:16 +02:00
|
|
|
}
|