diff --git a/example/example_gles2.c b/example/example_gles2.c index aa1b495..c57feff 100644 --- a/example/example_gles2.c +++ b/example/example_gles2.c @@ -17,13 +17,11 @@ // #include -#ifdef NANOVG_GLEW -# include -#endif #define GLFW_INCLUDE_ES2 #include #include "nanovg.h" #define NANOVG_GL2_IMPLEMENTATION +#define NANOVG_GLES2 #include "nanovg_gl2.h" #include "demo.h" @@ -60,10 +58,9 @@ int main() glfwSetErrorCallback(errorcb); + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); -// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); -// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); window = glfwCreateWindow(1000, 600, "NanoVG", NULL, NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); @@ -75,13 +72,6 @@ int main() glfwSetKeyCallback(window, key); glfwMakeContextCurrent(window); -#ifdef NANOVG_GLEW - glewExperimental = GL_TRUE; - if(glewInit() != GLEW_OK) { - printf("Could not init glew.\n"); - return -1; - } -#endif vg = nvgCreateGL2(512, 512, NVG_ANTIALIAS); if (vg == NULL) { diff --git a/src/nanovg_gl2.h b/src/nanovg_gl2.h index cd534ca..d0194f8 100644 --- a/src/nanovg_gl2.h +++ b/src/nanovg_gl2.h @@ -247,6 +247,10 @@ static int glnvg__renderCreate(void* uptr) struct GLNVGcontext* gl = (struct GLNVGcontext*)uptr; static const char* fillVertShader = +#ifdef NANOVG_GLES2 + "#version 100\n" + "precision mediump float;\n" +#endif "uniform vec2 viewSize;\n" "attribute vec2 vertex;\n" "attribute vec2 tcoord;\n" @@ -262,6 +266,10 @@ static int glnvg__renderCreate(void* uptr) "}\n"; static const char* fillFragShaderEdgeAA = +#ifdef NANOVG_GLES2 + "#version 100\n" + "precision mediump float;\n" +#endif "uniform mat3 scissorMat;\n" "uniform vec2 scissorExt;\n" "uniform mat3 paintMat;\n" @@ -326,6 +334,10 @@ static int glnvg__renderCreate(void* uptr) "}\n"; static const char* fillFragShader = +#ifdef NANOVG_GLES2 + "#version 100\n" + "precision mediump float;\n" +#endif "uniform mat3 scissorMat;\n" "uniform vec2 scissorExt;\n" "uniform mat3 paintMat;\n" @@ -415,14 +427,11 @@ static int glnvg__renderCreateTexture(void* uptr, int type, int w, int h, const glBindTexture(GL_TEXTURE_2D, tex->tex); glPixelStorei(GL_UNPACK_ALIGNMENT,1); - glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width); - glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); - glPixelStorei(GL_UNPACK_SKIP_ROWS, 0); if (type == NVG_TEXTURE_RGBA) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); else - glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_RED, GL_UNSIGNED_BYTE, data); + glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); @@ -448,14 +457,24 @@ static int glnvg__renderUpdateTexture(void* uptr, int image, int x, int y, int w glBindTexture(GL_TEXTURE_2D, tex->tex); glPixelStorei(GL_UNPACK_ALIGNMENT,1); +#ifdef NANOVG_GLES2 + // No support for all of unpack, need to update a whole row at a time. + if (tex->type == NVG_TEXTURE_RGBA) + data += y*tex->width*4; + else + data += y*tex->width; + x = 0; + w = tex->width; +#else glPixelStorei(GL_UNPACK_ROW_LENGTH, tex->width); glPixelStorei(GL_UNPACK_SKIP_PIXELS, x); glPixelStorei(GL_UNPACK_SKIP_ROWS, y); +#endif if (tex->type == NVG_TEXTURE_RGBA) glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data); else - glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RED, GL_UNSIGNED_BYTE, data); + glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_LUMINANCE, GL_UNSIGNED_BYTE, data); return 1; }