예제 #1
0
void SceneGraphComponent::getMeshData(MeshData* data)
{
	if (m_hordeID > 0)
	{
		H3DRes geoResource = 0;
		int vertexOffset = 0;
		int indexOffset = 0;
		switch(h3dGetNodeType(m_hordeID))
		{
		case H3DNodeTypes::Mesh:
			geoResource = h3dGetNodeParamI(h3dGetNodeParent(m_hordeID), H3DModel::GeoResI);
			data->NumVertices = h3dGetNodeParamI(m_hordeID, H3DMesh::VertREndI) - h3dGetNodeParamI(m_hordeID, H3DMesh::VertRStartI) + 1;
			data->NumTriangleIndices = h3dGetNodeParamI(m_hordeID, H3DMesh::BatchCountI);		
			data->VertRStart = h3dGetNodeParamI(m_hordeID, H3DMesh::VertRStartI);
			vertexOffset = h3dGetNodeParamI(m_hordeID, H3DMesh::VertRStartI) * 3;
			indexOffset = h3dGetNodeParamI(m_hordeID, H3DMesh::BatchStartI);
			break;
		case H3DNodeTypes::Model:
			geoResource = h3dGetNodeParamI(m_hordeID, H3DModel::GeoResI);
			data->NumVertices = h3dGetResParamI(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertexCountI);
			data->NumTriangleIndices = h3dGetResParamI(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexCountI);		
			break;
		case H3DEXT_NodeType_Terrain:
			unloadTerrainGeoRes();
			geoResource = h3dextCreateTerrainGeoRes( 
				m_hordeID, 
				h3dGetNodeParamStr( m_hordeID, H3DNodeParams::NameStr ), 
				h3dGetNodeParamF( m_hordeID, H3DEXTTerrain::MeshQualityF, 0) );		
			data->NumVertices = h3dGetResParamI(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertexCountI);
			data->NumTriangleIndices = h3dGetResParamI(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexCountI);		
			m_terrainGeoRes = geoResource;
			break;
		}
		float* vb = (float*)h3dMapResStream(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertPosStream, true, false);
		data->VertexBase = new float[(data->NumVertices*3)];
		memcpy(data->VertexBase, vb+vertexOffset, sizeof(float)*data->NumVertices*3);
		h3dUnmapResStream(geoResource);

		//Triangle indices, must cope with 16 bit and 32 bit
		if (h3dGetResParamI(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndices16I)) {
			unsigned short* tb = (unsigned short*)h3dMapResStream(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexStream, true, false);
			data->TriangleBase16 = new unsigned short[data->NumTriangleIndices];
			memcpy(data->TriangleBase16, tb+indexOffset, sizeof(unsigned short)*data->NumTriangleIndices);
			h3dUnmapResStream(geoResource);
		} else {
			unsigned int* tb = (unsigned int*)h3dMapResStream(geoResource, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexStream, true, false);
			data->TriangleBase32 = new unsigned int[data->NumTriangleIndices];
			memcpy(data->TriangleBase32, tb+indexOffset, sizeof(unsigned int)*data->NumTriangleIndices);
			h3dUnmapResStream(geoResource);
		}
		//data->TriangleMode = Horde3D::getResourceParami(geoResource, GeometryResParams::TriangleMode);
	}
}
예제 #2
0
void Animation::setResource(int resource)
{
	m_resource = resource;

	//intialize animation parameters
	m_numFrames = h3dGetResParamI(m_resource, H3DAnimRes::EntityElem, 0, H3DAnimRes::EntFrameCountI);

	//if no stroke information was given we'll define the whole animation as beeing the stroke
	if(m_file->getStrokeStart() < 0 || m_file->getStrokeEnd() < 0)
	{
		m_file->setForm(0, m_numFrames-1);
	}
}
예제 #3
0
bool VideoComponent::openAvi(const std::string& filename)
{
	// Stop any currently loaded avi
	closeAvi();

	AVIFileInit();							// Opens The AVIFile Library
	// Opens The AVI Stream
	if (AVIStreamOpenFromFile(&m_pavi, filename.c_str(), streamtypeVIDEO, 0, OF_READ, NULL) !=0)
	{
		GameLog::errorMessage("Error opening avi: %s", filename.c_str());
		// An Error Occurred Opening The Stream
		AVIFileExit();								// Release The File
		return false;
	}
	AVIStreamInfo(m_pavi, &m_psi, sizeof(m_psi));						// Reads Information About The Stream Into psi
	m_width = m_psi.rcFrame.right-m_psi.rcFrame.left;					// Width Is Right Side Of Frame Minus Left
	m_height = m_psi.rcFrame.bottom-m_psi.rcFrame.top;					// Height Is Bottom Of Frame Minus Top
	if (!m_resize)
	{
		// Size should be kept
		m_resizeWidth = m_width;
		m_resizeHeight = m_height;
	}
	m_lastframe = AVIStreamLength(m_pavi);								// The Last Frame Of The Stream
	m_timePerFrame = ((float)AVIStreamSampleToTime(m_pavi, m_lastframe) / (float) m_lastframe) / 1000.0f;	// Calculate Rough Seconds Per Frame
	m_bmih.biSize		= sizeof (BITMAPINFOHEADER);					// Size Of The BitmapInfoHeader
	m_bmih.biPlanes		= 1;					// Bitplanes
	m_bmih.biBitCount	= 24;					// Bits Format We Want 24 / 8  = 3 bytes
	m_bmih.biWidth		= m_resizeWidth;		// Width We Want
	m_bmih.biHeight		= m_resizeHeight;		// Height We Want
	m_bmih.biCompression= BI_RGB;				// Requested Mode = RGB
	m_hBitmap = CreateDIBSection (m_hdc, (BITMAPINFO*)(&m_bmih), DIB_RGB_COLORS, (void**)(&m_data), NULL, NULL);	
	SelectObject (m_hdc, m_hBitmap);					// Select hBitmap Into Our Device Context (hdc)
	// Bitmapinfo header for decoding (needed for xvid)
	m_bmiavih.biSize = sizeof(BITMAPINFOHEADER);
	m_bmiavih.biPlanes			= 1;					// Bitplanes
	m_bmiavih.biBitCount		= 24;					// Bits Format We Want 24 / 8  = 3 bytes
	m_bmiavih.biWidth			= m_width;				// Width We Want
	m_bmiavih.biHeight			= m_height;				// Height We Want
	m_bmiavih.biCompression		= BI_RGB;				// Requested Mode = RGB
	// And some more infos
	m_bmiavih.biClrImportant	= 0;
	m_bmiavih.biClrUsed			= 0;
	m_bmiavih.biXPelsPerMeter	= 0;
	m_bmiavih.biYPelsPerMeter	= 0;
	m_bmiavih.biSizeImage = (((m_bmiavih.biWidth * 3) + 3) & 0xFFFC) * m_bmiavih.biHeight;
	m_pgf=AVIStreamGetFrameOpen(m_pavi, &m_bmiavih);// Create The PGETFRAME Using Our Request Mode
	if (m_pgf==0x0)
	{
		GameLog::errorMessage("Error opening first frame of avi: %s", filename.c_str());
		// An Error Occurred Opening The Frame
		DeleteObject(m_hBitmap);					// Delete The Device Dependant Bitmap Object
		AVIStreamRelease(m_pavi);					// Release The Stream
		AVIFileExit();								// Release The File
		return false;
	}
	m_fileName = filename;

	// Create buffer for converted data
	// width*height = count pixel; each pixel has 4 channels for rgba with each one byte
	int dataSize = 4*m_resizeWidth*m_resizeHeight;
	m_bgraData = new unsigned char[dataSize];
	// Initialize with 255 (black screen with full alpha)
	memset(m_bgraData, 255, dataSize);

	// Prepare horde texture stream named like the video file name, to get a unique name
	m_videoTexture = h3dCreateTexture(filename.c_str(), m_resizeWidth, m_resizeHeight, H3DFormats::TEX_BGRA8, H3DResFlags::NoTexMipmaps);
	if (m_videoTexture == 0)
	{
		GameLog::errorMessage("Error creating texture for playing avi: %s", filename.c_str());
		// Failure creating the dynamic texture
		closeAvi();
		return false;
	}

	// Find the sampler index within the material
	m_samplerIndex = h3dFindResElem(m_material, H3DMatRes::SamplerElem, H3DMatRes::SampNameStr, "albedoMap");	
	if (m_samplerIndex == -1)
	{
		GameLog::errorMessage("Error preparing material with resID %d for playing avi: %s", m_material, filename.c_str());
		// No sampler found in material
		closeAvi();
		return false;
	}

	// Store old sampler
	m_originalSampler = h3dGetResParamI(m_material, H3DMatRes::SamplerElem, m_samplerIndex, H3DMatRes::SampTexResI);

	
	// Now open the audio stream
	PAVISTREAM audioStream;
	if (AVIStreamOpenFromFile(&audioStream, filename.c_str(), streamtypeAUDIO, 0, OF_READ, NULL) == 0)
	{
		// Audio stream found
		// Get format info
		PCMWAVEFORMAT audioFormat;
		long formatSize = sizeof(audioFormat);
		int start = AVIStreamStart(audioStream);
		// TODO get channelsmask and use it
		AVIStreamReadFormat(audioStream, start, &audioFormat, &formatSize);
		long numSamples = AVIStreamLength(audioStream);
		int bitsPerSample = (audioFormat.wf.nAvgBytesPerSec * 8) / (audioFormat.wf.nSamplesPerSec * audioFormat.wf.nChannels);
		/*if (audioFormat.wf.wFormatTag == WAVE_FORMAT_MPEGLAYER3)
		{
			// TODO
			MPEGLAYER3WAVEFORMAT mp3Format;
			formatSize = sizeof(mp3Format);
			AVIStreamReadFormat(audioStream, start, &mp3Format, &formatSize);
		}*/

		// Create buffer with appropriate size
		long bufferSize = (bitsPerSample * numSamples) / 8;
		char* buffer = new char[bufferSize];
		// Read the audio data
		long bytesWritten = 0;
		AVIStreamRead(audioStream, start, numSamples, buffer, bufferSize, &bytesWritten, 0x0);

		if (bytesWritten > 0)
		{
			// Send the audio data to the sound component
			SoundResourceData eventData(buffer, bytesWritten, audioFormat.wf.nSamplesPerSec, bitsPerSample, audioFormat.wf.nChannels);
			GameEvent event(GameEvent::E_SET_SOUND_WITH_USER_DATA, &eventData, this);
			m_owner->executeEvent(&event);
			m_hasAudio = true;
		}

		// Delete the buffer data
		delete[] buffer;
	}

	if (m_autoStart)
		// Play video directly
		playAvi();

	return true;
}
	btCollisionShape* createCollisionShape()
	{
		if (mesh->getModelNode() == 0) mesh->registerStart();

		m_btTriangleMesh = new btTriangleMesh();
		H3DNode meshNode = h3dGetNodeChild(mesh->getModelNode(), 0); //TODO handle multi-mesh stuff
		if (meshNode == 0) throw Exception("Couldn't get mesh node.\n");
		H3DRes geoRes = h3dGetNodeParamI(mesh->getModelNode(), H3DModel::GeoResI);
		unsigned int numVertices = h3dGetNodeParamI(meshNode, H3DMesh::VertREndI) - h3dGetNodeParamI(meshNode, H3DMesh::VertRStartI) + 1;//get mesh vertices count
		unsigned int numTriangleIndices = h3dGetNodeParamI(meshNode, H3DMesh::BatchCountI);//get mesh triangle indices count
		unsigned int vertRStart = h3dGetNodeParamI(meshNode, H3DMesh::VertRStartI);//get the first vertex of the mesh
		int off = 3;


		std::cout << numVertices << " vertices.\n";
		std::cout << numTriangleIndices << " indices.\n";

		int vertexOffset = vertRStart * 3;
		int indexOffset = h3dGetNodeParamI(meshNode, H3DMesh::BatchStartI);

		float* vertexBase = (float*) h3dMapResStream(geoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoVertPosStream, true, false);//gets pointer to the vertex data
		h3dUnmapResStream(geoRes);//closes vertex stream

		if(vertexBase) vertexBase += vertexOffset;

		unsigned int* TriangleBase32 = nullptr;
		unsigned short* TriangleBase16 = nullptr;

		if (h3dGetResParamI(geoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndices16I))
		{
			unsigned short* tb = (unsigned short*)h3dMapResStream(geoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexStream, true, false);
			TriangleBase16 = new unsigned short[ numTriangleIndices ];
			memcpy(TriangleBase16, tb+indexOffset, sizeof(unsigned short)*numTriangleIndices);
			h3dUnmapResStream(geoRes);
			delete TriangleBase32;
		}
		else
		{
			unsigned int* tb = (unsigned int*)h3dMapResStream(geoRes, H3DGeoRes::GeometryElem, 0, H3DGeoRes::GeoIndexStream, true, false);
			TriangleBase32 = new unsigned int[numTriangleIndices];
			memcpy(TriangleBase32, tb+indexOffset, sizeof(unsigned int)*numTriangleIndices);
			h3dUnmapResStream(geoRes);
			delete TriangleBase16;
		}

		unsigned int indices[numTriangleIndices];

		bool index16 = false;
		if (TriangleBase16)
			index16 = true;

	    for (unsigned int i = 0; i < numTriangleIndices - 2; i += 3)
		 {
			unsigned int index1 = index16 ? (TriangleBase16[i]   - vertRStart) * 3 : (TriangleBase32[i]   - vertRStart) * 3;
			unsigned int index2 = index16 ? (TriangleBase16[i+1] - vertRStart) * 3 : (TriangleBase32[i+1] - vertRStart) * 3;
			unsigned int index3 = index16 ? (TriangleBase16[i+2] - vertRStart) * 3 : (TriangleBase32[i+2] - vertRStart) * 3;

			m_btTriangleMesh->addTriangle(
			   btVector3(vertexBase[index1], vertexBase[index1+1], vertexBase[index1+2] ),
			   btVector3(vertexBase[index2], vertexBase[index2+1], vertexBase[index2+2] ),
			   btVector3(vertexBase[index3], vertexBase[index3+1], vertexBase[index3+2]),
			   false
			);
		 }

	    return new btBvhTriangleMeshShape(m_btTriangleMesh, true, true);
	}