| 1294 | -------------------------------------------------------------------------------*/ |
| 1295 | |
| 1296 | void drawLine( int x1, int y1, int x2, int y2, Uint32 color, Uint8 alpha ) |
| 1297 | { |
| 1298 | // read color |
| 1299 | Uint8 r, g, b, a; |
| 1300 | getColor(color, &r, &g, &b, &a); |
| 1301 | if (!alpha) { |
| 1302 | return; |
| 1303 | } |
| 1304 | |
| 1305 | // initialize shader if needed, then bind |
| 1306 | if (!lineShader.isInitialized()) { |
| 1307 | static const char v_glsl[] = |
| 1308 | "in vec4 iPosition;" |
| 1309 | "uniform mat4 uProj;" |
| 1310 | "uniform mat4 uMatrix0;" |
| 1311 | "uniform mat4 uMatrix1;" |
| 1312 | "void main() {" |
| 1313 | "if (gl_VertexID == 0) { gl_Position = uProj * uMatrix0 * iPosition; }" |
| 1314 | "else { gl_Position = uProj * uMatrix1 * iPosition; }" |
| 1315 | "}"; |
| 1316 | |
| 1317 | static const char f_glsl[] = |
| 1318 | "uniform vec4 uColor;" |
| 1319 | "out vec4 FragColor;" |
| 1320 | "void main() {" |
| 1321 | "FragColor = uColor;" |
| 1322 | "}"; |
| 1323 | |
| 1324 | lineShader.init("line shader"); |
| 1325 | lineShader.compile(v_glsl, sizeof(v_glsl), Shader::Type::Vertex); |
| 1326 | lineShader.compile(f_glsl, sizeof(f_glsl), Shader::Type::Fragment); |
| 1327 | lineShader.bindAttribLocation("iPosition", 0); |
| 1328 | lineShader.link(); |
| 1329 | } |
| 1330 | lineShader.bind(); |
| 1331 | GL_CHECK_ERR(glEnable(GL_BLEND)); |
| 1332 | |
| 1333 | // upload color |
| 1334 | float cv[] = {r / 255.f, g / 255.f, b / 255.f, alpha / 255.f}; |
| 1335 | GL_CHECK_ERR(glUniform4fv(lineShader.uniform("uColor"), 1, cv)); |
| 1336 | |
| 1337 | vec4_t v; |
| 1338 | mat4x4 m; |
| 1339 | |
| 1340 | // projection matrix |
| 1341 | mat4x4 proj(1.f); |
| 1342 | (void)ortho(&proj, 0, xres, 0, yres, -1.f, 1.f); |
| 1343 | GL_CHECK_ERR(glUniformMatrix4fv(lineShader.uniform("uProj"), 1, GL_FALSE, (float*)&proj)); |
| 1344 | |
| 1345 | // point 1 matrix |
| 1346 | mat4x4 view0(1.f); |
| 1347 | v = {(float)x1, (float)(yres - y1), 0.f, 0.f}; |
| 1348 | (void)translate_mat(&m, &view0, &v); view0 = m; |
| 1349 | GL_CHECK_ERR(glUniformMatrix4fv(lineShader.uniform("uMatrix0"), 1, GL_FALSE, (float*)&view0)); |
| 1350 | |
| 1351 | // point 2 matrix |
| 1352 | mat4x4 view1(1.f); |
| 1353 | v = {(float)x2, (float)(yres - y2), 0.f, 0.f}; |
no test coverage detected