Subversion Repositories gelsvn

Rev

Rev 10 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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