From 65c08442ce99cfeeeebd9da32344ad44f3ed42b6 Mon Sep 17 00:00:00 2001 From: Lynix Date: Thu, 30 May 2013 13:22:10 +0200 Subject: [PATCH] Added Primitive(List) Former-commit-id: 3080a1846616fb2dac25e6d341c8a9220f406de4 --- include/Nazara/Core/Enums.hpp | 18 ++++ include/Nazara/Core/Primitive.hpp | 71 +++++++++++++ include/Nazara/Core/PrimitiveList.hpp | 42 ++++++++ src/Nazara/Core/PrimitiveList.cpp | 144 ++++++++++++++++++++++++++ 4 files changed, 275 insertions(+) create mode 100644 include/Nazara/Core/Primitive.hpp create mode 100644 include/Nazara/Core/PrimitiveList.hpp create mode 100644 src/Nazara/Core/PrimitiveList.cpp diff --git a/include/Nazara/Core/Enums.hpp b/include/Nazara/Core/Enums.hpp index 08983cd0f..87b32fbd9 100644 --- a/include/Nazara/Core/Enums.hpp +++ b/include/Nazara/Core/Enums.hpp @@ -33,6 +33,15 @@ enum nzPlugin nzPlugin_FreeType }; +enum nzPrimitiveType +{ + nzPrimitiveType_Cube, + nzPrimitiveType_Plane, + nzPrimitiveType_Sphere, + + nzPrimitiveType_Max = nzPrimitiveType_Sphere +}; + enum nzProcessorCap { nzProcessorCap_x64, @@ -72,6 +81,15 @@ enum nzProcessorVendor nzProcessorVendor_Max = nzProcessorVendor_Vortex }; +enum nzSphereType +{ + nzSphereType_Cubic, + nzSphereType_Ico, + nzSphereType_UV, + + nzSphereType_Max = nzSphereType_UV +}; + enum nzStreamOptionFlags { nzStreamOption_None = 0x0, diff --git a/include/Nazara/Core/Primitive.hpp b/include/Nazara/Core/Primitive.hpp new file mode 100644 index 000000000..26164d07d --- /dev/null +++ b/include/Nazara/Core/Primitive.hpp @@ -0,0 +1,71 @@ +// Copyright (C) 2013 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_PRIMITIVE_HPP +#define NAZARA_PRIMITIVE_HPP + +#include +#include +#include +#include +#include + +struct NzPrimitive +{ + nzPrimitiveType type; + + union + { + struct + { + NzCubef cube; + NzMatrix4f matrix; + NzVector3ui subdivision; + } + cube; + + struct + { + NzVector2f size; + NzVector2ui subdivision; + NzVector3f normal; + NzVector3f position; + } + plane; + + struct + { + nzSphereType type; + NzMatrix4f matrix; + float size; + + union + { + struct + { + unsigned int subdivision; + } + cubic; + + struct + { + unsigned int recursionLevel; + } + ico; + + struct + { + unsigned int slices; + unsigned int stacks; + } + uv; + }; + } + sphere; + }; +}; + +#endif // NAZARA_PRIMITIVE_HPP diff --git a/include/Nazara/Core/PrimitiveList.hpp b/include/Nazara/Core/PrimitiveList.hpp new file mode 100644 index 000000000..52b56087c --- /dev/null +++ b/include/Nazara/Core/PrimitiveList.hpp @@ -0,0 +1,42 @@ +// Copyright (C) 2013 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#pragma once + +#ifndef NAZARA_PRIMITIVELIST_HPP +#define NAZARA_PRIMITIVELIST_HPP + +#include +#include +#include + +class NAZARA_API NzPrimitiveList +{ + public: + NzPrimitiveList() = default; + ~NzPrimitiveList() = default; + + void AddCube(const NzCubef& box, const NzVector3ui& subdivision = NzVector3ui(0U), const NzMatrix4f& matrix = NzMatrix4f::Identity()); + void AddCube(const NzCubef& box, const NzVector3ui& subdivision, const NzVector3f& position, const NzQuaternionf& rotation = NzQuaternionf::Identity()); + void AddCubicSphere(float size, unsigned int subdivision = 4, const NzMatrix4f& matrix = NzMatrix4f::Identity()); + void AddCubicSphere(float size, unsigned int subdivision, const NzVector3f& position, const NzQuaternionf& rotation = NzQuaternionf::Identity()); + void AddIcoSphere(float size, unsigned int recursionLevel = 1, const NzMatrix4f& matrix = NzMatrix4f::Identity()); + void AddIcoSphere(float size, unsigned int recursionLevel, const NzVector3f& position, const NzQuaternionf& rotation = NzQuaternionf::Identity()); + void AddPlane(const NzPlanef& plane, const NzVector2f& size, const NzVector2ui& subdivision = NzVector2ui(0U)); + void AddPlane(const NzVector3f& position, const NzVector3f& normal, const NzVector2f& size, const NzVector2ui& subdivision = NzVector2ui(0U)); + void AddUVSphere(float size, unsigned int slices = 4, unsigned int stacks = 4, const NzMatrix4f& matrix = NzMatrix4f::Identity()); + void AddUVSphere(float size, unsigned int slices, unsigned int stacks, const NzVector3f& position, const NzQuaternionf& rotation = NzQuaternionf::Identity()); + + NzPrimitive& GetPrimitive(unsigned int i); + const NzPrimitive& GetPrimitive(unsigned int i) const; + unsigned int GetSize() const; + + NzPrimitive& operator()(unsigned int i); + const NzPrimitive& operator()(unsigned int i) const; + + private: + std::vector m_primitives; +}; + +#endif // NAZARA_PRIMITIVELIST_HPP diff --git a/src/Nazara/Core/PrimitiveList.cpp b/src/Nazara/Core/PrimitiveList.cpp new file mode 100644 index 000000000..42868f8d1 --- /dev/null +++ b/src/Nazara/Core/PrimitiveList.cpp @@ -0,0 +1,144 @@ +// Copyright (C) 2013 Jérôme Leclercq +// This file is part of the "Nazara Engine - Core module" +// For conditions of distribution and use, see copyright notice in Config.hpp + +#include +#include +#include + +void NzPrimitiveList::AddCube(const NzCubef& cube, const NzVector3ui& subdivision, const NzMatrix4f& matrix) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Cube; + primitive.cube.cube = cube; // cube.cube = cube, parce que je le vaux bien + primitive.cube.matrix = matrix; + primitive.cube.subdivision = subdivision; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddCube(const NzCubef& cube, const NzVector3ui& subdivision, const NzVector3f& position, const NzQuaternionf& rotation) +{ + AddCube(cube, subdivision, NzMatrix4f::Transform(position, rotation)); +} + +void NzPrimitiveList::AddCubicSphere(float size, unsigned int subdivision, const NzMatrix4f& matrix) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Sphere; + primitive.sphere.matrix = matrix; + primitive.sphere.size = size; + primitive.sphere.type = nzSphereType_Cubic; + primitive.sphere.cubic.subdivision = subdivision; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddCubicSphere(float size, unsigned int subdivision, const NzVector3f& position, const NzQuaternionf& rotation) +{ + AddCubicSphere(size, subdivision, NzMatrix4f::Transform(position, rotation)); +} + +void NzPrimitiveList::AddIcoSphere(float size, unsigned int recursionLevel, const NzMatrix4f& matrix) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Sphere; + primitive.sphere.matrix = matrix; + primitive.sphere.size = size; + primitive.sphere.type = nzSphereType_Ico; + primitive.sphere.ico.recursionLevel = recursionLevel; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddIcoSphere(float size, unsigned int recursionLevel, const NzVector3f& position, const NzQuaternionf& rotation) +{ + AddIcoSphere(size, recursionLevel, NzMatrix4f::Transform(position, rotation)); +} + +void NzPrimitiveList::AddPlane(const NzPlanef& plane, const NzVector2f& size, const NzVector2ui& subdivision) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Plane; + primitive.plane.normal = plane.normal; + primitive.plane.position = plane.distance * plane.normal; + primitive.plane.size = size; + primitive.plane.subdivision = subdivision; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddPlane(const NzVector3f& position, const NzVector3f& normal, const NzVector2f& size, const NzVector2ui& subdivision) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Plane; + primitive.plane.normal = normal; + primitive.plane.position = position; + primitive.plane.size = size; + primitive.plane.subdivision = subdivision; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddUVSphere(float size, unsigned int slices, unsigned int stacks, const NzMatrix4f& matrix) +{ + NzPrimitive primitive; + primitive.type = nzPrimitiveType_Sphere; + primitive.sphere.matrix = matrix; + primitive.sphere.size = size; + primitive.sphere.type = nzSphereType_UV; + primitive.sphere.uv.slices = slices; + primitive.sphere.uv.stacks = stacks; + + m_primitives.push_back(primitive); +} + +void NzPrimitiveList::AddUVSphere(float size, unsigned int slices, unsigned int stacks, const NzVector3f& position, const NzQuaternionf& rotation) +{ + AddUVSphere(size, slices, stacks, NzMatrix4f::Transform(position, rotation)); +} + +NzPrimitive& NzPrimitiveList::GetPrimitive(unsigned int i) +{ + #if NAZARA_CORE_SAFE + if (i >= m_primitives.size()) + { + NazaraError("Primitive index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_primitives.size()) + ')'); + + static NzPrimitive dummy; + return dummy; + } + #endif + + return m_primitives[i]; +} + +const NzPrimitive& NzPrimitiveList::GetPrimitive(unsigned int i) const +{ + #if NAZARA_CORE_SAFE + if (i >= m_primitives.size()) + { + NazaraError("Primitive index out of range (" + NzString::Number(i) + " >= " + NzString::Number(m_primitives.size()) + ')'); + + static NzPrimitive dummy; + return dummy; + } + #endif + + return m_primitives[i]; +} + +unsigned int NzPrimitiveList::GetSize() const +{ + return m_primitives.size(); +} + +NzPrimitive& NzPrimitiveList::operator()(unsigned int i) +{ + return GetPrimitive(i); +} + +const NzPrimitive& NzPrimitiveList::operator()(unsigned int i) const +{ + return GetPrimitive(i); +}