Morph-SynRJ
view efx_harmonics.cxx @ 0:22de913c2d84
morph-synrj intro
| author | "Cedric Pinson <cedric.pinson@alcove.fr> <mornifle@plopbyte.net>" |
|---|---|
| date | Tue Nov 27 15:23:52 2007 +0100 (2007-11-27) |
| parents | |
| children |
line source
1 /** @file stv_harmonics.cxx
2 *
3 * @brief Sphericals Harmonics Effect
4 *
5 ******************************************************************************************************************
6 *
7 * @author Stv based on Paul Bourke's web site (http://astronomy.swin.edu.au/~pbourke/surfaces/sphericalh/)
8 *
9 * @date Created 2003/09
10 *
11 * @version $Id: efx_harmonics.cxx,v 1.9 2004/01/17 09:49:49 psc80 Exp $
12 *
13 *****************************************************************************************************************/
15 #include "efx_harmonics.h"
16 #include "mth_vector3.h"
17 #include "sys_assert.h"
19 #include "gl_header.h"
21 //109056 -> 38912
22 #if 0
23 void efxSphericalShape_t::Init(int _resolution)
24 {
26 Kill();
27 vertexes=new mth::Vector3_t[_resolution*_resolution];
28 normals=new mth::Vector3_t[_resolution*_resolution];
30 resolution=_resolution;
31 }
34 void efxSphericalShape_t::Kill()
35 {
36 if (vertexes)
37 delete [] vertexes;
38 vertexes=0;
40 if (normals)
41 delete [] normals;
42 normals=0;
44 }
45 #endif
46 mth::Vector3_t efxSphericalShape_t::ComputeVertex(double _theta, double _phi,double _scale)
47 {
48 double radius = 0;
49 mth::Vector3_t v_output;
51 radius += mth::Pow(mth::Sin(params[0]*_phi),params[1]);
52 radius += mth::Pow(mth::Cos(params[2]*_phi),params[3]);
53 radius += mth::Pow(mth::Sin(params[4]*_theta),params[5]);
54 radius += mth::Pow(mth::Cos(params[6]*_theta),params[7]);
55 radius*=_scale;
57 v_output[0] = float(radius * mth::Sin(_phi) * mth::Cos(_theta));
58 v_output[1] = float(radius * mth::Cos(_phi));
59 v_output[2] = float(radius * mth::Sin(_phi) * mth::Sin(_theta));
61 return v_output;
63 }
66 mth::Vector3_t efxSphericalShape_t::ComputeNormal(const mth::Vector3_t& _a,const mth::Vector3_t& _b, const mth::Vector3_t& _c)
67 {
68 mth::Vector3_t vNorm;
69 mth::Vector3_t v1 = _b - _a;
70 mth::Vector3_t v2 = _c - _a;
72 vNorm=v1.Cross(v2);
73 vNorm.Normalize();
75 return vNorm;
76 }
79 void efxSphericalShape_t::Params(int _a0,int _a1,int _a2,int _a3,int _a4,int _a5,int _a6,int _a7)
80 {
81 params[0] = _a0;
82 params[1] = _a1;
83 params[2] = _a2;
84 params[3] = _a3;
85 params[4] = _a4;
86 params[5] = _a5;
87 params[6] = _a6;
88 params[7] = _a7;
89 }
91 void efxSphericalShape_t::RandomizeParams()
92 {
93 for (int i=0; i<2; i++)
94 for (int s=0; s<8; s++) {
95 params[s] = (int)(6*mth::Rand()/mth::RANDOM_MAX+1);
96 params[s]=6;
97 }
98 //params[s] =mth::Rand()%6;
99 params[0]=2;
100 params[1]=4;
101 params[2]=6;
102 params[3]=2;
103 params[4]=4;
104 params[5]=6;
105 params[6]=2;
106 params[7]=4;
108 #if 0 // tested shape that are usable
109 params[0]=0;
110 params[1]=2;
111 params[2]=4;
112 params[3]=0;
113 params[4]=2;
114 params[5]=4;
115 params[6]=0;
116 params[7]=2;
119 params[0]=4;
120 params[1]=0;
121 params[2]=2;
122 params[3]=4;
123 params[4]=0;
124 params[5]=2;
125 params[6]=4;
126 params[7]=0;
128 #endif
130 }
133 void efxSphericalShape_t::Generate(float _scale,bool _reverse)
134 {
135 SYS_ASSERT(vertexes && normals);
137 double du = mth::TWOPI / (double)(ResX());
138 double dv = mth::PI / (double)(ResY()-1);
140 int i,j;
141 double u,v;
142 for (i=0; i<ResX(); i++)
143 for (j=0; j<ResY(); j++) {
144 u = i*du;
145 v = j*dv;
147 vertexes[i+j*ResX()]=ComputeVertex(u,v,_scale);
148 }
151 mth::Vector3_t normal;
153 // build normals
154 for (i=0; i<ResX(); i++)
155 for ( j=0; j<ResY(); j++) {
156 u = i*du;
157 v = j*dv;
159 normal=ComputeNormal(ComputeVertex(u,v,_scale),
160 ComputeVertex(u+du/10.0,v,_scale),
161 ComputeVertex(u,v+dv/10.0,_scale));
162 if (_reverse)
163 normal=-normal;
164 normals[i+j*ResX()]=normal;
165 //vertexes[i+j*resolution],
166 }
168 }
170 #if 0
171 void efxSphericalShape_t::Draw() const
172 {
174 glShadeModel(GL_SMOOTH);
177 glEnable(GL_LIGHTING);
178 glEnable(GL_LIGHT0);
180 float light[4]={0.333,0.333,0.333,0};
181 glLightfv(GL_LIGHT0,GL_POSITION,light);
183 int i,j;
185 #if 1 // test render mode
187 for (int t=0;t<2;t++) {
188 GLenum mode=GL_QUAD_STRIP;
189 if (t==1) {
190 mode=GL_LINE_STRIP;
191 glDisable(GL_CULL_FACE);
192 glDisable(GL_DEPTH_TEST);
193 }
196 glBegin(mode);
197 for (j=0; j<ResY()-1; j++)
198 for (i=0; i<ResX(); i++) {
199 glNormal3fv(normals[(i)%ResX()+(j%ResY())*ResX()]);
200 glVertex3fv(vertexes[(i)%ResX()+(j%ResY())*ResX()]);
202 glNormal3fv(normals[(i)%ResX()+((j+1)%ResY())*ResX()]);
203 glVertex3fv(vertexes[(i)%ResX()+((j+1)%ResY())*ResX()]);
204 }
205 glEnd();
206 }
208 #else
211 glBegin(GL_LINES);
212 for (j=0; j<resolution-1; j++)
213 for (i=0; i<resolution; i++) {
215 glVertex3fv(vertexes[(i)%resolution+(j%resolution)*resolution]);
216 glVertex3fv(vertexes[(i+1)%resolution+(j%resolution)*resolution]);
218 glVertex3fv(vertexes[(i)%resolution+(j%resolution)*resolution]);
219 glVertex3fv(vertexes[(i)%resolution+((j+1)%resolution)*resolution]);
222 }
223 glEnd();
224 #endif
226 glDisable(GL_LIGHT0);
227 glDisable(GL_LIGHTING);
228 }
229 #endif
233 // used for screen 2
234 void efxSphericalShape_t::Draw()
235 {
236 int i,j;
237 GLenum mode=GL_QUAD_STRIP;
239 #if 0
240 int sx;int sy;
241 sx=ResX();
242 sy=ResY();
243 txtBindTexture2d(textureBase,0);
245 int index;
246 glBegin(mode);
248 for (j=0; j<sy-1; j++)
249 for (i=0; i<sx; i++) {
250 index=i+j*sx;
251 glNormal3fv(normals[index]);
252 glTexCoord2fv(uvs[index]);
253 glVertex3fv(vertexes[index]);
255 index+=sx;
256 glNormal3fv(normals[index]);
257 glTexCoord2fv(uvs[index]);
258 glVertex3fv(vertexes[index]);
259 }
260 glEnd();
261 txtBindTexture2d(textureBase,0);
262 #else
264 txtBindTexture2d(textureBase,0);
266 FixForVArray();
267 EnableArray();
269 for (i=0;i<Indexes()->nbBuffers;i++)
270 glDrawElements(mode,Indexes()->sizeOfBuffer,GL_UNSIGNED_INT,Indexes()->indexes[i]);
272 DisableArray();
273 txtBindTexture2d(textureBase,0);
275 #endif
276 }
283 void efxSpherical_t::Init(int _nbShapes,int _resolutions,float _scale)
284 {
286 Kill();
287 resolutions=_resolutions;
288 nbShapes=_nbShapes;
290 int q=resolutions*resolutions;
292 #if 0
293 uvs=new mth::Vector2_t[q];
294 for (int i=0;i<resolutions;i++)
295 for (int j=0;j<resolutions;j++) {
296 uvs[i+j*resolutions][0]=i*4*1.0/resolutions;
297 uvs[i+j*resolutions][1]=j*4*1.0/resolutions;
298 }
300 SYS_ASSERT(uvs);
301 #endif
303 shapes=new efxSphericalShape_t[_nbShapes];
305 #if 1 // use this new version (remove old code in next commit)
306 shapes[0].Init(_resolutions,_resolutions);
307 uvs=shapes[0].GetNewUVsBuffer();
308 cmnSquareShape_t::IndexBuffers_t* idx=shapes[0].GetNewIndexBuffer();
309 shapes[0].SetUVs(uvs);
310 shapes[0].SetIndexes(idx);
312 for (int i=0;i<resolutions;i++)
313 for (int j=0;j<resolutions;j++) {
314 uvs[i+j*resolutions][0]=i*4.0/resolutions;
315 uvs[i+j*resolutions][1]=j*4.0/resolutions;
316 }
317 SYS_ASSERT(uvs);
319 for (i=1;i<_nbShapes;i++) {
320 shapes[i].Init(_resolutions,_resolutions);
321 shapes[i].RandomizeParams();
322 shapes[i].Generate(_scale);
323 shapes[i].SetUVs(uvs);
324 shapes[i].SetIndexes(idx);
325 }
326 morph.Init(_resolutions,_resolutions);
327 morph.SetUVs(uvs);
328 morph.SetIndexes(idx);
329 #else
330 for (i=0;i<_nbShapes;i++) {
331 shapes[i].Init(_resolutions,_resolutions);
332 shapes[i].RandomizeParams();
333 shapes[i].Generate(_scale);
334 shapes[i].SetUVs(uvs);
335 }
337 morph.Init(_resolutions,_resolutions);
338 morph.SetUVs(uvs);
339 #endif
340 nbShapes=_nbShapes;
341 resolutions=_resolutions;
343 }
345 void efxSpherical_t::SetTexture(GLuint _id){
347 for (int i=0;i<nbShapes;i++)
348 shapes[i].SetTexture(_id);
349 morph.SetTexture(_id);
350 }
353 void efxSpherical_t::Kill()
354 {
355 if (shapes)
356 delete [] shapes;
357 shapes=0;
359 morph.Kill();
360 if (uvs)
361 delete [] uvs;
362 uvs=0;
363 }
368 void efxSpherical_t::MakeMorph(int _shape1,int _shape2,float _t)
369 {
370 SYS_ASSERT(_shape1>=0 && _shape1<nbShapes);
371 SYS_ASSERT(_shape2>=0 && _shape2<nbShapes);
373 morph.MakeMorphFrom(shapes[_shape1],shapes[_shape2],_t);
375 }
