Subversion Repositories gelsvn

Rev

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