123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 |
- #ifdef __APPLE__
- #define _MACOSX
- #endif
- #include "quat_to_mat.h"
- #include "quat_mult.h"
- #include "axis_angle_to_quat.h"
- #include "trackball.h"
- using namespace igl;
- #include <stdlib.h>
- #include <cstdio>
- #include <cmath>
- using namespace std;
- #if defined(_WIN32) || defined(_WIN64)
- # define USE_MINI_GLUT
- #endif
- #if defined(USE_MINI_GLUT)
- # include "../src/MiniGLUT.h"
- #elif defined(_MACOSX)
- # include <GLUT/glut.h>
- #else
- # include <GL/glut.h>
- #endif
- typedef enum { SHAPE_TEAPOT=1, SHAPE_TORUS, SHAPE_CONE } Shape;
- #define NUM_SHAPES 3
- Shape g_CurrentShape = SHAPE_TEAPOT;
- float rotation[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- float down_rotation[4];
- const float g_MatAmbient[] = { 0.5f, 0.0f, 0.0f, 1.0f };
- const float g_MatDiffuse[] = { 1.0f, 1.0f, 0.0f, 1.0f };
- const double g_LightMultiplier = 1.0f;
- const float g_LightDirection[] = { -0.57735f, -0.57735f, -0.57735f };
- int down_mouse_x, down_mouse_y;
- int width, height;
- double speed_factor = 1;
- void mouse(int glutButton, int glutState, int mouse_x, int mouse_y)
- {
- down_mouse_x = mouse_x;
- down_mouse_y = mouse_y;
- copy(rotation,rotation+4,down_rotation);
- }
- void mouse_move(int mouse_x, int mouse_y)
- {
- trackball(
- width,
- height,
- speed_factor,
- down_rotation,
- down_mouse_x,
- down_mouse_y,
- mouse_x,
- mouse_y,
- rotation);
- glutPostRedisplay();
- }
- void key(unsigned char key, int mouse_x, int mouse_y)
- {
- if(key == ' ')
- {
- g_CurrentShape = (Shape)((g_CurrentShape)%NUM_SHAPES+1);
- }
- glutPostRedisplay();
- }
- void Display(void)
- {
-
-
- glClearColor(0, 0, 0, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-
- glEnable(GL_DEPTH_TEST);
- glDisable(GL_CULL_FACE);
- glEnable(GL_NORMALIZE);
-
- float v[4];
-
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- v[0] = v[1] = v[2] = g_LightMultiplier*0.4f; v[3] = 1.0f;
- glLightfv(GL_LIGHT0, GL_AMBIENT, v);
- v[0] = v[1] = v[2] = g_LightMultiplier*0.8f; v[3] = 1.0f;
- glLightfv(GL_LIGHT0, GL_DIFFUSE, v);
- v[0] = -g_LightDirection[0]; v[1] = -g_LightDirection[1]; v[2] = -g_LightDirection[2]; v[3] = 0.0f;
- glLightfv(GL_LIGHT0, GL_POSITION, v);
-
-
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, g_MatAmbient);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, g_MatDiffuse);
-
-
- glPushMatrix();
- glTranslatef(0.5f, -0.3f, 0.0f);
- float mat[4*4];
- quat_to_mat(rotation,mat);
- glMultMatrixf(mat);
- glCallList(g_CurrentShape);
- glPopMatrix();
-
-
- glutSwapBuffers();
-
-
-
- }
- void Reshape(int width, int height)
- {
-
- ::width = width;
- ::height = height;
-
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(40, (double)width/height, 1, 10);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0,0,5, 0,0,0, 0,1,0);
- glTranslatef(0, 0.6f, -1);
- }
- void Terminate(void)
- {
- glDeleteLists(SHAPE_TEAPOT, NUM_SHAPES);
- }
- int main(int argc, char *argv[])
- {
- bool help_and_quit = false;
- if(argc > 1)
- {
- if(
- argv[1][0] == '-' &&
- argv[1][1] == 'h')
- {
- help_and_quit = true;
- }else
- {
- int count = sscanf(argv[1],"%lg",&speed_factor);
- if(count != 1)
- {
- printf("Error: %s is not a valid speed factor.",
- argv[1]);
- help_and_quit = true;
- }
- }
- }
- if(help_and_quit)
- {
- printf(
- "Usage:\n ./example\nor\n ./example [positive speed factor]\n\n"
- "Interaction:\n Drag on screen to use trackball\n"
- " Press SPACE to change shape\n");
- return 1;
- }
-
-
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(640, 480);
- glutCreateWindow("AntTweakBar simple example using GLUT");
- glutCreateMenu(NULL);
-
-
- glutDisplayFunc(Display);
- glutReshapeFunc(Reshape);
- atexit(Terminate);
-
-
-
- glutMouseFunc(mouse);
-
- glutMotionFunc(mouse_move);
-
-
- glutKeyboardFunc(key);
-
-
-
-
- glNewList(SHAPE_TEAPOT, GL_COMPILE);
- glutSolidTeapot(1.0);
- glEndList();
- glNewList(SHAPE_TORUS, GL_COMPILE);
- glutSolidTorus(0.3, 1.0, 16, 32);
- glEndList();
- glNewList(SHAPE_CONE, GL_COMPILE);
- glutSolidCone(1.0, 1.5, 64, 4);
- glEndList();
-
-
- float axis[] = { 0.7f, 0.7f, 0.0f };
- float angle = 0.8f;
- axis_angle_to_quat(axis,angle,rotation);
-
- glutMainLoop();
-
- return 0;
- }
|