File: viewpoint.c

package info (click to toggle)
sipp 3.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,268 kB
  • ctags: 744
  • sloc: ansic: 8,534; makefile: 304; lex: 65; sh: 14
file content (264 lines) | stat: -rw-r--r-- 6,503 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
 ** sipp - SImple Polygon Processor
 **
 **  A general 3d graphic package
 **
 **  Copyright Equivalent Software HB  1992
 **
 ** This program is free software; you can redistribute it and/or modify
 ** it under the terms of the GNU General Public License as published by
 ** the Free Software Foundation; either version 1, or any later version.
 ** This program is distributed in the hope that it will be useful,
 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ** GNU General Public License for more details.
 ** You can receive a copy of the GNU General Public License from the
 ** Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 **/

/**
 ** viewpoint.c - Functions that handles the viewpoint definition and
 **               calculation of viewing transformation.
 **/

#include <sipp.h>
#include <smalloc.h>
#include <viewpoint.h>


Camera         *sipp_current_camera;/* Current camera used as viewpoint */
Camera         *sipp_camera;        /* Pointer to internal camera */
static Camera   intern_camera;      /* Internal camera */


/*
 * Constants used in viewing transformation.
 */
double        hither;          /* Hither z-clipping plane */
double        yon;             /* Yonder z-clipping plane */



/*
 * Initialize the internal camera.
 */
void
camera_init()
{
    sipp_camera = &intern_camera;
    camera_params(sipp_camera, 0.0, 0.0, 10.0,  0.0, 0.0, 0.0,  
                               0.0, 1.0, 0.0,  0.25);
    camera_use(sipp_camera);
}


/*
 * Create a virtual camera.
 */
Camera *
camera_create()
{
    Camera *cp;

    cp = (Camera *)smalloc(sizeof(Camera));
    camera_params(cp, 0.0, 0.0, 10.0,  0.0, 0.0, 0.0,  0.0, 1.0, 0.0,  0.25);
    return cp;
}


/*
 * Return memory used by a virtual camera.
 */
void
camera_destruct(cp)
    Camera *cp;
{
    if (cp != sipp_camera) {
        sfree(cp);
        if (cp == sipp_current_camera) {
            camera_use(sipp_camera);
        }
    }
}


/*
 * Change the position of a virtual camera.
 */
void 
camera_position(cp, x, y, z)
    Camera *cp;
    double  x, y, z;
{
    MakeVector(cp->position, x, y, z);
}

    
/*
 * Change the point a virtual camera is "looking" at.
 */
void 
camera_look_at(cp, x, y, z)
    Camera *cp;
    double  x, y, z;
{
    MakeVector(cp->lookat, x, y, z);
}
    

/*
 * Set the up vector of a virtual camera.
 */
void 
camera_up(cp, x, y, z)
    Camera *cp;
    double  x, y, z;
{
    MakeVector(cp->up, x, y, z);
}
    

/*
 * Set the focal factor of a virtual camera.
 */
void 
camera_focal(cp, focal)
    Camera *cp;
    double  focal;
{
    cp->focal_ratio = focal;
}


/*
 * Set all parameters in a virtual camera in a single call.
 */
void 
camera_params(cp, x0, y0, z0, x, y, z, ux, uy, uz, ratio)
    Camera *cp;
    double  x0, y0, z0, x, y, z, ux, uy, uz, ratio;
{
    MakeVector(cp->position, x0, y0, z0);
    MakeVector(cp->lookat, x, y, z);
    MakeVector(cp->up, ux, uy, uz);
    cp->focal_ratio = ratio;
}


/*
 * Set the current viewpoint parameters to be
 * the ones in the virtyal camera pointed to by CP.
 */
void camera_use(cp)
    Camera *cp;
{
    sipp_current_camera = cp;
}


/*
 * Build a transformation matrix for transformation
 * into view coordinates from a particular camera position.
 */
void
get_view_transf(view_mat, camera, render_mode)
    Transf_mat *view_mat;
    Camera     *camera;
    int         render_mode;
{
    Vector tmp;
    double transl[3];
    double vy, vz;
    int i, j;
    

     
    /*
     * We need a translation so the origo
     * of the view coordinate system is placed
     * in the viewpoint.
     */
    transl[0] = -camera->position.x;
    transl[1] = -camera->position.y;
    transl[2] = -camera->position.z;


    /*
     * Calculate a vector from the viewpoint to the looked at
     * point and use it's length to define heuristic values 
     * for hither and yon.
     */
    VecSub(tmp, camera->lookat, camera->position);
    hither = VecLen(tmp) / ZCLIPF;
    yon    = VecLen(tmp) * ZCLIPF;


    /*
     * Then we need a rotation that makes the
     * up-vector point up, and alignes the sightline
     * with the z-axis.
     * This code might seem magic but the algebra behind
     * it can be found in Jim Blinn's Corner in IEEE CG&A July 1988
     */
    vecnorm(&tmp);
    vecnorm(&camera->up);
    vz = VecDot(tmp, camera->up);
    if ((vz * vz) > 1.0) {        /* this should not happen, but... */
        vz = 1.0;
    }
    vy = sqrt(1.0 - vz * vz);
    if (vy == 0.0) {          /* oops, the world collapses... */
        vy = 1.0e10;
        vz = 1.0;
    } else {
        vy = 1.0 / vy;
    }

    view_mat->mat[0][2] = tmp.x;
    view_mat->mat[1][2] = tmp.y;
    view_mat->mat[2][2] = tmp.z;

    VecScalMul(tmp, vz, tmp);
    VecSub(tmp, camera->up, tmp);
    view_mat->mat[0][1] = tmp.x * vy;
    view_mat->mat[1][1] = tmp.y * vy;
    view_mat->mat[2][1] = tmp.z * vy;

    view_mat->mat[0][0] = (view_mat->mat[1][1] * view_mat->mat[2][2] 
                           - view_mat->mat[1][2] * view_mat->mat[2][1]);
    view_mat->mat[1][0] = (view_mat->mat[2][1] * view_mat->mat[0][2] 
                           - view_mat->mat[2][2] * view_mat->mat[0][1]);
    view_mat->mat[2][0] = (view_mat->mat[0][1] * view_mat->mat[1][2] 
                           - view_mat->mat[0][2] * view_mat->mat[1][1]);


    /*
     * Install the translation into the matrix.
     * Note that it is PRE-multiplied into the matrix.
     */
    for (i = 0; i < 3; i++) {
        view_mat->mat[3][i] = 0.0;
        for (j = 0; j < 3; j++) {
            view_mat->mat[3][i] += transl[j] * view_mat->mat[j][i];
        }
    }


    /*
     * Since the screen coordinates are defined in a left handed
     * coordinate system, we must switch the sign of the first
     * column in the matrix to get appropriate signs of x-values
     * unless LINE rendering is used. In that case we switch the
     * y-axis also to get the origin in the image in the upper left.
     */
    if (render_mode == LINE) {
        view_mat->mat[0][1] = -view_mat->mat[0][1];
        view_mat->mat[1][1] = -view_mat->mat[1][1];
        view_mat->mat[2][1] = -view_mat->mat[2][1];
        view_mat->mat[3][1] = -view_mat->mat[3][1];
    } 
    view_mat->mat[0][0] = -view_mat->mat[0][0];
    view_mat->mat[1][0] = -view_mat->mat[1][0];
    view_mat->mat[2][0] = -view_mat->mat[2][0];
    view_mat->mat[3][0] = -view_mat->mat[3][0];
}