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

Revision 1697, 3.3 kB (checked in by robert, 12 years ago)

Ported osgGLUT based src/Demos across to being osgProducer based, and placed
them in the new examples/ directory.

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