Esempio n. 1
0
bool Shader::load(FS::IFile& file)
{
	lua_State* L = luaL_newstate();
	luaL_openlibs(L);
	registerFunctions(this, &m_combintions, &getRenderer(), L);
	m_render_states = BGFX_STATE_DEPTH_TEST_LEQUAL;

	bool errors = luaL_loadbuffer(L, (const char*)file.getBuffer(), file.size(), "") != LUA_OK;
	errors = errors || lua_pcall(L, 0, 0, 0) != LUA_OK;
	if (errors)
	{
		g_log_error.log("Renderer") << getPath().c_str() << ": " << lua_tostring(L, -1);
		lua_pop(L, 1);
		return false;
	}
	
	if (!generateInstances())
	{
		g_log_error.log("Renderer") << "Could not load instances of shader " << getPath().c_str();
		return false;
	}

	m_size = file.size();
	lua_close(L);
	return true;
}
bool LuaScript::load(FS::IFile& file)
{
	m_properties.clear();
	m_source_code.set((const char*)file.getBuffer(), (int)file.size());
	parseProperties();
	m_size = file.size();
	return true;
}
Esempio n. 3
0
bool ShaderBinary::load(FS::IFile& file)
{
	auto* mem = bgfx::alloc((uint32)file.size() + 1);
	file.read(mem->data, file.size());
	mem->data[file.size()] = '\0';
	m_handle = bgfx::createShader(mem);
	m_size = file.size();
	return bgfx::isValid(m_handle);
}
void LuaScript::loaded(FS::IFile& file, bool success, FS::FileSystem& fs)
{
	if (success)
	{
		m_source_code.set((const char*)file.getBuffer(), file.size());
		parseProperties();
		m_size = file.size();
		decrementDepCount();
	}
	else
	{
		g_log_error.log("lua_script") << "Could not load script "
									  << m_path.c_str();
		onFailure();
	}
}
Esempio n. 5
0
bool Texture::load(FS::IFile& file)
{
	PROFILE_FUNCTION();

	const char* path = getPath().c_str();
	size_t len = getPath().length();
	bool loaded = false;
	if (len > 3 && compareString(path + len - 4, ".dds") == 0)
	{
		loaded = loadDDS(file);
	}
	else if (len > 3 && compareString(path + len - 4, ".raw") == 0)
	{
		loaded = loadRaw(file);
	}
	else
	{
		loaded = loadTGA(file);
	}
	if (!loaded)
	{
		g_log_warning.log("Renderer") << "Error loading texture " << path;
		return false;
	}

	m_size = file.size();
	return true;
}
Esempio n. 6
0
void Model::loaded(FS::IFile& file, bool success, FS::FileSystem& fs)
{
	PROFILE_FUNCTION();
	if (success)
	{
		FileHeader header;
		file.read(&header, sizeof(header));
		if (header.m_magic == FILE_MAGIC &&
			header.m_version <= (uint32_t)FileVersion::LATEST &&
			parseMeshes(file) && parseGeometry(file) && parseBones(file) &&
			parseLODs(file))
		{
			m_size = file.size();
			decrementDepCount();
		}
		else
		{
			g_log_warning.log("renderer") << "Error loading model "
										  << m_path.c_str();
			onFailure();
			return;
		}
	}
	else
	{
		g_log_warning.log("renderer") << "Error loading model "
									  << m_path.c_str();
		onFailure();
	}
}
Esempio n. 7
0
JsonSerializer::JsonSerializer(FS::IFile& file,
	AccessMode access_mode,
	const Path& path,
	IAllocator& allocator)
	: m_file(file)
	, m_access_mode(access_mode)
	, m_allocator(allocator)
{
	m_is_error = false;
	copyString(m_path, path.c_str());
	m_is_first_in_block = true;
	m_data = nullptr;
	m_is_string_token = false;
	if (m_access_mode == READ)
	{
		m_data_size = (int)file.size();
		if (file.getBuffer() != nullptr)
		{
			m_data = (const char*)file.getBuffer();
			m_own_data = false;
		}
		else
		{
			int size = (int)m_file.size();
			char* data = (char*)m_allocator.allocate(size);
			m_own_data = true;
			file.read(data, m_data_size);
			m_data = data;
		}
		m_token = m_data;
		m_token_size = 0;
		deserializeToken();
	}
}
Esempio n. 8
0
bool Texture::loadRaw(FS::IFile& file)
{
	PROFILE_FUNCTION();
	size_t size = file.size();
	m_BPP = 2;
	m_width = (int)sqrt(size / m_BPP);
	m_height = m_width;

	if (m_data_reference)
	{
		m_data.resize(size);
		file.read(&m_data[0], size);
	}

	const uint16_t* src_mem = (const uint16_t*)file.getBuffer();
	const bgfx::Memory* mem = bgfx::alloc(m_width * m_height * sizeof(float));
	float* dst_mem = (float*)mem->data;

	for (int i = 0; i < m_width * m_height; ++i)
	{
		dst_mem[i] = src_mem[i] / 65535.0f;
	}

	m_texture_handle = bgfx::createTexture2D(
		m_width, m_height, 1, bgfx::TextureFormat::R32F, 0, nullptr);
	bgfx::updateTexture2D(
		m_texture_handle,
		0,
		0,
		0,
		m_width,
		m_height,
		mem);
	return bgfx::isValid(m_texture_handle);
}
Esempio n. 9
0
bool Texture::loadDDS(FS::IFile& file)
{
	bgfx::TextureInfo info;
	m_texture_handle = bgfx::createTexture(
		bgfx::copy(file.getBuffer(), file.size()), 0, 0, &info);
	m_BPP = -1;
	m_width = info.width;
	m_height = info.height;
	return bgfx::isValid(m_texture_handle);
}
Esempio n. 10
0
bool Clip::load(FS::IFile& file)
{
	short* output = nullptr;
	auto res = stb_vorbis_decode_memory(
		(unsigned char*)file.getBuffer(), (int)file.size(), &m_channels, &m_sample_rate, &output);
	if (res <= 0) return false;

	m_data.resize(res * m_channels);
	copyMemory(&m_data[0], output, res * m_channels * sizeof(m_data[0]));
	free(output);

	return true;
}
Esempio n. 11
0
void Animation::loaded(FS::IFile& file, bool success, FS::FileSystem& fs)
{
	if (success)
	{
		IAllocator& allocator = getAllocator();
		allocator.deallocate(m_positions);
		allocator.deallocate(m_rotations);
		allocator.deallocate(m_bones);
		m_positions = nullptr;
		m_rotations = nullptr;
		m_bones = 0;
		m_frame_count = m_bone_count = 0;
		Header header;
		file.read(&header, sizeof(header));
		if (header.magic != HEADER_MAGIC)
		{
			onFailure();
			g_log_error.log("animation") << m_path.c_str() << " is not an animation file";
			return;
		}
		if (header.version > 1)
		{
			onFailure();
			g_log_error.log("animation") << "Unsupported animation version " << header.version << " (" << m_path.c_str() << ")";
			return;
		}
		m_fps = header.fps;
		file.read(&m_frame_count, sizeof(m_frame_count));
		file.read(&m_bone_count, sizeof(m_bone_count));

		m_positions = static_cast<Vec3*>(allocator.allocate(sizeof(Vec3) * m_frame_count * m_bone_count));
		m_rotations = static_cast<Quat*>(allocator.allocate(sizeof(Quat) * m_frame_count * m_bone_count));
		m_bones = static_cast<uint32_t*>(allocator.allocate(sizeof(uint32_t) * m_bone_count));
		file.read(&m_positions[0], sizeof(Vec3)* m_bone_count * m_frame_count);
		file.read(&m_rotations[0], sizeof(Quat)* m_bone_count * m_frame_count);
		file.read(m_bones, sizeof(m_bones[0]) * m_bone_count);
		
		m_size = file.size();
		decrementDepCount();
	}
	else
	{
		onFailure();
	}
}
Esempio n. 12
0
bool Model::load(FS::IFile& file)
{
	PROFILE_FUNCTION();
	FileHeader header;
	file.read(&header, sizeof(header));
	if (header.m_magic == FILE_MAGIC 
		&& header.m_version <= (uint32)FileVersion::LATEST 
		&& parseMeshes(file) 
		&& parseGeometry(file) 
		&& parseBones(file) 
		&& parseLODs(file))
	{
		m_size = file.size();
		return true;
	}

	g_log_warning.log("renderer") << "Error loading model " << getPath().c_str();
	return false;
}
Esempio n. 13
0
bool Model::load(FS::IFile& file)
{
	PROFILE_FUNCTION();
	FileHeader header;
	file.read(&header, sizeof(header));

	if (header.magic != FILE_MAGIC)
	{
		g_log_warning.log("Renderer") << "Corrupted model " << getPath().c_str();
		return false;
	}

	if(header.version > (u32)FileVersion::LATEST)
	{
		g_log_warning.log("Renderer") << "Unsupported version of model " << getPath().c_str();
		return false;
	}

	u32 global_flags = 0; // backward compatibility
	if(header.version > (u32)FileVersion::WITH_FLAGS)
	{
		file.read(&global_flags, sizeof(global_flags));
	}

	bgfx::VertexDecl global_vertex_decl;
	if (header.version > (u32)FileVersion::SINGLE_VERTEX_DECL && header.version <= (u32)FileVersion::MULTIPLE_VERTEX_DECLS)
	{
		parseVertexDeclEx(file, &global_vertex_decl);
	}

	if (parseMeshes(global_vertex_decl, file, (FileVersion)header.version, global_flags)
		&& parseBones(file)
		&& parseLODs(file))
	{
		m_size = file.size();
		return true;
	}

	g_log_error.log("Renderer") << "Error loading model " << getPath().c_str();
	return false;
}
Esempio n. 14
0
void Texture::loaded(FS::IFile& file, bool success, FS::FileSystem& fs)
{
	PROFILE_FUNCTION();
	if (success)
	{
		const char* path = m_path.c_str();
		size_t len = m_path.length();
		bool loaded = false;
		if (len > 3 && strcmp(path + len - 4, ".dds") == 0)
		{
			loaded = loadDDS(file);
		}
		else if (len > 3 && strcmp(path + len - 4, ".raw") == 0)
		{
			loaded = loadRaw(file);
		}
		else
		{
			loaded = loadTGA(file);
		}
		if (!loaded)
		{
			g_log_warning.log("renderer") << "Error loading texture "
										  << m_path.c_str();
			onFailure();
		}
		else
		{
			m_size = file.size();
			decrementDepCount();
		}
	}
	else
	{
		g_log_warning.log("renderer") << "Error loading texture "
									  << m_path.c_str();
		onFailure();
	}
}
Esempio n. 15
0
bool Animation::load(FS::IFile& file)
{
	IAllocator& allocator = getAllocator();
	allocator.deallocate(m_positions);
	allocator.deallocate(m_rotations);
	allocator.deallocate(m_bones);
	m_positions = nullptr;
	m_rotations = nullptr;
	m_bones = nullptr;
	m_frame_count = m_bone_count = 0;
	Header header;
	file.read(&header, sizeof(header));
	if (header.magic != HEADER_MAGIC)
	{
		g_log_error.log("Animation") << getPath() << " is not an animation file";
		return false;
	}
	if (header.version > 1)
	{
		g_log_error.log("Animation") << "Unsupported animation version " << header.version << " ("
									 << getPath() << ")";
		return false;
	}
	m_fps = header.fps;
	file.read(&m_frame_count, sizeof(m_frame_count));
	file.read(&m_bone_count, sizeof(m_bone_count));

	m_positions = static_cast<Vec3*>(allocator.allocate(sizeof(Vec3) * m_frame_count * m_bone_count));
	m_rotations = static_cast<Quat*>(allocator.allocate(sizeof(Quat) * m_frame_count * m_bone_count));
	m_bones = static_cast<uint32*>(allocator.allocate(sizeof(uint32) * m_bone_count));
	file.read(&m_positions[0], sizeof(Vec3)* m_bone_count * m_frame_count);
	file.read(&m_rotations[0], sizeof(Quat)* m_bone_count * m_frame_count);
	file.read(m_bones, sizeof(m_bones[0]) * m_bone_count);
		
	m_size = file.size();
	return true;
}
Esempio n. 16
0
bool Material::load(FS::IFile& file)
{
	PROFILE_FUNCTION();

	m_render_states = BGFX_STATE_DEPTH_TEST_LEQUAL | BGFX_STATE_CULL_CW;
	m_uniforms.clear();
	JsonSerializer serializer(file, JsonSerializer::READ, getPath().c_str(), m_allocator);
	serializer.deserializeObjectBegin();
	char path[MAX_PATH_LENGTH];
	char label[256];
	char material_dir[MAX_PATH_LENGTH];
	PathUtils::getDir(material_dir, MAX_PATH_LENGTH, getPath().c_str());
	bool b_value;
	while (!serializer.isObjectEnd())
	{
		serializer.deserializeLabel(label, 255);
		if (compareString(label, "uniforms") == 0)
		{
			deserializeUniforms(serializer);
		}
		else if (compareString(label, "texture") == 0)
		{
			if (!deserializeTexture(serializer, material_dir))
			{
				return false;
			}
		}
		else if (compareString(label, "alpha_cutout") == 0)
		{
			bool b;
			serializer.deserialize(b, false);
			enableAlphaCutout(b);
		}
		else if (compareString(label, "alpha_blending") == 0)
		{
			if (serializer.isNextBoolean())
			{
				bool is_alpha_blending;
				serializer.deserialize(is_alpha_blending, false);
				if (is_alpha_blending)
				{
					m_render_states |= BGFX_STATE_BLEND_ADD;
				}
				else
				{
					m_render_states &= ~BGFX_STATE_BLEND_MASK;
				}
			}
			else
			{
				serializer.deserialize(label, 255, "alpha");
				if (compareString(label, "alpha") == 0)
				{
					m_render_states |= BGFX_STATE_BLEND_ALPHA;
				}
				else if (compareString(label, "add") == 0)
				{
					m_render_states |= BGFX_STATE_BLEND_ADD;
				}
				else if (compareString(label, "disabled") == 0)
				{
					m_render_states &= ~BGFX_STATE_BLEND_MASK;
				}
			}
		}
		else if (compareString(label, "specular") == 0)
		{
			serializer.deserializeArrayBegin();
			serializer.deserializeArrayItem(m_specular.x, 1.0f);
			serializer.deserializeArrayItem(m_specular.y, 1.0f);
			serializer.deserializeArrayItem(m_specular.z, 1.0f);
			serializer.deserializeArrayEnd();
		}
		else if (compareString(label, "shininess") == 0)
		{
			serializer.deserialize(m_shininess, 4.0f);
		}
		else if (compareString(label, "shadow_receiver") == 0)
		{
			bool b;
			serializer.deserialize(b, true);
			enableShadowReceiving(b);
		}
		else if (compareString(label, "shader") == 0)
		{
			serializer.deserialize(path, MAX_PATH_LENGTH, "");
			setShader(static_cast<Shader*>(
				m_resource_manager.get(ResourceManager::SHADER)->load(Path(path))));
		}
		else if (compareString(label, "z_test") == 0)
		{
			serializer.deserialize(b_value, true);
			enableZTest(b_value);
		}
		else if (compareString(label, "backface_culling") == 0)
		{
			serializer.deserialize(b_value, true);
			enableBackfaceCulling(b_value);
		}
		else
		{
			g_log_warning.log("renderer") << "Unknown parameter " << label << " in material "
										  << getPath().c_str();
		}
	}
	serializer.deserializeObjectEnd();

	if (!m_shader)
	{
		g_log_error.log("renderer") << "Material " << getPath().c_str() << " without a shader";
		return false;
	}

	m_size = file.size();
	return true;
}
	bool PhysicsGeometry::load(FS::IFile& file)
	{
		Header header;
		file.read(&header, sizeof(header));
		if (header.m_magic != HEADER_MAGIC || header.m_version > (uint32)Versions::LAST)
		{
			return false;
		}

		auto* phy_manager = m_resource_manager.get(ResourceManager::PHYSICS);
		PhysicsSystem& system = static_cast<PhysicsGeometryManager*>(phy_manager)->getSystem();

		uint32 num_verts;
		Array<Vec3> verts(getAllocator());
		file.read(&num_verts, sizeof(num_verts));
		verts.resize(num_verts);
		file.read(&verts[0], sizeof(verts[0]) * verts.size());

		m_is_convex = header.m_convex != 0;
		if (!m_is_convex)
		{
			physx::PxTriangleMeshGeometry* geom =
				LUMIX_NEW(getAllocator(), physx::PxTriangleMeshGeometry)();
			m_geometry = geom;
			uint32 num_indices;
			Array<uint32> tris(getAllocator());
			file.read(&num_indices, sizeof(num_indices));
			tris.resize(num_indices);
			file.read(&tris[0], sizeof(tris[0]) * tris.size());

			physx::PxTriangleMeshDesc meshDesc;
			meshDesc.points.count = num_verts;
			meshDesc.points.stride = sizeof(physx::PxVec3);
			meshDesc.points.data = &verts[0];

			meshDesc.triangles.count = num_indices / 3;
			meshDesc.triangles.stride = 3 * sizeof(physx::PxU32);
			meshDesc.triangles.data = &tris[0];

			OutputStream writeBuffer(getAllocator());
			system.getCooking()->cookTriangleMesh(meshDesc, writeBuffer);

			InputStream readBuffer(writeBuffer.data, writeBuffer.size);
			geom->triangleMesh = system.getPhysics()->createTriangleMesh(readBuffer);
		}
		else
		{
			physx::PxConvexMeshGeometry* geom =
				LUMIX_NEW(getAllocator(), physx::PxConvexMeshGeometry)();
			m_geometry = geom;
			physx::PxConvexMeshDesc meshDesc;
			meshDesc.points.count = verts.size();
			meshDesc.points.stride = sizeof(Vec3);
			meshDesc.points.data = &verts[0];
			meshDesc.flags = physx::PxConvexFlag::eCOMPUTE_CONVEX;

			OutputStream writeBuffer(getAllocator());
			bool status = system.getCooking()->cookConvexMesh(meshDesc, writeBuffer);
			if (!status)
			{
				LUMIX_DELETE(getAllocator(), geom);
				m_geometry = nullptr;
				return false;
			}

			InputStream readBuffer(writeBuffer.data, writeBuffer.size);
			physx::PxConvexMesh* mesh = system.getPhysics()->createConvexMesh(readBuffer);
			geom->convexMesh = mesh;
		}

		m_size = file.size();
		return true;
	}