emscripten support

This commit is contained in:
Konstantin Käfer
2014-01-07 16:52:23 +01:00
parent 044a7b5dcd
commit 528eaf475e
24 changed files with 406 additions and 94 deletions

View File

@@ -1,4 +1,5 @@
#include <llmr/geometry/linevertexbuffer.hpp>
#include <llmr/platform/gl.hpp>
#include <cstdio>

View File

@@ -21,7 +21,7 @@ struct pbf {
// inline pbf(const std::string& buffer);
inline bool next();
inline uint64_t varint();
template <typename T = uint32_t> inline T varint();
inline int64_t svarint();
inline std::string string();
inline float float32();
@@ -68,17 +68,18 @@ bool pbf::next()
}
uint64_t pbf::varint()
template <typename T>
T pbf::varint()
{
uint8_t byte = 0x80;
uint64_t result = 0;
T result = 0;
int bitpos;
for (bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
if (data >= end) {
fprintf(stderr, "unterminated varint, unexpected end of buffer");
exit(1);
}
result |= ((uint64_t)(byte = *data) & 0x7F) << bitpos;
result |= ((T)(byte = *data) & 0x7F) << bitpos;
data++;
}

View File

@@ -69,15 +69,15 @@ void tile::parseFeature(const uint8_t *data, uint32_t bytes) {
pbf feature(data, bytes);
while (feature.next()) {
if (feature.tag == 1) {
uint64_t id = feature.varint();
uint32_t id = feature.varint();
} else if (feature.tag == 2) {
const uint8_t *tag_end = feature.data + feature.varint();
while (feature.data < tag_end) {
uint32_t key = (uint32_t)feature.varint();
uint32_t value = (uint32_t)feature.varint();
uint32_t key = feature.varint();
uint32_t value = feature.varint();
}
} else if (feature.tag == 3) {
uint32_t type = (uint32_t)feature.varint();
uint32_t type = feature.varint();
} else if (feature.tag == 4) {
uint32_t bytes = (uint32_t)feature.varint();
loadGeometry(feature.data, bytes);

View File

@@ -5,6 +5,7 @@
#include <llmr/platform/platform.hpp>
#include <llmr/map/transform.hpp>
#include <llmr/map/tile.hpp>
#include <llmr/platform/gl.hpp>
using namespace llmr;
@@ -44,6 +45,7 @@ void painter::setup() {
assert(fillShader);
assert(lineShader);
@@ -62,20 +64,8 @@ void painter::setup() {
}
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);
fillShader = new FillShader();
lineShader = new LineShader();
}
void painter::teardown() {
@@ -108,6 +98,8 @@ void painter::resize(GLuint new_width, GLuint new_height) {
void painter::changeMatrix() {
assert(transform);
assert(width);
assert(height);
// Initialize projection matrix
float projMatrix[16];

View File

@@ -1,9 +1,14 @@
#include <llmr/renderer/shader-fill.hpp>
#include <llmr/shader/shaders.h>
#include <llmr/platform/gl.hpp>
using namespace llmr;
FillShader::FillShader(const GLchar *vertSource, const GLchar *fragSource)
: Shader(vertSource, fragSource) {
FillShader::FillShader()
: Shader(
llmr_shaders[FILL_SHADER].vertex,
llmr_shaders[FILL_SHADER].fragment
) {
if (!valid) return;
a_pos = glGetAttribLocation(program, "a_pos");

View File

@@ -1,10 +1,24 @@
#include <llmr/renderer/shader-line.hpp>
#include <llmr/shader/shaders.h>
#include <llmr/platform/gl.hpp>
#include <cstdio>
using namespace llmr;
LineShader::LineShader(const GLchar *vertSource, const GLchar *fragSource)
: Shader(vertSource, fragSource) {
if (!valid) return;
LineShader::LineShader()
: Shader(
llmr_shaders[LINE_SHADER].vertex,
llmr_shaders[LINE_SHADER].fragment
),
a_pos(42) {
if (!valid) {
fprintf(stderr, "invalid line shader\n");
return;
}
GLint pos = glGetAttribLocation(program, "a_pos");
fprintf(stderr, "a_pos: %d\n", pos);
a_pos = glGetAttribLocation(program, "a_pos");
attributes.push_back(a_pos);

View File

@@ -1,4 +1,5 @@
#include <llmr/renderer/shader.hpp>
#include <llmr/renderer/shader.hpp>
#include <llmr/platform/gl.hpp>
#include <cstdlib>
#include <cstdio>
@@ -102,7 +103,20 @@ bool Shader::compileShader(GLuint *shader, GLenum type, const GLchar *source) {
GLint status;
*shader = glCreateShader(type);
#ifdef EMSCRIPTEN
// Add WebGL GSLSL precision premable
const char *preamble = "precision mediump float;\n\n";
int preamble_length = strlen(preamble);
int source_length = strlen(source);
char *modified_source = (char *)malloc(preamble_length + source_length);
strncpy(&modified_source[0], preamble, preamble_length);
strncpy(&modified_source[preamble_length], source, source_length);
glShaderSource(*shader, 1, (const GLchar **)(&modified_source), NULL);
#else
glShaderSource(*shader, 1, &source, NULL);
#endif
glCompileShader(*shader);
#if defined(DEBUG)

View File

@@ -1,3 +1,4 @@
uniform vec4 u_color;
void main() {

13
src/shader/shaders.c Normal file
View File

@@ -0,0 +1,13 @@
// NOTE: DO NOT CHANGE THIS FILE. IT IS AUTOMATICALLY GENERATED.
#include <llmr/shader/shaders.h>
const llmr_shader_t llmr_shaders[SHADER_COUNT] = {
[FILL_SHADER] = {
.vertex = "attribute vec2 a_pos;\n\nuniform mat4 u_matrix;\n\nvoid main() {\n gl_Position = u_matrix * vec4(a_pos, 0, 1);\n}\n",
.fragment = "uniform vec4 u_color;\n\nvoid main() {\n gl_FragColor = u_color;\n}\n",
},
[LINE_SHADER] = {
.vertex = "attribute vec2 a_pos;\n\nuniform mat4 u_matrix;\n\nvoid main() {\n float z = step(32767.0, a_pos.x) * 2.0;\n gl_Position = u_matrix * vec4(a_pos, z, 1);\n }\n",
.fragment = "uniform vec4 u_color;\n\nvoid main() {\n gl_FragColor = u_color;\n}\n",
}
};