summaryrefslogtreecommitdiff
path: root/Runtime/GUI/UIQuad.cpp
blob: c57685ce6344fc00adb46134a4253994a2828c9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "../Graphics/GfxDevice.h"
#include "UIQuad.h"
#include "Runtime/Math/Math.h"

struct UIQuadLayout
{
	Vector2 position;
	Vector2 uv;
};

static CustomVertexLayout layout;

InitializeStaticVariables([]() {
	VertexAttributeDescriptor POSITION = VertexAttributeDescriptor(0, 2, VertexAttrFormat_Float, sizeof(UIQuadLayout));
	VertexAttributeDescriptor UV = VertexAttributeDescriptor(sizeof(Vector2), 2, VertexAttrFormat_Float, sizeof(UIQuadLayout));

	layout.attributes.push_back(POSITION);
	layout.attributes.push_back(UV);
});

void UIQuad::Draw()
{
	const int nVerts = 4;
	const int nIndices = 6;

	float pos[] = {
		m_Left,  m_Bottom, // left-bottom
		m_Right, m_Bottom, // right-bottom
		m_Right, m_Top,    // right-top
		m_Left,  m_Top,    // top-left
	};

    int n = sizeof(Vector2);

	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(UIQuadLayout), sizeof(uint16), 4, 6, Primitive_Triangle, (void**)&vb, (void**)&ib);

	UIQuadLayout* dst = (UIQuadLayout*)vb;

	for (int i = 0; i < nVerts; ++i)
	{
		dst[i].position.Set(pos[2 * i], pos[2 * i + 1]);
		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);
}