Subversion Repositories gelsvn

Rev

Rev 107 | Rev 132 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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