Subversion Repositories gelsvn

Rev

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

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