Browse Source

Fixes for GLES2

shared-context
Mikko Mononen 11 years ago
parent
commit
d2acabf325
2 changed files with 26 additions and 17 deletions
  1. +2
    -12
      example/example_gles2.c
  2. +24
    -5
      src/nanovg_gl2.h

+ 2
- 12
example/example_gles2.c View File

@@ -17,13 +17,11 @@
// //


#include <stdio.h> #include <stdio.h>
#ifdef NANOVG_GLEW
# include <GL/glew.h>
#endif
#define GLFW_INCLUDE_ES2 #define GLFW_INCLUDE_ES2
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "nanovg.h" #include "nanovg.h"
#define NANOVG_GL2_IMPLEMENTATION #define NANOVG_GL2_IMPLEMENTATION
#define NANOVG_GLES2
#include "nanovg_gl2.h" #include "nanovg_gl2.h"
#include "demo.h" #include "demo.h"


@@ -60,10 +58,9 @@ int main()


glfwSetErrorCallback(errorcb); glfwSetErrorCallback(errorcb);


glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 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", NULL, NULL);
// window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL); // window = glfwCreateWindow(1000, 600, "NanoVG", glfwGetPrimaryMonitor(), NULL);
@@ -75,13 +72,6 @@ int main()
glfwSetKeyCallback(window, key); glfwSetKeyCallback(window, key);


glfwMakeContextCurrent(window); 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); vg = nvgCreateGL2(512, 512, NVG_ANTIALIAS);
if (vg == NULL) { if (vg == NULL) {


+ 24
- 5
src/nanovg_gl2.h View File

@@ -247,6 +247,10 @@ static int glnvg__renderCreate(void* uptr)
struct GLNVGcontext* gl = (struct GLNVGcontext*)uptr; struct GLNVGcontext* gl = (struct GLNVGcontext*)uptr;


static const char* fillVertShader = static const char* fillVertShader =
#ifdef NANOVG_GLES2
"#version 100\n"
"precision mediump float;\n"
#endif
"uniform vec2 viewSize;\n" "uniform vec2 viewSize;\n"
"attribute vec2 vertex;\n" "attribute vec2 vertex;\n"
"attribute vec2 tcoord;\n" "attribute vec2 tcoord;\n"
@@ -262,6 +266,10 @@ static int glnvg__renderCreate(void* uptr)
"}\n"; "}\n";


static const char* fillFragShaderEdgeAA = static const char* fillFragShaderEdgeAA =
#ifdef NANOVG_GLES2
"#version 100\n"
"precision mediump float;\n"
#endif
"uniform mat3 scissorMat;\n" "uniform mat3 scissorMat;\n"
"uniform vec2 scissorExt;\n" "uniform vec2 scissorExt;\n"
"uniform mat3 paintMat;\n" "uniform mat3 paintMat;\n"
@@ -326,6 +334,10 @@ static int glnvg__renderCreate(void* uptr)
"}\n"; "}\n";


static const char* fillFragShader = static const char* fillFragShader =
#ifdef NANOVG_GLES2
"#version 100\n"
"precision mediump float;\n"
#endif
"uniform mat3 scissorMat;\n" "uniform mat3 scissorMat;\n"
"uniform vec2 scissorExt;\n" "uniform vec2 scissorExt;\n"
"uniform mat3 paintMat;\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); glBindTexture(GL_TEXTURE_2D, tex->tex);


glPixelStorei(GL_UNPACK_ALIGNMENT,1); 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) if (type == NVG_TEXTURE_RGBA)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
else 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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_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); glBindTexture(GL_TEXTURE_2D, tex->tex);


glPixelStorei(GL_UNPACK_ALIGNMENT,1); 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_ROW_LENGTH, tex->width);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, x); glPixelStorei(GL_UNPACK_SKIP_PIXELS, x);
glPixelStorei(GL_UNPACK_SKIP_ROWS, y); glPixelStorei(GL_UNPACK_SKIP_ROWS, y);
#endif


if (tex->type == NVG_TEXTURE_RGBA) if (tex->type == NVG_TEXTURE_RGBA)
glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data); glTexSubImage2D(GL_TEXTURE_2D, 0, x,y, w,h, GL_RGBA, GL_UNSIGNED_BYTE, data);
else 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; return 1;
} }


Loading…
Cancel
Save