Beispiel #1
0
void Model::create(const bgfx::VertexDecl& def,
				   Material* material,
				   const int* indices_data,
				   int indices_size,
				   const void* attributes_data,
				   int attributes_size)
{
	m_geometry_buffer_object.setAttributesData(
		attributes_data, attributes_size, def);
	m_geometry_buffer_object.setIndicesData(indices_data, indices_size);

	m_meshes.emplace(def,
					 material,
					 0,
					 attributes_size,
					 0,
					 indices_size / sizeof(int),
					 "default",
					 m_allocator);

	Model::LOD lod;
	lod.m_distance = FLT_MAX;
	lod.m_from_mesh = 0;
	lod.m_to_mesh = 0;
	m_lods.push(lod);

	m_indices.resize(indices_size / sizeof(m_indices[0]));
	memcpy(&m_indices[0], indices_data, indices_size);

	m_vertices.resize(attributes_size / def.getStride());
	computeRuntimeData((const uint8_t*)attributes_data);

	onReady();
}
Beispiel #2
0
void write(bx::WriterI* _writer, const uint8_t* _vertices, uint32_t _numVertices, const bgfx::VertexDecl& _decl, const uint16_t* _indices, uint32_t _numIndices, const std::string& _material, const PrimitiveArray& _primitives)
{
	uint32_t stride = _decl.getStride();
	bx::write(_writer, BGFX_CHUNK_MAGIC_VB);
	writeBounds(_writer, _vertices, _numVertices, stride);

	bx::write(_writer, _decl);
	bx::write(_writer, uint16_t(_numVertices) );
	bx::write(_writer, _vertices, _numVertices*stride);

	bx::write(_writer, BGFX_CHUNK_MAGIC_IB);
	bx::write(_writer, _numIndices);
	bx::write(_writer, _indices, _numIndices*2);

	bx::write(_writer, BGFX_CHUNK_MAGIC_PRI);
	uint16_t nameLen = uint16_t(_material.size() );
	bx::write(_writer, nameLen);
	bx::write(_writer, _material.c_str(), nameLen);
	bx::write(_writer, uint16_t(_primitives.size() ) );
	for (PrimitiveArray::const_iterator primIt = _primitives.begin(); primIt != _primitives.end(); ++primIt)
	{
		const Primitive& prim = *primIt;
		nameLen = uint16_t(prim.m_name.size() );
		bx::write(_writer, nameLen);
		bx::write(_writer, prim.m_name.c_str(), nameLen);
		bx::write(_writer, prim.m_startIndex);
		bx::write(_writer, prim.m_numIndices);
		bx::write(_writer, prim.m_startVertex);
		bx::write(_writer, prim.m_numVertices);
		writeBounds(_writer, &_vertices[prim.m_startVertex*stride], prim.m_numVertices, stride);
	}
}
Beispiel #3
0
void Model::create(const bgfx::VertexDecl& def,
				   Material* material,
				   const int* indices_data,
				   int indices_size,
				   const void* attributes_data,
				   int attributes_size)
{
	ASSERT(!bgfx::isValid(m_vertices_handle));
	m_vertices_handle = bgfx::createVertexBuffer(bgfx::copy(attributes_data, attributes_size), def);
	m_vertices_size = attributes_size;

	ASSERT(!bgfx::isValid(m_indices_handle));
	auto* mem = bgfx::copy(indices_data, indices_size);
	m_indices_handle = bgfx::createIndexBuffer(mem, BGFX_BUFFER_INDEX32);
	m_indices_size = indices_size;

	m_meshes.emplace(def,
					 material,
					 0,
					 attributes_size,
					 0,
					 indices_size / int(sizeof(int)),
					 "default",
					 m_allocator);

	Model::LOD lod;
	lod.m_distance = FLT_MAX;
	lod.m_from_mesh = 0;
	lod.m_to_mesh = 0;
	m_lods.push(lod);

	m_indices.resize(indices_size / sizeof(m_indices[0]));
	copyMemory(&m_indices[0], indices_data, indices_size);

	m_vertices.resize(attributes_size / def.getStride());
	computeRuntimeData((const uint8*)attributes_data);

	onCreated(State::READY);
}
Beispiel #4
0
	void load(const void* _vertices, uint32_t _numVertices, const bgfx::VertexDecl _decl, const uint16_t* _indices, uint32_t _numIndices)
	{
		Group group;
		const bgfx::Memory* mem;
		uint32_t size;

		size = _numVertices*_decl.getStride();
		mem = bgfx::makeRef(_vertices, size);
		group.m_vbh = bgfx::createVertexBuffer(mem, _decl);

		size = _numIndices*2;
		mem = bgfx::makeRef(_indices, size);
		group.m_ibh = bgfx::createIndexBuffer(mem);

		//TODO:
		// group.m_sphere = ...
		// group.m_aabb = ...
		// group.m_obb = ...
		// group.m_prims = ...

		m_groups.push_back(group);
	}
Beispiel #5
0
bool Model::parseMeshesOld(bgfx::VertexDecl global_vertex_decl, FS::IFile& file, FileVersion version, u32 global_flags)
{
	int object_count = 0;
	file.read(&object_count, sizeof(object_count));
	if (object_count <= 0) return false;

	m_meshes.reserve(object_count);
	char model_dir[MAX_PATH_LENGTH];
	PathUtils::getDir(model_dir, MAX_PATH_LENGTH, getPath().c_str());
	struct Offsets
	{
		i32 attribute_array_offset;
		i32 attribute_array_size;
		i32 indices_offset;
		i32 mesh_tri_count;
	};
	Array<Offsets> mesh_offsets(m_allocator);
	for (int i = 0; i < object_count; ++i)
	{
		i32 str_size;
		file.read(&str_size, sizeof(str_size));
		char material_name[MAX_PATH_LENGTH];
		file.read(material_name, str_size);
		if (str_size >= MAX_PATH_LENGTH) return false;

		material_name[str_size] = 0;

		char material_path[MAX_PATH_LENGTH];
		copyString(material_path, model_dir);
		catString(material_path, material_name);
		catString(material_path, ".mat");

		auto* material_manager = m_resource_manager.getOwner().get(Material::TYPE);
		Material* material = static_cast<Material*>(material_manager->load(Path(material_path)));

		Offsets& offsets = mesh_offsets.emplace();
		file.read(&offsets.attribute_array_offset, sizeof(offsets.attribute_array_offset));
		file.read(&offsets.attribute_array_size, sizeof(offsets.attribute_array_size));
		file.read(&offsets.indices_offset, sizeof(offsets.indices_offset));
		file.read(&offsets.mesh_tri_count, sizeof(offsets.mesh_tri_count));

		file.read(&str_size, sizeof(str_size));
		if (str_size >= MAX_PATH_LENGTH)
		{
			material_manager->unload(*material);
			return false;
		}

		char mesh_name[MAX_PATH_LENGTH];
		mesh_name[str_size] = 0;
		file.read(mesh_name, str_size);

		bgfx::VertexDecl vertex_decl = global_vertex_decl;
		if (version <= FileVersion::SINGLE_VERTEX_DECL)
		{
			parseVertexDecl(file, &vertex_decl);
			if (i != 0 && global_vertex_decl.m_hash != vertex_decl.m_hash)
			{
				g_log_error.log("Renderer") << "Model " << getPath().c_str()
					<< " contains meshes with different vertex declarations.";
			}
			if(i == 0) global_vertex_decl = vertex_decl;
		}


		m_meshes.emplace(material,
			vertex_decl,
			mesh_name,
			m_allocator);
		addDependency(*material);
	}

	i32 indices_count = 0;
	file.read(&indices_count, sizeof(indices_count));
	if (indices_count <= 0) return false;

	u32 INDICES_16BIT_FLAG = 1;
	int index_size = global_flags & INDICES_16BIT_FLAG ? 2 : 4;
	Array<u8> indices(m_allocator);
	indices.resize(indices_count * index_size);
	file.read(&indices[0], indices.size());

	i32 vertices_size = 0;
	file.read(&vertices_size, sizeof(vertices_size));
	if (vertices_size <= 0) return false;

	Array<u8> vertices(m_allocator);
	vertices.resize(vertices_size);
	file.read(&vertices[0], vertices.size());

	int vertex_count = 0;
	for (const Offsets& offsets : mesh_offsets)
	{
		vertex_count += offsets.attribute_array_size / global_vertex_decl.getStride();
	}

	if (version > FileVersion::BOUNDING_SHAPES_PRECOMPUTED)
	{
		file.read(&m_bounding_radius, sizeof(m_bounding_radius));
		file.read(&m_aabb, sizeof(m_aabb));
	}

	float bounding_radius_squared = 0;
	Vec3 min_vertex(0, 0, 0);
	Vec3 max_vertex(0, 0, 0);

	int vertex_size = global_vertex_decl.getStride();
	int position_attribute_offset = global_vertex_decl.getOffset(bgfx::Attrib::Position);
	int uv_attribute_offset = global_vertex_decl.getOffset(bgfx::Attrib::TexCoord0);
	int weights_attribute_offset = global_vertex_decl.getOffset(bgfx::Attrib::Weight);
	int bone_indices_attribute_offset = global_vertex_decl.getOffset(bgfx::Attrib::Indices);
	bool keep_skin = global_vertex_decl.has(bgfx::Attrib::Weight) && global_vertex_decl.has(bgfx::Attrib::Indices);
	for (int i = 0; i < m_meshes.size(); ++i)
	{
		Offsets& offsets = mesh_offsets[i];
		Mesh& mesh = m_meshes[i];
		mesh.indices_count = offsets.mesh_tri_count * 3;
		mesh.indices.resize(mesh.indices_count * index_size);
		copyMemory(&mesh.indices[0], &indices[offsets.indices_offset * index_size], mesh.indices_count * index_size);

		int mesh_vertex_count = offsets.attribute_array_size / global_vertex_decl.getStride();
		int mesh_attributes_array_offset = offsets.attribute_array_offset;
		mesh.vertices.resize(mesh_vertex_count);
		mesh.uvs.resize(mesh_vertex_count);
		if (keep_skin) mesh.skin.resize(mesh_vertex_count);
		for (int j = 0; j < mesh_vertex_count; ++j)
		{
			int offset = mesh_attributes_array_offset + j * vertex_size;
			if (keep_skin)
			{
				mesh.skin[j].weights = *(const Vec4*)&vertices[offset + weights_attribute_offset];
				copyMemory(mesh.skin[j].indices,
					&vertices[offset + bone_indices_attribute_offset],
					sizeof(mesh.skin[j].indices));
			}
			mesh.vertices[j] = *(const Vec3*)&vertices[offset + position_attribute_offset];
			mesh.uvs[j] = *(const Vec2*)&vertices[offset + uv_attribute_offset];
			float sq_len = mesh.vertices[j].squaredLength();
			bounding_radius_squared = Math::maximum(bounding_radius_squared, sq_len > 0 ? sq_len : 0);
			min_vertex.x = Math::minimum(min_vertex.x, mesh.vertices[j].x);
			min_vertex.y = Math::minimum(min_vertex.y, mesh.vertices[j].y);
			min_vertex.z = Math::minimum(min_vertex.z, mesh.vertices[j].z);
			max_vertex.x = Math::maximum(max_vertex.x, mesh.vertices[j].x);
			max_vertex.y = Math::maximum(max_vertex.y, mesh.vertices[j].y);
			max_vertex.z = Math::maximum(max_vertex.z, mesh.vertices[j].z);
		}
	}

	if (version <= FileVersion::BOUNDING_SHAPES_PRECOMPUTED)
	{
		m_bounding_radius = sqrt(bounding_radius_squared);
		m_aabb = AABB(min_vertex, max_vertex);
	}

	for (int i = 0; i < m_meshes.size(); ++i)
	{
		Mesh& mesh = m_meshes[i];
		Offsets offsets = mesh_offsets[i];
		
		ASSERT(!bgfx::isValid(mesh.index_buffer_handle));
		if (global_flags & INDICES_16BIT_FLAG)
		{
			mesh.flags.set(Mesh::Flags::INDICES_16_BIT);
		}
		int indices_size = index_size * mesh.indices_count;
		const bgfx::Memory* mem = bgfx::copy(&indices[offsets.indices_offset * index_size], indices_size);
		mesh.index_buffer_handle = bgfx::createIndexBuffer(mem, index_size == 4 ? BGFX_BUFFER_INDEX32 : 0);
		if (!bgfx::isValid(mesh.index_buffer_handle)) return false;

		ASSERT(!bgfx::isValid(mesh.vertex_buffer_handle));
		const bgfx::Memory* vertices_mem = bgfx::copy(&vertices[offsets.attribute_array_offset], offsets.attribute_array_size);
		mesh.vertex_buffer_handle = bgfx::createVertexBuffer(vertices_mem, mesh.vertex_decl);
		if (!bgfx::isValid(mesh.vertex_buffer_handle)) return false;
	}

	return true;
}