summaryrefslogtreecommitdiff
path: root/Runtime/Rendering/Quad.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2021-10-31 14:27:26 +0800
committerchai <chaifix@163.com>2021-10-31 14:27:26 +0800
commit601442f94fc0dcfdc5a117c5f87d90b156d53045 (patch)
treeb006bcd6a28a965a900c64f4716007fcb45eee98 /Runtime/Rendering/Quad.cpp
parent26f05c6e3dcac9995345fb5a2b031be7e3ea79e9 (diff)
+static initiator
Diffstat (limited to 'Runtime/Rendering/Quad.cpp')
-rw-r--r--Runtime/Rendering/Quad.cpp61
1 files changed, 61 insertions, 0 deletions
diff --git a/Runtime/Rendering/Quad.cpp b/Runtime/Rendering/Quad.cpp
new file mode 100644
index 0000000..b4f81ee
--- /dev/null
+++ b/Runtime/Rendering/Quad.cpp
@@ -0,0 +1,61 @@
+#include "../Math/Vector2.h"
+#include "../Math/Vector3.h"
+#include "Quad.h"
+#include "../Graphics/GfxDevice.h"
+
+struct QuadVBOLayout
+{
+ Internal::Vector3 position;
+ Internal::Vector2 uv;
+};
+
+static CustomVertexLayout layout;
+
+InitializeStaticVariables([]() {
+ VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 3, VertexAttrFormat_Float, sizeof(QuadVBOLayout));
+ VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Internal::Vector3), 2, VertexAttrFormat_Float, sizeof(QuadVBOLayout));
+
+ layout.attributes.push_back(POSITION);
+ layout.attributes.push_back(UV);
+});
+
+void Quad::Draw()
+{
+ const int nVerts = 4;
+ const int nIndices = 6;
+
+ float pos[] = {
+ m_Left, m_Bottom, 0, // left-bottom
+ m_Right, m_Bottom, 0, // right-bottom
+ m_Right, m_Top, 0, // right-top
+ m_Left, m_Top, 0, // top-left
+ };
+ float uv[] = {
+ 0, 0,
+ 1, 0,
+ 1, 1,
+ 0, 1,
+ };
+ int indices[] = {
+ 0, 1, 3, // right-top
+ 1, 2, 3, // left-bottom
+ };
+
+ uint8* vb;
+ uint16* ib;
+
+ g_SharedVBO.GetChunk(sizeof(QuadVBOLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);
+
+ QuadVBOLayout* dst = (QuadVBOLayout*)vb;
+ for (int i = 0; i < nVerts; ++i)
+ {
+ dst[i].position.Set(pos[3 * i], pos[3 * i + 1], pos[3 * i + 2]);
+ dst[i].uv.Set(uv[2 * i], uv[2 * i + 1]);
+ }
+
+ for (int i = 0; i < nIndices; ++i)
+ ib[i] = indices[i];
+
+ g_SharedVBO.ReleaseChunk(4, 6);
+ g_SharedVBO.DrawChunk(layout);
+} \ No newline at end of file