1/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
2
3// calls to this function will be removed by the optimizer unless the size is too small
4void CVertexArray::CheckInitSize(const unsigned int vertexes, const unsigned int strips) {
5	if(vertexes>VA_INIT_VERTEXES || strips>VA_INIT_STRIPS) {
6		handleerror(drawArrayPos=NULL, "Vertex array initial size is too small", "Rendering error", MBF_OK | MBF_EXCL);
7	}
8}
9
10void CVertexArray::CheckEnlargeDrawArray() {
11	if ((char*) drawArrayPos > ((char*) drawArraySize - 10 * sizeof(float)))
12		EnlargeDrawArray();
13}
14
15void CVertexArray::EnlargeArrays(const unsigned int vertexes, const unsigned int strips, const unsigned int stripsize) {
16	while ((char*) drawArrayPos > ((char*) drawArraySize - stripsize * sizeof(float) * vertexes))
17		EnlargeDrawArray();
18
19	while ((char*) stripArrayPos > ((char*) stripArraySize - sizeof(unsigned int) * strips))
20		EnlargeStripArray();
21}
22
23
24//////////////////////////////////////////////////////////////////////
25//
26//////////////////////////////////////////////////////////////////////
27
28#ifdef DEBUG
29	#define ASSERT_SIZE(x) assert(drawArrayPos + x <= drawArraySize);
30#else
31	#define ASSERT_SIZE(x)
32#endif
33
34
35
36void CVertexArray::AddVertexQ0(const float3& pos) {
37	ASSERT_SIZE(VA_SIZE_0)
38	VA_TYPE_0* vat = GetTypedVertexArrayQ<VA_TYPE_0>(1);
39	vat->p = pos;
40}
41
42void CVertexArray::AddVertexQ0(float x, float y, float z) {
43	ASSERT_SIZE(VA_SIZE_0)
44	VA_TYPE_0* vat = GetTypedVertexArrayQ<VA_TYPE_0>(1);
45	vat->p.x = x;
46	vat->p.y = y;
47	vat->p.z = z;
48}
49
50void CVertexArray::AddVertexQN(const float3& pos, const float3& normal) {
51	ASSERT_SIZE(VA_SIZE_N)
52	VA_TYPE_N* vat = GetTypedVertexArrayQ<VA_TYPE_N>(1);
53	vat->p = pos;
54	vat->n = normal;
55}
56
57void CVertexArray::AddVertexQC(const float3& pos, const unsigned char* color) {
58	ASSERT_SIZE(VA_SIZE_C)
59	VA_TYPE_C* vat = GetTypedVertexArrayQ<VA_TYPE_C>(1);
60	vat->p = pos;
61	vat->c = SColor(color);
62}
63
64void CVertexArray::AddVertexQT(const float3& pos, float tx, float ty) {
65	ASSERT_SIZE(VA_SIZE_T)
66	VA_TYPE_T* vat = GetTypedVertexArrayQ<VA_TYPE_T>(1);
67	vat->p = pos;
68	vat->s = tx;
69	vat->t = ty;
70}
71
72void CVertexArray::AddVertexQTN(const float3& pos, float tx, float ty, const float3& norm) {
73	ASSERT_SIZE(VA_SIZE_TN)
74	VA_TYPE_TN* vat = GetTypedVertexArrayQ<VA_TYPE_TN>(1);
75	vat->p = pos;
76	vat->s = tx;
77	vat->t = ty;
78	vat->n = norm;
79}
80
81void CVertexArray::AddVertexQTNT(const float3& p, float tx, float ty, const float3& n, const float3& st, const float3& tt) {
82	ASSERT_SIZE(VA_SIZE_TNT)
83	VA_TYPE_TNT* vat = GetTypedVertexArrayQ<VA_TYPE_TNT>(1);
84	vat->p = p;
85	vat->s = tx;
86	vat->t = ty;
87	vat->n = n;
88	vat->uv1 = st;
89	vat->uv2 = tt;
90}
91
92void CVertexArray::AddVertexQTC(const float3& pos, float tx, float ty, const unsigned char* col) {
93	ASSERT_SIZE(VA_SIZE_TC)
94	VA_TYPE_TC* vat = GetTypedVertexArrayQ<VA_TYPE_TC>(1);
95	vat->p = pos;
96	vat->s = tx;
97	vat->t = ty;
98	vat->c = SColor(col);
99}
100
101void CVertexArray::AddVertexQ2d0(float x, float z) {
102	ASSERT_SIZE(VA_SIZE_2D0)
103	VA_TYPE_2d0* vat = GetTypedVertexArrayQ<VA_TYPE_2d0>(1);
104	vat->x = x;
105	vat->y = z;
106}
107
108void CVertexArray::AddVertexQ2dT(float x, float y, float tx, float ty) {
109	ASSERT_SIZE(VA_SIZE_2DT)
110	VA_TYPE_2dT* vat = GetTypedVertexArrayQ<VA_TYPE_2dT>(1);
111	vat->x = x;
112	vat->y = y;
113	vat->s = tx;
114	vat->t = ty;
115}
116
117void CVertexArray::AddVertexQ2dTC(float x, float y, float tx, float ty, const unsigned char* c) {
118	ASSERT_SIZE(VA_SIZE_2DT)
119	VA_TYPE_2dTC* vat = GetTypedVertexArrayQ<VA_TYPE_2dTC>(1);
120	vat->x = x;
121	vat->y = y;
122	vat->s = tx;
123	vat->t = ty;
124	vat->c = SColor(c);
125}
126
127
128
129//////////////////////////////////////////////////////////////////////
130//
131//////////////////////////////////////////////////////////////////////
132
133void CVertexArray::AddVertex0(const float3& pos) {
134	CheckEnlargeDrawArray();
135	AddVertexQ0(pos);
136}
137
138void CVertexArray::AddVertex0(float x, float y, float z) {
139	CheckEnlargeDrawArray();
140	AddVertexQ0(x,y,z);
141}
142
143void CVertexArray::AddVertexN(const float3& pos, const float3& normal) {
144	CheckEnlargeDrawArray();
145	AddVertexQN(pos, normal);
146}
147
148void CVertexArray::AddVertexC(const float3& pos, const unsigned char* color) {
149	CheckEnlargeDrawArray();
150	AddVertexQC(pos, color);
151}
152
153void CVertexArray::AddVertexT(const float3& pos, float tx, float ty) {
154	CheckEnlargeDrawArray();
155	AddVertexQT(pos, tx, ty);
156}
157
158void CVertexArray::AddVertexTN(const float3& pos, float tx, float ty, const float3& norm) {
159	CheckEnlargeDrawArray();
160	AddVertexQTN(pos, tx, ty, norm);
161}
162
163void CVertexArray::AddVertexTNT(const float3& p, float tx, float ty, const float3& n, const float3& st, const float3& tt) {
164	CheckEnlargeDrawArray();
165	AddVertexQTNT(p, tx, ty, n, st, tt);
166}
167
168void CVertexArray::AddVertexTC(const float3& pos, float tx, float ty, const unsigned char* col) {
169	CheckEnlargeDrawArray();
170	AddVertexQTC(pos, tx, ty, col);
171}
172
173void CVertexArray::AddVertex2d0(float x, float z) {
174	CheckEnlargeDrawArray();
175	AddVertexQ2d0(x,z);
176}
177
178void CVertexArray::AddVertex2dT(float x, float y, float tx, float ty) {
179	CheckEnlargeDrawArray();
180	AddVertexQ2dT(x, y, tx, ty);
181}
182
183void CVertexArray::AddVertex2dTC(float x, float y, float tx, float ty, const unsigned char* col) {
184	CheckEnlargeDrawArray();
185	AddVertexQ2dTC(x, y, tx, ty, col);
186}
187
188
189//////////////////////////////////////////////////////////////////////
190//
191//////////////////////////////////////////////////////////////////////
192
193void CVertexArray::CheckEndStrip() {
194	if (stripArrayPos == stripArray || ((ptrdiff_t) * (stripArrayPos - 1)) != ((char*) drawArrayPos - (char*) drawArray))
195		EndStrip();
196}
197
198