перешел на android studio, через native jni написал новый код на C++
отрывок из gl_code.cpp
Код:
static GLuint program;
static GLuint position;
static GLuint texcoord;
static GLuint textures[1];
GLuint loadShader(GLenum shaderType, const char* pSource) {
GLuint shader = glCreateShader(shaderType);
if (shader) {
glShaderSource(shader, 1, &pSource, NULL);
glCompileShader(shader);
GLint compiled = 0;
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen) {
char* buf = (char*) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
LOGE("Could not compile shader %d:\n%s\n",
shaderType, buf);
free(buf);
}
glDeleteShader(shader);
shader = 0;
}
}
}
return shader;
}
GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
if (!vertexShader) {
return 0;
}
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
if (!pixelShader) {
return 0;
}
GLuint program = glCreateProgram();
if (program) {
glAttachShader(program, vertexShader);
checkGlError("glAttachShader");
glAttachShader(program, pixelShader);
checkGlError("glAttachShader");
glLinkProgram(program);
GLint linkStatus = GL_FALSE;
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
if (linkStatus != GL_TRUE) {
GLint bufLength = 0;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
if (bufLength) {
char* buf = (char*) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
LOGE("Could not link program:\n%s\n", buf);
free(buf);
}
}
glDeleteProgram(program);
program = 0;
}
}
return program;
}
static const char VERTEX_SHADER[] =
"attribute vec4 position;"
"attribute vec2 texcoord;"
"varying vec2 texcoordVarying;"
"void main() {"
"gl_Position = position;"
"texcoordVarying = texcoord;"
"}";
static const char FRAGMENT_SHADER[] =
"precision mediump float;"
"varying vec2 texcoordVarying;"
"uniform sampler2D texture;"
"void main() {"
"gl_FragColor = texture2D(texture, texcoordVarying);"
"}";
bool setupGraphics(int w, int h) {
printGLString("Version", GL_VERSION);
printGLString("Vendor", GL_VENDOR);
printGLString("Renderer", GL_RENDERER);
printGLString("Extensions", GL_EXTENSIONS);
LOGI("setupGraphics(%d, %d)", w, h);
program = createProgram(VERTEX_SHADER, FRAGMENT_SHADER);
if (!program) {
LOGE("Could not create program.");
}
glUseProgram(program);
checkGlError("glUseProgram");
position = glGetAttribLocation(program, "position");
checkGlError("glGetAttribLocation position");
glEnableVertexAttribArray(position);
texcoord = glGetAttribLocation(program, "texcoord");
checkGlError("glGetAttribLocation texcoord");
glEnableVertexAttribArray(texcoord);
textures[0] = glGetUniformLocation(program, "texture");
checkGlError("glGetUniformLocation texture");
unsigned int* _pixels;// = (unsigned int*) env->GetPrimitiveArrayCritical(pixels, 0);
_pixels = (unsigned int*)malloc(2*h);
// ARGB ? RGBA ???????
const int size = 2*h;
for (int i = 0; i < size; i++) {
unsigned int px = _pixels[i];
_pixels[i] = (
((px ) & 0xFF000000) | // A
((px << 16) & 0x00FF0000) | // R
((px ) & 0x0000FF00) | // G
((px >> 16) & 0x000000FF) // B
);
}
glGenTextures(1, textures);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, _pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//env->ReleasePrimitiveArrayCritical(pixels, _pixels, JNI_ABORT);
return true;
}
static const GLfloat VERTEXS[] = {
-1.0f, 1.0f, 0.0f, // ??
-1.0f, -1.0f, 0.0f, // ??
1.0f, 1.0f, 0.0f, // ??
1.0f, -1.0f, 0.0f // ??
};
static const GLfloat TEXCOORDS[] = {
0.0f, 0.0f, // ??
0.0f, 1.0f, // ??
1.0f, 0.0f, // ??
1.0f, 1.0f // ??
};
static void updatetex()
{
//LOG("signal");
//////////////////////////////////////
//////////////get framebuffer here
////////////////////////////////////////
// glGenTextures(1, textures);
// glBindTexture(GL_TEXTURE_2D, textures[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 320, 240, 0, GL_RGB, GL_UNSIGNED_BYTE, framebuffer);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
void renderFrame() {
/* static float grey;
grey += 0.01f;
if (grey > 1.0f) {
grey = 0.0f;
}
glClearColor(grey, grey, grey, 1.0f);
checkGlError("glClearColor");
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
checkGlError("glClear");*/
updatetex();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textures[0]);
glUniform1i(textures[0], 0);
glVertexAttribPointer(texcoord, 2, GL_FLOAT, false, 0, TEXCOORDS);
glVertexAttribPointer(position, 3, GL_FLOAT, false, 0, VERTEXS);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
выводит текстуры но через некоторое время падает программа с ошибкой Heap Memory fatal 11 signal win death
В чем ошибка?