source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/leaves/foliage.cpp @ 1057

Revision 1057, 23.1 KB checked in by gumbau, 18 years ago (diff)
Line 
1#include <stdio.h>
2#include <string.h>
3#include <math.h>
4
5#include "Foliage.h"
6
7
8/******************************************   PANTALLA **************************************************/
9//--------------------------------------------------------------------------------------------------------------------------------
10// Void constructor
11// Parameters --> None
12//--------------------------------------------------------------------------------------------------------------------------------
13Foliage::Foliage(const Geometry::SubMesh *leavesSubMesh, const char *simpSeq, Geometry::CREATEVERTEXDATAFUNC vdfun, Geometry::CREATEINDEXDATAFUNC idfun):
14Acth(NULL),
15create_vertex_data_func(vdfun==NULL?Geometry::DefaultVertexDataCreator:vdfun),
16create_index_data_func(idfun==NULL?Geometry::DefaultIndexDataCreator:idfun),
17vertexdata(NULL), Leaves(NULL), MinDet(NULL)
18{
19        cx = cy= cz= 0;
20        ppio= final = -1;
21
22        ReadVertices(leavesSubMesh);
23        ReadLeafs(leavesSubMesh);
24        if (!ReadSimpSeq(simpSeq)) exit(1);
25        RellenoRaiz();
26        CalculaTexCoordsYNorms();
27
28
29        // esto no sé si devería haber akí
30        indexdata->SetNumValidIndices(0);
31
32        int h=0;
33
34
35        Acth  = new ActiveLeafNode[nHojas*8];
36
37        for ( h=0; h < nHojas; h++) {
38
39                Acth[h].index = h;
40                if ( h != 0)
41                {
42                        Acth[h].prev = (h-1);
43                        Acth[h-1].next = h;
44                }
45
46        }
47                               
48        ppio = 0;
49        final = nHojas-1;
50        nhactivas = nHojas;
51
52}
53
54/*
55//--------------------------------------------------------------------------------------------------------------------------------
56// Constructor with the name of a file cotaining the data
57//--------------------------------------------------------------------------------------------------------------------------------
58Foliage::Foliage ( Arbol *Arbol1)
59{
60        int h=0;
61
62
63        MiArbol = Arbol1;
64
65        Acth  = new Activas[(MiArbol->nHojas)*8];
66
67        for ( h=0; h < MiArbol->nHojas; h++) {
68
69                Acth[h].indice = h;
70                if ( h != 0)
71                {
72                        Acth[h].prev = (h-1);
73                        Acth[h-1].next = h;
74                }
75
76        }
77                               
78        ppio = 0;
79        final = (MiArbol->nHojas)-1;
80        nhactivas = MiArbol->nHojas;
81
82
83
84}*/
85
86//--------------------------------------------------------------------------------------------------------------------------------
87// Destructor. We must deallocate the memory allocated for pointers to vertices and edges
88//--------------------------------------------------------------------------------------------------------------------------------
89Foliage::~Foliage (void)
90{
91        if (vertexdata) delete vertexdata;
92        if (indexdata) delete indexdata;
93        delete[] Leaves;
94        delete MinDet;
95        delete Acth;
96}
97//
98
99//--------------------------------------------------------------------------------------------------------------------------------
100// Dibuja una hoja
101//--------------------------------------------------------------------------------------------------------------------------------
102                                /*
103void Foliage::Dibuja_Hoja ( Hoja &aHoja)
104{
105       
106        float a,b,c;
107
108        glBegin (GL_TRIANGLE_STRIP);
109                glNormal3fv (aHoja.Normal);
110                MiArbol->Vertices[aHoja.vertsLeaf[0]].GetCoordinates (a,b,c);
111                glTexCoord2f ( 0.0, 0.0);
112
113                glVertex3f (a,b,c);
114
115                MiArbol->Vertices[aHoja.vertsLeaf[1]].GetCoordinates (a,b,c);
116                glTexCoord2f ( 0.0, 1.0);
117                glVertex3f (a,b,c);
118       
119                MiArbol->Vertices[aHoja.vertsLeaf[2]].GetCoordinates (a,b,c);
120                glTexCoord2f ( 1.0, 0.0);
121                glVertex3f (a,b,c);
122
123                MiArbol->Vertices[aHoja.vertsLeaf[3]].GetCoordinates (a,b,c);
124                glTexCoord2f ( 1.0, 1.0);
125                glVertex3f (a,b,c);
126
127        glEnd ();
128
129}
130
131
132//--------------------------------------------------------------------------------------------------------------------------------
133// Dibujar todas las hojas activas
134//--------------------------------------------------------------------------------------------------------------------------------
135                               
136int Foliage::DibujarHojas (float frustum[6][4], float zoom)
137{
138        int hojactiva, i;
139
140
141        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
142
143        hojactiva = 0;
144        glBindTexture (GL_TEXTURE_2D,texName[0]);
145        glEnable(GL_ALPHA_TEST);
146
147        if ( zoom <75.0)
148        //if ( false)
149        {
150                //calculo el culling de las hojas de minimo detalle
151                Culling (frustum);
152                //printf ( "--------------------------->Entro en culling\n");
153
154                i= ppio;
155                while ( i != -1)
156                {
157                        if (MiArbol->Hojas[MiArbol->Hojas[i].raiz].visible == 1)
158                        {
159                                hojactiva ++;
160                                Dibuja_Hoja (MiArbol->Hojas[i]);
161                        }
162                        i = Acth[i].next;
163                }
164        }
165        else
166        {
167                i= ppio;
168        while ( i != -1)
169                {
170                        hojactiva ++;
171                        Dibuja_Hoja (MiArbol->Hojas[i]);
172                        i=Acth[i].next;
173                }
174
175        }
176
177
178
179        glDisable(GL_ALPHA_TEST);
180
181        return ( hojactiva);
182
183}
184
185//--------------------------------------------------------------------------------------------------------------------------------
186// Dibujar media copa del arbol
187// si el char c es 'f' dibuja la parte delantera
188// si es 'b' la parte trasera
189//--------------------------------------------------------------------------------------------------------------------------------
190                               
191int Foliage::DibujaMediaCopa (  char c, float rva, float rvb,float rvc, float rvd, float radio)
192{
193        int hojactiva, i;
194//      int count, count2;
195
196        glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
197
198        hojactiva = 0;
199        glBindTexture (GL_TEXTURE_2D,texName[0]);
200        glEnable(GL_ALPHA_TEST);
201
202        i= ppio;
203        if ( c == 'b')
204        {//count = 0;
205        while ( i != -1)
206        {  //count ++;
207                        if ( Criterio (MiArbol->Hojas[i],'p', rva,rvb,rvc,rvd, radio) == false) //esta detras del plano
208                {
209                        hojactiva ++;
210                        Dibuja_Hoja (MiArbol->Hojas[i]);
211                        }
212                i = Acth[i].next;
213
214        }
215//      printf ( " EN LA TRASERA chequeo %i hojas \n", count);
216        }
217        else
218        {// count2 = 0;
219
220                while ( i != -1)
221                { //count2++;
222                        if ( Criterio (MiArbol->Hojas[i],'p', rva,rvb,rvc,rvd, radio) == true) //esta detras del plano
223                {
224                        hojactiva ++;
225                        Dibuja_Hoja (MiArbol->Hojas[i]);
226                        }
227                i = Acth[i].next;
228        }
229                //      printf ( " EN LA DELANTERA chequeo %i hojas \n", count2);
230
231        }
232        glDisable(GL_ALPHA_TEST);
233
234//      nhactivas = hojactiva;
235
236
237        return ( hojactiva);
238
239
240
241}
242
243
244
245//-------------------------------------------------------------------------------------------------------------------------
246// dibuja la esfera
247//-------------------------------------------------------------------------------------------------------------------------
248
249void Foliage::DibujaEsfera(float r)
250{
251
252        glPushMatrix();
253                glTranslated (0.0,MiArbol->cy, 0.0);
254                glColor3f(1.0, 0.0, 0.0);
255                glDisable(GL_LIGHTING);
256                glLineWidth(1.2);
257                glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
258                glutSolidSphere ( r,20, 20);
259                glEnable(GL_LIGHTING);
260                glColor3f ( 1.0, 1.0, 1.0);
261        glPopMatrix();
262
263}
264
265/******************************************   CULLING **************************************************/
266//-------------------------------------------------------------------------------------------------------------------------
267// culling de las hojas de menor detalle
268//-------------------------------------------------------------------------------------------------------------------------
269
270void Foliage::Culling (float frustum[6][4])
271{
272        int i, j;
273
274        i = 0;
275        while ( MinDet[i].index != -1)
276        {
277                j = MinDet[i].index;
278                if (LeafinFrustum ( j, frustum) == true) {
279                //      printf("dentro %i y el padre es %i\n", j, MiArbol->Hojas[MiArbol->MinDet[i].indice].parent);
280                        Leaves[j].visible = 1;
281                }
282                else 
283                {
284                //      printf("fuera %i\n", j);
285                        Leaves[j].visible = 0;
286                }
287
288                i++;
289        }
290
291}
292
293
294//-------
295// si está en el frustrum --> true
296//      sino --> false
297//------------
298
299
300bool Foliage::LeafinFrustum ( int i, float frustum[6][4])
301{
302        float a,b,c, tmp;
303        int j,p;
304        Vector v;
305
306        for (j =0; j<4; j++)
307        {
308//              MiArbol->Vertices[MiArbol->Hojas[i].vertsLeaf[j]].GetCoordinates (a,b,c);
309                vertexdata->GetVertexCoord(Leaves[i].vertsLeaf[j],a,b,c);
310                v[0]=a; v[1]=b; v[2]=c; v[3]=1;
311
312                //  el punto resultante será
313                v = mat_aux*v;
314
315                for( p = 0; p < 5; p++ )
316                { tmp = float(frustum[p][0] * v[0] + frustum[p][1] * v[1] + frustum[p][2] * v[2] + frustum[p][3]);
317                  if ( tmp <= -2.0 )  return false;
318                }
319        }
320
321
322        return true;
323
324}
325
326
327/******************************************   CRITERIO **************************************************/
328
329
330
331//---------------------------------------------------------------------------------------------------------
332// es activo?
333//---------------------------------------------------------------------------------------------------------
334
335bool Foliage::IsActive (int num) const
336{
337        return ( (Acth[num].prev != -1) || (Acth[num].next != -1));
338}
339
340
341//--------------------------------------------------------------------------------------------------------------------------------
342// Colapsa dos hojas
343//--------------------------------------------------------------------------------------------------------------------------------
344                               
345void Foliage::ColapsaHoja (  int h,   int& post)
346{
347
348        //actualizo los punteros del ppio y del final, y post
349        while (( ppio == Leaves[h].childLeft) || ( ppio == Leaves[h].childRight))
350                ppio = Acth[ppio].next;
351        while (( final == Leaves[h].childLeft) || ( final == Leaves[h].childRight))
352                 final = Acth[final].prev;
353
354        while (( post == Leaves[h].childLeft) || ( post == Leaves[h].childRight))
355                 post = Acth[post].next;
356
357
358
359        //COLAPSAR = QUITO DOS DE ACTIVOS PONGO UNO A ACTIVO
360
361        if (Acth[Leaves[h].childLeft].next != -1)
362                Acth[Acth[Leaves[h].childLeft].next].prev = Acth[Leaves[h].childLeft].prev;
363        if (Acth[Leaves[h].childLeft].prev != -1)
364                Acth[Acth[Leaves[h].childLeft].prev].next = Acth[Leaves[h].childLeft].next;
365
366        if (Acth[Leaves[h].childRight].next != -1)
367                Acth[Acth[Leaves[h].childRight].next].prev = Acth[Leaves[h].childRight].prev;
368        if (Acth[Leaves[h].childRight].prev != -1)
369                Acth[Acth[Leaves[h].childRight].prev].next = Acth[Leaves[h].childRight].next;
370
371       
372        //  desconecto a los hijos
373        Acth[Leaves[h].childLeft].prev = -1;
374        Acth[Leaves[h].childLeft].next = -1;
375        Acth[Leaves[h].childRight].prev = -1;
376        Acth[Leaves[h].childRight].next = -1;
377
378        //añado al final
379        Acth[h].prev = final;
380        Acth[h].next = -1;
381        Acth[final].next = h;
382        final = h;
383
384       
385}
386
387//--------------------------------------------------------------------------------------------------------------------------------
388// Splitea una hoja
389//--------------------------------------------------------------------------------------------------------------------------------
390                               
391void Foliage::SplitHoja (int h, int &post)
392{
393
394        // SPLITEAR: QUITO UNO, PONGO DOS
395       
396        if ( ppio == h) ppio = Acth[h].next;
397        if ( final == h) final = Acth[h].prev;
398
399        // 1 enlazo los hijos
400        Acth[Leaves[h].childLeft].next = Leaves[h].childRight;
401        Acth[Leaves[h].childRight].prev = Leaves[h].childLeft;
402        Acth[Leaves[h].childRight].next = -1;
403
404        //y desconecto al padre
405        if ( Acth[h].prev != -1)
406                Acth[Acth[h].prev].next = Acth[h].next;
407        if ( Acth[h].next != -1)
408                Acth[Acth[h].next].prev = Acth[h].prev;
409        Acth[h].prev = -1;
410        Acth[h].next = -1;
411       
412        //añado al final los hijos
413        Acth[Leaves[h].childLeft].prev = final;
414        Acth[final].next = Leaves[h].childLeft;
415        final = Leaves[h].childRight;
416
417}
418
419
420
421
422
423//-------------------------------------------------------------------------------------------
424// fuerzo a splitear
425// hoja -> es el nodo que cumple la condicion
426// padre -> es el nodo que chequeaba de la lista de activos
427// hpost -> es el nodo siguiente en la lista de activos
428//-------------------------------------------------------------------------------------------
429
430bool Foliage::Forzar_Split(int hoja, int padre, int &hpost)
431{
432       
433        int h;
434        lista <int> listah;
435        bool bien;
436
437        bien = false;
438        h = Leaves[hoja].parent;
439        listah.Inserta (h); // Inserta un dato en la lista
440
441        while ( h != padre)
442        {
443                h = Leaves[h].parent;
444                listah.Inserta (h); // Inserta un dato en la lista
445        }
446
447
448        while  (listah.NDatos () != 0)
449        {
450                h = listah.GetCola ();//coger el padre más alto
451                SplitHoja(h, hpost);
452                listah.Borra(h);
453       
454        }
455
456        if ( listah.NDatos() == 0)
457                bien = true;
458        return (bien);
459               
460}               
461
462
463
464
465/******************************************   RESOLUCIÓN CONSTANTE **************************************************/
466
467
468int Foliage::ResolucionC (float dist, float dmax, float dmin)
469{
470
471        float difdist,  d;
472        int difhojas, nhojas;
473
474        if (( dist > dmin) && (dist <dmax))
475        {
476                difdist = dmax - dmin;
477                difhojas = nHojas - minHojas;
478
479                d = dist - dmin;
480
481                //regla de 3
482
483                nhojas = int(abs(d*difhojas /difdist));
484
485                AjusteHojas ((nHojas)-nhojas);
486
487        }
488        else if (dist > dmax) {
489                AjusteHojas (minHojas);
490        }
491        else if (dist < dmin) {
492                AjusteHojas (nHojas);
493        }
494               
495        return (nhactivas);
496}
497
498
499
500void Foliage::AjusteHojas( int nhojas)
501{
502        if ((nhojas <= nHojas) && (nhojas >= minHojas))
503        {
504                if ( nhojas < nhactivas) {
505                        RCecol ( nhactivas - nhojas);
506                }
507                else  {
508                         RCsplit (nhojas-nhactivas );
509                }
510
511                nhactivas = nhojas;
512        //      printf ( "ajusto a %i\n", nhactivas );
513
514        }
515
516}
517
518
519void Foliage::RCecol ( int num)
520{
521
522        int  j, h;
523
524        j = num;
525        h = final+1;
526                while ((h<=TotHojas) && (j>0))
527                {
528                        while (( ppio == Leaves[h].childLeft) || ( ppio == Leaves[h].childRight))
529                                ppio = Acth[ppio].next;
530
531                        while (( final == Leaves[h].childLeft) || ( final == Leaves[h].childRight))
532                                final = Acth[final].prev;
533
534
535                        //COLAPSAR = QUITO DOS DE ACTIVOS PONGO UNO A ACTIVO
536
537                        if (Acth[Leaves[h].childLeft].next != -1)
538                                Acth[Acth[Leaves[h].childLeft].next].prev = Acth[Leaves[h].childLeft].prev;
539                        if (Acth[Leaves[h].childLeft].prev != -1)
540                                Acth[Acth[Leaves[h].childLeft].prev].next = Acth[Leaves[h].childLeft].next;
541
542                        if (Acth[Leaves[h].childRight].next != -1)
543                                Acth[Acth[Leaves[h].childRight].next].prev = Acth[Leaves[h].childRight].prev;
544                        if (Acth[Leaves[h].childRight].prev != -1)
545                                Acth[Acth[Leaves[h].childRight].prev].next = Acth[Leaves[h].childRight].next;
546
547       
548                        //  desconecto a los hijos
549                        Acth[Leaves[h].childLeft].prev = -1;
550                        Acth[Leaves[h].childLeft].next = -1;
551                        Acth[Leaves[h].childRight].prev = -1;
552                        Acth[Leaves[h].childRight].next = -1;
553
554                        //añado al final
555                        Acth[h].prev = final;
556                        Acth[h].next = -1;
557                        Acth[final].next = h;
558                        final = h;
559
560
561                        // decremento el contador de colapsos
562                        j--;
563                        //incremento el posible siguiente colapso
564                        h++;
565
566
567                }
568
569}
570
571/*
572void Foliage::RCsplit (  int nhojas)
573{
574        int i, hpost;
575
576        i = ppio;
577       
578        while (( i != -1) && (nhojas>0)){
579                hpost = Acth[i].next;
580
581                if (MiArbol->Leaves[i].childLeft != -1){
582                                        SplitHoja(i, hpost);
583                                        nhojas --;
584
585                }
586               
587                i = hpost;
588
589        }
590}
591*/
592
593int Foliage::AnteriorActivo (int h)
594{
595        int i;
596
597       
598        if (ppio > h)  i = -1;
599        else
600        {
601                i = h--;
602       
603                while (IsActive(i) == false)   
604                        i--;
605        }
606
607        return (i);
608}
609
610int Foliage::PosteriorActivo (int h)
611{       int i;
612
613        i = h++;
614       
615        while ((IsActive(i) == false) || (i> TotHojas))
616                i++;
617
618        if (i > TotHojas) i=-1;
619
620
621        return (i);
622}
623
624void Foliage::RCsplit ( int num)
625{
626
627        int  j, h, ant, post;
628
629        j = num;
630        h = final;
631        while ((h > nHojas) &&  (j>0))
632        {
633
634                ///////////// insertar a los hijos en orden segun su indice
635                //hijo izquierdo
636                ant = AnteriorActivo (Leaves[h].childLeft);
637                post = PosteriorActivo (Leaves[h].childLeft);
638
639
640                Acth[Leaves[h].childLeft].next = post;
641                Acth[Leaves[h].childLeft].prev = ant;
642                if (ant != -1) Acth[ant].next = Leaves[h].childLeft;
643                        else ppio = Leaves[h].childLeft;
644                if (post != -1) Acth[post].prev = Leaves[h].childLeft;
645
646               
647                //hijo derecho
648                ant = AnteriorActivo (Leaves[h].childRight);
649                post = PosteriorActivo (Leaves[h].childRight);
650
651
652                Acth[Leaves[h].childRight].next = post;
653                Acth[Leaves[h].childRight].prev = ant;
654                if (ant != -1)  Acth[ant].next = Leaves[h].childRight;
655                                else ppio = Leaves[h].childRight;
656                if (post != -1) Acth[post].prev = Leaves[h].childRight;
657
658
659                // despues de insertar los hijos miro a ver cual spliteare el siguiente
660                final = Acth[h].prev;
661               
662                //y desconecto al padre
663                if ( Acth[h].prev != -1)
664                        Acth[Acth[h].prev].next = Acth[h].next;
665                //if ( Acth[h].next != -1)
666                //      Acth[Acth[h].next].prev = Acth[h].prev;
667                Acth[h].prev = -1;
668                Acth[h].next = -1;
669       
670                // decremento el contador de colapsos
671                j--;
672                //incremento el posible siguiente colapso
673                h--;
674
675
676        }
677
678
679 }
680
681void Foliage::ReadVertices(const Geometry::SubMesh *submesh)
682{
683        int countv= int(submesh->mVertexBuffer->mVertexCount);
684        vertexdata = create_vertex_data_func(2*countv);
685        Leaves = new Leaf[countv*2];
686        indexdata = create_index_data_func(countv*2*3); // 3 indices x 2 triangulos x hoja
687       
688        vertexdata->Begin();
689        for (int i=0; i<countv; i++)
690        {
691                vertexdata->SetVertexCoord( i,  submesh->mVertexBuffer->mPosition[i].x,
692                                                                                submesh->mVertexBuffer->mPosition[i].y,
693                                                                                submesh->mVertexBuffer->mPosition[i].z );
694        }
695        vertexdata->End();
696
697        TotVerts = countv;
698}
699
700
701void Foliage::GetNormalH (Leaf &aleaf)
702{
703
704        float onex, oney, onez;
705        float twox, twoy, twoz;
706        float threex, threey, threez;
707
708/*      Vertices[aHoja.vertsLeaf[0]].GetCoordinates (onex, oney, onez);
709        Vertices[aHoja.vertsLeaf[1]].GetCoordinates(twox, twoy, twoz);
710        Vertices[aHoja.vertsLeaf[2]].GetCoordinates (threex, threey, threez);*/
711
712        vertexdata->GetVertexCoord(aleaf.vertsLeaf[0],onex,oney,onez);
713        vertexdata->GetVertexCoord(aleaf.vertsLeaf[1],twox,twoy,twoz);
714        vertexdata->GetVertexCoord(aleaf.vertsLeaf[2],threex,threey,threez);
715
716        float v1[3]={twox-onex,twoy-oney,twoz-onez};
717        float v2[3]={threex-onex,threey-oney,threez-onez};
718
719        Normalize(v1,v1);
720        Normalize(v2,v2);
721       
722//      aleaf.Normal[0] = (twoz-onez)*(threey-oney) - (twoy-oney)*(threez-onez);
723//      aleaf.Normal[1] = (twox-onex)*(threez-onez) - (threex-onex)*(twoz-onez);
724//      aleaf.Normal[2] = (threex-onex)*(twoy-oney) - (twox-onex)*(threey-oney);
725
726        CrossProduct(v1,v2,aleaf.normal);
727       
728}
729
730void Foliage::CrossProduct(const float *v1, const float *v2, float *res)
731{
732        res[0] = v1[1]*v2[2] - v1[2]*v2[1];
733        res[1] = v1[2]*v2[0] - v1[0]*v2[2];
734        res[2] = v1[0]*v2[1] - v1[1]*v2[0];
735}
736
737void Foliage::Normalize(const float *v, float *res)
738{
739        float module=sqrtf(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
740        res[0]=v[0]/module;
741        res[1]=v[1]/module;
742        res[2]=v[2]/module;
743}
744
745void Foliage::ReadLeafs(const Geometry::SubMesh *submesh)
746{
747        int numtris = int(submesh->mIndexCount / 3);
748        nHojas =  numtris / 2;
749        for (int h=0; h<nHojas; h++)
750        {
751                Leaves[h].vertsLeaf[0] = submesh->mIndex[h*6+0];
752                Leaves[h].vertsLeaf[1] = submesh->mIndex[h*6+1];
753                Leaves[h].vertsLeaf[2] = submesh->mIndex[h*6+2];
754                Leaves[h].vertsLeaf[3] = submesh->mIndex[h*6+5];
755                Leaves[h].visible = 0;
756
757                GetNormalH ( Leaves[h]);
758        }
759}
760
761/// returns the number of total leafs
762bool Foliage::ReadSimpSeq(const char *simpSeqFile)
763{
764        FILE* fp_simpli;
765        char linea[256];
766        int v0, v1, v2, v3, tn, tv1,tv2, e=0;
767
768        if ((fp_simpli = fopen (simpSeqFile, "r")) == NULL)
769        {
770                printf ("No he podido abrir el fichero %s\n", simpSeqFile);
771                return false;
772        }
773        else
774        {
775                tn = nHojas;
776                while (fgets (linea, 255, fp_simpli) != NULL)
777                {
778                        if (linea[0]<'0' || linea[0]>'9')
779                                continue;
780               
781                        //N 446 Ver 10176 10178 10169 10171  Tv 156 154 E 2
782
783//                      sscanf(linea, "%s %lu %s %lu %lu  %lu %lu %s %lu %lu %s %i",
784//                              &str, &tn, &str, &v0, &v1, &v2, &v3, &str, &tv1, &tv2, &str, &e );
785                        long int triviej00=-1, triviej01=-1;
786                        long int triviej10=-1, triviej11=-1;
787                        sscanf(linea, "%lu %lu %lu %lu & %lu %lu %lu %lu", &triviej00,&triviej01,&triviej10,&triviej11, &v0,&v1,&v2,&v3);
788
789                        Leaves[tn].vertsLeaf[0] = v0;
790                        Leaves[tn].vertsLeaf[1] = v1;
791                        Leaves[tn].vertsLeaf[2] = v2;
792                        Leaves[tn].vertsLeaf[3] = v3;
793
794                        Leaves[tn].visible = 0;
795
796                        GetNormalH  (Leaves[tn]);
797
798                        tv1 = triviej00/2;
799                        tv2 = triviej10/2;
800
801                        Leaves[tn].childLeft= tv1;
802                        Leaves[tn].childRight= tv2;
803
804                        Leaves[tv1].parent = tn;
805                        Leaves[tv2].parent = tn;               
806
807                        tn++;
808                }
809
810        }
811
812        fclose(fp_simpli);
813
814        TotHojas=tn;
815               
816        return true;
817}
818
819void Foliage::RellenoRaiz (void)
820{
821        int i,j, k, t, cont;
822        bool esta, fin;
823
824        i=0;
825        k=-1;
826        cont =-1;
827
828        MinDet  = new ActiveLeafNode[nHojas*2];
829       
830        while (i<TotHojas)
831        {
832                j=i;
833                while (Leaves[j].parent > -1) j= Leaves[j].parent;
834                Leaves[i].root = j;
835
836
837                // para la estructura MinDet
838                if ( k == -1){
839                        k++;
840                        MinDet[k].index = j;
841                        cont =k;
842                }
843                else
844                {
845                        t = 0;
846                        esta = false;
847                        fin = false;
848
849                        while (( fin == false) && (esta == false))
850                        {       if ( MinDet[t].index == j) esta = true;
851                                        else t++;
852                                if (MinDet[t].index == -1) fin = true;
853                        }
854
855                        if ( esta == false) //añado al final
856                        {
857                                cont++;
858                                MinDet[cont].index = j;
859                        }
860                }
861
862        i++;
863        }       
864
865        minHojas = cont;
866
867}
868
869void Foliage::CalculaTexCoordsYNorms(void)
870{       
871        vertexdata->Begin();
872        for (int i=0; i<nHojas; i++)
873        {       
874                const float* lanormal = Leaves[i].normal;
875                vertexdata->SetVertexNormal(Leaves[i].vertsLeaf[0], lanormal[0], lanormal[1], lanormal[2]);
876                vertexdata->SetVertexTexCoord(Leaves[i].vertsLeaf[0], 0.0f, 1.0f);
877
878                vertexdata->SetVertexNormal(Leaves[i].vertsLeaf[1], lanormal[0], lanormal[1], lanormal[2]);
879                vertexdata->SetVertexTexCoord(Leaves[i].vertsLeaf[1], 0.0f, 0.0f);
880
881                vertexdata->SetVertexNormal(Leaves[i].vertsLeaf[2], lanormal[0], lanormal[1], lanormal[2]);
882                vertexdata->SetVertexTexCoord(Leaves[i].vertsLeaf[2], 1.0f, 1.0f);
883
884                vertexdata->SetVertexNormal(Leaves[i].vertsLeaf[3], lanormal[0], lanormal[1], lanormal[2]);
885                vertexdata->SetVertexTexCoord(Leaves[i].vertsLeaf[3], 1.0f, 0.0f);
886
887
888/*              MiArbol->Vertices[aHoja.vertsLeaf[1]].GetCoordinates (a,b,c);
889                glTexCoord2f ( 0.0, 1.0);
890                glVertex3f (a,b,c);
891       
892                MiArbol->Vertices[aHoja.vertsLeaf[2]].GetCoordinates (a,b,c);
893                glTexCoord2f ( 1.0, 0.0);
894                glVertex3f (a,b,c);
895
896                MiArbol->Vertices[aHoja.vertsLeaf[3]].GetCoordinates (a,b,c);
897                glTexCoord2f ( 1.0, 1.0);
898                glVertex3f (a,b,c);*/
899        }
900        vertexdata->End();
901}
902
903Foliage::Foliage(const Foliage *ar)
904{
905        nHojas=ar->nHojas;
906        MinDet  = new ActiveLeafNode[nHojas*2];
907//      for (unsigned int i=0; i<nHojas*2; i++)
908//              MinDet[i]=ar->MinDet[i];
909        memcpy(MinDet,ar->MinDet,sizeof(ActiveLeafNode)*nHojas*2);
910        TotHojas=ar->TotHojas;
911        cx=ar->cx;
912        cy=ar->cy;
913        cz=ar->cz;
914        rad=ar->rad;
915        minHojas=ar->minHojas;
916        TotVerts=ar->TotVerts;
917
918        create_vertex_data_func=ar->create_vertex_data_func;
919        create_index_data_func=ar->create_index_data_func;
920        vertexdata=create_vertex_data_func(ar->vertexdata->GetNumVertices());
921        vertexdata->Begin();
922        for (unsigned int i=0; i<vertexdata->GetNumVertices(); i++)
923        {
924                float va,vb,vc;
925                ar->vertexdata->GetVertexCoord(i,va,vb,vc);
926                vertexdata->SetVertexCoord(i,va,vb,vc);
927                ar->vertexdata->GetVertexNormal(i,va,vb,vc);
928                vertexdata->SetVertexNormal(i,va,vb,vc);
929        }
930        vertexdata->End();
931        indexdata=create_index_data_func(ar->indexdata->GetNumMaxIndices());
932        indexdata->Begin();
933        for (unsigned int i=0; i<indexdata->GetNumMaxIndices(); i++)
934                indexdata->SetIndex(i,ar->indexdata->GetIndex(i));
935        indexdata->End();
936
937        Leaves=new Leaf[vertexdata->GetNumVertices()];
938//      for (unsigned int i=0; i<vertexdata->GetNumVertices(); i++)
939//              Leaves[i]=ar->Leaves[i];
940        memcpy(Leaves,ar->Leaves,sizeof(Leaf)*vertexdata->GetNumVertices());
941
942        // esto no sé si devería haber akí
943        indexdata->SetNumValidIndices(0);
944
945        int h=0;
946
947        Acth  = new ActiveLeafNode[nHojas*8];
948
949        for ( h=0; h < nHojas; h++) {
950
951                Acth[h].index = h;
952                if ( h != 0)
953                {
954                        Acth[h].prev = (h-1);
955                        Acth[h-1].next = h;
956                }
957
958        }
959                               
960        ppio = 0;
961        final = nHojas-1;
962        nhactivas = nHojas;
963
964}
Note: See TracBrowser for help on using the repository browser.