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

@@ -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)