Merge branch 'ubo' into vulkan

This commit is contained in:
Lynix
2018-06-12 19:07:58 +02:00
64 changed files with 1041 additions and 430 deletions

View File

@@ -19,16 +19,16 @@ namespace Nz
/*!
* Write bits to the stream (if any) and reset the current bit cursor
* \see ResetBitPosition
* \see ResetWriteBitPosition
*/
void SerializationContext::FlushBits()
{
if (currentBitPos != 8)
if (writeBitPos != 8)
{
ResetBitPosition();
ResetWriteBitPosition();
// Serialize will reset the bit position
if (!Serialize(*this, currentByte))
if (!Serialize(*this, writeByte))
NazaraWarning("Failed to flush bits");
}
}

View File

@@ -3,6 +3,7 @@
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Graphics/AbstractViewer.hpp>
#include <Nazara/Renderer/RenderTarget.hpp>
#include <Nazara/Graphics/Debug.hpp>
namespace Nz
@@ -16,4 +17,50 @@ namespace Nz
*/
AbstractViewer::~AbstractViewer() = default;
Vector3f AbstractViewer::Project(const Nz::Vector3f& worldPosition) const
{
Vector4f pos4D(worldPosition, 1.f);
pos4D = GetViewMatrix() * pos4D;
pos4D = GetProjectionMatrix() * pos4D;
pos4D /= pos4D.w;
Rectf viewport = Rectf(GetViewport());
Nz::Vector3f screenPosition(pos4D.x * 0.5f + 0.5f, -pos4D.y * 0.5f + 0.5f, pos4D.z * 0.5f + 0.5f);
screenPosition.x = screenPosition.x * viewport.width + viewport.x;
screenPosition.y = screenPosition.y * viewport.height + viewport.y;
return screenPosition;
}
float AbstractViewer::ProjectDepth(float depth)
{
const Matrix4f& projectionMatrix = GetProjectionMatrix();
float a = projectionMatrix(2, 2);
float b = projectionMatrix(2, 3);
return (0.5f * (-a * depth + b) / depth + 0.5f);
}
Vector3f AbstractViewer::Unproject(const Nz::Vector3f& screenPos) const
{
Rectf viewport = Rectf(GetViewport());
Nz::Vector4f normalizedPosition;
normalizedPosition.x = (screenPos.x - viewport.x) / viewport.width * 2.f - 1.f;
normalizedPosition.y = (screenPos.y - viewport.y) / viewport.height * 2.f - 1.f;
normalizedPosition.z = screenPos.z * 2.f - 1.f;
normalizedPosition.w = 1.f;
normalizedPosition.y = -normalizedPosition.y;
Nz::Matrix4f invMatrix = GetViewMatrix() * GetProjectionMatrix();
invMatrix.Inverse();
Nz::Vector4f worldPos = invMatrix * normalizedPosition;
worldPos /= worldPos.w;
return Nz::Vector3f(worldPos.x, worldPos.y, worldPos.z);
}
}

View File

@@ -27,6 +27,14 @@ namespace Nz
renderQueue->AddBillboards(instanceData.renderOrder, GetMaterial(), 1, scissorRect, &position, &m_size, &m_sinCos, &m_color);
}
/*!
* \brief Clones this billboard
*/
std::unique_ptr<InstancedRenderable> Billboard::Clone() const
{
return std::make_unique<Billboard>(*this);
}
/*
* \brief Makes the bounding volume of this billboard
*/

View File

@@ -51,7 +51,6 @@ namespace Nz
* \param renderQueue Queue to be added
* \param instanceData Data used for this instance
*/
void Model::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData, const Recti& scissorRect) const
{
unsigned int submeshCount = m_mesh->GetSubMeshCount();
@@ -69,6 +68,14 @@ namespace Nz
}
}
/*!
* \brief Clones this model
*/
std::unique_ptr<InstancedRenderable> Model::Clone() const
{
return std::make_unique<Model>(*this);
}
/*!
* \brief Gets the material of the named submesh
* \return Pointer to the current material
@@ -252,9 +259,15 @@ namespace Nz
m_mesh = mesh;
if (m_mesh)
{
ResetMaterials(mesh->GetMaterialCount());
m_meshAABBInvalidationSlot.Connect(m_mesh->OnMeshInvalidateAABB, [this](const Nz::Mesh*) { InvalidateBoundingVolume(); });
}
else
{
ResetMaterials(0);
m_meshAABBInvalidationSlot.Disconnect();
}
InvalidateBoundingVolume();
}
@@ -271,5 +284,9 @@ namespace Nz
m_boundingVolume.MakeNull();
}
ModelLibrary::LibraryMap Model::s_library;
ModelLoader::LoaderList Model::s_loaders;
ModelManager::ManagerMap Model::s_managerMap;
ModelManager::ManagerParams Model::s_managerParameters;
ModelSaver::SaverList Model::s_savers;
}

View File

@@ -126,10 +126,9 @@ namespace Nz
* \brief Clones this skeletal model
* \return Pointer to newly allocated SkeletalModel
*/
SkeletalModel* SkeletalModel::Clone() const
std::unique_ptr<InstancedRenderable> SkeletalModel::Clone() const
{
return new SkeletalModel(*this);
return std::make_unique<SkeletalModel>(*this);
}
/*!

View File

@@ -21,17 +21,23 @@ namespace Nz
* \param renderQueue Queue to be added
* \param instanceData Data for the instance
*/
void Sprite::AddToRenderQueue(AbstractRenderQueue* renderQueue, const InstanceData& instanceData, const Recti& scissorRect) const
{
const VertexStruct_XYZ_Color_UV* vertices = reinterpret_cast<const VertexStruct_XYZ_Color_UV*>(instanceData.data.data());
renderQueue->AddSprites(instanceData.renderOrder, GetMaterial(), vertices, 1, scissorRect);
}
/*!
* \brief Clones this sprite
*/
std::unique_ptr<InstancedRenderable> Sprite::Clone() const
{
return std::make_unique<Sprite>(*this);
}
/*!
* \brief Makes the bounding volume of this text
*/
void Sprite::MakeBoundingVolume() const
{
Vector3f origin(m_origin.x, -m_origin.y, m_origin.z);

View File

@@ -41,6 +41,14 @@ namespace Nz
}
}
/*!
* \brief Clones this text sprite
*/
std::unique_ptr<InstancedRenderable> TextSprite::Clone() const
{
return std::make_unique<TextSprite>(*this);
}
/*!
* \brief Updates the text
*

View File

@@ -37,6 +37,14 @@ namespace Nz
}
}
/*!
* \brief Clones this tilemap
*/
std::unique_ptr<InstancedRenderable> TileMap::Clone() const
{
return std::make_unique<TileMap>(*this);
}
void TileMap::MakeBoundingVolume() const
{
Nz::Vector2f size = GetSize();

View File

@@ -47,7 +47,7 @@ namespace Nz
}
}
bool LuaCoroutine::Run(int argCount, int /*resultCount*/)
bool LuaCoroutine::Run(int argCount, int /*resultCount*/, int /*errHandler*/)
{
return Resume(argCount) != Ternary_False;
}

View File

@@ -144,12 +144,22 @@ namespace Nz
bool LuaState::Call(unsigned int argCount)
{
return Run(argCount, LUA_MULTRET);
return Run(argCount, LUA_MULTRET, 0);
}
bool LuaState::Call(unsigned int argCount, unsigned int resultCount)
{
return Run(argCount, resultCount);
return Run(argCount, resultCount, 0);
}
bool LuaState::CallWithHandler(unsigned int argCount, int errorHandler)
{
return Run(argCount, LUA_MULTRET, errorHandler);
}
bool LuaState::CallWithHandler(unsigned int argCount, unsigned int resultCount, int errorHandler)
{
return Run(argCount, resultCount, errorHandler);
}
void LuaState::CheckAny(int index) const
@@ -350,66 +360,37 @@ namespace Nz
luaL_error(m_state, message.GetConstBuffer());
}
bool LuaState::Execute(const String& code)
bool LuaState::Execute(const String& code, int errorHandler)
{
if (code.IsEmpty())
return true;
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
if (!Load(code))
return false;
}
return Run(0, LUA_MULTRET);
return CallWithHandler(errorHandler, 0);
}
bool LuaState::ExecuteFromFile(const String& filePath)
bool LuaState::ExecuteFromFile(const String& filePath, int errorHandler)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
if (!LoadFromFile(filePath))
return false;
}
std::size_t length = static_cast<std::size_t>(file.GetSize());
String source(length, '\0');
if (file.Read(&source[0], length) != length)
{
NazaraError("Failed to read file");
return false;
}
file.Close();
return Execute(source);
return CallWithHandler(errorHandler, 0);
}
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size)
bool LuaState::ExecuteFromMemory(const void* data, std::size_t size, int errorHandler)
{
MemoryView stream(data, size);
return ExecuteFromStream(stream);
return ExecuteFromStream(stream, errorHandler);
}
bool LuaState::ExecuteFromStream(Stream& stream)
bool LuaState::ExecuteFromStream(Stream& stream, int errorHandler)
{
StreamData data;
data.stream = &stream;
if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
if (!LoadFromStream(stream))
return false;
}
return Run(0, LUA_MULTRET);
return CallWithHandler(errorHandler, 0);
}
int LuaState::GetAbsIndex(int index) const
@@ -545,6 +526,65 @@ namespace Nz
return lua_isnoneornil(m_state, index) == 0;
}
bool LuaState::Load(const String& code)
{
if (luaL_loadstring(m_state, code.GetConstBuffer()) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
bool LuaState::LoadFromFile(const String& filePath)
{
File file(filePath);
if (!file.Open(OpenMode_ReadOnly | OpenMode_Text))
{
NazaraError("Failed to open file");
return false;
}
std::size_t length = static_cast<std::size_t>(file.GetSize());
String source(length, '\0');
if (file.Read(&source[0], length) != length)
{
NazaraError("Failed to read file");
return false;
}
file.Close();
return Load(source);
}
bool LuaState::LoadFromMemory(const void* data, std::size_t size)
{
MemoryView stream(data, size);
return LoadFromStream(stream);
}
bool LuaState::LoadFromStream(Stream& stream)
{
StreamData data;
data.stream = &stream;
if (lua_load(m_state, StreamReader, &data, "C++", nullptr) != 0)
{
m_lastError = lua_tostring(m_state, -1);
lua_pop(m_state, 1);
return false;
}
return true;
}
long long LuaState::Length(int index) const
{
return luaL_len(m_state, index);
@@ -779,14 +819,19 @@ namespace Nz
return luaL_testudata(m_state, index, tname.GetConstBuffer());
}
bool LuaState::Run(int argCount, int resultCount)
void LuaState::Traceback(const char* message, int level)
{
luaL_traceback(m_state, m_state, message, level);
}
bool LuaState::Run(int argCount, int resultCount, int errHandler)
{
LuaInstance& instance = GetInstance(m_state);
if (instance.m_level++ == 0)
instance.m_clock.Restart();
int status = lua_pcall(m_state, argCount, resultCount, 0);
int status = lua_pcall(m_state, argCount, resultCount, errHandler);
instance.m_level--;

View File

@@ -100,17 +100,17 @@ namespace Nz
if (entry.revents & (POLLWRNORM | POLLERR))
m_readyToWriteSockets.insert(entry.fd);
entry.revents = 0;
if (--socketRemaining == 0)
break;
}
else
{
NazaraWarning("Socket " + String::Number(entry.fd) + " was returned by WSAPoll without POLLRDNORM nor POLLWRNORM events (events: 0x" + String::Number(entry.revents, 16) + ')');
activeSockets--;
}
entry.revents = 0;
if (--socketRemaining == 0)
break;
}
}

View File

@@ -154,17 +154,17 @@ namespace Nz
if (entry.revents & (POLLWRNORM | POLLERR))
m_readyToWriteSockets.insert(entry.fd);
entry.revents = 0;
if (--socketRemaining == 0)
break;
}
else
{
NazaraWarning("Socket " + String::Number(entry.fd) + " was returned by WSAPoll without POLLRDNORM nor POLLWRNORM events (events: 0x" + String::Number(entry.revents, 16) + ')');
activeSockets--;
}
entry.revents = 0;
if (--socketRemaining == 0)
break;
}
}
#else

View File

@@ -311,14 +311,15 @@ namespace Nz
{
m_timestepAccumulator += timestep;
std::size_t stepCount = 0;
while (m_timestepAccumulator >= m_stepSize && stepCount < m_maxStepCount)
std::size_t stepCount = std::min(static_cast<std::size_t>(m_timestepAccumulator / m_stepSize), m_maxStepCount);
float invStepCount = 1.f / stepCount;
for (std::size_t i = 0; i < stepCount; ++i)
{
OnPhysWorld2DPreStep(this);
OnPhysWorld2DPreStep(this, invStepCount);
cpSpaceStep(m_handle, m_stepSize);
OnPhysWorld2DPostStep(this);
OnPhysWorld2DPostStep(this, invStepCount);
if (!m_rigidPostSteps.empty())
{
for (const auto& pair : m_rigidPostSteps)
@@ -331,7 +332,6 @@ namespace Nz
}
m_timestepAccumulator -= m_stepSize;
stepCount++;
}
}

View File

@@ -158,12 +158,7 @@ namespace Nz
#endif
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, header.num_vertices, parameters.storage, parameters.vertexBufferFlags);
StaticMeshRef subMesh = StaticMesh::New(mesh);
if (!subMesh->Create(vertexBuffer))
{
NazaraError("Failed to create SubMesh");
return false;
}
StaticMeshRef subMesh = StaticMesh::New(vertexBuffer, indexBuffer);
// Extracting vertices
stream.SetCursorPos(header.offset_frames);

View File

@@ -202,13 +202,9 @@ namespace Nz
mesh->SetMaterialData(i, std::move(matData));
// Submesh
SkeletalMeshRef subMesh = SkeletalMesh::New(mesh);
subMesh->Create(vertexBuffer);
subMesh->SetIndexBuffer(indexBuffer);
SkeletalMeshRef subMesh = SkeletalMesh::New(vertexBuffer, indexBuffer);
subMesh->GenerateNormalsAndTangents();
subMesh->SetMaterialIndex(i);
subMesh->SetPrimitiveMode(PrimitiveMode_TriangleList);
mesh->AddSubMesh(subMesh);
@@ -255,6 +251,9 @@ namespace Nz
}
indexMapper.Unmap();
if (parameters.optimizeIndexBuffers)
indexBuffer->Optimize();
// Vertex buffer
VertexBufferRef vertexBuffer = VertexBuffer::New(parameters.vertexDeclaration, UInt32(vertexCount), parameters.storage, parameters.vertexBufferFlags);
@@ -287,13 +286,7 @@ namespace Nz
vertexMapper.Unmap();
// Submesh
StaticMeshRef subMesh = StaticMesh::New(mesh);
subMesh->Create(vertexBuffer);
if (parameters.optimizeIndexBuffers)
indexBuffer->Optimize();
subMesh->SetIndexBuffer(indexBuffer);
StaticMeshRef subMesh = StaticMesh::New(vertexBuffer, indexBuffer);
subMesh->GenerateAABB();
subMesh->SetMaterialIndex(i);

View File

@@ -259,6 +259,9 @@ namespace Nz
indexMapper.Unmap(); // Pour laisser les autres tâches affecter l'index buffer
if (parameters.optimizeIndexBuffers)
indexBuffer->Optimize();
// Remplissage des vertices
// Make sure the normal matrix won't rescale our normals
@@ -311,20 +314,9 @@ namespace Nz
vertexMapper.Unmap();
StaticMeshRef subMesh = StaticMesh::New(mesh);
if (!subMesh->Create(vertexBuffer))
{
NazaraError("Failed to create StaticMesh");
continue;
}
if (parameters.optimizeIndexBuffers)
indexBuffer->Optimize();
StaticMeshRef subMesh = StaticMesh::New(vertexBuffer, indexBuffer);
subMesh->GenerateAABB();
subMesh->SetIndexBuffer(indexBuffer);
subMesh->SetMaterialIndex(meshes[i].material);
subMesh->SetPrimitiveMode(PrimitiveMode_TriangleList);
// Ce que nous pouvons générer dépend des données à disposition (par exemple les tangentes nécessitent des coordonnées de texture)
if (hasNormals && hasTexCoords)

View File

@@ -106,6 +106,7 @@ namespace Nz
void IndexBuffer::Reset(bool largeIndices, BufferRef buffer, UInt32 offset, UInt32 size)
{
NazaraAssert(buffer && buffer->IsValid(), "Invalid buffer");
NazaraAssert(buffer->GetType() == BufferType_Index, "Buffer must be an index buffer");
NazaraAssert(size > 0, "Invalid size");
NazaraAssert(offset + size > buffer->GetSize(), "Virtual buffer exceed buffer bounds");

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
@@ -56,62 +56,40 @@ namespace Nz
return true;
}
struct MeshImpl
{
MeshImpl()
{
materialData.resize(1); // One material by default
}
std::unordered_map<String, UInt32> subMeshMap;
std::vector<ParameterList> materialData;
std::vector<SubMeshRef> subMeshes;
AnimationType animationType;
Boxf aabb;
Skeleton skeleton; // Only used by skeletal meshes
String animationPath;
bool aabbUpdated = false;
UInt32 jointCount; // Only used by skeletal meshes
};
Mesh::~Mesh()
{
OnMeshRelease(this);
Destroy();
}
void Mesh::AddSubMesh(SubMesh* subMesh)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(subMesh, "Invalid submesh");
NazaraAssert(subMesh->GetAnimationType() == m_impl->animationType, "Submesh animation type doesn't match mesh animation type");
NazaraAssert(subMesh->GetAnimationType() == m_animationType, "Submesh animation type doesn't match mesh animation type");
m_impl->subMeshes.emplace_back(subMesh);
m_subMeshes.emplace_back();
SubMeshData& subMeshData = m_subMeshes.back();
subMeshData.subMesh = subMesh;
subMeshData.onSubMeshInvalidated.Connect(subMesh->OnSubMeshInvalidateAABB, [this](const SubMesh* /*subMesh*/) { InvalidateAABB(); });
InvalidateAABB();
}
void Mesh::AddSubMesh(const String& identifier, SubMesh* subMesh)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(!identifier.IsEmpty(), "Identifier is empty");
NazaraAssert(m_impl->subMeshMap.find(identifier) == m_impl->subMeshMap.end(), "SubMesh identifier \"" + identifier + "\" is already in use");
NazaraAssert(m_subMeshMap.find(identifier) == m_subMeshMap.end(), "SubMesh identifier \"" + identifier + "\" is already in use");
NazaraAssert(subMesh, "Invalid submesh");
NazaraAssert(subMesh->GetAnimationType() == m_impl->animationType, "Submesh animation type doesn't match mesh animation type");
NazaraAssert(subMesh->GetAnimationType() == m_animationType, "Submesh animation type doesn't match mesh animation type");
std::size_t index = m_impl->subMeshes.size();
std::size_t index = m_subMeshes.size();
m_impl->subMeshes.emplace_back(subMesh);
m_impl->subMeshMap[identifier] = static_cast<UInt32>(index);
AddSubMesh(subMesh);
InvalidateAABB();
m_subMeshMap[identifier] = static_cast<UInt32>(index);
}
SubMesh* Mesh::BuildSubMesh(const Primitive& primitive, const MeshParams& params)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Static, "Submesh building only works for static meshes");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Static, "Submesh building only works for static meshes");
NazaraAssert(params.IsValid(), "Invalid parameters");
NazaraAssert(params.vertexDeclaration->HasComponentOfType<Vector3f>(VertexComponent_Position), "The vertex declaration doesn't have a Vector3 position component");
@@ -266,18 +244,11 @@ namespace Nz
}
}
StaticMeshRef subMesh = StaticMesh::New(this);
if (!subMesh->Create(vertexBuffer))
{
NazaraError("Failed to create StaticMesh");
return nullptr;
}
if (params.optimizeIndexBuffers)
indexBuffer->Optimize();
StaticMeshRef subMesh = StaticMesh::New(vertexBuffer, indexBuffer);
subMesh->SetAABB(aabb);
subMesh->SetIndexBuffer(indexBuffer);
AddSubMesh(subMesh);
return subMesh;
@@ -293,16 +264,15 @@ namespace Nz
{
Destroy();
std::unique_ptr<MeshImpl> impl(new MeshImpl);
impl->animationType = AnimationType_Skeletal;
impl->jointCount = jointCount;
if (!impl->skeleton.Create(jointCount))
m_animationType = AnimationType_Skeletal;
m_jointCount = jointCount;
if (!m_skeleton.Create(jointCount))
{
NazaraError("Failed to create skeleton");
return false;
}
m_impl = impl.release();
m_isValid = true;
return true;
}
@@ -311,236 +281,244 @@ namespace Nz
{
Destroy();
m_impl = new MeshImpl;
m_impl->animationType = AnimationType_Static;
m_animationType = AnimationType_Static;
m_isValid = true;
return true;
}
void Mesh::Destroy()
{
if (m_impl)
if (m_isValid)
{
OnMeshDestroy(this);
delete m_impl;
m_impl = nullptr;
m_animationPath.Clear();
m_materialData.clear();
m_materialData.resize(1);
m_skeleton.Destroy();
m_subMeshes.clear();
m_subMeshMap.clear();
m_isValid = false;
}
}
void Mesh::GenerateNormals()
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
for (SubMesh* subMesh : m_impl->subMeshes)
subMesh->GenerateNormals();
for (SubMeshData& data : m_subMeshes)
data.subMesh->GenerateNormals();
}
void Mesh::GenerateNormalsAndTangents()
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
for (SubMesh* subMesh : m_impl->subMeshes)
subMesh->GenerateNormalsAndTangents();
for (SubMeshData& data : m_subMeshes)
data.subMesh->GenerateNormalsAndTangents();
}
void Mesh::GenerateTangents()
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
for (SubMesh* subMesh : m_impl->subMeshes)
subMesh->GenerateTangents();
for (SubMeshData& data : m_subMeshes)
data.subMesh->GenerateTangents();
}
const Boxf& Mesh::GetAABB() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
if (!m_impl->aabbUpdated)
if (!m_aabbUpdated)
{
std::size_t subMeshCount = m_impl->subMeshes.size();
std::size_t subMeshCount = m_subMeshes.size();
if (subMeshCount > 0)
{
m_impl->aabb.Set(m_impl->subMeshes[0]->GetAABB());
m_aabb.Set(m_subMeshes.front().subMesh->GetAABB());
for (std::size_t i = 1; i < subMeshCount; ++i)
m_impl->aabb.ExtendTo(m_impl->subMeshes[i]->GetAABB());
m_aabb.ExtendTo(m_subMeshes[i].subMesh->GetAABB());
}
else
m_impl->aabb.MakeZero();
m_aabb.MakeZero();
m_impl->aabbUpdated = true;
m_aabbUpdated = true;
}
return m_impl->aabb;
return m_aabb;
}
String Mesh::GetAnimation() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return m_impl->animationPath;
return m_animationPath;
}
AnimationType Mesh::GetAnimationType() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return m_impl->animationType;
return m_animationType;
}
UInt32 Mesh::GetJointCount() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Skeletal, "Mesh is not skeletal");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Skeletal, "Mesh is not skeletal");
return m_impl->jointCount;
return m_jointCount;
}
ParameterList& Mesh::GetMaterialData(UInt32 index)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(index < m_impl->materialData.size(), "Material index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(index < m_materialData.size(), "Material index out of range");
return m_impl->materialData[index];
return m_materialData[index];
}
const ParameterList& Mesh::GetMaterialData(UInt32 index) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(index < m_impl->materialData.size(), "Material index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(index < m_materialData.size(), "Material index out of range");
return m_impl->materialData[index];
return m_materialData[index];
}
UInt32 Mesh::GetMaterialCount() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return static_cast<UInt32>(m_impl->materialData.size());
return static_cast<UInt32>(m_materialData.size());
}
Skeleton* Mesh::GetSkeleton()
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Skeletal, "Mesh is not skeletal");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Skeletal, "Mesh is not skeletal");
return &m_impl->skeleton;
return &m_skeleton;
}
const Skeleton* Mesh::GetSkeleton() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Skeletal, "Mesh is not skeletal");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Skeletal, "Mesh is not skeletal");
return &m_impl->skeleton;
return &m_skeleton;
}
SubMesh* Mesh::GetSubMesh(const String& identifier)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
auto it = m_impl->subMeshMap.find(identifier);
NazaraAssert(it != m_impl->subMeshMap.end(), "SubMesh " + identifier + " not found");
auto it = m_subMeshMap.find(identifier);
NazaraAssert(it != m_subMeshMap.end(), "SubMesh " + identifier + " not found");
return m_impl->subMeshes[it->second];
return m_subMeshes[it->second].subMesh;
}
SubMesh* Mesh::GetSubMesh(UInt32 index)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(index < m_impl->subMeshes.size(), "Submesh index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(index < m_subMeshes.size(), "Submesh index out of range");
return m_impl->subMeshes[index];
return m_subMeshes[index].subMesh;
}
const SubMesh* Mesh::GetSubMesh(const String& identifier) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
auto it = m_impl->subMeshMap.find(identifier);
NazaraAssert(it != m_impl->subMeshMap.end(), "SubMesh " + identifier + " not found");
auto it = m_subMeshMap.find(identifier);
NazaraAssert(it != m_subMeshMap.end(), "SubMesh " + identifier + " not found");
return m_impl->subMeshes[it->second];
return m_subMeshes[it->second].subMesh;
}
const SubMesh* Mesh::GetSubMesh(UInt32 index) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(index < m_impl->subMeshes.size(), "Submesh index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(index < m_subMeshes.size(), "Submesh index out of range");
return m_impl->subMeshes[index];
return m_subMeshes[index].subMesh;
}
UInt32 Mesh::GetSubMeshCount() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return static_cast<UInt32>(m_impl->subMeshes.size());
return static_cast<UInt32>(m_subMeshes.size());
}
UInt32 Mesh::GetSubMeshIndex(const String& identifier) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
auto it = m_impl->subMeshMap.find(identifier);
NazaraAssert(it != m_impl->subMeshMap.end(), "SubMesh " + identifier + " not found");
auto it = m_subMeshMap.find(identifier);
NazaraAssert(it != m_subMeshMap.end(), "SubMesh " + identifier + " not found");
return it->second;
}
UInt32 Mesh::GetTriangleCount() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
UInt32 triangleCount = 0;
for (SubMesh* subMesh : m_impl->subMeshes)
triangleCount += subMesh->GetTriangleCount();
for (const SubMeshData& data : m_subMeshes)
triangleCount += data.subMesh->GetTriangleCount();
return triangleCount;
}
UInt32 Mesh::GetVertexCount() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
UInt32 vertexCount = 0;
for (SubMesh* subMesh : m_impl->subMeshes)
vertexCount += subMesh->GetVertexCount();
for (const SubMeshData& data : m_subMeshes)
vertexCount += data.subMesh->GetVertexCount();
return vertexCount;
}
void Mesh::InvalidateAABB() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
m_impl->aabbUpdated = false;
m_aabbUpdated = false;
OnMeshInvalidateAABB(this);
}
bool Mesh::HasSubMesh(const String& identifier) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return m_impl->subMeshMap.find(identifier) != m_impl->subMeshMap.end();
return m_subMeshMap.find(identifier) != m_subMeshMap.end();
}
bool Mesh::HasSubMesh(UInt32 index) const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return index < m_impl->subMeshes.size();
return index < m_subMeshes.size();
}
bool Mesh::IsAnimable() const
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
return m_impl->animationType != AnimationType_Static;
return m_animationType != AnimationType_Static;
}
bool Mesh::IsValid() const
{
return m_impl != nullptr;
return m_isValid;
}
bool Mesh::LoadFromFile(const String& filePath, const MeshParams& params)
@@ -560,20 +538,20 @@ namespace Nz
void Mesh::Recenter()
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Static, "Mesh is not static");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Static, "Mesh is not static");
// The center of our mesh is the center of our *global* AABB
Vector3f center = GetAABB().GetCenter();
for (SubMesh* subMesh : m_impl->subMeshes)
for (SubMeshData& data : m_subMeshes)
{
StaticMesh* staticMesh = static_cast<StaticMesh*>(subMesh);
StaticMesh& staticMesh = static_cast<StaticMesh&>(*data.subMesh);
BufferMapper<VertexBuffer> mapper(staticMesh->GetVertexBuffer(), BufferAccess_ReadWrite);
BufferMapper<VertexBuffer> mapper(staticMesh.GetVertexBuffer(), BufferAccess_ReadWrite);
MeshVertex* vertices = static_cast<MeshVertex*>(mapper.GetPointer());
UInt32 vertexCount = staticMesh->GetVertexCount();
UInt32 vertexCount = staticMesh.GetVertexCount();
for (UInt32 i = 0; i < vertexCount; ++i)
{
vertices->position -= center;
@@ -581,13 +559,11 @@ namespace Nz
}
// Our AABB doesn't change shape, only position
Boxf aabb = staticMesh->GetAABB();
Boxf aabb = staticMesh.GetAABB();
aabb.Translate(-center);
staticMesh->SetAABB(aabb);
staticMesh.SetAABB(aabb); // This will invalidate our AABB
}
InvalidateAABB();
}
void Mesh::RemoveSubMesh(const String& identifier)
@@ -595,22 +571,22 @@ namespace Nz
UInt32 index = GetSubMeshIndex(identifier);
// On déplace l'itérateur du début d'une distance de x
auto it2 = m_impl->subMeshes.begin();
auto it2 = m_subMeshes.begin();
std::advance(it2, index);
m_impl->subMeshes.erase(it2);
m_subMeshes.erase(it2);
InvalidateAABB();
}
void Mesh::RemoveSubMesh(UInt32 index)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(index < m_impl->subMeshes.size(), "Submesh index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(index < m_subMeshes.size(), "Submesh index out of range");
// On déplace l'itérateur du début de x
auto it = m_impl->subMeshes.begin();
auto it = m_subMeshes.begin();
std::advance(it, index);
m_impl->subMeshes.erase(it);
m_subMeshes.erase(it);
InvalidateAABB();
}
@@ -627,34 +603,34 @@ namespace Nz
void Mesh::SetAnimation(const String& animationPath)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
m_impl->animationPath = animationPath;
m_animationPath = animationPath;
}
void Mesh::SetMaterialData(UInt32 matIndex, ParameterList data)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(matIndex < m_impl->materialData.size(), "Material index out of range");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(matIndex < m_materialData.size(), "Material index out of range");
m_impl->materialData[matIndex] = std::move(data);
m_materialData[matIndex] = std::move(data);
}
void Mesh::SetMaterialCount(UInt32 matCount)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(matCount > 0, "A mesh should have at least a material");
m_impl->materialData.resize(matCount);
m_materialData.resize(matCount);
#ifdef NAZARA_DEBUG
for (SubMesh* subMesh : m_impl->subMeshes)
for (SubMeshData& data : m_subMeshes)
{
UInt32 matIndex = subMesh->GetMaterialIndex();
UInt32 matIndex = data.subMesh->GetMaterialIndex();
if (matIndex >= matCount)
{
subMesh->SetMaterialIndex(0); // To prevent a crash
NazaraWarning("SubMesh " + String::Pointer(subMesh) + " material index is over mesh new material count (" + String::Number(matIndex) + " >= " + String::Number(matCount) + "), setting it to first material");
data.subMesh->SetMaterialIndex(0); // To prevent a crash
NazaraWarning("SubMesh " + String::Pointer(data.subMesh) + " material index is over mesh new material count (" + String::Number(matIndex) + " >= " + String::Number(matCount) + "), setting it to first material");
}
}
#endif
@@ -662,19 +638,19 @@ namespace Nz
void Mesh::Transform(const Matrix4f& matrix)
{
NazaraAssert(m_impl, "Mesh should be created first");
NazaraAssert(m_impl->animationType == AnimationType_Static, "Mesh is not static");
NazaraAssert(m_isValid, "Mesh should be created first");
NazaraAssert(m_animationType == AnimationType_Static, "Mesh is not static");
for (SubMesh* subMesh : m_impl->subMeshes)
for (SubMeshData& data : m_subMeshes)
{
StaticMesh* staticMesh = static_cast<StaticMesh*>(subMesh);
StaticMesh& staticMesh = static_cast<StaticMesh&>(*data.subMesh);
BufferMapper<VertexBuffer> mapper(staticMesh->GetVertexBuffer(), BufferAccess_ReadWrite);
BufferMapper<VertexBuffer> mapper(staticMesh.GetVertexBuffer(), BufferAccess_ReadWrite);
MeshVertex* vertices = static_cast<MeshVertex*>(mapper.GetPointer());
Boxf aabb(vertices->position.x, vertices->position.y, vertices->position.z, 0.f, 0.f, 0.f);
UInt32 vertexCount = staticMesh->GetVertexCount();
UInt32 vertexCount = staticMesh.GetVertexCount();
for (UInt32 i = 0; i < vertexCount; ++i)
{
vertices->position = matrix.Transform(vertices->position);
@@ -683,10 +659,8 @@ namespace Nz
vertices++;
}
staticMesh->SetAABB(aabb);
staticMesh.SetAABB(aabb); //< This will invalidate our AABB
}
InvalidateAABB();
}
bool Mesh::Initialize()

View File

@@ -8,8 +8,16 @@
namespace Nz
{
SkeletalMesh::SkeletalMesh(const Mesh* parent) :
SubMesh(parent)
SkeletalMesh::SkeletalMesh(VertexBuffer* vertexBuffer, const IndexBuffer* indexBuffer) :
m_aabb(Nz::Boxf::Zero()),
m_indexBuffer(indexBuffer),
m_vertexBuffer(vertexBuffer)
{
NazaraAssert(m_vertexBuffer, "Invalid vertex buffer");
}
SkeletalMesh::SkeletalMesh(const Mesh* /*parent*/) :
m_aabb(Nz::Boxf::Zero())
{
}
@@ -90,6 +98,8 @@ namespace Nz
void SkeletalMesh::SetAABB(const Boxf& aabb)
{
m_aabb = aabb;
OnSubMeshInvalidateAABB(this);
}
void SkeletalMesh::SetIndexBuffer(const IndexBuffer* indexBuffer)

View File

@@ -10,8 +10,16 @@
namespace Nz
{
StaticMesh::StaticMesh(const Mesh* parent) :
SubMesh(parent)
StaticMesh::StaticMesh(VertexBuffer* vertexBuffer, const IndexBuffer* indexBuffer) :
m_aabb(Nz::Boxf::Zero()),
m_indexBuffer(indexBuffer),
m_vertexBuffer(vertexBuffer)
{
NazaraAssert(m_vertexBuffer, "Invalid vertex buffer");
}
StaticMesh::StaticMesh(const Mesh* /*parent*/) :
m_aabb(Nz::Boxf::Zero())
{
}
@@ -69,7 +77,7 @@ namespace Nz
{
// On lock le buffer pour itérer sur toutes les positions et composer notre AABB
VertexMapper mapper(m_vertexBuffer, BufferAccess_ReadOnly);
m_aabb = ComputeAABB(mapper.GetComponentPtr<const Vector3f>(VertexComponent_Position), m_vertexBuffer->GetVertexCount());
SetAABB(ComputeAABB(mapper.GetComponentPtr<const Vector3f>(VertexComponent_Position), m_vertexBuffer->GetVertexCount()));
return true;
}
@@ -117,10 +125,12 @@ namespace Nz
void StaticMesh::SetAABB(const Boxf& aabb)
{
m_aabb = aabb;
OnSubMeshInvalidateAABB(this);
}
void StaticMesh::SetIndexBuffer(const IndexBuffer* indexBuffer)
{
m_indexBuffer = indexBuffer;
}
}
}

View File

@@ -12,14 +12,18 @@
namespace Nz
{
SubMesh::SubMesh(const Mesh* parent) :
RefCounted(false), // Un SubMesh n'est pas persistant par défaut
SubMesh::SubMesh() :
RefCounted(false), // wut
m_primitiveMode(PrimitiveMode_TriangleList),
m_parent(parent),
m_matIndex(0)
{
}
SubMesh::SubMesh(const Mesh* /*parent*/) :
SubMesh()
{
}
SubMesh::~SubMesh()
{
OnSubMeshRelease(this);
@@ -160,11 +164,6 @@ namespace Nz
while (iterator.Advance());
}
const Mesh* SubMesh::GetParent() const
{
return m_parent;
}
PrimitiveMode SubMesh::GetPrimitiveMode() const
{
return m_primitiveMode;

View File

@@ -0,0 +1,118 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Utility module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Utility/UniformBuffer.hpp>
#include <Nazara/Core/Error.hpp>
#include <Nazara/Core/ErrorFlags.hpp>
#include <Nazara/Utility/Config.hpp>
#include <Nazara/Utility/Debug.hpp>
namespace Nz
{
UniformBuffer::UniformBuffer(BufferRef buffer)
{
ErrorFlags(ErrorFlag_ThrowException, true);
Reset(std::move(buffer));
}
UniformBuffer::UniformBuffer(BufferRef buffer, UInt32 offset, UInt32 size)
{
ErrorFlags(ErrorFlag_ThrowException, true);
Reset(std::move(buffer), offset, size);
}
UniformBuffer::UniformBuffer(UInt32 length, DataStorage storage, BufferUsageFlags usage)
{
ErrorFlags(ErrorFlag_ThrowException, true);
Reset(length, storage, usage);
}
UniformBuffer::UniformBuffer(const UniformBuffer& uniformBuffer) :
RefCounted(),
m_buffer(uniformBuffer.m_buffer),
m_endOffset(uniformBuffer.m_endOffset),
m_startOffset(uniformBuffer.m_startOffset)
{
}
UniformBuffer::~UniformBuffer()
{
OnUniformBufferRelease(this);
}
bool UniformBuffer::Fill(const void* data, UInt32 offset, UInt32 size)
{
NazaraAssert(m_buffer && m_buffer->IsValid(), "Invalid buffer");
NazaraAssert(m_startOffset + offset + size <= m_endOffset, "Exceeding virtual buffer size");
return m_buffer->Fill(data, m_startOffset + offset, size);
}
void* UniformBuffer::Map(BufferAccess access, UInt32 offset, UInt32 size)
{
NazaraAssert(m_buffer && m_buffer->IsValid(), "Invalid buffer");
NazaraAssert(m_startOffset + offset + size <= m_endOffset, "Exceeding virtual buffer size");
return m_buffer->Map(access, offset, size);
}
void* UniformBuffer::Map(BufferAccess access, UInt32 offset, UInt32 size) const
{
NazaraAssert(m_buffer && m_buffer->IsValid(), "Invalid buffer");
NazaraAssert(m_startOffset + offset + size <= m_endOffset, "Exceeding virtual buffer size");
return m_buffer->Map(access, offset, size);
}
void UniformBuffer::Reset()
{
m_buffer.Reset();
}
void UniformBuffer::Reset(BufferRef buffer)
{
NazaraAssert(buffer && buffer->IsValid(), "Invalid buffer");
Reset(buffer, 0, buffer->GetSize());
}
void UniformBuffer::Reset(BufferRef buffer, UInt32 offset, UInt32 size)
{
NazaraAssert(buffer && buffer->IsValid(), "Invalid buffer");
NazaraAssert(buffer->GetType() == BufferType_Uniform, "Buffer must be an uniform buffer");
NazaraAssert(size > 0, "Invalid size");
NazaraAssert(offset + size > buffer->GetSize(), "Virtual buffer exceed buffer bounds");
m_buffer = buffer;
m_endOffset = offset + size;
m_startOffset = offset;
}
void UniformBuffer::Reset(UInt32 size, DataStorage storage, BufferUsageFlags usage)
{
m_endOffset = size;
m_startOffset = 0;
m_buffer = Buffer::New(BufferType_Uniform, m_endOffset, storage, usage);
}
void UniformBuffer::Reset(const UniformBuffer& UniformBuffer)
{
m_buffer = UniformBuffer.m_buffer;
m_endOffset = UniformBuffer.m_endOffset;
m_startOffset = UniformBuffer.m_startOffset;
}
void UniformBuffer::Unmap() const
{
m_buffer->Unmap();
}
UniformBuffer& UniformBuffer::operator=(const UniformBuffer& uniformBuffer)
{
Reset(uniformBuffer);
return *this;
}
}

View File

@@ -98,6 +98,7 @@ namespace Nz
void VertexBuffer::Reset(VertexDeclarationConstRef vertexDeclaration, BufferRef buffer)
{
NazaraAssert(buffer && buffer->IsValid(), "Invalid buffer");
NazaraAssert(buffer->GetType() == BufferType_Vertex, "Buffer must be a vertex buffer");
UInt32 size = buffer->GetSize();
Reset(std::move(vertexDeclaration), std::move(buffer), 0, size);