Skip to content

Commit aa4b7da

Browse files
committed
simplify projection using Model4 (with bugfixes)
+ Model4 == Buffer4 + Buffer3 + BufferGL + Model Transform in one pack. + No Inline in CPP + Handle with default visualizers + Vector3 init bugfix + FORTH_GL_DRAW macro for OpenGL
1 parent 25a6355 commit aa4b7da

File tree

11 files changed

+140
-53
lines changed

11 files changed

+140
-53
lines changed

source/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ set(forth_math_srcs
2525
set(forth_rendering_srcs
2626
rendering/CrossSection.cpp
2727
rendering/CrossSection.h
28+
rendering/Model4.h
29+
rendering/Projector4.cpp
2830
rendering/Projector4.h
2931
rendering/Visualizer4.h
3032
)

source/common/Buffer4.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,71 @@
33

44
namespace Forth
55
{
6-
inline void Buffer4::EnsureIndices(const int incoming)
6+
void Buffer4::EnsureIndices(const int incoming)
77
{
88
if (indiceCount + incoming > indiceCap)
99
{
1010
Expand(&indices, indiceCount, indiceCap = Max(indiceCount + incoming, indiceCap << 1));
1111
}
1212
}
13-
inline void Buffer4::EnsureVertices(const int incoming)
13+
void Buffer4::EnsureVertices(const int incoming)
1414
{
1515
if (verticeCount + incoming > verticeCap)
1616
{
1717
Expand(&vertices, verticeCount, verticeCap = Max(verticeCount + incoming, verticeCap << 1));
1818
}
1919
}
2020

21-
inline void Buffer4::Clear()
21+
void Buffer4::Clear()
2222
{
2323
verticeCount = indiceCount = offset = 0;
2424
}
2525

26-
inline void Buffer4::Clean()
26+
void Buffer4::Clean()
2727
{
2828
Clear();
2929
indices = new int[indiceCap = 4];
3030
vertices = new Vector4[verticeCap = 4];
3131
}
3232

33-
inline Buffer4::Buffer4()
33+
Buffer4::Buffer4()
3434
{
3535
Clean();
3636
}
3737

38-
inline void Buffer4::Align() { offset = verticeCount; }
38+
void Buffer4::Align() { offset = verticeCount; }
3939

40-
inline void Buffer4::Align(int snapshot) { offset = snapshot; }
40+
void Buffer4::Align(int snapshot) { offset = snapshot; }
4141

4242
/// <summary>
4343
/// Send copy of current buffer position to be reused later.
4444
/// </summary>
4545
/// <seealso cref="Align(int)"/>
4646

47-
inline int Buffer4::Snapshot() { return verticeCount; }
47+
int Buffer4::Snapshot() { return verticeCount; }
4848

49-
inline void Buffer4::AddSimplex(int i)
49+
void Buffer4::AddSimplex(int i)
5050
{
5151
EnsureIndices(1);
5252
indices[indiceCount++] = (i + offset);
5353
}
5454

55-
inline void Buffer4::AddSimplex(int i, int j)
55+
void Buffer4::AddSimplex(int i, int j)
5656
{
5757
EnsureIndices(2);
5858
indices[indiceCount++] = (i + offset);
5959
indices[indiceCount++] = (j + offset);
6060
}
6161

62-
inline void Buffer4::AddSimplex(int i, int j, int k)
62+
void Buffer4::AddSimplex(int i, int j, int k)
6363
{
6464
EnsureIndices(3);
6565
indices[indiceCount++] = (i + offset);
6666
indices[indiceCount++] = (j + offset);
6767
indices[indiceCount++] = (k + offset);
6868
}
6969

70-
inline void Buffer4::AddSimplex(int i, int j, int k, int l)
70+
void Buffer4::AddSimplex(int i, int j, int k, int l)
7171
{
7272
EnsureIndices(2);
7373
indices[indiceCount++] = (i + offset);
@@ -76,7 +76,7 @@ namespace Forth
7676
indices[indiceCount++] = (l + offset);
7777
}
7878

79-
inline void Buffer4::AddPoint(int v0)
79+
void Buffer4::AddPoint(int v0)
8080
{
8181
switch (simplex)
8282
{
@@ -86,7 +86,7 @@ namespace Forth
8686
}
8787
}
8888

89-
inline void Buffer4::AddSegment(int v0, int v1)
89+
void Buffer4::AddSegment(int v0, int v1)
9090
{
9191
switch (simplex)
9292
{
@@ -99,7 +99,7 @@ namespace Forth
9999
}
100100
}
101101

102-
inline void Buffer4::AddTriangle(int v0, int v1, int v2)
102+
void Buffer4::AddTriangle(int v0, int v1, int v2)
103103
{
104104
switch (simplex)
105105
{
@@ -117,7 +117,7 @@ namespace Forth
117117
}
118118
}
119119

120-
inline void Buffer4::AddQuad(int v0, int v1, int v2, int v3)
120+
void Buffer4::AddQuad(int v0, int v1, int v2, int v3)
121121
{
122122
switch (simplex)
123123
{
@@ -137,7 +137,7 @@ namespace Forth
137137
}
138138
}
139139

140-
inline void Buffer4::AddTrimid(int v0, int v1, int v2, int v3)
140+
void Buffer4::AddTrimid(int v0, int v1, int v2, int v3)
141141
{
142142
switch (simplex)
143143
{
@@ -165,7 +165,7 @@ namespace Forth
165165
}
166166
}
167167

168-
inline void Buffer4::AddPyramid(int v0, int v1, int v2, int v3, int v4)
168+
void Buffer4::AddPyramid(int v0, int v1, int v2, int v3, int v4)
169169
{
170170
switch (simplex)
171171
{
@@ -196,7 +196,7 @@ namespace Forth
196196
}
197197
}
198198

199-
inline void Buffer4::AddPrism(int v0, int v1, int v2, int v3, int v4, int v5)
199+
void Buffer4::AddPrism(int v0, int v1, int v2, int v3, int v4, int v5)
200200
{
201201
switch (simplex)
202202
{
@@ -230,7 +230,7 @@ namespace Forth
230230
}
231231
}
232232

233-
inline void Buffer4::AddCube(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7)
233+
void Buffer4::AddCube(int v0, int v1, int v2, int v3, int v4, int v5, int v6, int v7)
234234
{
235235
switch (simplex)
236236
{
@@ -271,21 +271,21 @@ namespace Forth
271271
}
272272
}
273273

274-
inline int Buffer4::AddVertex(int idx)
274+
int Buffer4::AddVertex(int idx)
275275
{
276276
EnsureVertices(1);
277277
vertices[verticeCount] = (vertices[idx + offset]);
278278
return verticeCount++ - offset;
279279
}
280280

281-
inline int Buffer4::AddVertex(Vector4 vert)
281+
int Buffer4::AddVertex(Vector4 vert)
282282
{
283283
EnsureVertices(1);
284284
vertices[verticeCount] = vert;
285285
return verticeCount++ - offset;
286286
}
287287

288-
inline void Buffer4::Sequence(SequenceMode mode, int start, int count)
288+
void Buffer4::Sequence(SequenceMode mode, int start, int count)
289289
{
290290
offset += start;
291291
int end = count < 0 ? verticeCount - offset : count;
@@ -384,13 +384,13 @@ namespace Forth
384384
offset -= start;
385385
}
386386

387-
inline int Buffer4::SequenceIndex(int i, int j, int k, int l)
387+
int Buffer4::SequenceIndex(int i, int j, int k, int l)
388388
{
389389
return l + _seqW * (k + _seqZ * (j + _seqY * i));
390390
}
391391

392392
template <typename T>
393-
inline void Buffer4::Expand(T **arr, int count, int newSize)
393+
void Buffer4::Expand(T **arr, int count, int newSize)
394394
{
395395
T *newArr = new T[newSize];
396396

source/common/BufferGL.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Forth
1111
struct BufferGL
1212
{
1313
// Layout: 0-2 Position. 3-5 Normal
14-
std::vector<float> vertexbuffer;
14+
std::vector<float> vb;
1515

1616
BufferGL() {}
1717

@@ -22,7 +22,7 @@ namespace Forth
2222

2323
void Clear()
2424
{
25-
vertexbuffer.clear();
25+
vb.clear();
2626
}
2727

2828
void Copy(const Buffer3 &v)
@@ -49,7 +49,7 @@ namespace Forth
4949
};
5050
// clang-format on
5151

52-
vertexbuffer.insert(vertexbuffer.end(), arr, arr + 18);
52+
vb.insert(vb.end(), arr, arr + 18);
5353
}
5454
}
5555
};

source/forth.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#include "common/Buffer3.h"
1010
#include "common/Buffer4.h"
11+
#include "common/BufferGL.h"
1112
#include "math/Transform4.h"
1213
#include "rendering/CrossSection.h"
14+
#include "rendering/Model4.h"
1315
#include "visualizer/SolidVisualizer.h"
1416
#include "visualizer/WireVisualizer.h"
1517
#include "visualizer/ParticleVisualizer.h"

source/math/Matrix4.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,5 @@ namespace Forth
247247
/// Convert given degree rotation in given axis to orthogonal matrix rotation.
248248
/// </summary>
249249
/// <remarks>This method is much optimized than Euler(new Euler4(axis, degree))</remarks>
250-
inline Matrix4 Euler(int axis, float degree);
250+
Matrix4 Euler(int axis, float degree);
251251
}

source/math/Vector3.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Forth
99
{
1010
float x, y, z;
1111

12-
Vector3() { }
12+
Vector3() : x(0), y(0), z(0) { }
1313
Vector3(float xyz) : x(xyz), y(xyz), z(xyz) { }
1414
Vector3(float x, float y, float z) : x(x), y(y), z(z) { }
1515
Vector3(float *arr) : x(arr[0]), y(arr[1]), z(arr[2]) { }

source/rendering/CrossSection.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
namespace Forth
55
{
66

7-
CrossSection::CrossSection(void) {
7+
CrossSection::CrossSection(void) : Projector4() {
8+
89
Setup(Forth::Transform4::identity());
910
}
1011

@@ -92,11 +93,6 @@ void CrossSection::Project(const Buffer4 &source, const Transform4 &transform, V
9293
sides.push_back(Dot(vmw, source.vertices[i]) > vmwp);
9394
}
9495

95-
//if (source.profileCount == source.indiceCount)
96-
{
97-
98-
}
99-
//else
10096
{
10197
switch (source.simplex)
10298
{

source/rendering/Model4.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#pragma once
2+
3+
#include "../common/Buffer3.h"
4+
#include "../common/Buffer4.h"
5+
#include "../common/BufferGL.h"
6+
#include "../math/Transform4.h"
7+
#include "Projector4.h"
8+
9+
/** OpenGL Drawing Macro
10+
*
11+
* @model Model4
12+
* @vb GLuint to a vertex buffer
13+
*/
14+
#define FORTH_GL_DRAW(model, vb) \
15+
do \
16+
{ \
17+
glBindBuffer(GL_ARRAY_BUFFER, vb); \
18+
glBufferData(GL_ARRAY_BUFFER, model.driver.vb.size() * sizeof(float), &model.driver.vb[0], GL_STREAM_DRAW); \
19+
glEnableVertexAttribArray(0); \
20+
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(0)); \
21+
glEnableVertexAttribArray(1); \
22+
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void *)(3 * sizeof(float))); \
23+
glDrawArrays(GL_TRIANGLES, 0, model.driver.vb.size() / 6); \
24+
} while (0)
25+
26+
namespace Forth
27+
{
28+
class Model4
29+
{
30+
public:
31+
Buffer4 input = Buffer4();
32+
Buffer3 output = Buffer3();
33+
BufferGL driver = BufferGL();
34+
Transform4 matrix;
35+
36+
Model4() : matrix(Transform4::identity()) {}
37+
38+
void Render(Projector4 &projector)
39+
{
40+
projector.Project(input, matrix, output);
41+
this->driver.Copy(output);
42+
}
43+
};
44+
} // namespace Forth

source/rendering/Projector4.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#pragma once
2+
3+
#include "Projector4.h"
4+
5+
namespace Forth
6+
{
7+
8+
void Projector4::Project(const Buffer4 &from, const Transform4 &transform, Buffer3 &to) {
9+
Visualizer4* viz = ((Visualizer4*)defaultVisualizers[from.simplex - 1]);
10+
viz->Initialize(to);
11+
this->Project(from, transform, viz);
12+
viz->End();
13+
}
14+
15+
Projector4::Projector4()
16+
{
17+
defaultVisualizers[SM_Point] = new ParticleVisualizer();
18+
defaultVisualizers[SM_Line] = new WireVisualizer();
19+
defaultVisualizers[SM_Triangle] = new SolidVisualizer();
20+
}
21+
22+
Projector4::~Projector4()
23+
{
24+
delete defaultVisualizers[SM_Point];
25+
delete defaultVisualizers[SM_Line];
26+
delete defaultVisualizers[SM_Triangle];
27+
}
28+
}

0 commit comments

Comments
 (0)