source: branches/blt4/packages/vizservers/nanovis/vrmath/vrLinmath.cpp @ 2936

Last change on this file since 2936 was 2936, checked in by gah, 12 years ago

sync back with trunk

File size: 5.8 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2#include <cstdlib>
3#include <cmath>
4#include <vrmath/vrLinmath.h>
5#include <vrmath/vrVector3f.h>
6#include <memory.h>
7
8vrVector3f vrCalcuNormal(const vrVector3f& v1, const vrVector3f& v2, const vrVector3f& v3)
9{
10    vrVector3f temp;
11
12    float a[3], b[3];
13
14    a[0] = v2.x - v1.x;
15    a[1] = v2.y - v1.y;
16    a[2] = v2.z - v1.z;
17    b[0] = v3.x - v1.x;
18    b[1] = v3.y - v1.y;
19    b[2] = v3.z - v1.z;
20
21    temp.x = a[1] * b[2] - a[2] * b[1];
22    temp.y = a[2] * b[0] - a[0] * b[2];
23    temp.z = a[0] * b[1] - a[1] * b[0];
24
25    float leng = (float)sqrt(temp.x * temp.x + temp.y * temp.y + temp.z * temp.z);
26
27    if (leng != 0.0f) {
28        temp.x /= leng;
29        temp.y /= leng;
30        temp.z /= leng;
31    }
32    else temp.set(-1.0f, 0.0f, 0.0f);
33
34    return temp;
35}
36
37static void __gluMakeIdentityd(float m[16])
38{
39    m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0;
40    m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0;
41    m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0;
42    m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;
43}
44
45static int __gluInvertMatrixd(const float m[16], float invOut[16])
46{
47    float inv[16], det;
48    int i;
49
50    inv[0] =   m[5]*m[10]*m[15] - m[5]*m[11]*m[14] - m[9]*m[6]*m[15]
51        + m[9]*m[7]*m[14] + m[13]*m[6]*m[11] - m[13]*m[7]*m[10];
52    inv[4] =  -m[4]*m[10]*m[15] + m[4]*m[11]*m[14] + m[8]*m[6]*m[15]
53        - m[8]*m[7]*m[14] - m[12]*m[6]*m[11] + m[12]*m[7]*m[10];
54    inv[8] =   m[4]*m[9]*m[15] - m[4]*m[11]*m[13] - m[8]*m[5]*m[15]
55        + m[8]*m[7]*m[13] + m[12]*m[5]*m[11] - m[12]*m[7]*m[9];
56    inv[12] = -m[4]*m[9]*m[14] + m[4]*m[10]*m[13] + m[8]*m[5]*m[14]
57        - m[8]*m[6]*m[13] - m[12]*m[5]*m[10] + m[12]*m[6]*m[9];
58    inv[1] =  -m[1]*m[10]*m[15] + m[1]*m[11]*m[14] + m[9]*m[2]*m[15]
59        - m[9]*m[3]*m[14] - m[13]*m[2]*m[11] + m[13]*m[3]*m[10];
60    inv[5] =   m[0]*m[10]*m[15] - m[0]*m[11]*m[14] - m[8]*m[2]*m[15]
61        + m[8]*m[3]*m[14] + m[12]*m[2]*m[11] - m[12]*m[3]*m[10];
62    inv[9] =  -m[0]*m[9]*m[15] + m[0]*m[11]*m[13] + m[8]*m[1]*m[15]
63        - m[8]*m[3]*m[13] - m[12]*m[1]*m[11] + m[12]*m[3]*m[9];
64    inv[13] =  m[0]*m[9]*m[14] - m[0]*m[10]*m[13] - m[8]*m[1]*m[14]
65        + m[8]*m[2]*m[13] + m[12]*m[1]*m[10] - m[12]*m[2]*m[9];
66    inv[2] =   m[1]*m[6]*m[15] - m[1]*m[7]*m[14] - m[5]*m[2]*m[15]
67        + m[5]*m[3]*m[14] + m[13]*m[2]*m[7] - m[13]*m[3]*m[6];
68    inv[6] =  -m[0]*m[6]*m[15] + m[0]*m[7]*m[14] + m[4]*m[2]*m[15]
69        - m[4]*m[3]*m[14] - m[12]*m[2]*m[7] + m[12]*m[3]*m[6];
70    inv[10] =  m[0]*m[5]*m[15] - m[0]*m[7]*m[13] - m[4]*m[1]*m[15]
71        + m[4]*m[3]*m[13] + m[12]*m[1]*m[7] - m[12]*m[3]*m[5];
72    inv[14] = -m[0]*m[5]*m[14] + m[0]*m[6]*m[13] + m[4]*m[1]*m[14]
73        - m[4]*m[2]*m[13] - m[12]*m[1]*m[6] + m[12]*m[2]*m[5];
74    inv[3] =  -m[1]*m[6]*m[11] + m[1]*m[7]*m[10] + m[5]*m[2]*m[11]
75        - m[5]*m[3]*m[10] - m[9]*m[2]*m[7] + m[9]*m[3]*m[6];
76    inv[7] =   m[0]*m[6]*m[11] - m[0]*m[7]*m[10] - m[4]*m[2]*m[11]
77        + m[4]*m[3]*m[10] + m[8]*m[2]*m[7] - m[8]*m[3]*m[6];
78    inv[11] = -m[0]*m[5]*m[11] + m[0]*m[7]*m[9] + m[4]*m[1]*m[11]
79        - m[4]*m[3]*m[9] - m[8]*m[1]*m[7] + m[8]*m[3]*m[5];
80    inv[15] =  m[0]*m[5]*m[10] - m[0]*m[6]*m[9] - m[4]*m[1]*m[10]
81        + m[4]*m[2]*m[9] + m[8]*m[1]*m[6] - m[8]*m[2]*m[5];
82
83    det = m[0]*inv[0] + m[1]*inv[4] + m[2]*inv[8] + m[3]*inv[12];
84
85    if (det == 0)
86        return 0;
87
88    det = 1.0f / det;
89
90    for (i = 0; i < 16; i++)
91        invOut[i] = inv[i] * det;
92
93    return 1;
94}
95
96static void __gluMultMatrixVecd(const float matrix[16], const float in[4],
97                                float out[4])
98{
99    int i;
100
101    for (i=0; i<4; i++) {
102        out[i] =
103            in[0] * matrix[0*4+i] +
104            in[1] * matrix[1*4+i] +
105            in[2] * matrix[2*4+i] +
106            in[3] * matrix[3*4+i];
107    }
108}
109
110static void __gluMultMatricesd(const float a[16], const float b[16],
111                               float r[16])
112{
113    int i, j;
114
115    for (i = 0; i < 4; i++) {
116        for (j = 0; j < 4; j++) {
117            r[i*4+j] =
118                a[i*4+0]*b[0*4+j] +
119                a[i*4+1]*b[1*4+j] +
120                a[i*4+2]*b[2*4+j] +
121                a[i*4+3]*b[3*4+j];
122        }
123    }
124}
125
126int vrUnproject(float winx, float winy, float winz,
127                const float modelMatrix[16],
128                const float projMatrix[16],
129                const int viewport[4],
130                float *objx, float *objy, float *objz)
131{
132    float finalMatrix[16];
133    float in[4];
134    float out[4];
135
136    __gluMultMatricesd(modelMatrix, projMatrix, finalMatrix);
137    if (!__gluInvertMatrixd(finalMatrix, finalMatrix)) return(0);
138
139    in[0]=winx;
140    in[1]=winy;
141    in[2]=winz;
142    in[3]=1.0;
143
144    // Map x and y from window coordinates
145    in[0] = (in[0] - viewport[0]) / viewport[2];
146    in[1] = (in[1] - viewport[1]) / viewport[3];
147
148    // Map to range -1 to 1
149    in[0] = in[0] * 2 - 1;
150    in[1] = in[1] * 2 - 1;
151    in[2] = in[2] * 2 - 1;
152
153    __gluMultMatrixVecd(finalMatrix, in, out);
154    if (out[3] == 0.0) return(0);
155    out[0] /= out[3];
156    out[1] /= out[3];
157    out[2] /= out[3];
158    *objx = out[0];
159    *objy = out[1];
160    *objz = out[2];
161    return(1);
162}
163
164#define __glPi 3.14159265358979323846
165
166void vrPerspective(float fovy, float aspect, float zNear, float zFar, float* matrix)
167{
168    float m[4][4];
169    float sine, cotangent, deltaZ;
170    float radians = (float) (fovy / 2.0f * __glPi / 180.0f);
171
172    deltaZ = zFar - zNear;
173    sine = sin(radians);
174    if ((deltaZ == 0) || (sine == 0) || (aspect == 0)) {
175        return;
176    }
177    cotangent = cos(radians) / sine;
178
179    __gluMakeIdentityd(&m[0][0]);
180    m[0][0] = cotangent / aspect;
181    m[1][1] = cotangent;
182    m[2][2] = -(zFar + zNear) / deltaZ;
183    m[2][3] = -1;
184    m[3][2] = -2 * zNear * zFar / deltaZ;
185    m[3][3] = 0;
186    //glMultMatrixd(&m[0][0]);
187    memcpy(matrix, &m[0][0], sizeof(float) * 16);
188}
Note: See TracBrowser for help on using the repository browser.