-
Notifications
You must be signed in to change notification settings - Fork 5
/
poSimpleDrawing.cpp
346 lines (275 loc) · 9.66 KB
/
poSimpleDrawing.cpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* Copyright 2012 Potion Design. All rights reserved.
* This file is part of pocode.
*
* pocode is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* pocode is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with pocode. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "poSimpleDrawing.h"
#include "poHelpers.h"
#include "poTexture.h"
#include "poBitmapFont.h"
#include "poOpenGLState.h"
#include "poApplication.h"
#include "poLineExtruder.h"
#include "poResourceStore.h"
#include <cfloat>
#include <utf8.h>
#include <boost/foreach.hpp>
#include <boost/assign/list_of.hpp>
void po::drawLine(poPoint a, poPoint b) {
a = floor(a) + poPoint(0.5f, 0.5f);
b = floor(b) + poPoint(0.5f, 0.5f);
GLfloat points[2*3] = {
a.x, a.y, a.z,
b.x, b.y, b.z
};
use2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, points);
glDrawArrays(GL_LINES, 0, 2);
glDisableVertexAttribArray(0);
}
void po::drawString(const std::string &str, poFont *font, poPoint pos, int ptSize, float tracking) {
if(ptSize > 0)
font->setPointSize(ptSize);
font->setGlyph(' ');
float spacer = font->getGlyphAdvance().x * tracking;
poBitmapFont *bmpFont = poGetBitmapFont(font, ptSize);
std::string::const_iterator ch = str.begin();
while(ch != str.end()) {
uint codepoint = utf8::next(ch, str.end());
font->setGlyph(codepoint);
poPoint adv = font->getGlyphAdvance();
poPoint org = round(pos+font->getGlyphBearing());
org.y += font->getAscender();
bmpFont->drawGlyph( codepoint, org );
pos.x += adv.x * tracking;
}
}
namespace {
void drawQuad(GLenum type, float x, float y, float w, float h) {
x = floor(x) + .5f;
y = floor(y) + .5f;
w = floor(w);
h = floor(h);
GLfloat quad[4*3] = {
x, y, 0,
x, y+h, 0,
x+w, y, 0,
x+w, y+h, 0
};
if(type == GL_LINE_STRIP || type == GL_LINE_LOOP)
std::swap(quad[7], quad[10]);
po::use2DShader();
po::updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, quad);
glDrawArrays(type, 0, 4);
glDisableVertexAttribArray(0);
}
}
void po::drawStrokedRect(float x, float y, float w, float h) {
drawQuad(GL_LINE_LOOP,x,y,w,h);
}
void po::drawStrokedRect(poRect rect) {
drawStrokedRect(rect.x, rect.y, rect.width, rect.height);
}
void po::drawFilledRect(float x, float y, float w, float h) {
drawQuad(GL_TRIANGLE_STRIP, x,y,w,h);
}
void po::drawFilledRect(poRect rect) {
drawFilledRect(rect.x, rect.y, rect.width, rect.height);
}
void po::drawFilledCircle(float x, float y, float rad) {
int segs = 10.f * sqrtf(rad);
float theta = 2.f * M_PI / (float)segs;
float tan_t = tanf(theta);
float cos_t = cosf(theta);
float x1 = rad;
float y1 = 0.f;
std::vector<poPoint> pt;
for(int i=0; i<segs; ++i) {
pt.push_back(poPoint(x1+x, y1+y));
float tx = -y1;
float ty = x1;
x1 = (x1 + tx * tan_t) * cos_t;
y1 = (y1 + ty * tan_t) * cos_t;
}
drawPoints(pt, GL_TRIANGLE_FAN);
}
void po::drawTexturedRect(poTexture *tex) {
drawTexturedRect(tex, poRect(0,0,tex->getWidth(),tex->getHeight()), poRect(0,0,1,1));
}
void po::drawTexturedRect(poTexture *tex, float x, float y, float w, float h) {
drawTexturedRect(tex, poRect(x,y,w,h), poRect(0,0,1,1));
}
void po::drawTexturedRect(poTexture *tex, poRect rect) {
drawTexturedRect(tex, rect, poRect(0,0,1,1));
}
void po::drawTexturedRect(poTexture *tex, poRect rect, poTextureFitOption fit, poAlignment align) {
std::vector<poPoint> coords = textureFit(rect, tex, fit, align);
poRect coords_rect(coords[0], coords[2]);
drawTexturedRect(tex, rect, coords_rect);
}
void po::drawTexturedRect(poTexture *tex, poRect rect, poRect coords) {
GLfloat quad[4*3] = {
rect.x, rect.y, 0,
rect.x+rect.width, rect.y, 0,
rect.x+rect.width, rect.y+rect.height, 0,
rect.x, rect.y+rect.height, 0,
};
GLfloat tcoords[4*2] = {
coords.x, coords.y+coords.height,
coords.x+coords.width, coords.y+coords.height,
coords.x+coords.width, coords.y,
coords.x, coords.y,
};
useTexture(tex->getUid(), tex->hasAlpha());
if(tex->getConfig().internalFormat == GL_ALPHA)
useTex2DMaskShader();
else
useTex2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, quad);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, tcoords);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
void po::drawStrokedPolygon(const std::vector<poPoint> &points) {
drawPoints(points, GL_LINE_LOOP);
}
void po::drawStrokedPolygon(const std::vector<poPoint> &points, const std::vector<unsigned short> &indices) {
drawPoints(points, indices, GL_LINE_LOOP);
}
void po::drawFilledPolygon(const std::vector<poPoint> &points) {
drawPoints(points, GL_TRIANGLE_FAN);
}
void po::drawFilledPolygon(const std::vector<poPoint> &points, const std::vector<unsigned short> &indices) {
drawPoints(points, indices, GL_TRIANGLE_FAN);
}
void po::drawTexturedPolygon(const std::vector<poPoint> &points, poTexture *tex, const std::vector<poPoint> &texCoords) {
drawPoints(points, tex, texCoords, GL_TRIANGLE_FAN);
}
void po::drawTexturedPolygon(const std::vector<poPoint> &points, const std::vector<unsigned short> &indices, poTexture *tex, const std::vector<poPoint> &texCoords) {
drawPoints(points, indices, tex, texCoords, GL_TRIANGLE_FAN);
}
void po::drawTexturedPolygon(const std::vector<poPoint> &points, poTexture *tex, poTextureFitOption fit, poAlignment align) {
if(points.empty())
return;
poRect bounds( points[0].x, points[0].y, 0, 0 );
BOOST_FOREACH(poPoint p, points) {
bounds.include(p);
}
std::vector<poPoint> coords(points.size());
textureFit(bounds, tex, fit, align, coords, points);
po::drawTexturedPolygon(points, tex, coords);
}
void po::drawPoints(const std::vector<poPoint> &points, GLenum type) {
use2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0]);
glDrawArrays(type, 0, points.size());
glDisableVertexAttribArray(0);
}
void po::drawPoints(const std::vector<poPoint> &points, const std::vector<unsigned short> &indices, GLenum type) {
use2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0]);
glDrawElements(type, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
glDisableVertexAttribArray(0);
}
void po::drawPoints(const std::vector<poPoint> &points, poTexture *tex, const std::vector<poPoint> &texCoords, GLenum type) {
useTexture(tex->getUid(), tex->hasAlpha());
useTex2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0].x);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(poPoint), &texCoords[0].x);
glDrawArrays(type, 0, points.size());
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
void po::drawPoints(const std::vector<poPoint> &points, const std::vector<unsigned short> &indices, poTexture *tex, const std::vector<poPoint> &texCoords, GLenum type) {
useTexture(tex->getUid(), tex->hasAlpha());
useTex2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &points[0]);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(poPoint), &texCoords[0]);
glDrawElements(type, indices.size(), GL_UNSIGNED_SHORT, &indices[0]);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
void po::drawPoints(poPoint* pt, int count, GLenum type) {
use2DShader();
updateActiveShader();
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, &pt[0].x);
glDrawArrays(type, 0, count);
glDisableVertexAttribArray(0);
}
std::vector<poPoint> po::generateStroke(std::vector<poPoint> &points,
int strokeWidth,
bool close,
poStrokePlacementProperty place,
poStrokeJoinProperty join,
poStrokeCapProperty cap)
{
std::vector<poPoint> stroke;
std::vector<poExtrudedLineSeg> segments;
poPoint p1, p2, p3, p4, tmp;
for(uint i=0; i<points.size()-1; i++) {
p1 = points[i];
p2 = points[i+1];
segments.push_back(poExtrudedLineSeg(p1, p2, strokeWidth, place));
}
if(close) {
segments.push_back(poExtrudedLineSeg(points.back(), points.front(), strokeWidth, place));
makeStrokeForJoint(stroke, segments.back(), segments.front(), join, strokeWidth);
}
else {
// add the first cap
stroke.push_back(segments[0].p2);
stroke.push_back(segments[0].p1);
}
// generate middle points
for(uint i=0; i<segments.size()-1; i++) {
makeStrokeForJoint(stroke, segments[i], segments[i+1], join, strokeWidth);
}
if(close) {
makeStrokeForJoint(stroke, segments.back(), segments.front(), join, strokeWidth);
}
else {
stroke.push_back(segments.back().p4);
stroke.push_back(segments.back().p3);
}
return stroke;
}
std::vector<poPoint> po::generateOval(float xRad, float yRad, uint resolution) {
std::vector<poPoint> response;
for(uint i=0; i<resolution; i++) {
float phase = (i / float(resolution)) * (M_PI*2);
response.push_back(poPoint(cosf(phase)*xRad, sinf(phase)*yRad, 0.f));
}
return response;
}