Пример #1
0
bool OpenALMusicPlayer::playAndManageBuffer()
{
	if (!ready) {
		return false;
	}
	
	int processed;
	bool active = true;

	alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);

	while(processed--) {
		ALuint buffer;
		
		alSourceUnqueueBuffers(source, 1, &buffer);
		check();
		active = streamBuffer(buffer);
		alSourceQueueBuffers(source, 1, &buffer);
		check();
	}

	if (!active && !isPlaying()) {
		// Try to reanimate playback
		if(!startPlayback()) {
			GfError("OpenALMusicPlayer: Cannot play stream.\n");
		}
	}
	
	return true;
}
Пример #2
0
bool OpenALMusicPlayer::startPlayback()
{
    if(isPlaying()) {
        return true;
	}
	
    if(!streamBuffer(buffers[0])) {
        return false;
	}
        
    if(!streamBuffer(buffers[1])) {
        return false;
	}
    
    alSourceQueueBuffers(source, 2, buffers);
    alSourcePlay(source);
    
    return true;
}
		void optimize()
		{
			if(size.x == 0 || size.y == 0)
				return;

			boost::shared_ptr<filesystem::MemoryStreamBuffer> streamBuffer(new filesystem::MemoryStreamBuffer());
			filesystem::OutputStream outputStream;
			outputStream.setBuffer(streamBuffer);
			
			filesystem::InputStream inputStream;
			inputStream.setBuffer(streamBuffer);

			for(int yb = 0; yb < blockAmount.y; ++yb)
			for(int xb = 0; xb < blockAmount.x; ++xb)
			{
				int xo = xb * (BLOCK_SIZE - 1);
				int yo = yb * (BLOCK_SIZE - 1);

				// Collect texture influences
				std::vector<OptimizeLayer> amounts(blendings.size());
				{
					for(unsigned int i = 0; i < blendings.size(); ++i)
					{
						amounts[i].index = i;
						BlendStorage &blend = blendings[i];

						for(int y = 0; y < BLOCK_SIZE; ++y)
						for(int x = 0; x < BLOCK_SIZE; ++x)
						{
							int index = (yo + y) * size.x + xo + x;
							int value = blend.weights[index];
							/*
							if(value < 50)
							{
								value = 0;
								blend.weights[index] = 0;
							}
							*/

							amounts[i].order += value;
						}
					}

					std::sort(amounts.begin(), amounts.end(), OptimizeLayerSorter());
				}

				// Normalize out all others
				{
					for(int y = 0; y < BLOCK_SIZE; ++y)
					for(int x = 0; x < BLOCK_SIZE; ++x)
					{
						int xp = xo + x;
						int yp = yo + y;
						if(xp < 0 || yp < 0 || xp >= size.x || yp >= size.y)
							continue;

						int index = yp * size.x + xp;
						int layerAmount = min(MAX_OPTIMIZE_LAYERS, int(amounts.size()));
						if(!layerAmount)
							continue;

						// Find highest amount of used textures (default 0) and normalize by
						// adding missing blend weights to it
						int highest = amounts[0].index;
						int highestAmount = blendings[highest].weights[index];
						int combined = 0;

						if(highestAmount < 255)
							int a = 0;

						int i = 0;
						for(i = 0; i < layerAmount; ++i)
						{
							int current = blendings[amounts[i].index].weights[index];
							combined += current;

							if(current > highestAmount)
							{
								highest = amounts[i].index;
								highestAmount = current;
							}
						}

						for(i = layerAmount; i < int(amounts.size()); ++i)
							blendings[amounts[i].index].weights[index] = 0;

						blendings[highest].weights[index] += 255 - combined;
					}
				}


			}

			/*
			// highest weight, texture index pairs for normalization
			std::vector<std::pair<int, int> > weights;
			// Amount of weights removed from given pixel
			std::vector<unsigned char> weightSub;

			weights.resize(size.x * size.y, std::pair<int, int> (-1, 0));
			weightSub.resize(size.x * size.y);

			for(unsigned int i = 0; i < blendings.size(); ++i)
			{
				BlendStorage &blend = blendings[i];

				for(unsigned int j = 0; j < blend.weights.size(); ++j)
				{
					std::pair<int, int> currentWeight(blend.weights[j], i);
					weights[j] = std::max(weights[j], currentWeight);

					if(blend.weights[j] < 75)
					{
						weightSub[j] += blend.weights[j];
						blend.weights[j] = 0;
					}
				}
			}

			// Normalize
			for(unsigned int j = 0; j < weightSub.size(); ++j)
			{
				std::pair<int, int> &currentWeight = weights[j];
				if(currentWeight.second < 0)
					continue;

				blendings[currentWeight.second].weights[j] += weightSub[j];
			}
			*/

			write(outputStream);
			read(inputStream);
		}
Пример #4
0
void Mesh::transform()
{
	const NifModel * nif = static_cast<const NifModel *>( iBlock.model() );
	if ( ! nif || ! iBlock.isValid() )
	{
		clear();
		return;
	}
	
	if ( upData )
	{
		upData = false;
		
		// update for NiMesh
		if ( nif->checkVersion( 0x14050000, 0 ) && nif->inherits( iBlock, "NiMesh" ) )
		{
#ifndef QT_NO_DEBUG
			// do stuff
			qWarning() << "Entering NiMesh decoding...";
			// mesh primitive type
			QString meshPrimitiveType = NifValue::enumOptionName( "MeshPrimitiveType", nif->get<uint>( iData, "Primitive Type" ) );
			qWarning() << "Mesh uses" << meshPrimitiveType;
			for ( int i = 0; i < nif->rowCount( iData ); i ++ )
			{
				// each data reference is to a single data stream
				quint32 stream = nif->getLink( iData.child( i, 0 ), "Stream" );
				qWarning() << "Data stream: " << stream;
				// can have multiple submeshes, unsure of exact meaning
				ushort numSubmeshes = nif->get<ushort>( iData.child( i, 0 ), "Num Submeshes" );
				qWarning() << "Submeshes: " << numSubmeshes;
				QPersistentModelIndex submeshMap = nif->getIndex( iData.child( i, 0 ), "Submesh To Region Map" );
				for ( int j = 0; j < numSubmeshes; j++ )
				{
					qWarning() << "Submesh" << j << "maps to region" << nif->get<ushort>( submeshMap.child( j, 0 ) );
				}
				// each stream can have multiple components, and each has a starting index
				QMap<uint, QString> componentIndexMap;
				int numComponents = nif->get<int>( iData.child( i, 0 ), "Num Components" );
				qWarning() << "Components: " << numComponents;
				// semantics determine the usage
				QPersistentModelIndex componentSemantics = nif->getIndex( iData.child( i, 0 ), "Component Semantics" );
				for( int j = 0; j < numComponents; j++ )
				{
					QString name = nif->get<QString>( componentSemantics.child( j, 0 ), "Name" );
					uint index = nif->get<uint>( componentSemantics.child( j, 0 ), "Index" );
					qWarning() << "Component" << name << "at position" << index << "of component" << j << "in stream" << stream;
					componentIndexMap.insert( j, QString( "%1 %2").arg( name ).arg( index ) );
				}
				
				// now the data stream itself...
				QPersistentModelIndex dataStream = nif->getBlock( stream );
				QByteArray streamData = nif->get<QByteArray>( nif->getIndex( dataStream, "Data" ).child( 0, 0 ) );
				QBuffer streamBuffer( &streamData );
				streamBuffer.open( QIODevice::ReadOnly );
				// probably won't use this
				QDataStream streamReader( &streamData, QIODevice::ReadOnly );
				// we should probably check the header here, but we expect things to be little endian
				streamReader.setByteOrder( QDataStream::LittleEndian );
				// each region exists within the data stream at the specified index
				quint32 numRegions = nif->get<quint32>( dataStream, "Num Regions");
				QPersistentModelIndex regions = nif->getIndex( dataStream, "Regions" );
				quint32 totalIndices = 0;
				if ( regions.isValid() )
				{
					qWarning() << numRegions << " regions in this stream";
					for( quint32 j = 0; j < numRegions; j++ )
					{
						qWarning() << "Start index: " << nif->get<quint32>( regions.child( j, 0 ), "Start Index" );
						qWarning() << "Num indices: " << nif->get<quint32>( regions.child( j, 0 ), "Num Indices" );
						totalIndices += nif->get<quint32>( regions.child( j, 0 ), "Num Indices" );
					}
					qWarning() << totalIndices << "total indices in" << numRegions << "regions";
				}
				uint numStreamComponents = nif->get<uint>( dataStream, "Num Components" );
				qWarning() << "Stream has" << numStreamComponents << "components";
				QPersistentModelIndex streamComponents = nif->getIndex( dataStream, "Component Formats" );
				// stream components are interleaved, so we need to know their type before we read them
				QList<uint> typeList;
				for( uint j = 0; j < numStreamComponents; j++ )
				{
					uint compFormat = nif->get<uint>( streamComponents.child( j, 0 ) );
					QString compName = NifValue::enumOptionName( "ComponentFormat", compFormat );
					qWarning() << "Component format is" << compName;
					qWarning() << "Stored as a" << compName.split( "_" )[1];
					typeList.append( compFormat - 1 );
					
					// this can probably wait until we're reading the stream values
					QString compNameIndex = componentIndexMap.value( j );
					QString compType = compNameIndex.split( " " )[0];
					uint startIndex = compNameIndex.split( " " )[1].toUInt();
					qWarning() << "Component" << j << "contains" << compType << "starting at index" << startIndex;

					// try and sort out texcoords here...
					if( compType == "TEXCOORD" )
					{
						QVector<Vector2> tempCoords;
						coords.append( tempCoords );
						qWarning() << "Assigning coordinate set" << startIndex;
					}

				}	
				
				// for each component
				// get the length
				// get the underlying type (will probably need OpenEXR to deal with float16 types)
				// read that type in, k times, where k is the length of the vector
				// start index will not be 0 if eg. multiple UV maps, but hopefully we don't have multiple components
				
				for( uint j = 0; j < totalIndices; j++ )
				{
					for( uint k = 0; k < numStreamComponents; k++ )
					{
						int typeLength = ( ( typeList[k] & 0x000F0000 ) >> 0x10 );
						int typeSize = ( ( typeList[k] & 0x00000F00 ) >> 0x08 );
						qWarning() << "Reading" << typeLength << "values" << typeSize << "bytes";

						NifIStream tempInput( new NifModel, &streamBuffer );
						QList<NifValue> values;
						NifValue tempValue;
						// if we had the right types, we could read in Vector etc. and not have the mess below
						switch( ( typeList[k] & 0x00000FF0 ) >> 0x04 )
						{
							case 0x10:
								tempValue.changeType( NifValue::tByte );
								break;
							case 0x21:
								tempValue.changeType( NifValue::tShort );
								break;
							case 0x42:
								tempValue.changeType( NifValue::tInt );
								break;
							case 0x43:
								tempValue.changeType( NifValue::tFloat );
								break;
						}
						for( int l = 0; l < typeLength; l++ )
						{
							tempInput.read( tempValue );
							values.append( tempValue );
							qWarning() << tempValue.toString();
						}
						QString compType = componentIndexMap.value( k ).split( " " )[0];
						qWarning() << "Will store this value in" << compType;
						// the mess begins...
						if( NifValue::enumOptionName( "ComponentFormat", (typeList[k] + 1 ) ) == "F_FLOAT32_3" )
						{
							Vector3 tempVect3( values[0].toFloat(), values[1].toFloat(), values[2].toFloat() );
							if( compType == "POSITION" )
							{
								verts.append( tempVect3 );
							}
							else if( compType == "NORMAL" )
							{
								norms.append( tempVect3 );
							}
						}
						else if( compType == "INDEX" )
						{
							indices.append( values[0].toCount() );
						}
						else if( compType == "TEXCOORD" )
						{
							Vector2 tempVect2( values[0].toFloat(), values[1].toFloat() );
							quint32 coordSet = componentIndexMap.value( k ).split( " " )[1].toUInt();
							qWarning() << "Need to append" << tempVect2 << "to texcoords" << coordSet;
							QVector<Vector2> currentSet = coords[coordSet];
							currentSet.append( tempVect2 );
							coords[coordSet] = currentSet;
						}
					}
				}

				// build triangles, strips etc.
				if( meshPrimitiveType == "MESH_PRIMITIVE_TRIANGLES" )
				{
					for( int k = 0; k < indices.size(); )
					{
						Triangle tempTri( indices[k], indices[k+1], indices[k+2] );
						qWarning() << "Inserting triangle" << tempTri;
						triangles.append( tempTri );
						k = k+3;
					}
				}
			}
#endif
		}
		else
		{