Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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