Subversion Repositories gelsvn

Rev

Rev 125 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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