Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 27, 2006, 3:35:01 PM (18 years ago)
Author:
bottac
Message:

further reformating

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/bsp_model/src/lib/graphics/importer/bsp_file.cc

    r7385 r7395  
    11/*
    22   orxonox - the future of 3D-vertical-scrollers
    3 
     3 
    44   Copyright (C) 2006 orx
    5 
     5 
    66   This program is free software; you can redistribute it and/or modify
    77   it under the terms of the GNU General Public License as published by
    88   the Free Software Foundation; either version 2, or (at your option)
    99   any later version.
    10 
     10 
    1111   ### File Specific:
    1212   main-programmer: bottac@ee.ethz.ch
     
    3434// Constructor
    3535BspFile::BspFile()
    36 {
    37 
    38 }
     36{}
    3937
    4038int BspFile::read(char* name)
     
    4442  struct stat results;
    4543  name = "/root/data/Kanti175.bsp";
    46  
    47    if (stat( name , &results) == 0)
    48   {
    49      PRINTF(0)("BSP FILE: Datei gefunden. \n");
    50      ifstream bspFile (name, ios::in | ios::binary);
    51      bspFile.read(this->header, 260);
    52      PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
    53      if((((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
    54      else    PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");
    55 
    56      // Get the Nodes
    57     offset = ((int *)(header) )[8];
    58     size    = ((int *)(header))[9];
    59     PRINTF(0)("BSP FILE: NodeSize: %i Bytes. \n", size);
    60     PRINTF(0)("BSP FILE: NumNodes: %i. \n", size / 36);
    61     PRINTF(0)("BSP FILE: Remainder: %i. \n", size %36);
    62     PRINTF(0)("BSP FILE: NodeOffset: %i. \n", offset);
    63     this->numNodes = size/36;
    64     this->nodes = new char [size];
    65     bspFile.seekg(offset);
    66     bspFile.read(this->nodes, size);
    67 
    68      // and their Planes
    69     offset = ((int *)(header) )[6];
    70     size    = ((int *)(header))[7];
    71     PRINTF(0)("BSP FILE: PlanesSize: %i Bytes. \n", size);
    72     PRINTF(0)("BSP FILE: NumPlanes: %i. \n", size / 16);
    73     PRINTF(0)("BSP FILE: Remainder: %i. \n", size %16);
    74     PRINTF(0)("BSP FILE: PlanesOffset: %i. \n", offset);
    75     this->numPlanes = size/16;
    76     this->planes = new char [size];
    77     bspFile.seekg(offset);
    78     bspFile.read(this->planes, size);
    79 
    80      // Get the Leafs
    81     offset = ((int *)(header) )[10];
    82     size    = ((int *)(header))[11];
    83     PRINTF(0)("BSP FILE: LeaveSize: %i Bytes. \n", size);
    84     PRINTF(0)("BSP FILE: NumLeaves: %i. \n", size / 48);
    85     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 48);
    86     PRINTF(0)("BSP FILE: LeaveOffset: %i. \n", offset);
    87     this->numLeafs = size/48;
    88     this->leaves = new char [size];
    89     bspFile.seekg(offset);
    90     bspFile.read(this->leaves, size);
     44
     45  if (stat( name , &results) == 0) {
     46    PRINTF(0)("BSP FILE: Datei %s gefunden. \n", name);
     47    ifstream bspFile (name, ios::in | ios::binary);
     48    bspFile.read(this->header, 260);
     49    PRINTF(0)("BSP FILE: BSPVersion: %i. \n", ((int *)(header) )[1]);
     50    if(SDL_SwapLE32(((int *)(header) )[1]) == 46)    PRINTF(0)("BSP FILE: This is the good one! :-)  \n");
     51    else  PRINTF(0)("BSP FILE: Wrong BSPVersion.\n");   //!< now, we should do some error handling
     52
     53    // Get the Nodes
     54    offset = SDL_SwapLE32(((int *)(header) )[8]);
     55    size    = SDL_SwapLE32(((int *)(header))[9]);
     56    PRINTF(4)("BSP FILE: NodeSize: %i Bytes. \n", size);
     57    PRINTF(4)("BSP FILE: NumNodes: %i. \n", size / sizeof(node));
     58    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(node));
     59    PRINTF(4)("BSP FILE: NodeOffset: %i. \n", offset);
     60    this->numNodes = size/sizeof(node);
     61    this->nodes = new node [this->numNodes];
     62    bspFile.seekg(offset);
     63    bspFile.read((char*)this->nodes, size);
     64
     65    // and their Planes
     66    offset = SDL_SwapLE32(((int *)(header) )[6]);
     67    size    = SDL_SwapLE32(((int *)(header))[7]);
     68    PRINTF(4)("BSP FILE: PlanesSize: %i Bytes. \n", size);
     69    PRINTF(4)("BSP FILE: NumPlanes: %i. \n", size / sizeof(plane));
     70    PRINTF(4)("BSP FILE: Remainder: %i. \n", sizeof(plane));
     71    PRINTF(4)("BSP FILE: PlanesOffset: %i. \n", offset);
     72    this->numPlanes = size/sizeof(plane);
     73    this->planes = new plane [this->numPlanes];
     74    bspFile.seekg(offset);
     75    bspFile.read((char*)this->planes, size);
     76
     77    // Get the Leafs
     78    offset = SDL_SwapLE32(((int *)(header) )[10]);
     79    size    = SDL_SwapLE32(((int *)(header))[11]);
     80    PRINTF(4)("BSP FILE: LeaveSize: %i Bytes. \n", size);
     81    PRINTF(4)("BSP FILE: NumLeaves: %i. \n", size / sizeof(leaf));
     82    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(leaf));
     83    PRINTF(4)("BSP FILE: LeaveOffset: %i. \n", offset);
     84    this->numLeafs = size/sizeof(leaf);
     85    this->leaves = new leaf [this->numLeafs];
     86    bspFile.seekg(offset);
     87    bspFile.read((char*)this->leaves, size);
    9188
    9289    // Get the Models
    93     offset = ((int *)(header))[16];
    94     size    = ((int *)(header))[17];
    95     PRINTF(0)("BSP FILE: ModelsSize: %i Bytes. \n", size);
    96     PRINTF(0)("BSP FILE: NumModels: %i. \n", size / 40);
    97     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 40);
    98     PRINTF(0)("BSP FILE: ModelsOffset: %i. \n", offset);
    99     this->numBspModels = size/40;
    100     this->bspModels = new char [size];
    101     bspFile.seekg(offset);
    102     bspFile.read(this->bspModels, size);
     90    offset = SDL_SwapLE32(((int *)(header))[16]);
     91    size    = SDL_SwapLE32(((int *)(header))[17]);
     92    PRINTF(4)("BSP FILE: ModelsSize: %i Bytes. \n", size);
     93    PRINTF(4)("BSP FILE: NumModels: %i. \n", size / sizeof(model));
     94    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(model));
     95    PRINTF(4)("BSP FILE: ModelsOffset: %i. \n", offset);
     96    this->numBspModels = size/sizeof(model);
     97    this->bspModels = new model [this->numBspModels];
     98    bspFile.seekg(offset);
     99    bspFile.read((char*)this->bspModels, size);
    103100
    104101    // Get the leafFaces
    105     offset = ((int *)(header))[12];
    106     size    = ((int *)(header))[13];
    107     PRINTF(0)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
    108     PRINTF(0)("BSP FILE: NumleafFaces: %i. \n", size / 4);
    109     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 4);
    110     PRINTF(0)("BSP FILE: leafFacesOffset: %i. \n", offset);
     102    offset = SDL_SwapLE32(((int *)(header))[12]);
     103    size    = SDL_SwapLE32(((int *)(header))[13]);
     104    PRINTF(4)("BSP FILE: leafFacesSize: %i Bytes. \n", size);
     105    PRINTF(4)("BSP FILE: NumleafFaces: %i. \n", size / 4);
     106    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
     107    PRINTF(4)("BSP FILE: leafFacesOffset: %i. \n", offset);
    111108    this->numLeafFaces = size/4;
    112109    this->leafFaces = new char [size];
     
    115112
    116113    // Get the leafBrushes
    117     offset = ((int *)(header))[14];
    118     size    = ((int *)(header))[15];
    119     PRINTF(0)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
    120     PRINTF(0)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
    121     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 4);
    122     PRINTF(0)("BSP FILE: leafBrushesOffset: %i. \n", offset);
     114    offset = SDL_SwapLE32(((int *)(header))[14]);
     115    size    = SDL_SwapLE32(((int *)(header))[15]);
     116    PRINTF(4)("BSP FILE: leafBrushesSize: %i Bytes. \n", size);
     117    PRINTF(4)("BSP FILE: NumleafBrushes: %i. \n", size / 4);
     118    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
     119    PRINTF(4)("BSP FILE: leafBrushesOffset: %i. \n", offset);
    123120    this->numLeafBrushes = size/4;
    124121    this->leafBrushes = new char [size];
     
    127124
    128125    // Get the brushes
    129     offset = ((int *)(header))[18];
    130     size    = ((int *)(header))[19];
    131     PRINTF(0)("BSP FILE: BrushesSize: %i Bytes. \n", size);
    132     PRINTF(0)("BSP FILE: NumBrushes: %i. \n", size / 12);
    133     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 12);
    134     PRINTF(0)("BSP FILE: BrushesOffset: %i. \n", offset);
     126    offset = SDL_SwapLE32(((int *)(header))[18]);
     127    size    = SDL_SwapLE32(((int *)(header))[19]);
     128    PRINTF(4)("BSP FILE: BrushesSize: %i Bytes. \n", size);
     129    PRINTF(4)("BSP FILE: NumBrushes: %i. \n", size / 12);
     130    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 12);
     131    PRINTF(4)("BSP FILE: BrushesOffset: %i. \n", offset);
    135132    this->brushes = new char [size];
    136133    bspFile.seekg(offset);
     
    138135
    139136    // Get the brushSides
    140     offset = ((int *)(header))[20];
     137    offset =   SDL_SwapLE32(((int *)(header))[20]);
    141138    size    = SDL_SwapLE32(((int *)(header))[21]);
    142     PRINTF(0)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
    143     PRINTF(0)("BSP FILE: NumBrushSides: %i. \n", size / 8);
    144     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 8);
    145     PRINTF(0)("BSP FILE: BrushSidesOffset: %i. \n", offset);
     139    PRINTF(4)("BSP FILE: BrushSidesSize: %i Bytes. \n", size);
     140    PRINTF(4)("BSP FILE: NumBrushSides: %i. \n", size / 8);
     141    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 8);
     142    PRINTF(4)("BSP FILE: BrushSidesOffset: %i. \n", offset);
    146143    this->brushSides = new char [size];
    147144    this->numBrushSides = size/8;
     
    150147
    151148    // Get the Vertice
    152     offset = ((int *)(header))[22];
    153     size    = ((int *)(header))[23];
    154     PRINTF(0)("BSP FILE: VerticeSize: %i Bytes. \n", size);
    155     PRINTF(0)("BSP FILE: NumVertice: %i. \n", size / 44);
    156     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 44);
    157     PRINTF(0)("BSP FILE: VerticeOffset: %i. \n", offset);
     149    offset = SDL_SwapLE32(((int *)(header))[22]);
     150    size    = SDL_SwapLE32(((int *)(header))[23]);
     151    PRINTF(4)("BSP FILE: VerticeSize: %i Bytes. \n", size);
     152    PRINTF(4)("BSP FILE: NumVertice: %i. \n", size / 44);
     153    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 44);
     154    PRINTF(4)("BSP FILE: VerticeOffset: %i. \n", offset);
    158155    this->numVertex = size/44;
    159156    this->vertice = new char [size];
     
    162159
    163160    // Get the MeshVerts
    164     offset = ((int *)(header))[24];
    165     size    = ((int *)(header))[25];
    166     PRINTF(0)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
    167     PRINTF(0)("BSP FILE: NumMeshVerts: %i. \n", size / 4);
    168     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 4);
    169     PRINTF(0)("BSP FILE: MeshVertsOffset: %i. \n", offset);
     161    offset = SDL_SwapLE32(((int *)(header))[24]);
     162    size    = SDL_SwapLE32(((int *)(header))[25]);
     163    PRINTF(4)("BSP FILE: MeshVertsSize: %i Bytes. \n", size);
     164    PRINTF(4)("BSP FILE: NumMeshVerts: %i. \n", size / 4);
     165    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 4);
     166    PRINTF(4)("BSP FILE: MeshVertsOffset: %i. \n", offset);
    170167    this->meshverts = new char [size];
    171168    bspFile.seekg(offset);
     
    173170
    174171    // Get the Faces
    175     offset = ((int *)(header))[28];
    176     size    = ((int *)(header))[29];
    177     PRINTF(0)("BSP FILE: FacesSize: %i Bytes. \n", size);
    178     PRINTF(0)("BSP FILE: NumFaces: %i. \n", size / 104);
    179     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 104);
    180     PRINTF(0)("BSP FILE: FacesOffset: %i. \n", offset);
    181     this->numFaces = size/104;
    182     this->faces = new char [size];
    183     bspFile.seekg(offset);
    184     bspFile.read(this->faces, size);
     172    offset = SDL_SwapLE32(((int *)(header))[28]);
     173    size    = SDL_SwapLE32(((int *)(header))[29]);
     174    PRINTF(4)("BSP FILE: FacesSize: %i Bytes. \n", size);
     175    PRINTF(4)("BSP FILE: NumFaces: %i. \n", size / sizeof(face));
     176    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % sizeof(face));
     177    PRINTF(4)("BSP FILE: FacesOffset: %i. \n", offset);
     178    this->numFaces = size/sizeof(face);
     179    this->faces = new face [this->numFaces];
     180    bspFile.seekg(offset);
     181    bspFile.read((char*)this->faces, size);
    185182
    186183    // Get the Visdata
    187     offset = ((int *)(header))[34];
    188     size    = ((int *)(header))[35];
     184    offset = SDL_SwapLE32(((int *)(header))[34]);
     185    size    = SDL_SwapLE32(((int *)(header))[35]);
    189186
    190187    this->visData = new char [size];
     
    192189    bspFile.read(this->visData, size);
    193190
    194     PRINTF(0)("BSP FILE: VisDataSize: %i Bytes. \n", size);
    195     PRINTF(0)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
    196     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 1);
    197     PRINTF(0)("BSP FILE: VisDataOffset: %i. \n", offset);
     191    PRINTF(4)("BSP FILE: VisDataSize: %i Bytes. \n", size);
     192    PRINTF(4)("BSP FILE: NumVisData: %i. \n", size /1 - 8);
     193    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 1);
     194    PRINTF(4)("BSP FILE: VisDataOffset: %i. \n", offset);
    198195
    199196    // Get the Textures
    200     offset = ((int *)(header))[4];
    201     size    = ((int *)(header))[5];
     197    offset = SDL_SwapLE32(((int *)(header))[4]);
     198    size    = SDL_SwapLE32(((int *)(header))[5]);
    202199
    203200    this->textures= new char [size];
     
    205202    bspFile.read(this->textures, size);
    206203
    207     PRINTF(0)("BSP FILE: TextureSize: %i Bytes. \n", size);
    208     PRINTF(0)("BSP FILE: NumTextures: %i. \n", size /72);
    209     PRINTF(0)("BSP FILE: Remainder: %i. \n", size % 72);
    210     PRINTF(0)("BSP FILE: TextureOffset: %i. \n", offset);
     204    PRINTF(4)("BSP FILE: TextureSize: %i Bytes. \n", size);
     205    PRINTF(4)("BSP FILE: NumTextures: %i. \n", size /72);
     206    PRINTF(4)("BSP FILE: Remainder: %i. \n", size % 72);
     207    PRINTF(4)("BSP FILE: TextureOffset: %i. \n", offset);
    211208    this->numTextures = size/72;
    212209
    213      bspFile.close();
    214        
    215    for(int i = 0 ; i < this->numTextures; i++)
    216    PRINTF(0)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
    217    this->load_textures();
    218 
    219    // Get the number of patches
    220    this->numPatches = 0;
    221    this->patchOffset   = 0;
    222 
    223    for( int i = 0; i < this->numFaces; i++)
    224         {
    225         face& cFace = ((face *)(this->faces))[i];
    226         if (    cFace.type == 2)
    227                 this->numPatches += (cFace.size[0] -1 ) / 2  * (cFace.size[1] -1) / 2;
    228         }
    229 
    230     // Allocate Memory
    231         this->patchVertice = new char[8*8*44*(this->numPatches+10)];
    232         this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
    233         this->patchRowIndexes = new int*[7*4*this->numPatches];
    234         this->patchTrianglesPerRow = new char[7*4*this->numPatches];
    235         this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
     210    bspFile.close();
     211
     212    for(int i = 0 ; i < this->numTextures; i++)
     213      PRINTF(4)("BSP FILE: Texture 0: %s. \n", &this->textures[8+ 72*i]);
     214    this->load_textures();
     215
     216    // Get the number of patches
     217    this->numPatches = 0;
     218    this->patchOffset   = 0;
     219
     220    for( int i = 0; i < this->numFaces; i++) {
     221      face& cFace = ((face *)(this->faces))[i];
     222      if (      cFace.type == 2)
     223        this->numPatches += (cFace.size[0] -1 ) / 2  * (cFace.size[1] -1) / 2;
     224    }
     225
     226    // Allocate Memory
     227    this->patchVertice = new char[8*8*44*(this->numPatches+10)];
     228    this->patchIndexes = new char[7*8*2*4*(this->numPatches+10)];
     229    this->patchRowIndexes = new int*[7*4*this->numPatches];
     230    this->patchTrianglesPerRow = new char[7*4*this->numPatches];
     231    this->VertexArrayModels  = new VertexArrayModel*[this->numPatches];
    236232
    237233    PRINTF(0)("BSP FILE:NumberOfPatches: %i . \n", numPatches);
    238234    // Do tesselation for all Faces of type 2
    239    for( int i = 0; i < this->numFaces; i++)
    240         {
    241         if (    ((face *)(this->faces))[i].type == 2)
    242                 this->tesselate(i);
    243         }
    244 
    245   PRINTF(0)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
    246 
    247      return  1;
     235    for( int i = 0; i < this->numFaces; i++) {
     236      if (      ((face *)(this->faces))[i].type == 2)
     237        this->tesselate(i);
     238    }
     239
     240    PRINTF(0)("BSP FILE:PatchOffset: %i . \n", this->patchOffset);
     241
     242    return  1;
     243  } else {
     244    PRINTF(0)("BSP FILE: Datei nicht gefunden. \n");
     245    return -1;
    248246  }
    249   else
    250   {
    251      PRINTF(0)("BSP FILE: Datei nicht gefunden. \n");
    252      return -1;
    253   }
    254  
     247
    255248}
    256249
     
    258251{
    259252
    260    PRINTF(0)("BSP FILE:\n");
    261    PRINTF(0)("BSP FILE: Building Tree...\n");
    262    root = this->build_tree_rec(0);
    263    PRINTF(0)("BSP FILE: ...done. \n");
    264    PRINTF(0)("BSP FILE:  \n");
    265    PRINTF(0)("BSP FILE: Node #0: \n");
    266    PRINTF(0)("BSP FILE:  x: %f \n",root->plane.x);
    267    PRINTF(0)("BSP FILE:  y: %f\n",root->plane.y);
    268    PRINTF(0)("BSP FILE:  z: %f\n",root->plane.z);
     253  PRINTF(0)("BSP FILE:\n");
     254  PRINTF(0)("BSP FILE: Building Tree...\n");
     255  root = this->build_tree_rec(0);
     256  PRINTF(0)("BSP FILE: ...done. \n");
     257  PRINTF(0)("BSP FILE:  \n");
     258  PRINTF(0)("BSP FILE: Node #0: \n");
     259  PRINTF(0)("BSP FILE:  x: %f \n",root->plane.x);
     260  PRINTF(0)("BSP FILE:  y: %f\n",root->plane.y);
     261  PRINTF(0)("BSP FILE:  z: %f\n",root->plane.z);
    269262}
    270263
    271264BspTreeNode*   BspFile::build_tree_rec(int i)
    272265{
    273    // PRINTF(0)("BSP FILE: Node #%i\n", i);
    274    BspTreeNode* thisNode = new BspTreeNode();
    275    int left =(((node *) nodes) [i]).left;
    276    int right =(((node *) nodes) [i]).right;
    277    int planeIndex = (((node *) nodes) [i]).plane;
    278    float x1 =(((plane *) this->planes) [planeIndex]).x;
    279    float y1 =(((plane *) this->planes) [planeIndex]).y;
    280    float z1 =(((plane *) this->planes) [planeIndex]).z;
    281    thisNode->leafIndex = 0;
    282    thisNode->d         = (((plane *) this->planes) [planeIndex]).d;
    283    
    284    thisNode->plane = Vector(x1,y1,z1);
    285    thisNode->isLeaf = false;
    286 
    287    if(left >= 0)
    288    {
    289        thisNode->left = this->build_tree_rec(left);
    290    }
    291    else
    292    {
    293         //BspTreeLeaf tmp =  BspTreeLeaf();
    294         //tmp.isLeaf = true;
    295         //tmp.leafIndex = -left -1;
    296         //thisNode->left = (BspTreeNode*) (&tmp);
    297         thisNode->left  = new BspTreeNode();
    298         thisNode->left->isLeaf = true;
    299         thisNode->left->leafIndex = - (left +1);
    300         //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-left);
    301    } // assign leav
    302    if(right >= 0)
    303    {
    304        thisNode->right =  this->build_tree_rec(right);
    305    }
    306    else
    307   {
    308         //BspTreeLeaf tmp =  BspTreeLeaf();
    309         //tmp.isLeaf = true;
    310         //tmp.leafIndex = -right -1;
    311         //thisNode->right = (BspTreeNode*) (&tmp);
    312         thisNode->right = new BspTreeNode();
    313         thisNode->right->isLeaf = true;
    314         thisNode->right->leafIndex = -(right +1);
    315         //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-right);
     266  // PRINTF(0)("BSP FILE: Node #%i\n", i);
     267  BspTreeNode* thisNode = new BspTreeNode();
     268  int left =(((node *) nodes) [i]).left;
     269  int right =(((node *) nodes) [i]).right;
     270  int planeIndex = (((node *) nodes) [i]).plane;
     271  float x1 =(((plane *) this->planes) [planeIndex]).x;
     272  float y1 =(((plane *) this->planes) [planeIndex]).y;
     273  float z1 =(((plane *) this->planes) [planeIndex]).z;
     274  thisNode->leafIndex = 0;
     275  thisNode->d          = (((plane *) this->planes) [planeIndex]).d;
     276
     277  thisNode->plane = Vector(x1,y1,z1);
     278  thisNode->isLeaf = false;
     279
     280  if(left >= 0) {
     281    thisNode->left = this->build_tree_rec(left);
     282  } else {
     283    //BspTreeLeaf tmp =  BspTreeLeaf();
     284    //tmp.isLeaf = true;
     285    //tmp.leafIndex = -left -1;
     286    //thisNode->left = (BspTreeNode*) (&tmp);
     287    thisNode->left  = new BspTreeNode();
     288    thisNode->left->isLeaf = true;
     289    thisNode->left->leafIndex = - (left +1);
     290    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-left);
    316291  } // assign leav
    317  return thisNode;
     292  if(right >= 0) {
     293    thisNode->right =  this->build_tree_rec(right);
     294  } else {
     295    //BspTreeLeaf tmp =  BspTreeLeaf();
     296    //tmp.isLeaf = true;
     297    //tmp.leafIndex = -right -1;
     298    //thisNode->right = (BspTreeNode*) (&tmp);
     299    thisNode->right = new BspTreeNode();
     300    thisNode->right->isLeaf = true;
     301    thisNode->right->leafIndex = -(right +1);
     302    //PRINTF(0)("BSP FILE:  LeafIndex: %i\n",-right);
     303  } // assign leaf
     304  return thisNode;
    318305}
    319306
    320307BspTreeNode* BspFile::get_root()
    321308{
    322 return root;
     309  return root;
    323310}
    324311
     
    331318
    332319  this->Materials = new AMat[this->numTextures];
    333   for(int i = 0 ; i < this->numTextures; i++)
    334   {
     320  for(int i = 0 ; i < this->numTextures; i++) {
    335321    PRINTF(0)("BSP FILE: Texture : %s. \n", &this->textures[8+ 72*i]);
    336    
    337      // Check for tga
     322
     323    // Check for tga
    338324    strcpy(fileName, &this->textures[8+ 72*i]);
    339325    strcpy (absFileName,"/root/data/trunk/");
     
    342328    strncat(absFileName,fileName,strlen(fileName));
    343329    // absFileName = ResourceManager::getFullName(fileName);
    344  
    345     if(stat( absFileName , &results) == 0)
    346     {
    347         PRINTF(0)("BSP FILE: gefunden . \n");
    348         this->Materials[i] = this->loadMat(fileName);
    349         continue;
     330
     331    if(stat( absFileName , &results) == 0) {
     332      PRINTF(0)("BSP FILE: gefunden . \n");
     333      this->Materials[i] = this->loadMat(fileName);
     334      continue;
    350335    }
    351336    // Check for TGA
     
    355340    strncat (fileName, ext, strlen(fileName));
    356341    strncat(absFileName,fileName,strlen(fileName));
    357     // absFileName = ResourceManager::getFullName(fileName);
    358 
    359     if(stat( absFileName , &results) == 0)
    360     {
    361         PRINTF(0)("BSP FILE: gefunden . \n");
    362         this->Materials[i] = this->loadMat(fileName);
    363         continue;
     342    // absFileName = ResourceManager::getFullName(fileName);
     343
     344    if(stat( absFileName , &results) == 0) {
     345      PRINTF(0)("BSP FILE: gefunden . \n");
     346      this->Materials[i] = this->loadMat(fileName);
     347      continue;
    364348    }
    365349    // Check for jpg
     
    369353    strncat (fileName, ext, strlen(fileName));
    370354    strncat(absFileName,fileName,strlen(fileName));
    371     // absFileName = ResourceManager::getFullName(fileName);   
    372     if(stat( absFileName , &results) == 0)
    373     {
    374         PRINTF(0)("BSP FILE: gefunden . \n");
    375         this->Materials[i] =this->loadMat(fileName);
    376         continue;
    377     }
    378 
    379 
    380    // Check for JPG
    381    strcpy(fileName, &this->textures[8+ 72*i]);
    382    strcpy (absFileName,"/root/data/trunk/");
    383    strcpy(ext, ".JPG");
    384    strncat (fileName, ext, strlen(fileName));
    385    strncat(absFileName,fileName,strlen(fileName));
    386    // absFileName = ResourceManager::getFullName(fileName);
    387     if(stat( absFileName , &results) == 0)
    388     {
    389         PRINTF(0)("BSP FILE: gefunden . \n");
    390         this->Materials[i] =this->loadMat(fileName);
    391         continue;
    392     }
    393 
    394    // Check for jpeg
     355    // absFileName = ResourceManager::getFullName(fileName);
     356    if(stat( absFileName , &results) == 0) {
     357      PRINTF(0)("BSP FILE: gefunden . \n");
     358      this->Materials[i] =this->loadMat(fileName);
     359      continue;
     360    }
     361
     362
     363    // Check for JPG
     364    strcpy(fileName, &this->textures[8+ 72*i]);
     365    strcpy (absFileName,"/root/data/trunk/");
     366    strcpy(ext, ".JPG");
     367    strncat (fileName, ext, strlen(fileName));
     368    strncat(absFileName,fileName,strlen(fileName));
     369    // absFileName = ResourceManager::getFullName(fileName);
     370    if(stat( absFileName , &results) == 0) {
     371      PRINTF(0)("BSP FILE: gefunden . \n");
     372      this->Materials[i] =this->loadMat(fileName);
     373      continue;
     374    }
     375
     376    // Check for jpeg
    395377    strcpy(fileName, &this->textures[8+ 72*i]);
    396378    strcpy (absFileName,"/root/data/trunk/");
     
    399381    strncat(absFileName,fileName,strlen(fileName));
    400382    // absFileName = ResourceManager::getFullName(fileName);
    401     if(stat( absFileName , &results) == 0)
    402     {
    403         PRINTF(0)("BSP FILE: gefunden . \n");
    404         this->Materials[i] =this->loadMat(fileName);
    405         continue;
     383    if(stat( absFileName , &results) == 0) {
     384      PRINTF(0)("BSP FILE: gefunden . \n");
     385      this->Materials[i] =this->loadMat(fileName);
     386      continue;
    406387    }
    407388
     
    415396    // absFileName = ResourceManager::getFullName(fileName);
    416397    PRINTF(0)("BSP FILE: %s . \n", absFileName);
    417     if(stat( absFileName , &results) == 0)
    418     {
    419         PRINTF(0)("BSP FILE: gefunden . \n");
    420         this->Materials[i] =this->loadMat(fileName);
    421         continue;
    422     }
    423 
    424    // Check for bmp
    425    strcpy(fileName, &this->textures[8+ 72*i]);
    426    strcpy (absFileName,"/root/data/trunk/");
    427    strcpy(ext, ".bmp");
    428    strncat (fileName, ext, strlen(fileName));
    429    strncat(absFileName,fileName,strlen(fileName));
    430    // absFileName = ResourceManager::getFullName(fileName);
    431  
    432     if(stat( absFileName , &results) == 0)
    433     {
    434         PRINTF(0)("BSP FILE: gefunden . \n");
    435         this->Materials[i] =this->loadMat(fileName);
    436         continue;
    437     }
    438 
    439    // Check for BMP
     398    if(stat( absFileName , &results) == 0) {
     399      PRINTF(0)("BSP FILE: gefunden . \n");
     400      this->Materials[i] =this->loadMat(fileName);
     401      continue;
     402    }
     403
     404    // Check for bmp
     405    strcpy(fileName, &this->textures[8+ 72*i]);
     406    strcpy (absFileName,"/root/data/trunk/");
     407    strcpy(ext, ".bmp");
     408    strncat (fileName, ext, strlen(fileName));
     409    strncat(absFileName,fileName,strlen(fileName));
     410    // absFileName = ResourceManager::getFullName(fileName);
     411
     412    if(stat( absFileName , &results) == 0) {
     413      PRINTF(0)("BSP FILE: gefunden . \n");
     414      this->Materials[i] =this->loadMat(fileName);
     415      continue;
     416    }
     417
     418    // Check for BMP
    440419    strcpy(fileName, &this->textures[8+ 72*i]);
    441420    strcpy (absFileName,"/root/data/trunk/");
     
    445424    // absFileName = ResourceManager::getFullName(fileName);
    446425
    447     if(stat( absFileName , &results) == 0)
    448     {
    449         PRINTF(0)("BSP FILE: gefunden . \n");
    450         this->Materials[i] = this->loadMat(fileName);
    451         continue;
    452     }
    453         //      Default Material
    454         this->Materials[i].mat = new Material();
    455         this->Materials[i].mat->setDiffuse(0.1,0.1,0.1);
    456         this->Materials[i].mat->setAmbient(0.1,0.1,0.1 );
    457         this->Materials[i].mat->setSpecular(0.4,0.4,0.4);
    458         //this->Materials[i]->setShininess(100.0);
    459         this->Materials[i].mat->setTransparency(1.0);
    460         this->Materials[i].mat->setDiffuseMap("pictures/ground.tga");
    461         this->Materials[i].mat->setAmbientMap("pictures/ground.tga");
    462         this->Materials[i].mat->setSpecularMap("pictures/ground.tga");
    463         this->Materials[i].alpha = false;
     426    if(stat( absFileName , &results) == 0) {
     427      PRINTF(0)("BSP FILE: gefunden . \n");
     428      this->Materials[i] = this->loadMat(fileName);
     429      continue;
     430    }
     431    //  Default Material
     432    this->Materials[i].mat = new Material();
     433    this->Materials[i].mat->setDiffuse(0.1,0.1,0.1);
     434    this->Materials[i].mat->setAmbient(0.1,0.1,0.1 );
     435    this->Materials[i].mat->setSpecular(0.4,0.4,0.4);
     436    //this->Materials[i]->setShininess(100.0);
     437    this->Materials[i].mat->setTransparency(1.0);
     438    this->Materials[i].mat->setDiffuseMap("pictures/ground.tga");
     439    this->Materials[i].mat->setAmbientMap("pictures/ground.tga");
     440    this->Materials[i].mat->setSpecularMap("pictures/ground.tga");
     441    this->Materials[i].alpha = false;
    464442  }
    465443}
     
    472450
    473451  this->testSurf = IMG_Load(ResourceManager::getFullName(mat).c_str());
    474   if(this->testSurf != NULL)
    475         {
    476   if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
    477   else                                 tmpAMat.alpha = false;
    478         }
    479   else   tmpAMat.alpha = false;
     452  if(this->testSurf != NULL) {
     453    if(this->testSurf->format->Amask != 0 ) tmpAMat.alpha = true;
     454    else                                       tmpAMat.alpha = false;
     455  } else   tmpAMat.alpha = false;
    480456
    481457  Material* tmp = new Material();
    482         tmp->setDiffuse(1.0,1.0,1.0);
    483         tmp->setAmbient(1.0,1.0,1.0 );
    484         tmp->setSpecular(1.0,1.0,1.0);
    485    //   tmp->setShininess(.5);
    486    //   tmp->setTransparency(1.0);
    487 
    488         tmp->setDiffuseMap(mat);
    489         tmp->setAmbientMap(mat);
    490         tmp->setSpecularMap(mat);
    491  
     458  tmp->setDiffuse(1.0,1.0,1.0);
     459  tmp->setAmbient(1.0,1.0,1.0 );
     460  tmp->setSpecular(1.0,1.0,1.0);
     461  //    tmp->setShininess(.5);
     462  //    tmp->setTransparency(1.0);
     463
     464  tmp->setDiffuseMap(mat);
     465  tmp->setAmbientMap(mat);
     466  tmp->setSpecularMap(mat);
     467
    492468  tmpAMat.mat = tmp;
    493469
     
    497473void BspFile::tesselate(int iface)
    498474{
    499          face*  Face =  & (((face *)(this->faces))[iface]);
    500          BspVertex *  BspVrtx = (BspVertex*)this->vertice;
    501          int level = 7;
    502          int level1 = 8;
    503 
    504         // For each patch...
    505         for(int i = 0; i <(Face->size[0] - 1)    ; i+=2)
    506         {
    507                 for(int j = 0; j <(Face->size[1] -1)    ; j+=2)
    508                 {
    509 
    510                        
    511                         // Make a patch...
    512                         // Get controls[9];
    513                         BspVec controls[9];
    514                         BspVec controlsTmp[9];
    515                         BspVertex VControls[9];
    516                         for(int k = 0; k < 3; k++)
    517                         {
    518                                 for(int l = 0; l < 3; l++)
    519                                 {
    520                                         controls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
    521                                         controls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
    522                                         controls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
    523 
    524                                        controlsTmp[2-k +6-3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
    525                                         controlsTmp[2-k +6-3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
    526                                         controlsTmp[2-k +6-3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
    527 
    528                                         VControls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
    529                                         VControls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
    530                                         VControls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2];
    531 
    532                                         VControls[k +3*l]. normal[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].normal[0];
    533                                         VControls[k +3*l]. normal[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].normal[1];
    534                                         VControls[k +3*l]. normal[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].normal[2];
    535 
    536                                         VControls[k +3*l]. texcoord[0][0]=   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[0][0];
    537                                         VControls[k +3*l]. texcoord[0][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[0][1];
    538                                         VControls[k +3*l]. texcoord[1][0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[1][0];
    539                                         VControls[k +3*l]. texcoord[1][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[1][1];
    540                                        
    541 
    542                                 }
    543                         }
    544                         //***********************************************************************************************************************
    545                         // Compute the vertice
    546                         //***********************************************************************************************************************
    547                         float px, py;
    548                         BspVertex temp[3];
    549                         BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]);
    550 
    551                         for(int v=0; v<=level; ++v)
    552                         {
    553                                         px=(float)v/level;
    554 
    555                                         Vertice[v].position[0]=VControls[0].position[0]*((1.0f-px)*(1.0f-px))+VControls[3].position[0]*((1.0f-px)*px*2)+VControls[6].position[0]*(px*px);
    556                                         Vertice[v].position[1]=VControls[0].position[1]*((1.0f-px)*(1.0f-px))+VControls[3].position[1]*((1.0f-px)*px*2)+VControls[6].position[1]*(px*px);
    557                                         Vertice[v].position[2]=VControls[0].position[2]*((1.0f-px)*(1.0f-px))+VControls[3].position[2]*((1.0f-px)*px*2)+VControls[6].position[2]*(px*px);
    558 
    559                                         Vertice[v].normal[0]=VControls[0].normal[0]*((1.0f-px)*(1.0f-px))+VControls[3].normal[0]*((1.0f-px)*px*2)+VControls[6].normal[0]*(px*px);
    560                                         Vertice[v].normal[1]=VControls[0].normal[1]*((1.0f-px)*(1.0f-px))+VControls[3].normal[1]*((1.0f-px)*px*2)+VControls[6].normal[1]*(px*px);
    561                                         Vertice[v].normal[2]=VControls[0].normal[2]*((1.0f-px)*(1.0f-px))+VControls[3].normal[2]*((1.0f-px)*px*2)+VControls[6].normal[2]*(px*px);
    562 
    563                                         Vertice[v].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][0]*((1.0f-px)*px*2)+VControls[6].texcoord[0][0]*(px*px);
    564                                         Vertice[v].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][1]*((1.0f-px)*px*2)+VControls[6].texcoord[0][1]*(px*px);
    565                                         Vertice[v].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][0]*((1.0f-px)*px*2)+VControls[6].texcoord[1][0]*(px*px);
    566                                         Vertice[v].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][1]*((1.0f-px)*px*2)+VControls[6].texcoord[1][1]*(px*px);
    567                                        
    568                         }
    569 
    570 
    571                         for(int u=1; u<=level; ++u)
    572                         {
    573                                 py=(float)u/level;
    574 
    575                                         // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py);
    576                                         temp[0].position[0]=VControls[0].position[0]*((1.0f-py)*(1.0f-py))+VControls[1].position[0]*((1.0f-py)*py*2)+VControls[2].position[0]*(py*py);
    577                                         temp[0].position[1]=VControls[0].position[1]*((1.0f-py)*(1.0f-py))+VControls[1].position[1]*((1.0f-py)*py*2)+VControls[2].position[1]*(py*py);
    578                                         temp[0].position[2]=VControls[0].position[2]*((1.0f-py)*(1.0f-py))+VControls[1].position[2]*((1.0f-py)*py*2)+VControls[2].position[2]*(py*py);
    579 
    580                                         temp[0].normal[0]=VControls[0].normal[0]*((1.0f-py)*(1.0f-py))+VControls[1].normal[0]*((1.0f-py)*py*2)+VControls[2].normal[0]*(py*py);
    581                                         temp[0].normal[1]=VControls[0].normal[1]*((1.0f-py)*(1.0f-py))+VControls[1].normal[1]*((1.0f-py)*py*2)+VControls[2].normal[1]*(py*py);
    582                                         temp[0].normal[2]=VControls[0].normal[2]*((1.0f-py)*(1.0f-py))+VControls[1].normal[2]*((1.0f-py)*py*2)+VControls[2].normal[2]*(py*py);
    583 
    584                                         temp[0].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][0]*((1.0f-py)*py*2)+VControls[2].texcoord[0][0]*(py*py);
    585                                         temp[0].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][1]*((1.0f-py)*py*2)+VControls[2].texcoord[0][1]*(py*py);
    586                                         temp[0].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][0]*((1.0f-py)*py*2)+VControls[2].texcoord[1][0]*(py*py);
    587                                         temp[0].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][1]*((1.0f-py)*py*2)+VControls[2].texcoord[1][1]*(py*py);
    588                                        
    589                
    590 
    591                                         // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py);
    592                                         temp[1].position[0]=VControls[3].position[0]*((1.0f-py)*(1.0f-py))+VControls[4].position[0]*((1.0f-py)*py*2)+VControls[5].position[0]*(py*py);
    593                                         temp[1].position[1]=VControls[3].position[1]*((1.0f-py)*(1.0f-py))+VControls[4].position[1]*((1.0f-py)*py*2)+VControls[5].position[1]*(py*py);
    594                                         temp[1].position[2]=VControls[3].position[2]*((1.0f-py)*(1.0f-py))+VControls[4].position[2]*((1.0f-py)*py*2)+VControls[5].position[2]*(py*py);
    595 
    596                                         temp[1].normal[0]=VControls[3].normal[0]*((1.0f-py)*(1.0f-py))+VControls[4].normal[0]*((1.0f-py)*py*2)+VControls[5].normal[0]*(py*py);
    597                                         temp[1].normal[1]=VControls[3].normal[1]*((1.0f-py)*(1.0f-py))+VControls[4].normal[1]*((1.0f-py)*py*2)+VControls[5].normal[1]*(py*py);
    598                                         temp[1].normal[2]=VControls[3].normal[2]*((1.0f-py)*(1.0f-py))+VControls[4].normal[2]*((1.0f-py)*py*2)+VControls[5].normal[2]*(py*py);
    599 
    600                                         temp[1].texcoord[0][0]=VControls[3].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][0]*((1.0f-py)*py*2)+VControls[5].texcoord[0][0]*(py*py);
    601                                         temp[1].texcoord[0][1]=VControls[3].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][1]*((1.0f-py)*py*2)+VControls[5].texcoord[0][1]*(py*py);
    602                                         temp[1].texcoord[1][0]=VControls[3].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][0]*((1.0f-py)*py*2)+VControls[5].texcoord[1][0]*(py*py);
    603                                         temp[1].texcoord[1][1]=VControls[3].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][1]*((1.0f-py)*py*2)+VControls[5].texcoord[1][1]*(py*py);
    604                                        
    605 
    606                                         // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py);
    607                                         temp[2].position[0]=VControls[6].position[0]*((1.0f-py)*(1.0f-py))+VControls[7].position[0]*((1.0f-py)*py*2)+VControls[8].position[0]*(py*py);
    608                                         temp[2].position[1]=VControls[6].position[1]*((1.0f-py)*(1.0f-py))+VControls[7].position[1]*((1.0f-py)*py*2)+VControls[8].position[1]*(py*py);
    609                                         temp[2].position[2]=VControls[6].position[2]*((1.0f-py)*(1.0f-py))+VControls[7].position[2]*((1.0f-py)*py*2)+VControls[8].position[2]*(py*py);
    610 
    611                                         temp[2].normal[0]=VControls[6].normal[0]*((1.0f-py)*(1.0f-py))+VControls[7].normal[0]*((1.0f-py)*py*2)+VControls[8].normal[0]*(py*py);
    612                                         temp[2].normal[1]=VControls[6].normal[1]*((1.0f-py)*(1.0f-py))+VControls[7].normal[1]*((1.0f-py)*py*2)+VControls[8].normal[1]*(py*py);
    613                                         temp[2].normal[2]=VControls[6].normal[2]*((1.0f-py)*(1.0f-py))+VControls[7].normal[2]*((1.0f-py)*py*2)+VControls[8].normal[2]*(py*py);
    614 
    615                                         temp[2].texcoord[0][0]=VControls[6].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][0]*((1.0f-py)*py*2)+VControls[8].texcoord[0][0]*(py*py);
    616                                         temp[2].texcoord[0][1]=VControls[6].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][1]*((1.0f-py)*py*2)+VControls[8].texcoord[0][1]*(py*py);
    617                                         temp[2].texcoord[1][0]=VControls[6].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][0]*((1.0f-py)*py*2)+VControls[8].texcoord[1][0]*(py*py);
    618                                         temp[2].texcoord[1][1]=VControls[6].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][1]*((1.0f-py)*py*2)+VControls[8].texcoord[1][1]*(py*py);
    619                                        
    620                                        
    621                                        
    622 
    623                                 for(int v=0; v<=level; ++v)
    624                                 {
    625                                         px=(float)v/level;
    626 
    627                                         //Vertice[u*(tesselation+1)+v]= temp[0]*((1.0f-px)*(1.0f-px))+ temp[1]*((1.0f-px)*px*2)+ temp[2]*(px*px);
    628                                         Vertice[u*(level1)+v].position[0]=temp[0].position[0]*((1.0f-px)*(1.0f-px))+temp[1].position[0]*((1.0f-px)*px*2)+temp[2].position[0]*(px*px);
    629                                         Vertice[u*(level1)+v].position[1]=temp[0].position[1]*((1.0f-px)*(1.0f-px))+temp[1].position[1]*((1.0f-px)*px*2)+temp[2].position[1]*(px*px);
    630                                         Vertice[u*(level1)+v].position[2]=temp[0].position[2]*((1.0f-px)*(1.0f-px))+temp[1].position[2]*((1.0f-px)*px*2)+temp[2].position[2]*(px*px);
    631 
    632                                         Vertice[u*(level1)+v].normal[0]=temp[0].normal[0]*((1.0f-px)*(1.0f-px))+temp[1].normal[0]*((1.0f-px)*px*2)+temp[2].normal[0]*(px*px);
    633                                         Vertice[u*(level1)+v].normal[1]=temp[0].normal[1]*((1.0f-px)*(1.0f-px))+temp[1].normal[1]*((1.0f-px)*px*2)+temp[2].normal[1]*(px*px);
    634                                         Vertice[u*(level1)+v].normal[2]=temp[0].normal[2]*((1.0f-px)*(1.0f-px))+temp[1].normal[2]*((1.0f-px)*px*2)+temp[2].normal[2]*(px*px);
    635 
    636                                         Vertice[u*(level1)+v].texcoord[0][0]=temp[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][0]*((1.0f-px)*px*2)+temp[2].texcoord[0][0]*(px*px);
    637                                         Vertice[u*(level1)+v].texcoord[0][1]=temp[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][1]*((1.0f-px)*px*2)+temp[2].texcoord[0][1]*(px*px);
    638                                         Vertice[u*(level1)+v].texcoord[1][0]=temp[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][0]*((1.0f-px)*px*2)+temp[2].texcoord[1][0]*(px*px);
    639                                         Vertice[u*(level1)+v].texcoord[1][1]=temp[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][1]*((1.0f-px)*px*2)+temp[2].texcoord[1][1]*(px*px);
    640                                        
    641 
    642 
    643                                 }
    644                         }
    645 
    646                         //Create indices
    647                         GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
    648                        
    649                         for(int row=0; row<level; ++row)
    650                         {
    651                                 for(int point=0; point<=level; ++point)
    652                                 {
    653                                         //calculate indices
    654                                         //reverse them to reverse winding
    655                                         indices[(row*(level1)+point)*2+1]=row*(level1)+point;
    656                                         indices[(row*(level1)+point)*2]=(row+1)*(level1)+point;
    657                                 }
    658                         }
    659 
    660 
    661                         //***********************************************************************************************************************
    662                         // Debug Model
    663                         //***********************************************************************************************************************
    664                         this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
    665                         VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
    666                         tmp->newStripe();
    667                                 int a = 0;
    668                                 int b = -1;
    669                                 tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);     
    670                                 tmp->addNormal(1,0,0);
    671                                 tmp->addTexCoor(0.0,0.0);
    672                                 tmp->addColor(0.3,0.0,0.0);
    673                                 tmp->addIndice(1+b);
    674                                 tmp->addIndice(4+a);
    675                                 tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
    676                                 tmp->addNormal(1,0,0); 
    677                                 tmp->addTexCoor(0.0,0.4);
    678                                 tmp->addColor(0.3,0.0,0.0);
    679                                 tmp->addIndice(2+b);
    680                                 tmp->addIndice(5+a);
    681                                 tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);     
    682                                 tmp->addNormal(1,0,0); 
    683                                 tmp->addTexCoor(0.0,1.0);
    684                                 tmp->addColor(0.1,0.0,0.0);
    685                                 tmp->addIndice(3+b);
    686                                 tmp->addIndice(6+a);
    687                        
    688                                 tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);     
    689                                 tmp->addNormal(1,0,0); 
    690                                 tmp->addTexCoor(0.0,1.0);
    691                                 tmp->addColor(0.1,0.0,0.0);
    692                                 tmp->addIndice(7+a);
    693                                 //tmp->addIndice(6);
    694                        
    695                                 tmp->newStripe();
    696 
    697                                 tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);     
    698                                 tmp->addNormal(1,0,0);
    699                                 tmp->addTexCoor(0.0,0.0);
    700                                 tmp->addColor(0.1,0.1,0.1);
    701                                 tmp->addIndice(5+b);
    702                                 tmp->addIndice(8+a);
    703                                 tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
    704                                 tmp->addNormal(1,0,0); 
    705                                 tmp->addTexCoor(0.0,0.4);
    706                                 tmp->addColor(0.1,0.1,0.1);
    707                                 tmp->addIndice(6+b);
    708                                 tmp->addIndice(9+a);
    709                                 tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);     
    710                                 tmp->addNormal(1,0,0); 
    711                                 tmp->addTexCoor(0.0,1.0);
    712                                 tmp->addColor(0.1,0.1,0.1);
    713                                 tmp->addIndice(7+b);
    714                                 tmp->addIndice(10+a);
    715                                 tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
    716                                 tmp->addNormal(1,0,0); 
    717                                 tmp->addTexCoor(0.0,1.0);
    718                                 tmp->addColor(0.1,0.1,0.1);
    719                                 //tmp->addIndice(5);
    720                                 tmp->addIndice(11+a);
    721                        
    722                                 tmp->newStripe();
    723 
    724 
    725 
    726                                 tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);     
    727                                 tmp->addNormal(0,1,0);
    728                                 tmp->addTexCoor(0.5,0.0);
    729                                 tmp->addColor(0.1,0.1,0.1);
    730                                 tmp->addIndice(9+b);
    731                                 tmp->addIndice(12+a);
    732                                 tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);     
    733                                 tmp->addNormal(1,0,0);
    734                                 tmp->addTexCoor(0.5,0.5);
    735                                 tmp->addColor(0.1,0.1,0.1);
    736                                 tmp->addIndice(10+b);
    737                                 tmp->addIndice(13+a);
    738                                 tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);     
    739                                 tmp->addNormal(1,0,0);
    740                                 tmp->addTexCoor(0.5,1.0);
    741                                 tmp->addColor(0.1,0.1,0.1);
    742                                 tmp->addIndice(11+b);
    743                                 tmp->addIndice(14+a);
    744                                 tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
    745                                 tmp->addNormal(1,0,0);
    746                                 tmp->addTexCoor(0.5,1.0);
    747                                 tmp->addColor(0.1,0.1,0.1);
    748                                
    749                                 tmp->addIndice(15+a);
    750                                 //tmp->addIndice(9);
    751                                 tmp->newStripe();
    752 
    753                                 tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
    754                                 tmp->addNormal(1,0,0); 
    755                                 tmp->addTexCoor(1.0,0.0);
    756                                 tmp->addColor(0.1,0.1,0.1);
    757                                 tmp->addIndice(13+b);   
    758                                 tmp->addIndice(16+a);   
    759                                
    760                                 tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
    761                                 tmp->addNormal(0,1,0); 
    762                                 tmp->addTexCoor(1.0,0.5);
    763                                 tmp->addColor(0.1,0.1,0.1);
    764                                 tmp->addIndice(14+b);
    765                                 tmp->addIndice(17+a);   
    766                                 tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
    767                                 tmp->addNormal(1,0,0); 
    768                                 tmp->addTexCoor(1.0,1.0);
    769                                 tmp->addColor(0.1,0.1,0.1);
    770                                 tmp->addIndice(15+b);
    771                                 tmp->addIndice(18+a);   
    772                                 tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
    773                                 tmp->addNormal(1,0,0); 
    774                                 tmp->addTexCoor(1.0,1.0);
    775                                 tmp->addColor(0.1,0.1,0.1);
    776                                 tmp->addIndice(19+a);   
    777                                 //tmp->addIndice(13);
    778 
    779                                 tmp->newStripe();
    780                                 tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
    781                                 tmp->addNormal(1,0,0); 
    782                                 tmp->addTexCoor(1.0,0.0);
    783                                 tmp->addColor(0.1,0.1,0.1);
    784                                 tmp->addIndice(17+b);   
    785                                                                
    786                                 tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
    787                                 tmp->addNormal(0,1,0); 
    788                                 tmp->addTexCoor(1.0,0.5);
    789                                 tmp->addColor(0.1,0.1,0.1);
    790                                 tmp->addIndice(18+b);
    791                        
    792                                 tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
    793                                 tmp->addNormal(1,0,0); 
    794                                 tmp->addTexCoor(1.0,1.0);
    795                                 tmp->addColor(0.1,0.1,0.1);
    796                                 tmp->addIndice(19+b);
    797                
    798                                 tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
    799                                 tmp->addNormal(1,0,0); 
    800                                 tmp->addTexCoor(1.0,1.0);
    801                                 tmp->addColor(0.1,0.1,0.1);
    802                
    803                                 tmp->newStripe();
    804 
    805                                 tmp->finalize();
    806                                 // End of DebugModel
    807                                
    808                         this->patchOffset++;
    809                 }// For
    810         } // For
    811 
    812                         // Overwrite Face->meshvert;
    813                         // Overwrite Face->n_meshverts;
    814                         int sz = (Face->size[0] -1)/2 * (Face->size[1] -1)/2; // num patches
    815                         Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
    816                         Face->n_meshverts = sz;
    817                         PRINTF(0)("BSP FILE: sz: %i. \n", sz);
    818                         PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
    819 
    820                         //Face->n_meshverts = sz;
     475  face*  Face =  & (((face *)(this->faces))[iface]);
     476  BspVertex *  BspVrtx = (BspVertex*)this->vertice;
     477  int level = 7;
     478  int level1 = 8;
     479
     480  // For each patch...
     481  for(int i = 0; i <(Face->size[0] - 1)    ; i+=2) {
     482    for(int j = 0; j <(Face->size[1] -1)    ; j+=2) {
     483
     484
     485      // Make a patch...
     486      // Get controls[9];
     487      BspVec controls[9];
     488      BspVec controlsTmp[9];
     489      BspVertex VControls[9];
     490      for(int k = 0; k < 3; k++) {
     491        for(int l = 0; l < 3; l++) {
     492          controls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
     493          controls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
     494          controls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
     495
     496          controlsTmp[2-k +6-3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
     497          controlsTmp[2-k +6-3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
     498          controlsTmp[2-k +6-3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2]; /*Face->n_vertexes*/
     499
     500          VControls[k +3*l]. position[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].position[0];
     501          VControls[k +3*l]. position[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].position[1];
     502          VControls[k +3*l]. position[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].position[2];
     503
     504          VControls[k +3*l]. normal[0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].normal[0];
     505          VControls[k +3*l]. normal[1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].normal[1];
     506          VControls[k +3*l]. normal[2] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i + l+ Face->size[0]*k].normal[2];
     507
     508          VControls[k +3*l]. texcoord[0][0]=   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[0][0];
     509          VControls[k +3*l]. texcoord[0][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[0][1];
     510          VControls[k +3*l]. texcoord[1][0] =   BspVrtx[Face->vertex +( j * Face->size[0])+ i + l+ Face->size[0]*k].texcoord[1][0];
     511          VControls[k +3*l]. texcoord[1][1] =   BspVrtx[Face->vertex + (j * Face->size[0])+ i +l+ Face->size[0]*k].texcoord[1][1];
     512
     513
     514        }
     515      }
     516      //***********************************************************************************************************************
     517      // Compute the vertice
     518      //***********************************************************************************************************************
     519      float px, py;
     520      BspVertex temp[3];
     521      BspVertex* Vertice = &(((BspVertex*)this->patchVertice)[level1*level1*this->patchOffset]);
     522
     523      for(int v=0; v<=level; ++v) {
     524        px=(float)v/level;
     525
     526        Vertice[v].position[0]=VControls[0].position[0]*((1.0f-px)*(1.0f-px))+VControls[3].position[0]*((1.0f-px)*px*2)+VControls[6].position[0]*(px*px);
     527        Vertice[v].position[1]=VControls[0].position[1]*((1.0f-px)*(1.0f-px))+VControls[3].position[1]*((1.0f-px)*px*2)+VControls[6].position[1]*(px*px);
     528        Vertice[v].position[2]=VControls[0].position[2]*((1.0f-px)*(1.0f-px))+VControls[3].position[2]*((1.0f-px)*px*2)+VControls[6].position[2]*(px*px);
     529
     530        Vertice[v].normal[0]=VControls[0].normal[0]*((1.0f-px)*(1.0f-px))+VControls[3].normal[0]*((1.0f-px)*px*2)+VControls[6].normal[0]*(px*px);
     531        Vertice[v].normal[1]=VControls[0].normal[1]*((1.0f-px)*(1.0f-px))+VControls[3].normal[1]*((1.0f-px)*px*2)+VControls[6].normal[1]*(px*px);
     532        Vertice[v].normal[2]=VControls[0].normal[2]*((1.0f-px)*(1.0f-px))+VControls[3].normal[2]*((1.0f-px)*px*2)+VControls[6].normal[2]*(px*px);
     533
     534        Vertice[v].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][0]*((1.0f-px)*px*2)+VControls[6].texcoord[0][0]*(px*px);
     535        Vertice[v].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[0][1]*((1.0f-px)*px*2)+VControls[6].texcoord[0][1]*(px*px);
     536        Vertice[v].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][0]*((1.0f-px)*px*2)+VControls[6].texcoord[1][0]*(px*px);
     537        Vertice[v].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+VControls[3].texcoord[1][1]*((1.0f-px)*px*2)+VControls[6].texcoord[1][1]*(px*px);
     538
     539      }
     540
     541
     542      for(int u=1; u<=level; ++u) {
     543        py=(float)u/level;
     544
     545        // temp[0]=controlPoints[0]*((1.0f-py)*(1.0f-py))+ controlPoints[1]*((1.0f-py)*py*2)+ controlPoints[2]*(py*py);
     546        temp[0].position[0]=VControls[0].position[0]*((1.0f-py)*(1.0f-py))+VControls[1].position[0]*((1.0f-py)*py*2)+VControls[2].position[0]*(py*py);
     547        temp[0].position[1]=VControls[0].position[1]*((1.0f-py)*(1.0f-py))+VControls[1].position[1]*((1.0f-py)*py*2)+VControls[2].position[1]*(py*py);
     548        temp[0].position[2]=VControls[0].position[2]*((1.0f-py)*(1.0f-py))+VControls[1].position[2]*((1.0f-py)*py*2)+VControls[2].position[2]*(py*py);
     549
     550        temp[0].normal[0]=VControls[0].normal[0]*((1.0f-py)*(1.0f-py))+VControls[1].normal[0]*((1.0f-py)*py*2)+VControls[2].normal[0]*(py*py);
     551        temp[0].normal[1]=VControls[0].normal[1]*((1.0f-py)*(1.0f-py))+VControls[1].normal[1]*((1.0f-py)*py*2)+VControls[2].normal[1]*(py*py);
     552        temp[0].normal[2]=VControls[0].normal[2]*((1.0f-py)*(1.0f-py))+VControls[1].normal[2]*((1.0f-py)*py*2)+VControls[2].normal[2]*(py*py);
     553
     554        temp[0].texcoord[0][0]=VControls[0].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][0]*((1.0f-py)*py*2)+VControls[2].texcoord[0][0]*(py*py);
     555        temp[0].texcoord[0][1]=VControls[0].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[0][1]*((1.0f-py)*py*2)+VControls[2].texcoord[0][1]*(py*py);
     556        temp[0].texcoord[1][0]=VControls[0].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][0]*((1.0f-py)*py*2)+VControls[2].texcoord[1][0]*(py*py);
     557        temp[0].texcoord[1][1]=VControls[0].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[1].texcoord[1][1]*((1.0f-py)*py*2)+VControls[2].texcoord[1][1]*(py*py);
     558
     559
     560
     561        // temp[1]=controlPoints[3]*((1.0f-py)*(1.0f-py))+ controlPoints[4]*((1.0f-py)*py*2)+ controlPoints[5]*(py*py);
     562        temp[1].position[0]=VControls[3].position[0]*((1.0f-py)*(1.0f-py))+VControls[4].position[0]*((1.0f-py)*py*2)+VControls[5].position[0]*(py*py);
     563        temp[1].position[1]=VControls[3].position[1]*((1.0f-py)*(1.0f-py))+VControls[4].position[1]*((1.0f-py)*py*2)+VControls[5].position[1]*(py*py);
     564        temp[1].position[2]=VControls[3].position[2]*((1.0f-py)*(1.0f-py))+VControls[4].position[2]*((1.0f-py)*py*2)+VControls[5].position[2]*(py*py);
     565
     566        temp[1].normal[0]=VControls[3].normal[0]*((1.0f-py)*(1.0f-py))+VControls[4].normal[0]*((1.0f-py)*py*2)+VControls[5].normal[0]*(py*py);
     567        temp[1].normal[1]=VControls[3].normal[1]*((1.0f-py)*(1.0f-py))+VControls[4].normal[1]*((1.0f-py)*py*2)+VControls[5].normal[1]*(py*py);
     568        temp[1].normal[2]=VControls[3].normal[2]*((1.0f-py)*(1.0f-py))+VControls[4].normal[2]*((1.0f-py)*py*2)+VControls[5].normal[2]*(py*py);
     569
     570        temp[1].texcoord[0][0]=VControls[3].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][0]*((1.0f-py)*py*2)+VControls[5].texcoord[0][0]*(py*py);
     571        temp[1].texcoord[0][1]=VControls[3].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[0][1]*((1.0f-py)*py*2)+VControls[5].texcoord[0][1]*(py*py);
     572        temp[1].texcoord[1][0]=VControls[3].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][0]*((1.0f-py)*py*2)+VControls[5].texcoord[1][0]*(py*py);
     573        temp[1].texcoord[1][1]=VControls[3].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[4].texcoord[1][1]*((1.0f-py)*py*2)+VControls[5].texcoord[1][1]*(py*py);
     574
     575
     576        // temp[2]=controlPoints[6]*((1.0f-py)*(1.0f-py))+controlPoints[7]*((1.0f-py)*py*2)+controlPoints[8]*(py*py);
     577        temp[2].position[0]=VControls[6].position[0]*((1.0f-py)*(1.0f-py))+VControls[7].position[0]*((1.0f-py)*py*2)+VControls[8].position[0]*(py*py);
     578        temp[2].position[1]=VControls[6].position[1]*((1.0f-py)*(1.0f-py))+VControls[7].position[1]*((1.0f-py)*py*2)+VControls[8].position[1]*(py*py);
     579        temp[2].position[2]=VControls[6].position[2]*((1.0f-py)*(1.0f-py))+VControls[7].position[2]*((1.0f-py)*py*2)+VControls[8].position[2]*(py*py);
     580
     581        temp[2].normal[0]=VControls[6].normal[0]*((1.0f-py)*(1.0f-py))+VControls[7].normal[0]*((1.0f-py)*py*2)+VControls[8].normal[0]*(py*py);
     582        temp[2].normal[1]=VControls[6].normal[1]*((1.0f-py)*(1.0f-py))+VControls[7].normal[1]*((1.0f-py)*py*2)+VControls[8].normal[1]*(py*py);
     583        temp[2].normal[2]=VControls[6].normal[2]*((1.0f-py)*(1.0f-py))+VControls[7].normal[2]*((1.0f-py)*py*2)+VControls[8].normal[2]*(py*py);
     584
     585        temp[2].texcoord[0][0]=VControls[6].texcoord[0][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][0]*((1.0f-py)*py*2)+VControls[8].texcoord[0][0]*(py*py);
     586        temp[2].texcoord[0][1]=VControls[6].texcoord[0][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[0][1]*((1.0f-py)*py*2)+VControls[8].texcoord[0][1]*(py*py);
     587        temp[2].texcoord[1][0]=VControls[6].texcoord[1][0]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][0]*((1.0f-py)*py*2)+VControls[8].texcoord[1][0]*(py*py);
     588        temp[2].texcoord[1][1]=VControls[6].texcoord[1][1]*((1.0f-py)*(1.0f-py))+VControls[7].texcoord[1][1]*((1.0f-py)*py*2)+VControls[8].texcoord[1][1]*(py*py);
     589
     590
     591
     592
     593        for(int v=0; v<=level; ++v) {
     594          px=(float)v/level;
     595
     596          //Vertice[u*(tesselation+1)+v]=       temp[0]*((1.0f-px)*(1.0f-px))+ temp[1]*((1.0f-px)*px*2)+ temp[2]*(px*px);
     597          Vertice[u*(level1)+v].position[0]=temp[0].position[0]*((1.0f-px)*(1.0f-px))+temp[1].position[0]*((1.0f-px)*px*2)+temp[2].position[0]*(px*px);
     598          Vertice[u*(level1)+v].position[1]=temp[0].position[1]*((1.0f-px)*(1.0f-px))+temp[1].position[1]*((1.0f-px)*px*2)+temp[2].position[1]*(px*px);
     599          Vertice[u*(level1)+v].position[2]=temp[0].position[2]*((1.0f-px)*(1.0f-px))+temp[1].position[2]*((1.0f-px)*px*2)+temp[2].position[2]*(px*px);
     600
     601          Vertice[u*(level1)+v].normal[0]=temp[0].normal[0]*((1.0f-px)*(1.0f-px))+temp[1].normal[0]*((1.0f-px)*px*2)+temp[2].normal[0]*(px*px);
     602          Vertice[u*(level1)+v].normal[1]=temp[0].normal[1]*((1.0f-px)*(1.0f-px))+temp[1].normal[1]*((1.0f-px)*px*2)+temp[2].normal[1]*(px*px);
     603          Vertice[u*(level1)+v].normal[2]=temp[0].normal[2]*((1.0f-px)*(1.0f-px))+temp[1].normal[2]*((1.0f-px)*px*2)+temp[2].normal[2]*(px*px);
     604
     605          Vertice[u*(level1)+v].texcoord[0][0]=temp[0].texcoord[0][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][0]*((1.0f-px)*px*2)+temp[2].texcoord[0][0]*(px*px);
     606          Vertice[u*(level1)+v].texcoord[0][1]=temp[0].texcoord[0][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[0][1]*((1.0f-px)*px*2)+temp[2].texcoord[0][1]*(px*px);
     607          Vertice[u*(level1)+v].texcoord[1][0]=temp[0].texcoord[1][0]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][0]*((1.0f-px)*px*2)+temp[2].texcoord[1][0]*(px*px);
     608          Vertice[u*(level1)+v].texcoord[1][1]=temp[0].texcoord[1][1]*((1.0f-px)*(1.0f-px))+temp[1].texcoord[1][1]*((1.0f-px)*px*2)+temp[2].texcoord[1][1]*(px*px);
     609
     610
     611
     612        }
     613      }
     614
     615      //Create indices
     616      GLuint* indices= & ((GLuint*)(this->patchIndexes))[level*level1*2*this->patchOffset];
     617
     618      for(int row=0; row<level; ++row) {
     619        for(int point=0; point<=level; ++point) {
     620          //calculate indices
     621          //reverse them to reverse winding
     622          indices[(row*(level1)+point)*2+1]=row*(level1)+point;
     623          indices[(row*(level1)+point)*2]=(row+1)*(level1)+point;
     624        }
     625      }
     626
     627
     628      //***********************************************************************************************************************
     629      // Debug Model
     630      //***********************************************************************************************************************
     631      this->VertexArrayModels[this->patchOffset] = new VertexArrayModel();
     632      VertexArrayModel*  tmp = this->VertexArrayModels[this->patchOffset];
     633      tmp->newStripe();
     634      int a = 0;
     635      int b = -1;
     636      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
     637      tmp->addNormal(1,0,0);
     638      tmp->addTexCoor(0.0,0.0);
     639      tmp->addColor(0.3,0.0,0.0);
     640      tmp->addIndice(1+b);
     641      tmp->addIndice(4+a);
     642      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
     643      tmp->addNormal(1,0,0);
     644      tmp->addTexCoor(0.0,0.4);
     645      tmp->addColor(0.3,0.0,0.0);
     646      tmp->addIndice(2+b);
     647      tmp->addIndice(5+a);
     648      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
     649      tmp->addNormal(1,0,0);
     650      tmp->addTexCoor(0.0,1.0);
     651      tmp->addColor(0.1,0.0,0.0);
     652      tmp->addIndice(3+b);
     653      tmp->addIndice(6+a);
     654
     655      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
     656      tmp->addNormal(1,0,0);
     657      tmp->addTexCoor(0.0,1.0);
     658      tmp->addColor(0.1,0.0,0.0);
     659      tmp->addIndice(7+a);
     660      //tmp->addIndice(6);
     661
     662      tmp->newStripe();
     663
     664      tmp->addVertex(controlsTmp[0].position[0],controlsTmp[0].position[1], controlsTmp[0].position[2]);
     665      tmp->addNormal(1,0,0);
     666      tmp->addTexCoor(0.0,0.0);
     667      tmp->addColor(0.1,0.1,0.1);
     668      tmp->addIndice(5+b);
     669      tmp->addIndice(8+a);
     670      tmp->addVertex(controlsTmp[1].position[0],controlsTmp[1].position[1], controlsTmp[1].position[2]);
     671      tmp->addNormal(1,0,0);
     672      tmp->addTexCoor(0.0,0.4);
     673      tmp->addColor(0.1,0.1,0.1);
     674      tmp->addIndice(6+b);
     675      tmp->addIndice(9+a);
     676      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]);
     677      tmp->addNormal(1,0,0);
     678      tmp->addTexCoor(0.0,1.0);
     679      tmp->addColor(0.1,0.1,0.1);
     680      tmp->addIndice(7+b);
     681      tmp->addIndice(10+a);
     682      tmp->addVertex(controlsTmp[2].position[0],controlsTmp[2].position[1], controlsTmp[2].position[2]+0.01);
     683      tmp->addNormal(1,0,0);
     684      tmp->addTexCoor(0.0,1.0);
     685      tmp->addColor(0.1,0.1,0.1);
     686      //tmp->addIndice(5);
     687      tmp->addIndice(11+a);
     688
     689      tmp->newStripe();
     690
     691
     692
     693      tmp->addVertex(controlsTmp[3].position[0],controlsTmp[3].position[1], controlsTmp[3].position[2]);
     694      tmp->addNormal(0,1,0);
     695      tmp->addTexCoor(0.5,0.0);
     696      tmp->addColor(0.1,0.1,0.1);
     697      tmp->addIndice(9+b);
     698      tmp->addIndice(12+a);
     699      tmp->addVertex(controlsTmp[4].position[0],controlsTmp[4].position[1], controlsTmp[4].position[2]);
     700      tmp->addNormal(1,0,0);
     701      tmp->addTexCoor(0.5,0.5);
     702      tmp->addColor(0.1,0.1,0.1);
     703      tmp->addIndice(10+b);
     704      tmp->addIndice(13+a);
     705      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]);
     706      tmp->addNormal(1,0,0);
     707      tmp->addTexCoor(0.5,1.0);
     708      tmp->addColor(0.1,0.1,0.1);
     709      tmp->addIndice(11+b);
     710      tmp->addIndice(14+a);
     711      tmp->addVertex(controlsTmp[5].position[0],controlsTmp[5].position[1], controlsTmp[5].position[2]+0.01);
     712      tmp->addNormal(1,0,0);
     713      tmp->addTexCoor(0.5,1.0);
     714      tmp->addColor(0.1,0.1,0.1);
     715
     716      tmp->addIndice(15+a);
     717      //tmp->addIndice(9);
     718      tmp->newStripe();
     719
     720      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
     721      tmp->addNormal(1,0,0);
     722      tmp->addTexCoor(1.0,0.0);
     723      tmp->addColor(0.1,0.1,0.1);
     724      tmp->addIndice(13+b);
     725      tmp->addIndice(16+a);
     726
     727      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
     728      tmp->addNormal(0,1,0);
     729      tmp->addTexCoor(1.0,0.5);
     730      tmp->addColor(0.1,0.1,0.1);
     731      tmp->addIndice(14+b);
     732      tmp->addIndice(17+a);
     733      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
     734      tmp->addNormal(1,0,0);
     735      tmp->addTexCoor(1.0,1.0);
     736      tmp->addColor(0.1,0.1,0.1);
     737      tmp->addIndice(15+b);
     738      tmp->addIndice(18+a);
     739      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
     740      tmp->addNormal(1,0,0);
     741      tmp->addTexCoor(1.0,1.0);
     742      tmp->addColor(0.1,0.1,0.1);
     743      tmp->addIndice(19+a);
     744      //tmp->addIndice(13);
     745
     746      tmp->newStripe();
     747      tmp->addVertex(controlsTmp[6].position[0],controlsTmp[6].position[1], controlsTmp[6].position[2]);
     748      tmp->addNormal(1,0,0);
     749      tmp->addTexCoor(1.0,0.0);
     750      tmp->addColor(0.1,0.1,0.1);
     751      tmp->addIndice(17+b);
     752
     753      tmp->addVertex(controlsTmp[7].position[0],controlsTmp[7].position[1], controlsTmp[7].position[2]);
     754      tmp->addNormal(0,1,0);
     755      tmp->addTexCoor(1.0,0.5);
     756      tmp->addColor(0.1,0.1,0.1);
     757      tmp->addIndice(18+b);
     758
     759      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
     760      tmp->addNormal(1,0,0);
     761      tmp->addTexCoor(1.0,1.0);
     762      tmp->addColor(0.1,0.1,0.1);
     763      tmp->addIndice(19+b);
     764
     765      tmp->addVertex(controlsTmp[8].position[0],controlsTmp[8].position[1], controlsTmp[8].position[2]);
     766      tmp->addNormal(1,0,0);
     767      tmp->addTexCoor(1.0,1.0);
     768      tmp->addColor(0.1,0.1,0.1);
     769
     770      tmp->newStripe();
     771
     772      tmp->finalize();
     773      // End of DebugModel
     774
     775      this->patchOffset++;
     776    }// For
     777  } // For
     778
     779  // Overwrite Face->meshvert;
     780  // Overwrite Face->n_meshverts;
     781  int sz = (Face->size[0] -1)/2 * (Face->size[1] -1)/2; // num patches
     782  Face->meshvert = patchOffset -sz;  //3*(patchOffset-sz)*level1*level1;
     783  Face->n_meshverts = sz;
     784  PRINTF(0)("BSP FILE: sz: %i. \n", sz);
     785  PRINTF(0)("BSP FILE: Face->meshvert %i . \n", Face->meshvert);
     786
     787  //Face->n_meshverts = sz;
    821788}
Note: See TracChangeset for help on using the changeset viewer.