1 //
2 // s.h
3 //
4 
5 #include <math.h>
6 #include <string.h>
7 
8 #ifndef M_PI
9 #define M_PI 3.14159265358979323846264338327950288
10 #endif
11 
_norm_vec(float * v)12 static void _norm_vec( float *v )
13 {
14     double x = v[0];
15     double y = v[1];
16     double z = v[2];
17     double d = sqrt( x*x + y*y + z*z );
18 
19     if ( d > 0.0 ) {
20         v[0] = (float)( x / d );
21         v[1] = (float)( y / d );
22         v[2] = (float)( z / d );
23     }
24 }
25 
_outp_vec(float * ans,float * v1,float * v2)26 static void _outp_vec( float *ans, float *v1, float *v2 ) /* ans = v1 * v2 */
27 {
28     ans[0] = ( v1[1] * v2[2] - v1[2] * v2[1] );
29     ans[1] = ( v1[2] * v2[0] - v1[0] * v2[2] );
30     ans[2] = ( v1[0] * v2[1] - v1[1] * v2[0] );
31 }
32 
_store_array(int idx,float * v,float * n,float * t,float * b,float * st,float * c,float * v_array,float * n_array,float * t_array,float * b_array,float * st_array,float * c_array,unsigned short * i_array)33 static void _store_array( int idx, float *v, float *n, float *t, float *b, float *st, float *c,
34                         float *v_array, float *n_array, float *t_array, float *b_array, float *st_array, float *c_array, unsigned short *i_array )
35 {
36     for ( unsigned int i = 0; i < 3; i++ ) {
37         v_array[idx*3+i] = v[i];
38         //v_array[idx*4+i] = v[i];
39         n_array[idx*3+i] = n[i];
40         t_array[idx*3+i] = t[i];
41         b_array[idx*3+i] = b[i];
42     }
43     //v_array[idx*4+3] = 1.f;
44     for ( unsigned int i = 0; i < 2; i++ )
45         st_array[idx*2+i] = st[i];
46     for ( unsigned int i = 0; i < 4; i++ )
47         c_array[idx*4+i] = c[i];
48     i_array[idx] = idx;
49 }
50 
_tToVTBN(float * v,float * t,float * b,float * n,float * st,float r)51 static void _tToVTBN( float *v, float *t, float *b, float *n, float *st, float r )
52 {
53     float theta = ( float )M_PI * st[1];
54     float phai = 2.f * ( float )M_PI * st[0];
55     float y = - r * cos( theta );
56     float z = r * sin( theta ) * cos( phai );
57     float x = r * sin( theta ) * sin( phai );
58     float dxdt = r * cos( theta ) * sin( phai );
59     float dzdt = r * cos( theta ) * cos( phai );
60     float dydt = r * sin( theta );
61     float dxdp = cos( phai );
62     float dzdp = - sin( phai );
63     float dydp = 0.f;
64     v[0] = x;
65     v[1] = y;
66     v[2] = z;
67     t[0] = dxdp;
68     t[1] = dydp;
69     t[2] = dzdp;
70     _norm_vec( t );
71     b[0] = dxdt;
72     b[1] = dydt;
73     b[2] = dzdt;
74     _norm_vec( b );
75     _outp_vec( n, t, b );
76     _norm_vec( n );
77 }
78 
_tToVTBN2(float * v,float * t,float * b,float * n,float * st,float l)79 static void _tToVTBN2( float *v, float *t, float *b, float *n, float *st, float l )
80 {
81     v[0] = l * ( st[0] - 0.5f );
82     v[1] = l * ( st[1] - 0.5f );
83     v[2] = 0.f;
84     t[0] = 1.f;
85     t[1] = 0.f;
86     t[2] = 0.f;
87     n[0] = 0.f;
88     n[1] = 0.f;
89     n[2] = 1.f;
90     b[0] = 0.f;
91     b[1] = 1.f;
92     b[2] = 0.f;
93 }
94 
95 
96 //
97 // namespace util
98 //
99 
100 //namespace util {
101 
102 typedef struct vertex_attr {
103     float x, y, z;
104     float nx, ny, nz;
105     float tx, ty, tz;
106     float s, t;
107     float r, g, b, a;
108 } vertex_attr_t;
109 
110 class base
111 {
112 public:
base()113     base()
114     {
115         m_vertex    = 0;
116         m_normal    = 0;
117         m_tangent    = 0;
118         m_binormal    = 0;
119         m_color        = 0;
120         m_texture    = 0;
121         m_index        = 0;
122         m_vertex_attr = 0;
123         m_triangle_count = 0;
124         m_index_count = 0;
125         m_vertex_attr_stride = sizeof( vertex_attr_t );
126     }
~base()127     virtual ~base()
128     {
129         if ( m_vertex )
130             delete [] m_vertex;
131         if ( m_normal )
132             delete [] m_normal;
133         if ( m_tangent )
134             delete [] m_tangent;
135         if ( m_binormal )
136             delete [] m_binormal;
137         if ( m_color )
138             delete [] m_color;
139         if ( m_texture )
140             delete [] m_texture;
141         if ( m_index )
142             delete [] m_index;
143         if ( m_vertex_attr )
144             delete [] m_vertex_attr;
145     }
146 
147 public:
get_vertex()148     virtual float *get_vertex() { return m_vertex; }
get_normal()149     virtual float *get_normal() { return m_normal; }
get_tangent()150     virtual float *get_tangent() { return m_tangent; }
get_binormal()151     virtual float *get_binormal() { return m_binormal; }
get_color()152     virtual float *get_color() { return m_color; }
get_texture()153     virtual float *get_texture() { return m_texture; }
get_vertex_array()154     virtual    void *get_vertex_array() { return ( void* )m_vertex_attr; }
get_index()155     virtual unsigned short *get_index() { return m_index; }
get_triangle_count()156     virtual unsigned int get_triangle_count() { return m_triangle_count; }
get_index_count()157     virtual unsigned int get_index_count() { return m_index_count; }
get_vertex_attr_stride()158     virtual unsigned int get_vertex_attr_stride() { return m_vertex_attr_stride; }
159 
160 protected:
161     float    *m_vertex;
162     float    *m_normal;
163     float    *m_tangent;
164     float    *m_binormal;
165     float    *m_color;
166     float    *m_texture;
167     vertex_attr_t    *m_vertex_attr;
168     unsigned short    *m_index;
169     unsigned int    m_triangle_count;
170     unsigned int    m_index_count; // = vertex count
171     unsigned int    m_vertex_attr_stride;
172 };
173 
174 class sphere : public base
175 {
176 public:
177     sphere();
178     sphere( unsigned int i, unsigned int j, float r );
179     virtual ~sphere();
180 };
181 
sphere()182 sphere::sphere()
183 {
184     sphere( 10, 10, 1.f );
185 }
186 
sphere(unsigned int ni,unsigned int nj,float r)187 sphere::sphere( unsigned int ni, unsigned int nj, float r )
188 {
189     float v[3], n[3], t[3], b[3], st[2], c[4];
190 
191     m_triangle_count = ni * nj * 2;
192     m_index_count = m_triangle_count * 3;
193 
194     m_vertex  = new float [ m_index_count * 3 ];
195     //m_vertex  = new float [ m_index_count * 4 ];
196     m_normal = new float [ m_index_count * 3 ];
197     m_tangent = new float [ m_index_count * 3 ];
198     m_binormal = new float [ m_index_count * 3 ];
199     m_color = new float [ m_index_count * 4 ];
200     m_texture = new float [ m_index_count * 2 ];
201     m_index = new unsigned short [ m_index_count ];
202     m_vertex_attr = new vertex_attr_t [ m_index_count ];
203 
204     int idx = 0;
205     for ( unsigned int i = 0; i < ni; i++ ) {
206         for ( unsigned int j = 0; j < nj; j++ ) {
207 
208             if ( j != 0 ) {
209                 // 0-0
210                 st[0] = ( float )i / ( float )ni;
211                 st[1] = ( float )j / ( float )nj;
212                 _tToVTBN( v, t, b, n, st, r );
213                 c[0] = 1.f; c[1] = 0.f; c[2] = 0.f; c[3] = 1.f;
214                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
215                 // 0-1
216                 st[0] = ( float )( i + 1 ) / ( float )ni;
217                 st[1] = ( float )j / ( float )nj;
218                 _tToVTBN( v, t, b, n, st, r );
219                 c[0] = 0.f; c[1] = 1.f; c[2] = 0.f; c[3] = 1.f;
220                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
221                 // 0-2
222                 st[0] = ( float )( i + 1 ) / ( float )ni;
223                 st[1] = ( float )( j + 1 ) / ( float )nj;
224                 _tToVTBN( v, t, b, n, st, r );
225                 c[0] = 0.f; c[1] = 0.f; c[2] = 1.f; c[3] = 1.f;
226                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
227             }
228             if ( j != nj-1 ) {
229                 // 1-0
230                 st[0] = ( float )i / ( float )ni;
231                 st[1] = ( float )j / ( float )nj;
232                 _tToVTBN( v, t, b, n, st, r );
233                 c[0] = 1.f; c[1] = 1.f; c[2] = 0.f; c[3] = 1.f;
234                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
235                 // 1-1
236                 st[0] = ( float )( i + 1 ) / ( float )ni;
237                 st[1] = ( float )( j + 1 ) / ( float )nj;
238                 _tToVTBN( v, t, b, n, st, r );
239                 c[0] = 1.f; c[1] = 0.f; c[2] = 1.f; c[3] = 1.f;
240                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
241                 // 1-2
242                 st[0] = ( float )i / ( float )ni;
243                 st[1] = ( float )( j + 1 ) / ( float )nj;
244                 _tToVTBN( v, t, b, n, st, r );
245                 c[0] = 0.f; c[1] = 1.f; c[2] = 1.f; c[3] = 1.f;
246                 _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
247             }
248         }
249     }
250 
251     m_index_count = idx;
252 
253     for ( unsigned int i = 0; i < m_index_count; i++ ) {
254         memcpy( &m_vertex_attr[i].x,    &m_vertex[ i*3 ],    sizeof( float )*3 );
255         memcpy( &m_vertex_attr[i].nx,    &m_normal[ i*3 ],    sizeof( float )*3 );
256         memcpy( &m_vertex_attr[i].tx,    &m_tangent[ i*3 ],    sizeof( float )*3 );
257         memcpy( &m_vertex_attr[i].s,    &m_texture[ i*2 ],    sizeof( float )*2 );
258         memcpy( &m_vertex_attr[i].r,    &m_color[ i*4 ],    sizeof( float )*4 );
259     }
260 }
261 
~sphere()262 sphere::~sphere()
263 {
264 }
265 
266 class squre : public base
267 {
268 public:
269     squre();
270     squre( unsigned int i, unsigned int j, float l );
271     virtual ~squre();
272 };
273 
squre()274 squre::squre()
275 {
276     squre( 10, 10, 1.f );
277 }
278 
squre(unsigned int ni,unsigned int nj,float l)279 squre::squre( unsigned int ni, unsigned int nj, float l )
280 {
281     float v[3], n[3], t[3], b[3], st[2], c[4];
282 
283     m_triangle_count = ni * nj * 2;
284     m_index_count = m_triangle_count * 3;
285 
286     m_vertex  = new float [ m_index_count * 3 ];
287     m_normal = new float [ m_index_count * 3 ];
288     m_tangent = new float [ m_index_count * 3 ];
289     m_binormal = new float [ m_index_count * 3 ];
290     m_color = new float [ m_index_count * 4 ];
291     m_texture = new float [ m_index_count * 2 ];
292     m_index = new unsigned short [ m_index_count ];
293 
294     int idx = 0;
295     for ( unsigned int i = 0; i < ni; i++ ) {
296         for ( unsigned int j = 0; j < nj; j++ ) {
297 
298             // 0-0
299             st[0] = ( float )i / ( float )ni;
300             st[1] = ( float )j / ( float )nj;
301             _tToVTBN2( v, t, b, n, st, l );
302             c[0] = 1.f; c[1] = 0.f; c[2] = 0.f; c[3] = 1.f;
303             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
304             // 0-1
305             st[0] = ( float )( i + 1 ) / ( float )ni;
306             st[1] = ( float )j / ( float )nj;
307             _tToVTBN2( v, t, b, n, st, l );
308             c[0] = 0.f; c[1] = 1.f; c[2] = 0.f; c[3] = 1.f;
309             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
310             // 0-2
311             st[0] = ( float )( i + 1 ) / ( float )ni;
312             st[1] = ( float )( j + 1 ) / ( float )nj;
313             _tToVTBN2( v, t, b, n, st, l );
314             c[0] = 0.f; c[1] = 0.f; c[2] = 1.f; c[3] = 1.f;
315             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
316             // 1-0
317             st[0] = ( float )i / ( float )ni;
318             st[1] = ( float )j / ( float )nj;
319             _tToVTBN2( v, t, b, n, st, l );
320             c[0] = 1.f; c[1] = 1.f; c[2] = 0.f; c[3] = 1.f;
321             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
322             // 1-1
323             st[0] = ( float )( i + 1 ) / ( float )ni;
324             st[1] = ( float )( j + 1 ) / ( float )nj;
325             _tToVTBN2( v, t, b, n, st, l );
326             c[0] = 1.f; c[1] = 0.f; c[2] = 1.f; c[3] = 1.f;
327             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
328             // 1-2
329             st[0] = ( float )i / ( float )ni;
330             st[1] = ( float )( j + 1 ) / ( float )nj;
331             _tToVTBN2( v, t, b, n, st, l );
332             c[0] = 0.f; c[1] = 1.f; c[2] = 1.f; c[3] = 1.f;
333             _store_array( idx++, v, n, t, b, st, c, m_vertex, m_normal, m_tangent, m_binormal, m_texture, m_color, m_index );
334         }
335     }
336 
337     m_index_count = idx;
338 }
339 
~squre()340 squre::~squre()
341 {
342 }
343 
344 //} // end namespace
345 
346 
347 
348