Subversion Repositories gelsvn

Rev

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

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