Skip to content

Commit 0a0f584

Browse files
committed
Accumulated Fix for v0.2
+ Updated README + Added Frustum Projection + Added few math function utilities to ease procedural shape generations + Rendering Optimizations. + Fix some rendering and physics bug
1 parent 5d1f789 commit 0a0f584

31 files changed

+807
-161
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
66
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
77
endif()
88

9-
set(forth_version 0.1)
9+
set(forth_version 0.2)
1010

1111
add_subdirectory(source)
1212

README.md

+29-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,37 @@ Forth-Library is a Library to manipulate four dimensional objects via Cross Sect
88

99
Forth-Library is part of higher project *Forth Engine*. Forth Engine is not just about displaying higher dimensional objects: There will be Physics, Scene Management, Import/Export, Graphics Patches, The Editor, etc.. It's future aim is to became a full blown Independent Game Engine.
1010

11-
Playable demos is at [Forth-Engine/demo](https://github.com/forth-engine/demo)
11+
This Library does not depends on any other library except STL (Standard Libraries) implemented in C++11. Currently it's tested only in MSVC 15 (Windows). More platform and compiler supports is on the way.
1212

13-
# Prelease Checklist
13+
Playable demos is at [Forth-Engine/demo](https://github.com/forth-engine/demo)
1414

15-
+ ✅ Core Rendering Library
16-
+ ⌛ Full OpenGL integration
17-
+ ⌛ Physics Library
18-
+ ⌛ FOBJ (Forth-OBJ File) Import/Export
15+
# Prelease Feature Checklists
16+
17+
*Legend: ✅ Finished ⌛ Not implemented / WIP*
18+
19+
+ ⌛ Core Rendering Functions
20+
- ✅ Procedural Shape Generation Functions
21+
- ✅ Primitive Shapes (Procedural)
22+
- ✅ CrossSection (Flatland) Projection
23+
- ✅ Frustum (Perspective) Projection
24+
- ✅ (Basic) Shape Import/Export
25+
- ✅ OpenGL Integration
26+
- ⌛ Vertex Buffer (UV + Color)
27+
+ ⌛ Graphic Features
28+
- ✅ Simplexes (Point+Line+Surface Rendering)
29+
- ✅ Culling Optimizations
30+
- ⌛ Particle System
31+
- ⌛ Line Trails
32+
+ ⌛ Physics Module
33+
- ✅ Collisions and Rigidbody
34+
- ✅ Bounce and Frictions
35+
- ✅ Primitive Collider (Box + Sphere + Capsule)
36+
- ⌛ Convex Collider
37+
- ⌛ Static Concave Collider
38+
- ⌛ Kinematic Character Controller
39+
- ⌛ Joints/Effectors
40+
+ ⌛ Scene Managements
41+
+ ⌛ Documentation
1942
+ ⌛ ...etc...
2043

2144
# LICENSE

source/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ set(forth_math_srcs
2626
math/SphereBounds4.h
2727
math/Tensor4.cpp
2828
math/Tensor4.h
29+
math/Transform4.cpp
2930
math/Transform4.h
3031
math/Vector3.h
3132
math/Vector4.cpp
@@ -35,6 +36,8 @@ set(forth_math_srcs
3536
set(forth_rendering_srcs
3637
rendering/CrossSection.cpp
3738
rendering/CrossSection.h
39+
rendering/Frustum4.cpp
40+
rendering/Frustum4.h
3841
rendering/Model4.h
3942
rendering/Projector4.cpp
4043
rendering/Projector4.h

source/common/Buffer4.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -523,8 +523,8 @@ namespace Forth
523523
break;
524524

525525
case SQM_LineLoop:
526-
for (int i = 0; i < va;)
527-
AddSegment(v[i], (v[++i % va]));
526+
for (int i = 0; i < va; ++i)
527+
AddSegment(v[i], (v[(i + 1) % va]));
528528
break;
529529

530530
case SQM_Triangles:
@@ -553,8 +553,8 @@ namespace Forth
553553
break;
554554

555555
case SQM_Polygon:
556-
for (int i = 1; i < va;)
557-
AddTriangle(v[0], v[i], (v[++i % va]));
556+
for (int i = 1; i < va; ++i)
557+
AddTriangle(v[(i + 1) % va], v[i], v[0]);
558558
break;
559559

560560
case SQM_Trimids:

source/common/Buffer4.h

-12
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,6 @@ namespace Forth
7878
/// </summary>
7979
void AddPoint(int v0);
8080

81-
void AddPoints(int v...) // recursive variadic function
82-
{
83-
va_list args;
84-
va_start(args, v);
85-
for (int i = 0; i < v; i++)
86-
{
87-
int a = va_arg(args, int);
88-
AddPoint(a);
89-
}
90-
va_end(args);
91-
}
92-
9381
/// <summary>
9482
/// Add a segment. Order doesn't matter.
9583
/// </summary>

source/common/BufferGL.h

+61-26
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@ namespace Forth
1414
// Layout: 0-2 Position. 3-5 Normal
1515
FORTH_ARRAY(vb, float);
1616

17+
struct
18+
{
19+
int counts[10] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
20+
int slots = 0;
21+
int stripe = 0;
22+
int simplex = 0;
23+
int vertexs = 0;
24+
} attr;
25+
26+
struct
27+
{
28+
bool normal = true;
29+
} generate;
30+
1731
BufferGL(void) {}
1832

1933
~BufferGL(void)
@@ -26,41 +40,62 @@ namespace Forth
2640
vb_count = 0;
2741
}
2842

29-
void Copy(const Buffer3 &v)
43+
void FillVertices(const Buffer3 &v, int offset)
3044
{
31-
Clear();
32-
33-
int len = v.indices_count;
34-
35-
const int BUFFER_PER_VERTEX = 6;
36-
const int BUFFER_PER_TRIS = BUFFER_PER_VERTEX * 3;
37-
38-
EnsureCapacity(&vb, 0, &vb_cap, len * BUFFER_PER_VERTEX);
45+
for (int i = 0; i < attr.vertexs; ++i)
46+
{
47+
auto &f = v.vertices[v.indices[i]];
48+
vb[i * attr.stripe + offset + 0] = f.x;
49+
vb[i * attr.stripe + offset + 1] = f.y;
50+
vb[i * attr.stripe + offset + 2] = f.z;
51+
}
52+
}
3953

40-
for (int i = 0; i < len;)
54+
void FillNormals(const Buffer3 &v, int offset)
55+
{
56+
for (int i = 0; i < attr.vertexs;i += 3)
4157
{
42-
const int a = v.indices[i++],
43-
b = v.indices[i++],
44-
c = v.indices[i++];
58+
const int a = v.indices[i + 0],
59+
b = v.indices[i + 1],
60+
c = v.indices[i + 2];
4561

46-
const Vector3 av = v.vertices[a],
47-
bv = v.vertices[b],
48-
cv = v.vertices[c],
62+
const Vector3 &av = v.vertices[a],
63+
&bv = v.vertices[b],
64+
&cv = v.vertices[c],
4965
nv = Forth::Normalize(Forth::Cross(bv - cv, av - cv));
5066

51-
// clang-format off
52-
float arr[] = {
53-
av.x, av.y, av.z, nv.x, nv.y, nv.z,
54-
bv.x, bv.y, bv.z, nv.x, nv.y, nv.z,
55-
cv.x, cv.y, cv.z, nv.x, nv.y, nv.z,
56-
};
57-
// clang-format on
58-
59-
for (int j = 0; j < BUFFER_PER_TRIS;)
67+
for (int j = 0; j < 3;++j)
6068
{
61-
vb[vb_count++] = arr[j++];
69+
vb[(i + j) * attr.stripe + offset + 0] = nv.x;
70+
vb[(i + j) * attr.stripe + offset + 1] = nv.y;
71+
vb[(i + j) * attr.stripe + offset + 2] = nv.z;
6272
}
6373
}
6474
}
75+
76+
void Copy(const Buffer3 &v)
77+
{
78+
Clear();
79+
80+
attr.vertexs = v.indices_count;
81+
attr.simplex = v.simplex + 1;
82+
attr.stripe = 3;
83+
attr.slots = 1;
84+
attr.counts[0] = 3; // First: Vertice Positions
85+
86+
bool genNormal = v.simplex == SM_Triangle && generate.normal;
87+
if (genNormal)
88+
{
89+
attr.counts[1] = 3; // Second: Normal
90+
attr.slots++;
91+
attr.stripe += 3;
92+
}
93+
94+
EnsureCapacity(&vb, 0, &vb_cap, vb_count = attr.vertexs * attr.stripe);
95+
96+
FillVertices(v, 0);
97+
if (genNormal)
98+
FillNormals(v, 3);
99+
}
65100
};
66101
} // namespace Forth

source/extras/MeshGen.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ namespace Forth
125125

126126
void MeshGen::MakeTesseract(Buffer4 &input, float scale)
127127
{
128+
scale *= .5;
128129
// Permutation of (-+1, -+1, -+1, -+1)
129130
// Idk why the order have to be like this?
130131
for (float w = -1; w <= 1; w += 2)
@@ -176,16 +177,16 @@ namespace Forth
176177
switch (input.simplex)
177178
{
178179
case SM_Point:
179-
input.AddPoints(a, b, c, d, e, f);
180+
input.AddPoint({ a, b, c, d, e, f });
180181
break;
181182
case SM_Line:
182183
input.AddQuad(c, d, e, f);
183184
input.AddBySequence(SQM_LineFan, {a, c, d, e, f});
184185
input.AddBySequence(SQM_LineFan, {b, c, d, e, f});
185186
break;
186187
case SM_Triangle:
187-
input.AddPolygon({{a, c, d, e, f}});
188-
input.AddPolygon({{b, c, d, e, f}});
188+
input.AddPolygon({a, c, d, e, f});
189+
input.AddPolygon({b, c, d, e, f});
189190
break;
190191
case SM_Tetrahedron:
191192
input.AddPyramid(a, d, c, f, e);
@@ -252,9 +253,9 @@ namespace Forth
252253
{
253254
case SM_Point:
254255

255-
input.AddPoints(
256+
input.AddPoint({
256257
a, b, c, d, e, f, g, h, i, j,
257-
k, l, m, n, o, p, q, r, s, t);
258+
k, l, m, n, o, p, q, r, s, t });
258259
break;
259260

260261
case SM_Line:

source/extras/MeshGen.h

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#pragma once
33

44
#include "../common/Buffer4.h"
5+
#include "../math/Transform4.h"
56

67
namespace Forth
78
{
@@ -148,5 +149,18 @@ namespace Forth
148149
else
149150
input.SequenceGrid(state.subdiv * 2 + 1, state.subdiv + 1, state.subdiv + 1);
150151
}
152+
153+
static void Transform(Buffer4 &input, const Transform4& transform, bool realign = true)
154+
{
155+
for(int i = input.verticeCount; i-- > input.offset;)
156+
{
157+
input.vertices[i] = transform * input.vertices[i];
158+
}
159+
160+
if (realign)
161+
{
162+
input.Align();
163+
}
164+
}
151165
};
152166
} // namespace Forth

source/extras/Utils.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ namespace Forth
2121
}
2222
}
2323

24-
25-
#define SWAP(a, b, c) c=a;a=b;b=c;
24+
#define SWAP(a, b, c) \
25+
c = a; \
26+
a = b; \
27+
b = c;
2628

2729
#define FORTH_ARRAY(name, T) \
2830
T *name = new T[4]; \
29-
int name##_cap = 4; \
31+
int name##_cap = 4; \
3032
int name##_count = 0
3133

3234
} // namespace Forth

source/forth.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
#define FORTH_H
55

66
#define FORTH_MAJOR 0
7-
#define FORTH_MINOR 1
7+
#define FORTH_MINOR 2
88

99
#include "common/Buffer3.h"
1010
#include "common/Buffer4.h"
1111
#include "common/BufferGL.h"
1212
#include "extras/MeshGen.h"
1313
#include "math/Transform4.h"
14+
#include "rendering/Frustum4.h"
1415
#include "rendering/CrossSection.h"
1516
#include "rendering/Model4.h"
1617
#include "visualizer/SolidVisualizer.h"

source/math/Math.h

+6
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ namespace Forth
103103
return a + (b - a) * t;
104104
}
105105

106+
template <class T>
107+
inline T SmoothDamp(T a, T b, T dt)
108+
{
109+
return Lerp(a, b, T(1) - std::exp(-dt));
110+
}
111+
106112
inline float Invert(float f)
107113
{
108114
return 1.f / f;

source/math/Matrix4.cpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ namespace Forth
9797
Matrix4 Euler(int axis, float degree)
9898
{
9999
float s = sin(degree * DEG2RAD), c = cos(degree * DEG2RAD);
100-
Matrix4 m = Matrix4::identity();
100+
Matrix4 m = Matrix4::identity;
101101
// clang-format off
102102
switch (axis)
103103
{
@@ -111,4 +111,12 @@ namespace Forth
111111
}
112112
// clang-format on
113113
}
114+
115+
/// <summary>
116+
/// Get a 4x4 identity matrix
117+
/// </summary>
118+
const Matrix4 Matrix4::identity = Matrix4(1);
119+
120+
const Matrix4 Matrix4::zero = Matrix4();
121+
114122
} // namespace Forth

source/math/Matrix4.h

+2-13
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,9 @@ namespace Forth
173173
/// <summary>
174174
/// Get a 4x4 identity matrix
175175
/// </summary>
176-
static Matrix4 identity(void)
177-
{
178-
Matrix4 m = Matrix4();
179-
m.ex.x = m.ey.y = m.ez.z = m.ew.w = 1;
180-
return m;
181-
}
176+
static const Matrix4 identity;
182177

183-
/// <summary>
184-
/// Get a 4x4 identity matrix
185-
/// </summary>
186-
static Matrix4 zero(void)
187-
{
188-
return Matrix4();
189-
}
178+
static const Matrix4 zero;
190179

191180
private:
192181
void Transpose(void)

0 commit comments

Comments
 (0)