opengl-intro: rotacija.c

File rotacija.c, 1.4 KB (added by msitar, 15 years ago)

Kvaternionska rotacija

Line 
1#include <GL/glut.h>
2#include "trackball.h"
3#include "trackball.c"
4
5GLfloat m[4][4];               
6float last[4];
7float cur[4]; 
8int width; 
9int height;
10int beginx;
11int beginy;
12float p1x; 
13float p1y; 
14float p2x; 
15float p2y;
16
17void display()
18{
19  glPushMatrix();
20  build_rotmatrix(m, cur); 
21  glLoadIdentity();
22  glMultMatrixf(*m);
23  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
24  glutSolidTeapot(0.5); 
25  glPopMatrix();
26  glutSwapBuffers();
27}
28
29void mouse(int button,int state, int x, int y)   
30{
31  beginx = x;
32  beginy = y;
33}
34
35void motion(int x,int y)   
36{
37  p1x = (2.0*beginx - width)/width;
38  p1y = (height - 2.0*beginy)/height;
39  p2x = (2.0 * x - width) / width;
40  p2y = (height - 2.0 * y) / height;
41  trackball(last,p1x, p1y, p2x, p2y);   
42  add_quats(last, cur, cur);   
43  beginx = x;
44  beginy = y;
45  glutPostRedisplay();   
46}
47
48void reshape (int w, int h)
49{
50  width=w;
51  height=h;
52  double l;
53  l = 1;
54  glViewport (0, 0, w, h);
55  glMatrixMode (GL_PROJECTION);
56  glLoadIdentity();
57  glOrtho(-l, l, -l, l, -l, l);
58  glMatrixMode(GL_MODELVIEW);
59  glLoadIdentity();
60}
61
62
63int main(int argc, char *argv[])
64{
65  glutInit(&argc, argv);
66  trackball(cur, 0.0, 0.0, 0.0, 0.0);
67  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
68  glutCreateWindow("Rotacija z misko");
69  glutDisplayFunc(display);
70  glutMouseFunc(mouse);
71  glutMotionFunc(motion);
72  glutReshapeFunc(reshape);
73  glEnable(GL_LIGHTING);
74  glEnable(GL_LIGHT0);
75  glEnable(GL_DEPTH_TEST);
76  glutMainLoop();
77  return 0;
78}