MxEngine
Primitives.h
1 // Copyright(c) 2019 - 2020, #Momo
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met :
6 //
7 // 1. Redistributions of source code must retain the above copyright notice, this
8 // list of conditions and the following disclaimer.
9 //
10 // 2. Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and /or other materials provided with the distribution.
13 //
14 // 3. Neither the name of the copyright holder nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 // DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 // DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 // OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
29 #pragma once
30 
31 #include "Utilities/Array/ArrayView.h"
32 #include "Core/Resources/AssetManager.h"
33 #include "Utilities/Array/Array2D.h"
34 
35 namespace MxEngine
36 {
37  class Primitives
38  {
39  public:
41 
42  static MeshHandle CreateMesh(const AABB& boundingBox, MeshData meshData);
43  static MeshHandle CreateCube();
44  static MeshHandle CreatePlane(size_t UVrepeats = 1);
45  static MeshHandle CreateSphere(size_t polygons = 30);
46  static MeshHandle CreateSurface(const Array2D<float>& heights);
47  static TextureHandle CreateGridTexture(size_t textureSize = 512, float borderScale = 0.01f);
48 
49  template<typename Func>
50  static MeshHandle CreateSurface(Func&& f, float xsize, float ysize, float step)
51  {
52  static_assert(std::is_same<float, decltype(f(0.0f, 0.0f))>::value, "Func must accept two floats and output one float");
53  MX_ASSERT(step > 0.0f);
54  MX_ASSERT(xsize > 0.0f);
55  MX_ASSERT(ysize > 0.0f);
56 
57  size_t intxsize = static_cast<size_t>(xsize / step);
58  size_t intysize = static_cast<size_t>(ysize / step);
59  Array2D<float> heights;
60  heights.resize(intxsize, intysize);
61 
62  for (size_t x = 0; x < intxsize; x++)
63  {
64  for (size_t y = 0; y < intysize; y++)
65  {
66  float fx = x * step;
67  float fy = y * step;
68  heights[x][y] = f(fx, fy);
69  }
70  }
71  return Primitives::CreateSurface(heights);
72  }
73  };
74 }
Definition: AbstractFactory.h:61
Definition: MeshData.h:48
Definition: Primitives.h:37
Definition: Array2D.h:41
void resize(size_t width, size_t height, T value=T())
Definition: Array2D.h:166
Definition: Application.cpp:49
Definition: AABB.h:35