summaryrefslogtreecommitdiff
path: root/Client/Source/Graphics/DefaultVertexLayout.cpp
blob: a46810024cf2071433e171eb4e88bc9a95322cc4 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "DefaultVertexLayout.h"

namespace VertexLayout
{
	// ĬÈÏvertex layout
	static const int kVertexAttrSize[VertexAttr_Count] = {
		3 * sizeof(float),	// position
		3 * sizeof(float),	// normal
		4 * sizeof(float),	// tangent
		4 * sizeof(byte),   // color
		2 * sizeof(float),	// uv
		2 * sizeof(float),	// uv2
		2 * sizeof(float),	// uv3
		2 * sizeof(float),	// uv4
	};

	static const int kVertexAttrDimension[VertexAttr_Count] = {
		3, // position
		3, // normal
		4, // tangent
		4, // color
		2, // uv
		2, // uv2
		2, // uv3
		2, // uv4
	};

	bool IsGLVertexAttrNeedNormalized(uint attr/*, uint format*/)
	{
		if (attr == VertexAttr_Color)
			return true;
		/*
		if (format == VertexAttrFormat_Color || format == VertexAttrFormat_Byte)
			return true;
		*/
		return false;
	}

	uint GetDefaultShaderChannelFormat(uint attr)
	{
		return attr == VertexAttr_Color ? VertexAttrFormat_Color : VertexAttrFormat_Float;
	}

	uint32 GetDefaultVertexAttrSize(int attr)
	{
		return kVertexAttrSize[attr];
	}

	uint GetDefaultVertexAttrDimension(uint attr)
	{
		return kVertexAttrDimension[attr];
	}

	uint GetDefaultShaderChannelDimension(uint attr)
	{
		return attr == VertexAttr_Color ? 4 : GetDefaultVertexAttrDimension(attr);
	}

	GLenum GetDefaultVertexAttrcomponentFormat(uint attr)
	{
		uint componentFormat = GetDefaultShaderChannelFormat(attr);
		return VertexAttribute::ConvertAttrFormatToGLFormat(componentFormat);
	}

	uint32 GetDynamicChunkStride(uint32 vertexAttrMask)
	{
		uint32 stride = 0;
		for (int i = 0; i < vertexAttrMask; ++i)
		{
			if (vertexAttrMask & Mask(i))
				stride += VertexLayout::GetDefaultVertexAttrSize(i);
		}
		return stride;
	}

	static uint32 sEnabledArrays = 0;

	void SetupDefaultVertexLayout(const DefaultVertexLayout& info)
	{
		glBindBuffer(GL_ARRAY_BUFFER, info.buffer);

		for (int attrIdx = 0; attrIdx < VertexAttr_Count; ++attrIdx)
		{
			if (info.enableMask & Mask(attrIdx))
			{
				if (!sEnabledArrays & Mask(attrIdx))
					glEnableVertexAttribArray(attrIdx);
				int numCompo = info.attributes[attrIdx].componentNum;
				GLenum compoType = VertexLayout::GetDefaultVertexAttrcomponentFormat(attrIdx);
				bool normalized = VertexLayout::IsGLVertexAttrNeedNormalized(attrIdx);
				uint stride = info.attributes[attrIdx].stride;
				const void* pointer = info.attributes[attrIdx].pointer;

				glVertexAttribPointer(attrIdx, numCompo, compoType, normalized ? GL_TRUE : GL_FALSE, stride, pointer);
			}
			else if (sEnabledArrays & Mask(attrIdx))
				glDisableVertexAttribArray(attrIdx);
		}
		sEnabledArrays = info.enableMask;
	}

	void InvalidateVertexInputCache()
	{
		sEnabledArrays = 0;
		for (int attrIdx = 0; attrIdx < VertexAttr_Count; ++attrIdx)
			glDisableVertexAttribArray(attrIdx);
	}

	// Ë÷Òý±£´æÎª unsgined short (GL_UNSIGNED_SHORT)
	uint GetDefaultIndexSize()
	{
		return sizeof(uint16);
	}
	GLenum GetDefaultIndexFormat()
	{
		return GL_UNSIGNED_SHORT;
	}

}