1 | |
---|
2 | |
---|
3 | |
---|
4 | |
---|
5 | |
---|
6 | |
---|
7 | |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | |
---|
12 | |
---|
13 | |
---|
14 | |
---|
15 | |
---|
16 | |
---|
17 | |
---|
18 | |
---|
19 | #ifdef _MSC_VER |
---|
20 | #include <Windows.h> |
---|
21 | #pragma warning( disable : 4244 ) |
---|
22 | #endif |
---|
23 | |
---|
24 | #include <osg/GL> |
---|
25 | #include <osg/Math> |
---|
26 | #include <stdio.h> |
---|
27 | |
---|
28 | #include "terrain_coords.h" |
---|
29 | #include "hat.h" |
---|
30 | |
---|
31 | static int inited = 0; |
---|
32 | |
---|
33 | static float dbcenter[3]; |
---|
34 | static float dbradius; |
---|
35 | |
---|
36 | static void getDatabaseCenterRadius( float dbcenter[3], float *dbradius ) |
---|
37 | { |
---|
38 | int i; |
---|
39 | double n=0.0; |
---|
40 | double center[3] = { 0.0f, 0.0f, 0.0f }; |
---|
41 | float cnt; |
---|
42 | |
---|
43 | cnt = 39 * 38; |
---|
44 | for( i = 0; i < cnt; i++ ) |
---|
45 | { |
---|
46 | center[0] += (double)vertex[i][0]; |
---|
47 | center[1] += (double)vertex[i][1]; |
---|
48 | center[2] += (double)vertex[i][2]; |
---|
49 | |
---|
50 | n = n + 1.0; |
---|
51 | } |
---|
52 | |
---|
53 | center[0] /= n; |
---|
54 | center[1] /= n; |
---|
55 | center[2] /= n; |
---|
56 | |
---|
57 | float r = 0.0; |
---|
58 | |
---|
59 | |
---|
60 | for( i = 0; i < cnt; i++ ) |
---|
61 | { |
---|
62 | double d = sqrt( |
---|
63 | (((double)vertex[i][0] - center[0]) * ((double)vertex[i][0] - center[0])) + |
---|
64 | (((double)vertex[i][1] - center[1]) * ((double)vertex[i][1] - center[1])) + |
---|
65 | (((double)vertex[i][2] - center[2]) * ((double)vertex[i][2] - center[2])) ); |
---|
66 | |
---|
67 | if( d > (double)r ) r = (float)d; |
---|
68 | |
---|
69 | } |
---|
70 | |
---|
71 | *dbradius = r; |
---|
72 | dbcenter[0] = (float)center[0]; |
---|
73 | dbcenter[1] = (float)center[1]; |
---|
74 | dbcenter[2] = (float)center[2]; |
---|
75 | |
---|
76 | int index = 19 * 39 + 19; |
---|
77 | dbcenter[0] = vertex[index][0] - 0.15; |
---|
78 | dbcenter[1] = vertex[index][1]; |
---|
79 | dbcenter[2] = vertex[index][2] + 0.35; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | static void init( void ) |
---|
84 | { |
---|
85 | getDatabaseCenterRadius( dbcenter, &dbradius ); |
---|
86 | inited = 1; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | static void getNormal( float *v1, float *v2, float *v3, float *n ) |
---|
91 | { |
---|
92 | float V1[4], V2[4]; |
---|
93 | float f; |
---|
94 | int i; |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | for( i = 0; i < 3; i++ ) |
---|
99 | { |
---|
100 | V1[i] = v1[i] - v2[i]; |
---|
101 | V2[i] = v3[i] - v2[i]; |
---|
102 | } |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | n[0] = (V1[1] * V2[2]) - (V1[2] * V2[1]); |
---|
107 | n[1] = -((V1[0] * V2[2]) - ( V1[2] * V2[0] )); |
---|
108 | n[2] = (V1[0] * V2[1] ) - (V1[1] * V2[0] ); |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | f = sqrtf( ( n[0] * n[0] ) + ( n[1] * n[1] ) + ( n[2] * n[2] ) ); |
---|
113 | n[0] /= f; |
---|
114 | n[1] /= f; |
---|
115 | n[2] /= f; |
---|
116 | } |
---|
117 | |
---|
118 | |
---|
119 | float Hat( float x, float y, float z ) |
---|
120 | { |
---|
121 | int m, n; |
---|
122 | int i, j; |
---|
123 | float tri[3][3]; |
---|
124 | float norm[3]; |
---|
125 | float d, pz; |
---|
126 | |
---|
127 | if( inited == 0 ) init(); |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | m = (sizeof( vertex ) /(sizeof( float[3])))/39; |
---|
132 | n = 39; |
---|
133 | |
---|
134 | i = 0; |
---|
135 | while( i < ((m-1)*39) && x > (vertex[i+n][0] - dbcenter[0]) ) |
---|
136 | i += n; |
---|
137 | |
---|
138 | j = 0; |
---|
139 | |
---|
140 | while( j < n-1 && y > (vertex[i+j+1][1] - dbcenter[1]) ) |
---|
141 | j++; |
---|
142 | |
---|
143 | tri[0][0] = vertex[i+0+j+0][0] - dbcenter[0]; |
---|
144 | tri[0][1] = vertex[i+0+j+0][1] - dbcenter[1]; |
---|
145 | |
---|
146 | tri[0][2] = vertex[i+0+j+0][2]; |
---|
147 | |
---|
148 | tri[1][0] = vertex[i+n+j+0][0] - dbcenter[0]; |
---|
149 | tri[1][1] = vertex[i+n+j+0][1] - dbcenter[1]; |
---|
150 | |
---|
151 | tri[1][2] = vertex[i+n+j+0][2]; |
---|
152 | |
---|
153 | tri[2][0] = vertex[i+0+j+1][0] - dbcenter[0]; |
---|
154 | tri[2][1] = vertex[i+0+j+1][1] - dbcenter[1]; |
---|
155 | |
---|
156 | tri[2][2] = vertex[i+0+j+1][2]; |
---|
157 | |
---|
158 | getNormal( tri[0], tri[1], tri[2], norm ); |
---|
159 | |
---|
160 | d = (tri[0][0] * norm[0]) + |
---|
161 | (tri[0][1] * norm[1]) + |
---|
162 | (tri[0][2] * norm[2]); |
---|
163 | |
---|
164 | d *= -1; |
---|
165 | pz = (-(norm[0] * x) - (norm[1] * y) - d)/norm[2]; |
---|
166 | |
---|
167 | return z - pz; |
---|
168 | } |
---|