Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#include<typeinfo>
2
 
3
#include "CGLA/Mat4x4f.h"
4
#include "CGLA/Mat2x2f.h"
5
 
6
#include "CGLA/Vec2f.h"
7
#include "CGLA/Vec2i.h"
8
#include "CGLA/Vec3i.h"
9
#include "CGLA/Vec3f.h"
10
#include "CGLA/Vec3Hf.h"
11
 
12
#include <GL/glut.h>
13
 
14
using namespace CGLA;
15
 
16
static void display( void )
17
{
18
 
19
  // ----------------------------------------
20
  // 0. Set up viewing parameters
21
 
22
  Vec3f up(0,1,0);             // The direction that is most nearly up ..
23
  Vec3f eye(3,3,3);            // position of eye 
24
  Vec3f centre(0,0,0);         // what we are looking at 
25
  float image_plane_dist = 1;  // distance from eye to image plane.
26
 
27
 
28
  // ----------------------------------------
29
  // 1. Create view coordinate system
30
  //
31
  // Note that the args in the cross(.,.) call
32
  // do not commute
33
 
34
  Vec3f n = centre - eye;
35
  n.normalize();
36
 
37
  Vec3f u = cross(n,up);
38
  u.normalize();
39
 
40
  Vec3f v = cross(u,n);
41
 
42
  //----------------------------------------
43
  // 2. Create matrices
44
 
45
  // Create viewing matrix. We use the basis change method.
46
  // Notice how the direction of z is flipped. That is because
47
  // we look down the -z direction
48
  Mat4x4f mview(Vec3Hf(u,0), Vec3Hf(v,0), Vec3Hf(-n,0), Vec3Hf());
49
 
50
  //Create translation matrix. 
51
  Mat4x4f mtrans = translation_Mat4x4f(centre-eye);
52
 
53
  // Create projection matrix
54
  Mat4x4f mpers  = perspective_Mat4x4f(image_plane_dist);
55
 
56
  // Concatenate the translation, viewing and projection matrices
57
  Mat4x4f m = mpers * mview * mtrans;
58
 
59
  //----------------------------------------
60
  // 3. Create points 
61
 
62
  Vec3Hf axes[3]={Vec3Hf(2,0,0),Vec3Hf(0,2,0),Vec3Hf(0,0,2)};
63
  Vec3Hf paxes[3];
64
  Vec3Hf p[9] ={Vec3Hf(0,0,0), Vec3Hf(1,0,0), Vec3Hf(0,1,0),  
65
		Vec3Hf(1,1,0), Vec3Hf(0,0,1), Vec3Hf(1,0,1),  
66
		Vec3Hf(0,1,1), Vec3Hf(1,1,1)};
67
  Vec3Hf pp[9];
68
 
69
  //----------------------------------------
70
  // 4. project and dehomogenize points
71
 
72
  paxes[0] = m * axes[0];
73
  paxes[1] = m * axes[1];
74
  paxes[2] = m * axes[2];
75
  paxes[0].de_homogenize();
76
  paxes[1].de_homogenize();
77
  paxes[2].de_homogenize();
78
 
79
  for (int i=0;i<9;i++) 
80
    {
81
      pp[i] = m * p[i];
82
      pp[i].de_homogenize();
83
    }
84
 
85
 
86
  //----------------------------------------
87
  // 5. Draw _projected_ points in 2D using OpenGL
88
 
89
  // Clear screen
90
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
91
 
92
  glBegin(GL_LINES);
93
  glColor3f(1,0,0);
94
  glVertex2fv(pp[0].get());
95
  glVertex2fv(paxes[0].get());
96
 
97
  glColor3f(0,1,0);
98
  glVertex2fv(pp[0].get());
99
  glVertex2fv(paxes[1].get());
100
 
101
  glColor3f(0,0,1);
102
  glVertex2fv(pp[0].get());
103
  glVertex2fv(paxes[2].get());
104
 
105
  glColor3f(0,0,0);  
106
  for(int i=0;i<4;i++)
107
    {
108
      glVertex2fv(pp[2*i           + 0 ].get());
109
      glVertex2fv(pp[2*i           + 1 ].get());
110
    }
111
  for(int i=0;i<4;i++)
112
    {
113
      glVertex2fv(pp[(4*(i/2) + i%2) + 0 ].get());
114
      glVertex2fv(pp[(4*(i/2) + i%2) + 2 ].get());
115
    }
116
  for(int i=0;i<4;i++)
117
    { 
118
      glVertex2fv(pp[1*i           + 0 ].get());
119
      glVertex2fv(pp[1*i           + 4 ].get());
120
    }
121
  glEnd();
122
	glFlush();
123
}
124
 
125
static void reshape( int width, int height )
126
{
127
  glViewport( 0, 0, width, height );
128
}
129
 
130
 
131
static void key( unsigned char key, int x, int y )
132
{
133
  switch (key) {
134
  case 27:
135
    exit(0);
136
    break;
137
  }
138
}
139
static void init_GL()
140
{
141
  // Initialize GL, i.e. setup projection
142
  // and possibly other things as well
143
  glClearColor(1,1,1,1);
144
  gluOrtho2D(-3,3,-3,3);    
145
}
146
 
147
// static void idle()
148
// {
149
//   glutPostRedisplay();
150
// }
151
 
152
static void init_GLUT(int argc, char *argv[])
153
{
154
  // Initialize glut, open and create window.
155
  glutInit( &argc, argv );
156
  glutInitWindowSize( 400 , 400 );
157
  glutCreateWindow(argv[0]);
158
 
159
  // Register callback functions
160
  glutReshapeFunc( reshape );
161
  glutKeyboardFunc( key );
162
  glutDisplayFunc( display );
163
  // glutIdleFunc( idle );
164
 
165
}
166
 
167
int main( int argc, char *argv[] )
168
{
169
  init_GLUT(argc, argv);
170
  init_GL();
171
  glutMainLoop();
172
  return 0;
173
}
174
 
175
 
176
 
177
 
178