Oppure

Loading
11/01/11 19:28
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[][].

while(index < strlen(objDescription)){
   theFirstHasMinus = false;
   theSecondHasMinus = false;
   theThirdHasMinus = false;

   if(objDescription[index] == 'v' && objDescription[index-1] == '\n' && objDescription[index+1] == ' '){
	 //X coord
	 if(objDescription[index+2] == '-'){
	   coordIntNumber[CoordIndex][0] = atoi(&objDescription[index+2]);
	   coordDecimalNumber[CoordIndex][0] = atoi(&objDescription[index+5]);

       fe_out<<coordIntNumber[CoordIndex][0]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][0]<<endl;

       theFirstHasMinus = true;
	 }else{
	   coordIntNumber[CoordIndex][0] = atoi(&objDescription[index+2]);
	   coordDecimalNumber[CoordIndex][0]= atoi(&objDescription[index+4]);

       fe_out<<coordIntNumber[CoordIndex][0]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][0]<<endl;

       theFirstHasMinus = false;
	 }
     //Y coord
     if(!theFirstHasMinus){
	  if(objDescription[index+11] == '-'){
	   coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+11]);
	   coordDecimalNumber[CoordIndex][1]= atoi(&objDescription[index+14]);

	   fe_out<<coordIntNumber[CoordIndex][1]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;

       theSecondHasMinus = true;
	  }else{
	   coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+11]);
	   coordDecimalNumber[CoordIndex][1] = atoi(&objDescription[index+13]);

       fe_out<<coordIntNumber[CoordIndex][1]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;

       theSecondHasMinus = false;
	  }
     }else{
       if(objDescription[index+12] == '-'){
	   coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+12]);
	   coordDecimalNumber[CoordIndex][1]= atoi(&objDescription[index+15]);

	   fe_out<<coordIntNumber[CoordIndex][1]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;

       theSecondHasMinus = true;
	  }else{
	   coordIntNumber[CoordIndex][1] = atoi(&objDescription[index+12]);
	   coordDecimalNumber[CoordIndex][1] = atoi(&objDescription[index+14]);

       fe_out<<coordIntNumber[CoordIndex][1]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][1]<<endl;

       theSecondHasMinus = false;
	  }
     }
     //Z coord
     if(theFirstHasMinus == false && theSecondHasMinus == false){
	  if(objDescription[index+20] == '-'){
	   coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+20]);
	   coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+23]);

	   fe_out<<coordIntNumber[CoordIndex][2]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }else{
	   coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+20]);
	   coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+22]);

	   fe_out<<coordIntNumber[CoordIndex][2]<<" ";
       fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }
     }else if((theFirstHasMinus == true && theSecondHasMinus == false) || (theFirstHasMinus == false && theSecondHasMinus == true)){
       if(objDescription[index+21] == '-'){
	    coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+21]);
	    coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+24]);

	    fe_out<<coordIntNumber[CoordIndex][2]<<" ";
        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }else{
	    coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+21]);
	    coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+23]);

	    fe_out<<coordIntNumber[CoordIndex][2]<<" ";
        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }
     }else if(theFirstHasMinus == true && theSecondHasMinus == true){
       if(objDescription[index+22] == '-'){
	    coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+22]);
	    coordDecimalNumber[CoordIndex][2] = atoi(&objDescription[index+25]);

	    fe_out<<coordIntNumber[CoordIndex][2]<<" ";
        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }else{
	    coordIntNumber[CoordIndex][2] = atoi(&objDescription[index+22]);
	    coordDecimalNumber[CoordIndex][2]= atoi(&objDescription[index+24]);

	    fe_out<<coordIntNumber[CoordIndex][2]<<" ";
        fe_out<<coordDecimalNumber[CoordIndex][2]<<endl;
	  }
     }

	 fe_out<<endl;

    //Assign coordinates to vertexCoord
     cout<<"Coord "<<CoordIndex<<": ";

     //X coord
	 if(coordIntNumber[CoordIndex][0] >= 0){
 	  vertexCoord[CoordIndex][0] = coordIntNumber[CoordIndex][0]+(coordDecimalNumber[CoordIndex][0]/1000000);
	 }else{
      vertexCoord[CoordIndex][0] = coordIntNumber[CoordIndex][0]-(coordDecimalNumber[CoordIndex][0]/1000000);
	 }

	  cout<<vertexCoord[CoordIndex][0]<<" ";
	 


    //Y coord
	if(coordIntNumber[CoordIndex][1] >= 0){
	  vertexCoord[CoordIndex][1] = coordIntNumber[CoordIndex][1]+(coordDecimalNumber[CoordIndex][1]/1000000);
	}else{
      vertexCoord[CoordIndex][1] = coordIntNumber[CoordIndex][1]-(coordDecimalNumber[CoordIndex][1]/1000000);
    }

	  cout<<vertexCoord[CoordIndex][1]<<" ";
	 

    //Z coord
	if(coordIntNumber[CoordIndex][2] >= 0){
	 vertexCoord[CoordIndex][2] = coordIntNumber[CoordIndex][2]+(coordDecimalNumber[CoordIndex][2]/1000000);
	}else{
     vertexCoord[CoordIndex][2] = coordIntNumber[CoordIndex][2]-(coordDecimalNumber[CoordIndex][2]/1000000);
    }

	 cout<<vertexCoord[CoordIndex][2]<<endl;
     

     cout<<"Re-try:"<<endl;
     cout<<" "<<vertexCoord[CoordIndex][0]
         <<" "<<vertexCoord[CoordIndex][1]
         <<" "<<vertexCoord[CoordIndex][2]<<endl;

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

    cout<<endl;

    ++CoordIndex;
   }//End IF

   index++;
 }//End WHILE


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

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


il loro valore è cambiato!
Come puo essere successo :-? :-?
Ultima modifica effettuata da 11/01/11 19:30