Skip to content

Commit a8ee80e

Browse files
committed
OpenGL Improvements
- C++ New colorVary shader, colorAttribute VBO support - Cleaned up C++ Vector, Color to use .x, .y, .z instead of .x(), .y(), .z()
1 parent 8fda7bf commit a8ee80e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+784
-391
lines changed

Kaiju/Xcode/Kaiju/Kaiju.xcodeproj/project.pbxproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@
9191
CD6FEB7529170AE600F674D3 /* KaijuUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KaijuUITests.m; sourceTree = "<group>"; };
9292
CD6FEB7729170AE600F674D3 /* KaijuUITestsLaunchTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = KaijuUITestsLaunchTests.m; sourceTree = "<group>"; };
9393
CDF0FB7729EE11DA00B9EF1F /* AppDelegateBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegateBridge.swift; sourceTree = "<group>"; };
94+
CDF0FB8229EF943F00B9EF1F /* TestColorVaryScene.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestColorVaryScene.h; sourceTree = "<group>"; };
95+
CDF0FB8329EF951200B9EF1F /* TestMeshPathScene.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TestMeshPathScene.h; sourceTree = "<group>"; };
9496
/* End PBXFileReference section */
9597

9698
/* Begin PBXFrameworksBuildPhase section */
@@ -166,6 +168,7 @@
166168
CD6FEB5429170AE500F674D3 /* Kaiju */ = {
167169
isa = PBXGroup;
168170
children = (
171+
CDF0FB8029EF941700B9EF1F /* Scenes */,
169172
CD420AE82959FE6900C5D3E1 /* SDLTest.cpp */,
170173
CD420AE72959FE6900C5D3E1 /* SDLTest.h */,
171174
CD5FB84029204DD2004F2FB4 /* Views */,
@@ -203,6 +206,23 @@
203206
path = KaijuUITests;
204207
sourceTree = "<group>";
205208
};
209+
CDF0FB8029EF941700B9EF1F /* Scenes */ = {
210+
isa = PBXGroup;
211+
children = (
212+
CDF0FB8129EF941D00B9EF1F /* Test */,
213+
);
214+
path = Scenes;
215+
sourceTree = "<group>";
216+
};
217+
CDF0FB8129EF941D00B9EF1F /* Test */ = {
218+
isa = PBXGroup;
219+
children = (
220+
CDF0FB8229EF943F00B9EF1F /* TestColorVaryScene.h */,
221+
CDF0FB8329EF951200B9EF1F /* TestMeshPathScene.h */,
222+
);
223+
path = Test;
224+
sourceTree = "<group>";
225+
};
206226
/* End PBXGroup section */
207227

208228
/* Begin PBXNativeTarget section */

Kaiju/Xcode/Kaiju/Kaiju/SDLTest.cpp

Lines changed: 11 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,12 @@
1111
#include <SDL2/SDL_video.h>
1212
#include <PhaseJump/PhaseJump.h>
1313
#include <iostream>
14+
#include "TestColorVaryScene.h"
15+
#include "TestMeshPathScene.h"
1416

1517
using namespace PJ;
1618
using namespace std;
1719

18-
void foo1(std::shared_ptr<SDLWorld> world) {
19-
auto pathNode = std::make_shared<WorldNode>();
20-
auto pathComponent = std::make_shared<CirclePathLayout2D>(500);
21-
pathNode->AddComponent(pathComponent);
22-
world->Add(pathNode);
23-
24-
int count = 20;
25-
for (int i = 0; i < count; i++) {
26-
auto meshNode = std::make_shared<WorldNode>();
27-
auto meshRenderer = std::make_shared<MeshRenderer>();
28-
meshNode->AddComponent(meshRenderer);
29-
30-
float progress = (float)i/(float)count;
31-
32-
auto material = std::make_shared<RenderMaterial>();
33-
auto program = GLShaderProgram::registry["colorUniform"];
34-
if (program) {
35-
material->shaderProgram = program;
36-
material->uniformColors.Add(Color(progress, 1.0f - progress, 0, 1));
37-
38-
EllipseRenderMeshBuilder builder(Angle::DegreesAngle(5.0f), Vector2(40, 20));
39-
auto renderMesh = builder.BuildRenderMesh();
40-
meshRenderer->material = material;
41-
meshRenderer->mesh = renderMesh;
42-
}
43-
// window->World()->Add(meshNode);
44-
meshNode->transform->scale = Vector3(1.0f + progress, 1.0f + progress, 1);
45-
// meshNode->transform->rotation.z() = -(progress * 360.0f);
46-
pathNode->AddChild(meshNode);
47-
}
48-
49-
pathComponent->ApplyLayout();
50-
}
51-
52-
void foo2(std::shared_ptr<SDLWorld> world) {
53-
auto pathNode = std::make_shared<WorldNode>();
54-
auto pathComponent = std::make_shared<CirclePathLayout2D>(500);
55-
pathNode->AddComponent(pathComponent);
56-
world->Add(pathNode);
57-
58-
int count = 20;
59-
for (int i = 0; i < count; i++) {
60-
auto meshNode = std::make_shared<WorldNode>();
61-
auto meshRenderer = std::make_shared<MeshRenderer>();
62-
meshNode->AddComponent(meshRenderer);
63-
64-
float progress = (float)i/(float)count;
65-
66-
auto material = std::make_shared<RenderMaterial>();
67-
auto program = GLShaderProgram::registry["colorUniform"];
68-
if (program) {
69-
material->shaderProgram = program;
70-
material->uniformColors.Add(Color(1.0f, 0, 0, 1));
71-
72-
EllipseRenderMeshBuilder builder(Angle::DegreesAngle(5.0f), Vector2(40, 20));
73-
auto renderMesh = builder.BuildRenderMesh();
74-
meshRenderer->material = material;
75-
meshRenderer->mesh = renderMesh;
76-
}
77-
pathNode->AddChild(meshNode);
78-
}
79-
80-
pathComponent->orientToPath = false;
81-
pathComponent->ApplyLayout();
82-
}
83-
8420
bool my_tool_active = false;
8521
float my_color[4];
8622
bool isDone = false;
@@ -152,9 +88,16 @@ void SDLFoo() {
15288
window->World()->SetRenderContext(renderContext);
15389
window->World()->uiEventPoller = std::make_shared<SDLImGuiEventPoller>(*window);
15490

155-
foo1(window->World());
156-
foo2(window->World());
91+
TestMeshPathScene testMeshPathScene;
92+
testMeshPathScene.LoadInto(*window->World());
93+
94+
TestColorVaryScene testColorVaryScene;
95+
testColorVaryScene.LoadInto(*window->World());
15796

97+
// SDLLoadTextureOperation loadTexture(window->SDL_Renderer(), "resources/heart-full.png");
98+
// loadTexture.Go();
99+
// auto texture = loadTexture.texture;
100+
158101
window->World()->Go();
159102
#else
160103
auto windowConfig = SDLWindow::Configuration::native;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// TestColorVaryScene.h
3+
// Kaiju
4+
//
5+
// Created by Jeremy Vineyard on 4/18/23.
6+
//
7+
8+
#ifndef TestColorVaryScene_h
9+
#define TestColorVaryScene_h
10+
11+
#include <PhaseJump/PhaseJump.h>
12+
13+
using namespace PJ;
14+
15+
class TestColorVaryScene : public Scene {
16+
public:
17+
void LoadInto(World& world) {
18+
auto meshNode = std::make_shared<WorldNode>();
19+
auto meshRenderer = std::make_shared<MeshRenderer>();
20+
meshNode->AddComponent(meshRenderer);
21+
22+
auto material = std::make_shared<RenderMaterial>();
23+
auto program = GLShaderProgram::registry["colorVary"];
24+
if (program) {
25+
material->shaderProgram = program;
26+
material->colors.Add(Color::white);
27+
material->colors.Add(Color::black);
28+
material->colors.Add(Color::white);
29+
material->colors.Add(Color::black);
30+
31+
QuadRenderMeshBuilder builder(Vector2(400, 400));
32+
auto renderMesh = builder.BuildRenderMesh();
33+
meshRenderer->material = material;
34+
meshRenderer->mesh = renderMesh;
35+
}
36+
world.Add(meshNode);
37+
}
38+
};
39+
40+
#endif
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//
2+
// TestColorVaryScene.h
3+
// Kaiju
4+
//
5+
// Created by Jeremy Vineyard on 4/18/23.
6+
//
7+
8+
#ifndef TestMeshPathScene_h
9+
#define TestMeshPathScene_h
10+
11+
#include <PhaseJump/PhaseJump.h>
12+
13+
using namespace PJ;
14+
15+
class TestMeshPathScene : public Scene {
16+
public:
17+
void LoadInto(World& world) {
18+
foo1(world);
19+
foo2(world);
20+
}
21+
22+
void foo1(World& world) {
23+
auto pathNode = std::make_shared<WorldNode>();
24+
auto pathComponent = std::make_shared<CirclePathLayout2D>(500);
25+
pathNode->AddComponent(pathComponent);
26+
world.Add(pathNode);
27+
28+
int count = 20;
29+
for (int i = 0; i < count; i++) {
30+
auto meshNode = std::make_shared<WorldNode>();
31+
auto meshRenderer = std::make_shared<MeshRenderer>();
32+
meshNode->AddComponent(meshRenderer);
33+
34+
float progress = (float)i/(float)count;
35+
36+
auto material = std::make_shared<RenderMaterial>();
37+
auto program = GLShaderProgram::registry["colorUniform"];
38+
if (program) {
39+
material->shaderProgram = program;
40+
material->uniformColors.Add(Color(progress, 1.0f - progress, 0, 1));
41+
42+
EllipseRenderMeshBuilder builder(Angle::DegreesAngle(5.0f), Vector2(40, 20));
43+
auto renderMesh = builder.BuildRenderMesh();
44+
meshRenderer->material = material;
45+
meshRenderer->mesh = renderMesh;
46+
}
47+
// window->World()->Add(meshNode);
48+
meshNode->transform->scale = Vector3(1.0f + progress, 1.0f + progress, 1);
49+
// meshNode->transform->rotation.z = -(progress * 360.0f);
50+
pathNode->AddChild(meshNode);
51+
}
52+
53+
pathComponent->ApplyLayout();
54+
}
55+
56+
void foo2(World& world) {
57+
auto pathNode = std::make_shared<WorldNode>();
58+
auto pathComponent = std::make_shared<CirclePathLayout2D>(500);
59+
pathNode->AddComponent(pathComponent);
60+
world.Add(pathNode);
61+
62+
int count = 20;
63+
for (int i = 0; i < count; i++) {
64+
auto meshNode = std::make_shared<WorldNode>();
65+
auto meshRenderer = std::make_shared<MeshRenderer>();
66+
meshNode->AddComponent(meshRenderer);
67+
68+
float progress = (float)i/(float)count;
69+
70+
auto material = std::make_shared<RenderMaterial>();
71+
auto program = GLShaderProgram::registry["colorUniform"];
72+
if (program) {
73+
material->shaderProgram = program;
74+
material->uniformColors.Add(Color(1.0f, 0, 0, 1));
75+
76+
EllipseRenderMeshBuilder builder(Angle::DegreesAngle(5.0f), Vector2(40, 20));
77+
auto renderMesh = builder.BuildRenderMesh();
78+
meshRenderer->material = material;
79+
meshRenderer->mesh = renderMesh;
80+
}
81+
pathNode->AddChild(meshNode);
82+
}
83+
84+
pathComponent->orientToPath = false;
85+
pathComponent->ApplyLayout();
86+
}
87+
};
88+
89+
#endif
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"id": "colorUniform",
3-
"shader.vertex": "colorUniform",
3+
"shader.vertex": "vertex",
44
"shader.fragment": "colorUniform"
55
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": "colorVary",
3+
"shader.vertex": "vertex+color",
4+
"shader.fragment": "colorVary"
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Render varying color
3+
*/
4+
in vec4 v_color;
5+
6+
out vec4 fragColor;
7+
8+
void main()
9+
{
10+
fragColor = v_color;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
in vec4 a_position;
2+
in vec4 a_color;
3+
4+
uniform mat4 u_mvpMatrix;
5+
6+
out vec4 v_color;
7+
8+
/*
9+
Uniform color across vertices
10+
*/
11+
void main()
12+
{
13+
gl_Position = u_mvpMatrix * a_position;
14+
v_color = a_color;
15+
}

Kaiju/Xcode/Kaiju/resources/pj_shaders/vertex/colorUniform.vsh renamed to Kaiju/Xcode/Kaiju/resources/pj_shaders/vertex/vertex.vsh

File renamed without changes.

PhaseJump/PhaseJump/Classes/Collections/VectorList.h

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,52 @@
1010
CODE REVIEW: 11/20/22
1111
*/
1212
namespace PJ {
13+
/// An object with accessible raw memory data
14+
class SomeDataContainer {
15+
public:
16+
virtual void* Data() const = 0;
17+
virtual uint32_t DataSize() const = 0;
18+
};
19+
20+
/// A collection with accessible raw memory data
21+
template <class T>
22+
class SomeDataCollection : public SomeDataContainer {
23+
public:
24+
virtual size_t ItemCount() const = 0;
25+
virtual uint32_t ItemSize() const { return sizeof(T); }
26+
27+
uint32_t DataSize() const override { return (uint32_t)ItemCount() * ItemSize(); }
28+
};
29+
30+
template <class T>
31+
class CollectionData : public SomeDataCollection<T> {
32+
public:
33+
size_t itemCount;
34+
void* data;
35+
36+
CollectionData(size_t itemCount, void* data) : itemCount(itemCount), data(data) {
37+
}
38+
39+
size_t ItemCount() const override { return itemCount; }
40+
void* Data() const override { return data; }
41+
};
42+
1343
/// <summary>
1444
/// Extends std::vector with convenience methods
1545
/// Called VectorList to avoid confusion with Vector geometry objects.
1646
/// </summary>
17-
template <class T, class Allocator = std::allocator<T>> class VectorList : public std::vector<T> {
47+
template <class T, class Allocator = std::allocator<T>>
48+
class VectorList : public std::vector<T> {
1849
public:
1950
using Base = std::vector<T>;
2051

2152
VectorList() {
2253
}
2354

2455
constexpr VectorList(std::initializer_list<T> init,
25-
const Allocator& alloc = Allocator()) : Base(init, alloc) {
56+
const Allocator& alloc = Allocator()) : Base(init, alloc) {
57+
}
58+
explicit VectorList(typename Base::size_type count) : Base(count) {
2659
}
2760

2861
// Convenience
@@ -31,6 +64,11 @@ namespace PJ {
3164

3265
// NOTE: Avoid Remove for large lists, it is inefficient
3366
COLLECTION_METHODS(VectorList<T>, T)
67+
68+
operator CollectionData<T>() const {
69+
CollectionData<T> result(this->size(), (void*)this->data());
70+
return result;
71+
}
3472
};
3573

3674
// Convenience names

0 commit comments

Comments
 (0)