root/OpenSceneGraph/trunk/examples/osghangglide/hat.cpp @ 6941

Revision 6941, 4.3 kB (checked in by robert, 7 years ago)

From Martin Lavery and Robert Osfield, Updated examples to use a variation of the MIT License

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* OpenSceneGraph example, osghangglide.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
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
31static int inited = 0;
32
33static float dbcenter[3];
34static float dbradius;
35
36static 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    //    for( i = 0; i < sizeof( vertex ) / (sizeof( float[3] )); i++ )
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
83static void init( void )
84{
85    getDatabaseCenterRadius( dbcenter, &dbradius );
86    inited = 1;
87}
88
89
90static 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    /* Two vectors v2->v1 and v2->v3 */
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    /* Cross product between V1 and V2 */
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    /* Normalize */
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
119float 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    // m = columns
130    // n = rows
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    //tri[0][2] = vertex[i+0+j+0][2] - dbcenter[2];
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    //tri[1][2] = vertex[i+n+j+0][2] - dbcenter[2];
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    //tri[2][2] = vertex[i+0+j+1][2] - dbcenter[2];
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}
Note: See TracBrowser for help on using the browser.