Пример #1
0
void ColladaMeshFactory::GenerateMeshDatafromPolygonsFull(	
	RevModel** aModel,
	RevIndexData& aIndexData,
	RevVertexDataNormal& aVertexData,
	RevInputData& aInputData,
	FCDGeometryInstance* aInstance,
	FCDGeometryMesh* aMesh,
	FCDocument* aColladaDocument,
	bool aHasBoneWeightsFlag,
	FCDController* aController,
	FCDControllerInstance* aControllerInstance)
{
	int polygonalMeshes= static_cast<int>(aMesh->GetPolygonsCount());

	//	assert(polygonalMeshes==1); // how to handle multiple meshes ? handle later

	// To do add support for multiple meshes ?

	FCDSkinController* skinControl=NULL;
	if(aHasBoneWeightsFlag==true)
	{
		skinControl=aController->GetSkinController();
		skinControl->ReduceInfluences(4);
	}

	int myVertexCount=0;
	int duplicatesCount=0;
	int currentIndex=0;

	std::map<RevVertexIndexmapKey,int> vertexList;
	std::map<RevVertexIndexmapKey,int>::iterator vertexIterator;
	std::vector<RevVertexIndexmapKey> vertexes;

	aIndexData.myNrOfIndexes =0;
	aIndexData.myFormat =DXGI_FORMAT_R32_UINT;
	for(int polygonMeshIndex=0;polygonMeshIndex<static_cast<int>(polygonalMeshes);polygonMeshIndex++)
	{
		FCDGeometryPolygons* somePolygons=aMesh->GetPolygons(polygonMeshIndex);
		FCDGeometryPolygonsInput* positionInput=somePolygons->FindInput(FUDaeGeometryInput::POSITION);
		aIndexData.myNrOfIndexes+=positionInput->GetIndexCount();

	}

	aIndexData.mySize=aIndexData.myNrOfIndexes*sizeof(unsigned int);

	aIndexData.myIndexData = new char [aIndexData.mySize] ();
	UINT* indexArray=reinterpret_cast<unsigned int*>(aIndexData.myIndexData);

	FCDGeometrySource* positionSource=aMesh->FindSourceByType(FUDaeGeometryInput::POSITION);
	FCDGeometrySource* normalSource=aMesh->FindSourceByType(FUDaeGeometryInput::NORMAL);
	FCDGeometrySource* colorSource=aMesh->FindSourceByType(FUDaeGeometryInput::COLOR);
	FCDGeometrySource* textureCordinatesSource=aMesh->FindSourceByType(FUDaeGeometryInput::TEXCOORD);
	FCDGeometrySource* geoTangentSource=aMesh->FindSourceByType(FUDaeGeometryInput::GEOTANGENT);
	FCDGeometrySource* texTangentSource=aMesh->FindSourceByType(FUDaeGeometryInput::TEXTANGENT);
	FCDGeometrySource* geoBiNormalSource=aMesh->FindSourceByType(FUDaeGeometryInput::GEOBINORMAL);
	FCDGeometrySource* texBiNormalSource=aMesh->FindSourceByType(FUDaeGeometryInput::TEXBINORMAL);


	RevModelRenderEssentials renderEssentials( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

	SelectEffectOnModel(aModel, aInputData);

	RevTextures tex;

	for(int polygonMeshIndex=0;polygonMeshIndex<polygonalMeshes;polygonMeshIndex++)
	{	
		(*aModel)->SetIsNullObjectFlag( false );
		FCDGeometryPolygons* somePolygons=aMesh->GetPolygons(polygonMeshIndex);

		FCDGeometryPolygonsInput* positionInput=somePolygons->FindInput(FUDaeGeometryInput::POSITION);
		FCDGeometryPolygonsInput* normalInput=somePolygons->FindInput(FUDaeGeometryInput::NORMAL);
		FCDGeometryPolygonsInput* colorInput=somePolygons->FindInput(FUDaeGeometryInput::COLOR);
		FCDGeometryPolygonsInput* texCoordInput=somePolygons->FindInput(FUDaeGeometryInput::TEXCOORD);
		FCDGeometryPolygonsInput* geoTangentInput=somePolygons->FindInput(FUDaeGeometryInput::GEOTANGENT);
		FCDGeometryPolygonsInput* texTangentInput=somePolygons->FindInput(FUDaeGeometryInput::TEXTANGENT);
		FCDGeometryPolygonsInput* geoBiNormalInput=somePolygons->FindInput(FUDaeGeometryInput::GEOBINORMAL);
		FCDGeometryPolygonsInput* texBiNormalInput=somePolygons->FindInput(FUDaeGeometryInput::TEXBINORMAL);

		somePolygons->GetFaceVertexCount();


		renderEssentials.myVertexStart = 0;
		renderEssentials.myIndexStart = currentIndex;
		renderEssentials.myIndexCount = positionInput->GetIndexCount();

		for(int i=0;i<static_cast<int>(positionInput->GetIndexCount());i++)
		{
			RevVertexIndexmapKey key;
			if(positionInput!=NULL)
			{
				key.myKeys[0]=positionInput->GetIndices()[i];
			}
			if(normalInput!=NULL)
			{
				key.myKeys[1]=normalInput->GetIndices()[i];
			}
			if(colorInput!=NULL)
			{
				key.myKeys[2]=colorInput->GetIndices()[i];
			}
			if(texCoordInput!=NULL)
			{
				key.myKeys[3]=texCoordInput->GetIndices()[i];
			}
			if(geoTangentInput!=NULL)
			{
				key.myKeys[4]=geoTangentInput->GetIndices()[i];
			}
			if(texTangentInput!=NULL)
			{
				key.myKeys[5]=texTangentInput->GetIndices()[i];
			}

			if(geoBiNormalInput!=NULL)
			{
				key.myKeys[6]=geoBiNormalInput->GetIndices()[i];
			}

			if(texBiNormalInput!=NULL)
			{
				key.myKeys[7]=texBiNormalInput->GetIndices()[i];
			}


			vertexIterator=vertexList.find(key);
			if(vertexIterator==vertexList.end())
			{
				indexArray[currentIndex]=myVertexCount;
				std::pair<RevVertexIndexmapKey,int> tempPair(key,myVertexCount);
				vertexList.insert(tempPair);
				vertexes.push_back(key);
				myVertexCount++;
				// create indexes
			}
			else
			{
				indexArray[currentIndex]=(*vertexIterator).second;
				duplicatesCount++;
				// Read Created Vertex
			}
			currentIndex++;
		}

		for (unsigned int k=0; k<aInstance->GetMaterialInstanceCount(); k++) 
		{
			FCDMaterialInstance* materialInstance;
			materialInstance=aInstance->GetMaterialInstance(k);
			if(aMesh->GetPolygons(polygonMeshIndex)->GetMaterialSemantic()==materialInstance->GetSemantic())
			{
				LoadMaterialNew(polygonMeshIndex,materialInstance->GetMaterial(),aColladaDocument, tex);
			}
		}
	}

	(*aModel)->SetModelTextures( tex );


	aVertexData.myNrOfVertexes=myVertexCount;
	aVertexData.mySize=aVertexData.myStride*aVertexData.myNrOfVertexes;
	aVertexData.myVertexData =new char [aVertexData.mySize] ();

	for(int i=0;i<aIndexData.myNrOfIndexes;i+=3)
	{
		unsigned int temp;	
		temp=indexArray[i+0];
		indexArray[i+0]=indexArray[i+2];
		indexArray[i+2]=temp;
	}

	myVertexCount=0;
	for(int i=0;i<static_cast<int>(vertexes.size());i++)
	{
		RevVertexIndexmapKey key=vertexes[i];

		int offset=0;
		std::pair<RevVertexIndexmapKey,int> tempPair(key,myVertexCount);
		vertexList.insert(tempPair);

		// Create Vertex
		if(key.myKeys[0]!=-1)
		{
			CU::Vector3f pos;
			memcpy(&pos,
				&positionSource->GetData()[key.myKeys[0]*positionSource->GetStride()],12);

			//			pos.y=-pos.y;
			//			pos.z=-pos.z;
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&pos,12);

			offset+=12;
		}
		if(aHasBoneWeightsFlag==true)
		{
			assert(key.myKeys[0]!=-1);
			FCDSkinControllerVertex* vertexsInfluence;

			vertexsInfluence=skinControl->GetVertexInfluence(key.myKeys[0]);

			int boneData=0;
			int weightData=0;

			assert(vertexsInfluence->GetPairCount()<=4);
			for(int j=0;j<static_cast<int>(vertexsInfluence->GetPairCount());j++)
			{
				assert(j<4);
				FCDJointWeightPair* pair=vertexsInfluence->GetPair(j);

				assert(pair->jointIndex<256);
				boneData |= pair->jointIndex << (j*8);

				int weight=static_cast<int>((pair->weight*255.0f+0.5));
				assert(weight>=0);
				assert(weight<=255);
				weightData |= weight << (j*8);
			}
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],&weightData,4);
			offset+=4;
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],&boneData,4);
			offset+=4;

		}
		if(key.myKeys[1]!=-1)
		{
			CU::Vector3f normal;
			memcpy(&normal,
				&normalSource->GetData()[key.myKeys[1]*normalSource->GetStride()],12);

			//			normal.y=-normal.y;
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&normal,12);
			offset+=12;
		}
		if(key.myKeys[2]!=-1)
		{
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&colorSource->GetData()[key.myKeys[2]*colorSource->GetStride()],16);
			offset+=16;
		}
		if(key.myKeys[3]!=-1)
		{
			CU::Vector2f UV;
			memcpy(&UV,
				&textureCordinatesSource->GetData()[key.myKeys[3]*textureCordinatesSource->GetStride()],8);

			UV.v=1.0f-UV.v;




			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&UV,8);
			offset+=8;
		}
		if(key.myKeys[4]!=-1)
		{
			CU::Vector3f tangent;
			memcpy(&tangent,
				&geoTangentSource->GetData()[key.myKeys[4]*geoTangentSource->GetStride()],12);
			//			tangent.y=-tangent.y;
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&tangent,12);
			offset+=12;
		}
		if(key.myKeys[5]!=-1)
		{
			CU::Vector3f tangent;
			memcpy(&tangent,
				&texTangentSource->GetData()[key.myKeys[5]*texTangentSource->GetStride()],12);
			//			tangent.y=-tangent.y;
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&tangent,12);
			offset+=12;
		}

		if(key.myKeys[6]!=-1)
		{
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&geoBiNormalSource->GetData()[key.myKeys[6]*geoBiNormalSource->GetStride()],12);
			offset+=12;
		}
		if(key.myKeys[7]!=-1)
		{
			memcpy(&aVertexData.myVertexData[myVertexCount*aVertexData.myStride+offset],
				&texBiNormalSource->GetData()[key.myKeys[7]*texBiNormalSource->GetStride()],12);
			offset+=12;
		}

		myVertexCount++;
	}
	if( (*aModel)->Init( aVertexData, aIndexData, aInputData, renderEssentials ) ==false)
	{
		assert(0 && "Something went wrong in trying to init the current model");
	}
}
Пример #2
0
//*************************************************************************************************
//Function to create a distribution stacked histogram for all the bkg processes 
//for the variable given
//*************************************************************************************************
THStack* makeBkgDistributionHistogram ( int variableIndex, MitNtupleEvent* event , 
                                        Int_t nentries, TLegend *legend, 
                                        int OnlyThisFinalState = -1) {
  assert(variableIndex >= 0 && variableIndex < NVARIABLES);
  string histName = fVariableNames[variableIndex] + "_bkg";
  string axisLabel = ";" + fVariableNames[variableIndex] + ";Number of Events";
  THStack *stackHistogram = new THStack(histName.c_str(),axisLabel.c_str());
  //stackHistogram->SetDirectory(0);

  vector< pair<int, TH1F*> > bkgHistograms;

  for (int n=0;n<nentries;n++) { 
    event->GetEntry(n);
    float eventweight = event->H_weight;
    int finalstatetype = (int)event->H_ltype;
    int decay = (int)event->H_decay;

    //only look at events with the given finalstatetype
    if (OnlyThisFinalState >= 0 && OnlyThisFinalState != finalstatetype)
      continue;

    Float_t cutAboveValues[NVARIABLES];
    Float_t cutBelowValues[NVARIABLES];
    Float_t variableValues[NVARIABLES];   

    //only look at events with the given finalstatetype
    //convert finalstatetype into the array index
    int fs;
    if (finalstatetype == 10)
      fs = 0;
    else if (finalstatetype == 11)
      fs = 1;
    else if (finalstatetype == 12)
      fs = 2;
    else {
      fs = -1;
      continue;
    }

    for (int j=0;j<NVARIABLES;j++) {
      cutAboveValues[j] = fCutAboveInitialValue[fs][j];
      cutBelowValues[j] = fCutBelowInitialValue[fs][j];
      variableValues[j] = GetVariableValue(j, event);      
    }
    //printf(" B %d\n", n);

    //For N-1 Distributions
    //if (passCut(event,variableValues, cutAboveValues, cutBelowValues, variableIndex)) {
    if (passCut(event,variableValues, cutAboveValues, cutBelowValues)) {

      //Search for the bkg process to see if it already exists
      bool foundProcess = false;
      int foundIndex = -1;      
      for (UInt_t p=0;p<bkgHistograms.size();p++) {
        //cout << "Check bkgHistograms " << p << " " << bkgHistograms[p].first << " " 
        //       <<  bkgHistograms[p].second << endl;
        if (decay == bkgHistograms[p].first) {
          foundProcess = true;
          foundIndex = p;
        }
      }

      if (!foundProcess) {
        //get name of the process
        int decayIndex = -1;

        for (int k=0;k<NPROCESSES;k++) {
          if (fProcessCode[k] == decay)
            decayIndex = k;
        }
        string temphistname = "";
        if (decayIndex >= 0) {
          temphistname = fProcessName[decayIndex];
        }

        string temphistAxisLabel = ";" + fVariableNames[variableIndex] + ";Number of Events";
        TH1F *temphist = new TH1F(temphistname.c_str(),axisLabel.c_str(),NBINS, 
                                  fVariableRangeMin[variableIndex], 
                                  fVariableRangeMax[variableIndex]);
        temphist->SetDirectory(0);

        //temphist->SetFillStyle(1001);
        temphist->SetFillColor(fColors[decayIndex]);
        temphist->SetLineWidth(1);

        //Fill histogram
        temphist->Fill( variableValues[variableIndex], eventweight);

        pair< int, TH1F* > tempPair(decay,temphist);
        bkgHistograms.push_back(tempPair);

        //Add legend entry
        legend->AddEntry(temphist, temphistname.c_str(), "f"); 

      } else {       
        //fill histogram
        bkgHistograms[foundIndex].second->Fill( variableValues[variableIndex], eventweight);
      }
    }
  }

  //add to the stack
  for (UInt_t p=0;p<bkgHistograms.size();p++) {
    stackHistogram->Add(bkgHistograms[p].second);
  }

  return stackHistogram;
}