Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - Variabili di array sovrascritte ?
Forum - C/C++ - Variabili di array sovrascritte ?

Avatar
()
Newbie


Messaggi:
Iscritto:

Segnala al moderatore
Postato alle 1:00
Giovedì, 01/01/1970
Salve.
Nella mia classe Obj3DMesh ho creato un metodo per caricare modelli 3d da file .obj
Le coordinate dei punti, che leggo e salvo in un array, sono lette correttamente ma nel ciclo successivo dell'iterazione alterano il proprio valore.
Posto il codice per essere piu chiaro :rotfl:

objDescription è un char*, che contiene ciò che è scritto nel file.
Penso che sia comodo memorizzare il contenuto in una stringa, cosi puo sapere anche qual è il carattere successivo senza dover aumentare la seek position.
Per leggere le cordinate uso questo metodo :
-memorizzo la parte intera nel numero nell' array float coordIntNumber[][];
-memorizzo la parte decimale nell'array float coordDecimalNumber[][];
-se la parte intera è maggiore di 0 sommo quella decimale, altrimenti sottraggo e memorizzo il risultato nell'array float vertexCoord[][].

Codice sorgente - presumibilmente Plain Text

  1. while(index < strlen(objDescription)){
  2.    theFirstHasMinus = false;
  3.    theSecondHasMinus = false;
  4.    theThirdHasMinus = false;
  5.  
  6.    if(objDescription[index] == 'v' && objDescription[index-1] == '\n' && objDescription[index+1] == ' '){
  7.          //X coord
  8.          if(objDescription[index+2] == '-'){
  9.            coordIntNumber[CoordIndex][0] = atoi(&objDescription[index+2]);
  10.            coordDecimalNumber[CoordIndex][0] = atoi(&objDescription[index+5]);
  11.  
  12.        fe_out<<coordIntNumber[CoordIndex][0]<<" ";
  13.        fe_out<<coordDecimalNumber[CoordIndex][0]<<endl;
  14.  
  15.        theFirstHasMinus = true;
  16.          }else{
  17.            coordIntNumber[CoordIndex][0] = atoi(&objDescription[index+2]);
  18.            coordDecimalNumber[CoordIndex][0]= atoi(&objDescription[index+4]);
  19.  
  20.        fe_out<<coordIntNumber[CoordIndex][0]<<" ";
  21.        fe_out<<coordDecimalNumber[CoordIndex][0]<<endl;
  22.  
  23.        theFirstHasMinus = false;
  24.          }
  25.      //Y coord
  26.      if(!theFirstHasMinus){
  27.           if(objDescription[index+11] == '-'){
  28.            coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+11]);
  29.            coordDecimalNumber[CoordIndex][1]= atoi(&objDescription[index+14]);
  30.  
  31.            fe_out<<coordIntNumber[CoordIndex][1]<<" ";
  32.        fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;
  33.  
  34.        theSecondHasMinus = true;
  35.           }else{
  36.            coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+11]);
  37.            coordDecimalNumber[CoordIndex][1] = atoi(&objDescription[index+13]);
  38.  
  39.        fe_out<<coordIntNumber[CoordIndex][1]<<" ";
  40.        fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;
  41.  
  42.        theSecondHasMinus = false;
  43.           }
  44.      }else{
  45.        if(objDescription[index+12] == '-'){
  46.            coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+12]);
  47.            coordDecimalNumber[CoordIndex][1]= atoi(&objDescription[index+15]);
  48.  
  49.            fe_out<<coordIntNumber[CoordIndex][1]<<" ";
  50.        fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;
  51.  
  52.        theSecondHasMinus = true;
  53.           }else{
  54.            coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+12]);
  55.            coordDecimalNumber[CoordIndex][1] = atoi(&objDescription[index+14]);
  56.  
  57.        fe_out<<coordIntNumber[CoordIndex][1]<<" ";
  58.        fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;
  59.  
  60.        theSecondHasMinus = false;
  61.           }
  62.      }
  63.      //Z coord
  64.      if(theFirstHasMinus == false && theSecondHasMinus == false){
  65.           if(objDescription[index+20] == '-'){
  66.            coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+20]);
  67.            coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+23]);
  68.  
  69.            fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  70.        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  71.           }else{
  72.            coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+20]);
  73.            coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+22]);
  74.  
  75.            fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  76.        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  77.           }
  78.      }else if((theFirstHasMinus == true && theSecondHasMinus == false) || (theFirstHasMinus == false && theSecondHasMinus == true)){
  79.        if(objDescription[index+21] == '-'){
  80.             coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+21]);
  81.             coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+24]);
  82.  
  83.             fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  84.         fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  85.           }else{
  86.             coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+21]);
  87.             coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+23]);
  88.  
  89.             fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  90.         fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  91.           }
  92.      }else if(theFirstHasMinus == true && theSecondHasMinus == true){
  93.        if(objDescription[index+22] == '-'){
  94.             coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+22]);
  95.             coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+25]);
  96.  
  97.             fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  98.         fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  99.           }else{
  100.             coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+22]);
  101.             coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+24]);
  102.  
  103.             fe_out<<coordIntNumber[CoordIndex][2]<<" ";
  104.         fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
  105.           }
  106.      }
  107.  
  108.          fe_out<<endl;
  109.  
  110.     //Assign coordinates to vertexCoord
  111.      cout<<"Coord "<<CoordIndex<<": ";
  112.  
  113.      //X coord
  114.          if(coordIntNumber[CoordIndex][0] >= 0){
  115.           vertexCoord[CoordIndex][0] = coordIntNumber[CoordIndex][0]+(coordDecimalNumber[CoordIndex][0]/1000000);
  116.          }else{
  117.       vertexCoord[CoordIndex][0] = coordIntNumber[CoordIndex][0]-(coordDecimalNumber[CoordIndex][0]/1000000);
  118.          }
  119.  
  120.           cout<<vertexCoord[CoordIndex][0]<<" ";
  121.          
  122.  
  123.  
  124.     //Y coord
  125.         if(coordIntNumber[CoordIndex][1] >= 0){
  126.           vertexCoord[CoordIndex][1] = coordIntNumber[CoordIndex][1]+(coordDecimalNumber[CoordIndex][1]/1000000);
  127.         }else{
  128.       vertexCoord[CoordIndex][1] = coordIntNumber[CoordIndex][1]-(coordDecimalNumber[CoordIndex][1]/1000000);
  129.     }
  130.  
  131.           cout<<vertexCoord[CoordIndex][1]<<" ";
  132.          
  133.  
  134.     //Z coord
  135.         if(coordIntNumber[CoordIndex][2] >= 0){
  136.          vertexCoord[CoordIndex][2] = coordIntNumber[CoordIndex][2]+(coordDecimalNumber[CoordIndex][2]/1000000);
  137.         }else{
  138.      vertexCoord[CoordIndex][2] = coordIntNumber[CoordIndex][2]-(coordDecimalNumber[CoordIndex][2]/1000000);
  139.     }
  140.  
  141.          cout<<vertexCoord[CoordIndex][2]<<endl;
  142.      
  143.  
  144.      cout<<"Re-try:"<<endl;
  145.      cout<<" "<<vertexCoord[CoordIndex][0]
  146.          <<" "<<vertexCoord[CoordIndex][1]
  147.          <<" "<<vertexCoord[CoordIndex][2]<<endl;
  148.  
  149.     if(CoordIndex > 1){
  150.       cout<<"Now "<<CoordIndex<<" before: "<<CoordIndex-1<<endl;
  151.       cout<<"Reading values of precedent coord:"<<endl;
  152.       cout<<"   "<<vertexCoord[CoordIndex-1][0]
  153.          <<" "<<vertexCoord[CoordIndex-1][1]
  154.          <<" "<<vertexCoord[CoordIndex-1][2]<<endl;
  155.     }
  156.  
  157.     cout<<endl;
  158.  
  159.     ++CoordIndex;
  160.    }//End IF
  161.  
  162.    index++;
  163.  }//End WHILE



Dall'output su console, la prima volta le variabili sono giuste; quando invece le ristampo nell'iterazione successiva

Codice sorgente - presumibilmente C/C++

  1. if(CoordIndex > 1){
  2.       cout<<"Now "<<CoordIndex<<" before: "<<CoordIndex-1<<endl;
  3.       cout<<"Reading values of precedent coord:"<<endl;
  4.       cout<<"   "<<vertexCoord[CoordIndex-1][0]
  5.          <<" "<<vertexCoord[CoordIndex-1][1]
  6.          <<" "<<vertexCoord[CoordIndex-1][2]<<endl;
  7.     }



il loro valore è cambiato!
Come puo essere successo :-? :-?

Ultima modifica effettuata da il 11/01/2011 alle 20:30
PM
Usa i commenti per chiedere spiegazioni o ringraziare le risposte.