Example #1
0
File: Mesh.cpp Project: jomanto/le2
Mesh::Mesh()
{
  BufferLayout layout;
  layout.add(ET_vec3_f32, UT_position);
  layout.add(ET_vec2_f32, UT_texcoord0);
  init(layout, ET_u16);
}
Example #2
0
	void GLVertexBuffer::SetLayout(const BufferLayout& bufferLayout)
	{
		m_Layout = bufferLayout;
		const std::vector<BufferElement>& layout = bufferLayout.GetLayout();
		for (uint i = 0; i < layout.size(); i++)
		{
			const BufferElement& element = layout[i];
			GLCall(glEnableVertexAttribArray(i));
			GLCall(glVertexAttribPointer(i, element.count, element.type, element.normalized, bufferLayout.GetStride(), (const void*)element.offset));
		}
	}
Example #3
0
DrawContext::DrawContext(Context* ctx)
{
  glContext = ctx;
  
  _textBuffer = new TextBuffer;
  
  // load some common shaders
  colorShader = Application::instance()->resourceManager->shader("resources/glsl/color");
  textureShader = Application::instance()->resourceManager->shader("resources/glsl/texture");
  
  // create buffers for efficient quad drawing. Vertex and index buffers are reused as often as possible
  BufferLayout layout;
  layout.add(ET_vec2_f32, UT_position);
  layout.add(ET_vec2_f32, UT_texcoord0);
  bgquad = Mesh::create(layout, ET_u16);
  bgquad->resetSize(4, 6);
  bgquad->indexBuffer->drawMode = GL_TRIANGLES;
  
  bgquad->set(0, UT_position, Vec2(0,0));
  bgquad->set(1, UT_position, Vec2(1,0));
  bgquad->set(2, UT_position, Vec2(1,1));
  bgquad->set(3, UT_position, Vec2(0,1));
  
  bgquad->set(0,UT_texcoord0, Vec2(0,0));
  bgquad->set(1,UT_texcoord0, Vec2(1,0));
  bgquad->set(2,UT_texcoord0, Vec2(1,1));
  bgquad->set(3,UT_texcoord0, Vec2(0,1));
  
  bgquad->set(0, UT_index, (u16)0);
  bgquad->set(1, UT_index, (u16)1);
  bgquad->set(2, UT_index, (u16)2);
  bgquad->set(3, UT_index, (u16)2);
  bgquad->set(4, UT_index, (u16)3);
  bgquad->set(5, UT_index, (u16)0);
  
  bgquad->material->shader = colorShader;
  bgquad->material->color = whiteColor;
  bgquad->material->blendPremultiplied();
  
  textMesh.reset(new TextMesh);
  textMesh->material->shader = textureShader;
  textMesh->material->color = whiteColor;
  
  _flipX = false;
  _flipY = false;
  updateTexCoords();
  
  ninePatch.reset(new NinePatch);
  ninePatch->flip = true;
  ninePatch->material->shader = textureShader;
  ninePatch->material->blendPremultiplied();
}
Example #4
0
HybridIndexBuffer::HybridIndexBuffer(ElementType et)
{
  // an indexbuffer only ever has one attribute with usage type index in a single partition
  // only the element type can vary, to optimize the buffer for hardware requirements or
  // mesh sizes.
  BufferLayout layout;
  layout.add(et, UT_index);
  switch(et)
  {
    case ET_u8:type = GL_UNSIGNED_BYTE;break;
    case ET_u16:type = GL_UNSIGNED_SHORT;break;
    case ET_u32:type = GL_UNSIGNED_INT;break;
    default:
      ASSERT(false,"only u8, u16, u32 are allowed");
  }
  drawMode = GL_TRIANGLES;
  init(GL_ELEMENT_ARRAY_BUFFER, layout);
}
Example #5
0
void NinePatch::init()
{
  BufferLayout layout;
  layout.add(ET_vec2_f32, UT_position);
  layout.add(ET_vec2_f32, UT_texcoord0);
  this->resetBuffers(layout, ET_u16);

  indexBuffer->drawMode = GL_TRIANGLES;

  u32 numVertices = 16; // draw it on paper and you'll see it's correct
  uint32_t numQuads = 9; // it's a 3x3 matrix of quads
  uint32_t numTris = numQuads*2; // each quad is drawn with two tris
  u32 numIndices = numTris*3; // currently, each tri is drawn with 3 indices

  this->vertexBuffer->reset(numVertices);
  this->indexBuffer->reset(numIndices);

//  updateTexCoords();
  updateIndices();

}
HybridIndexBuffer::HybridIndexBuffer(ElementType et)
{
  // an indexbuffer only ever has one attribute with usage type index in a single partition
  // only the element type can vary, to optimize the buffer for hardware requirements or
  // mesh sizes.
  BufferLayout layout;
  layout.add(et, UT_index);
  switch(et)
  {
    case ET_u8:type = GL_UNSIGNED_BYTE;break;
    case ET_u16:type = GL_UNSIGNED_SHORT;break;
    case ET_u32:type = GL_UNSIGNED_INT;break;
    default:
      lost::common::StringStream os;
      os << "only u8, u16, u32 are allowed";
      LOGTHROW(std::runtime_error(os.str().c_str()));
  }
  drawMode = GL_TRIANGLES;
  init(GL_ELEMENT_ARRAY_BUFFER, layout);
  BB_INC(bb_hib_key);
}
Example #7
0
	void P3DImporter::importMeshes( Scene* scene )
	{
		int numMeshes = 0;
		fread( &m_framesPerSecond, sizeof( int ), 1, m_fh );
		fread( &numMeshes, sizeof( int ), 1, m_fh );

		for( int i = 0; i < numMeshes; ++i )
		{
			int numAttribs = 0;
			int id = -1;
			int parentID = -1;
			int meshType = -1;

			fread( &id, sizeof( int ), 1, m_fh );
			fread( &parentID, sizeof( int ), 1, m_fh );
			fread( &meshType, sizeof( int ), 1, m_fh );
			fread( &numAttribs, sizeof( int ), 1, m_fh );
			BufferLayout* vtxLayout = new BufferLayout();
			BufferLayout* idxLayout = new BufferLayout( 1, BufferLayout::INT );
			
			for( int a = 0; a < numAttribs; ++a )
			{
				int type = 0;
				fread( &type, sizeof( int ), 1, m_fh );
				vtxLayout->pushAttribute( (BufferLayout::DataType )type );
			}

			
			int numTriBatches = 0;
			fread( &numTriBatches, sizeof( int ), 1, m_fh );
			

			Mesh* mesh = nullptr;				

			for( int t = 0; t < numTriBatches; ++t )
			{					
				int matID = -1;
				int numIndices = 0;
				int numVertices = 0;
					
				fread( &matID, sizeof( int ), 1, m_fh );
				fread( &numIndices, sizeof( int ), 1, m_fh );
				fread( &numVertices, sizeof( int ), 1, m_fh );

				int vtxBufferSize = vtxLayout->stride() * numVertices;
				int idxBufferSize = idxLayout->stride() * numIndices;
				char* vertices = new char[ vtxBufferSize ];
				char* indices = new char[ idxBufferSize ];

				fread( indices, sizeof( char ), idxBufferSize, m_fh );
				fread( vertices, sizeof( char ), vtxBufferSize, m_fh );

				GLBuffer* vtxBuffer = new GLBuffer( vertices, numVertices, vtxLayout, GL_ARRAY_BUFFER, GL_TRIANGLES, GL_STATIC_DRAW );
				GLBuffer* idxBuffer = new GLBuffer( indices, numIndices, idxLayout, GL_ELEMENT_ARRAY_BUFFER, GL_TRIANGLES, GL_STATIC_DRAW );

				
				std::string matName;
				if( matID == -1 )
					if( meshType == 1 )
					{
						matName = "SkinningMaterial";
						//Renderer::getMaterial( matName )->changeShaderProgram( "SkinningShader" );
					}
					else
						matName = "DiffuseMaterial";
				else
				{
					matName = m_materials[matID];
					if( meshType == 1 )
					{
						Renderer::getMaterial( matName )->changeShaderProgram( "SkinningShader" );
					}
				}

				if( mesh == nullptr )
				{
					if( meshType == 1 )
						mesh = new SkeletalMesh( vtxBuffer, idxBuffer, nullptr, matName, true );
					else
						mesh = new Mesh( vtxBuffer, idxBuffer, nullptr, matName, true );
				}
				else
					mesh->pushTriangleBatch( vtxBuffer, idxBuffer, nullptr, matName, true );

				vtxBuffer->sendToOpenGL();
				idxBuffer->sendToOpenGL();
			}
			
			
			// create the actor for the mesh
			Actor* actor = new Actor( m_fileName + toString( i ), mesh );

			int numFrames = 0;
			fread( &numFrames, sizeof( int ), 1, m_fh );
			actor->setFrameCount( numFrames );
			for( int f = 0; f < numFrames; ++f )
			{
				vec3f translation;
				quatf rotation;
				vec3f scale;

				fread( &translation, sizeof( vec3f ), 1, m_fh );
				fread( &rotation, sizeof( quatf ), 1, m_fh );
				fread( &scale, sizeof( vec3f ), 1, m_fh );

				actor->setFrame( Node::Frame( translation, mat3f::createMatrixFromQuaternion( rotation ), scale ), f );
			}
			actor->setFrameRate( 1 / static_cast<float>(m_framesPerSecond) );
			scene->actors.push_back( actor );
			m_parentIDToActorIndex[ id ] = scene->actors.size() - 1;
			
			if( parentID != -1 )
			{
				auto iter = m_parentIDToActorIndex.find( parentID );
				if( iter != m_parentIDToActorIndex.end() )
				{
					scene->actors[ iter->second ]->addChild( actor );
				}				
			}

			// If skeletal mesh
			if( meshType == 1 )
			{
				
				SkeletalMesh* skelMesh = reinterpret_cast< SkeletalMesh* >( mesh );
				skelMesh->setFrameRate( 1 / static_cast<float>(m_framesPerSecond) );
				int numBones = 0;
				int numFrames = 0;
				fread( &numBones, sizeof( int ), 1, m_fh );
				fread( &numFrames, sizeof( int ), 1, m_fh );
				
				std::vector< mat4f > bone;
				for( int b = 0; b < numBones; ++b )
				{
					bone.clear();
					for( int bf = 0; bf < numFrames; ++bf )
					{
						vec3f translation;
						quatf rotation;
						vec3f scale;
						
						fread( &translation, sizeof( vec3f ), 1, m_fh );
						fread( &rotation, sizeof( quatf ), 1, m_fh );
						fread( &scale, sizeof( vec3f ), 1, m_fh );

						mat4f boneFrameTM( mat4f::IDENTITY );
						boneFrameTM.scale( scale );
						boneFrameTM.rotate( rotation );
						boneFrameTM.translate( translation );
						
						bone.push_back( boneFrameTM );
					}
					skelMesh->addBone( bone );
				}
				skelMesh->play();
			}
		}
	}