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
|
#include <llmr/renderer/painter.hpp>
#include <iostream>
#include <assert.h>
#include <llmr/util/mat4.h>
#include <llmr/platform/platform.hpp>
#include <llmr/map/transform.hpp>
#include <llmr/map/tile.hpp>
using namespace llmr;
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
GLfloat triangle_vertices[] = {
100, 100,
100, 300,
150, 150
};
GLfloat fill_vertices[] = {
0, 0,
300, 0,
0, 300,
300, 300
};
painter::painter(class platform *platform)
: platform(platform),
transform(NULL),
width(0),
height(0),
currentShader(NULL),
fillShader(NULL) {
}
void painter::setTransform(class transform *transform) {
this->transform = transform;
}
void painter::setup() {
setupShaders();
assert(fillShader);
glGenBuffers(1, &triangleVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
glGenBuffers(1, &fillVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, fillVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(fill_vertices), fill_vertices, GL_STATIC_DRAW);
// glGenVertexArraysOES(1, &vertexArray);
// glBindVertexArrayOES(vertexArray);
// glBindVertexArrayOES(0);
}
void painter::setupShaders() {
const GLchar *vertexSource, *fragmentSource;
vertexSource = platform->shaderSource("fill", "vertex.glsl");
assert((vertexSource, "Missing fill vertex shader source"));
fragmentSource = platform->shaderSource("fill", "fragment.glsl");
assert((fragmentSource, "Missing fill fragment shader source"));
fillShader = new FillShader(vertexSource, fragmentSource);
vertexSource = platform->shaderSource("line", "vertex.glsl");
assert((vertexSource, "Missing line vertex shader source"));
fragmentSource = platform->shaderSource("line", "fragment.glsl");
assert((fragmentSource, "Missing line fragment shader source"));
lineShader = new LineShader(vertexSource, fragmentSource);
}
void painter::teardown() {
glDeleteBuffers(1, &triangleVertexBuffer);
glDeleteBuffers(1, &fillVertexBuffer);
// glDeleteVertexArraysOES(1, &vertexArray);
if (fillShader) {
delete fillShader;
fillShader = NULL;
}
if (lineShader) {
delete lineShader;
lineShader = NULL;
}
}
void painter::resize(GLuint new_width, GLuint new_height) {
if (width == new_width && height == new_height) {
return;
}
fprintf(stderr, "changing viewport size to %d/%d\n", new_width, new_height);
width = new_width;
height = new_height;
}
void painter::changeMatrix() {
assert(transform);
// Initialize projection matrix
float projMatrix[16];
mat4_ortho(projMatrix, 0, width, height, 0, 1, 10);
float mvMatrix[16];
mat4_identity(mvMatrix);
mat4_translate(mvMatrix, mvMatrix, transform->x, transform->y, -1);
mat4_scale(mvMatrix, mvMatrix, transform->scale / 8, transform->scale / 8, 1);
mat4_multiply(matrix, projMatrix, mvMatrix);
}
void painter::clear() {
glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
}
void painter::render(tile *tile) {
changeMatrix();
glDisable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// fprintf(stderr, "render tile\n");
switchShader(lineShader, matrix);
glUniformMatrix4fv(lineShader->u_matrix, 1, GL_FALSE, matrix);
// glEnable(GL_BLEND);
// glVertexAttribPointer(fillShader->a_pos, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// glVertexAttribPointer(fillShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0));
// glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBuffer);
tile->lineVertex.bind();
glVertexAttribPointer(lineShader->a_pos, 2, GL_SHORT, GL_FALSE, 0, BUFFER_OFFSET(0));
glUniform4f(lineShader->u_color, 0.0f, 0.0f, 0.0f, 1.0f);
glLineWidth(1.0f);
glDrawArrays(GL_LINE_STRIP, 0, tile->lineVertex.length());
// switchShader(fillShader, matrix);
// glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBuffer);
// glVertexAttribPointer(fillShader->a_pos, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// glUniform4f(fillShader->u_color, 0.0f, 1.0f, 0.0f, 1.0f);
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
// glBindVertexArrayOES(vertexArray);
// switchShader(fillShader, matrix);
// glUniformMatrix4fv(fillShader->u_matrix, 1, GL_FALSE, matrix);
// glEnable(GL_STENCIL_TEST);
// Draw stencil mask.
// glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
// glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP);
// glStencilFunc(GL_EQUAL, 0xFF, 0xFF);
// glStencilMask(0xFF);
// glBindBuffer(GL_ARRAY_BUFFER, triangleVertexBuffer);
// glVertexAttribPointer(fillShader->a_pos, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// glUniform4f(fillShader->u_color, 0.0f, 1.0f, 0.0f, 1.0f);
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);
// // Draw covering fill.
// glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
// glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
// glStencilFunc(GL_EQUAL, 0xFF, 0xFF);
// glBindBuffer(GL_ARRAY_BUFFER, fillVertexBuffer);
// glVertexAttribPointer(fillShader->a_pos, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
// glUniform4f(fillShader->u_color, 1.0f, 0.0f, 0.0f, 1.0f);
// glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void painter::viewport() {
}
// Switches to a different shader program.
void painter::switchShader(Shader *shader, float matrix[16]) {
if (currentShader != shader) {
glUseProgram(shader->program);
// Disable all attributes from the existing shader that aren't used in
// the new shader. Note: attribute indices are *not* program specific!
if (currentShader) {
// const std::vector<GLuint> &enabled = currentShader->attributes;
// const std::vector<GLuint> &required = shader->attributes;
// TODO
// for (var i = 0; i < enabled.length; i++) {
// if (required.indexOf(enabled[i]) < 0) {
// gl.disableVertexAttribArray(enabled[i]);
// }
// }
// // Enable all attributes for the new shader.
// for (var j = 0; j < required.length; j++) {
// if (enabled.indexOf(required[j]) < 0) {
// gl.enableVertexAttribArray(required[j]);
// }
// }
} else {
// Enable all attributes for the new shader.
for (GLuint index : shader->attributes) {
glEnableVertexAttribArray(index);
}
}
}
currentShader = shader;
}
|