Android: Framebuffer missing attachment - android

I'm trying to create a render to texture class that has a framebuffer for position and a framebuffer for rotation. That part works great. But when I switch out and back to my app it crashes because checking the framebuffer status returns frameBuffer incomplete missing attachment.
public void setup(Context context) {
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = 1.5f;
final float lookX = 0.0f;
final float lookY = 0.0f;
final float lookZ = -5.0f;
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER,
ShaderHelper.readShader(context, R.raw.vertex_shader));
int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER,
ShaderHelper.readShader(context, R.raw.fragment_shader));
mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
new String[] {"a_Position", "a_TexCoord"});
vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER,
ShaderHelper.readShader(context, R.raw.particle_vertex_shader));
fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER,
ShaderHelper.readShader(context, R.raw.particle_fragment_shader));
mPartProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
new String[] {"a_Position"});
// create the ints for the framebuffer, depth render buffer and texture
final int[] fb = new int[2];
// generate
GLES20.glGenFramebuffers(2, fb, 0);
if (!setup) {
posTex = ShaderHelper.genTexture(texW, texH, null);
velTex = ShaderHelper.genTexture(texW, texH, null);
rotTex = ShaderHelper.genTexture(texW, texH, null);
rotVelTex = ShaderHelper.genTexture(texW, texH, null);
atlasTex = ShaderHelper.loadTexture(context, R.drawable.ship_atlas);
}
posFb = fb[0];
rotFb = fb[1];
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, posFb);
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, posTex, 0);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, rotFb);
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, rotTex, 0);
final int buffers[] = new int[2];
GLES20.glGenBuffers(2, buffers, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, spriteBuffer.capacity() * BYTES_PER_FLOAT, spriteBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, renderQuad.capacity() * BYTES_PER_FLOAT, renderQuad, GLES20.GL_STATIC_DRAW);
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
mAtlasTexHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_TexAtlas");
mPosTexHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_PosTexture");
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
mTexCoordHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoord");
mXTexHandle = GLES20.glGetUniformLocation(mPartProgramHandle, "u_Values");
mDXTexHandle = GLES20.glGetUniformLocation(mPartProgramHandle, "u_Transforms");
mPassHandle = GLES20.glGetUniformLocation(mPartProgramHandle, "u_Pass");
mFBPositionHandle = GLES20.glGetAttribLocation(mPartProgramHandle, "a_Position");
spriteBufferIdx = buffers[0];
fbQuadBufferIdx = buffers[1];
setup = true;
}
From what I have researched, textures do not need to be reloaded and only the framebuffers do. Which is good because then I would have to somehow put the pixels from the old texture into the new texture. But without remaking the textures it crashes with the framebuffer error. To make it clear, the code is ran everytime onSurfaceChanged is called.
How can I make it so I dont have to generate a new texture each time onSurfaceChanged is called without getting a FrameBuffer Incomplete Missing Attachment error?

Since android recreates the context every time it is paused I needed to add this line
glSurfaceView.setPreserveEGLContextOnPause(true);

Related

I'm trying to draw shapes using opengles in Android Native Activity. But it's not drawn [duplicate]

I'm developing my game using opengles1.0 in Native Activity.
However, source code, which used to work well, doesn't work.
Nothing is drawing on the screen, but I want to know the cause.
This is my source code.
static int engine_init_display(struct engine* engine) {
// initialize OpenGL ES and EGL
/*
* Here specify the attributes of the desired configuration.
* Below, we select an EGLConfig with at least 8 bits per color
* component compatible with on-screen windows
*/
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
EGLint w, h, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);[
eglInitialize(display, 0, 0);
/* Here, the application chooses the configuration it desires. In this
* sample, we have a very simplified selection process, where we pick
* the first EGLConfig that matches our criteria */
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
* As soon as we picked a EGLConfig, we can safely reconfigure the
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
engine->display = display;
engine->context = context;
engine->surface = surface;
engine->width = w;
engine->height = h;
engine->state.angle = 0;
renderModule = RenderModule::getInstance();
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager;
glManager = GLManager::getInstance();
glManager->engin = engine;
glManager->setAssetManager(engine->app->activity->assetManager);
ScreenManager* screenManager = glManager->screen();
screenManager->scrWidth = engine->width;
screenManager->scrHeight = engine->height;
screenManager->engine = engine;
renderModule->setGLManager(glManager);
renderModule->rendererSetting();
...
return 0;
}
draw_frame function
static void engine_draw_frame(struct engine* engine) {
if (engine->display == NULL) {
// No display.
return;
}
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager = GLManager::getInstance();
ScreenManager* screenManager = glManager->screen();
GLfloat x = screenManager->getOGLX(engine->state.x[0]);
GLfloat y = screenManager->getOGLY(engine->state.y[0]);
//GLuint scrX = glManager->screen()->getScrX(x);
//GLuint scrY = glManager->screen()->getScrY(y);
if (screenTouched) {
renderModule->setFakeMode(true);
}
renderModule->render();
if (screenTouched) {
screenTouched = false;
glReadPixels(engine->state.x[pointerIndex],
screenManager->scrHeight - engine->state.y[pointerIndex],
1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
renderModule->setFakeMode(false);
renderModule->render();
}
glClear(GL_DEPTH_BUFFER_BIT);
eglSwapBuffers(engine->display, engine->surface);
}
rendererSetting function
void RenderModule::rendererSetting() {
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
/*
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
*/
...
}
and setupViewPort function
void RenderModule::setupViewPort(GLuint width, GLuint height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = (GLfloat)((GLfloat)width * 1.0f) / ((GLfloat)height * 1.0f);
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 200, //eyeXYZ
0.0f, 0.0f, 0.0f, //centerXYZ
0.0f, 1.0f, 0.0f);
viewportSetUp = true;
}
finally
gluLookAt function
void gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ,
GLfloat centreX, GLfloat centreY, GLfloat centreZ, GLfloat upX, GLfloat upY, GLfloat upZ)
{
GLfloat f[3] =
{
centreX - eyeX,
centreY - eyeY,
centreZ - eyeZ,
};
// make f and the up vector have unit length
GLfloat lengthOfF = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]);
f[0] /= lengthOfF;
f[1] /= lengthOfF;
f[2] /= lengthOfF;
GLfloat lengthOfUp = sqrt(upX*upX + upY*upY + upZ*upZ);
upX /= lengthOfUp;
upY /= lengthOfUp;
upZ /= lengthOfUp;
// use the cross product of f and Up to get s,
// and the cross product of s and f to get u
GLfloat s[3] =
{
f[1] * upZ - upY*f[2],
f[2] * upX - upZ*f[0],
f[0] * upY - upX*f[1]
};
GLfloat u[3] =
{
s[1] * f[2] - f[1] * s[2],
s[2] * f[0] - f[2] * s[0],
s[0] * f[1] - f[0] * s[1]
};
// Fill the matrix as prescribed.
// Note: OpenGL is "column major"
GLfloat M[16] =
{
s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
glMultMatrixf(M);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
In my opinion, there is a problem with renderSeting function and setupViewPort function. I would like to ask for help to resolve this issue.
If the distance from the center of the object is 200, but the size of the radius of the object is 1000, you are inside the object.
The distance to the back of the object is 1200 (1000+200). Since the distance to the near plane is 1000,
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
the object by the far plane of the viewing frustum.

I'm trying to draw objects using opengles in Android Native-activity, but I don't see anything

I'm developing my game using opengles1.0 in Native Activity.
However, source code, which used to work well, doesn't work.
Nothing is drawing on the screen, but I want to know the cause.
This is my source code.
static int engine_init_display(struct engine* engine) {
// initialize OpenGL ES and EGL
/*
* Here specify the attributes of the desired configuration.
* Below, we select an EGLConfig with at least 8 bits per color
* component compatible with on-screen windows
*/
const EGLint attribs[] = {
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_NONE
};
EGLint w, h, format;
EGLint numConfigs;
EGLConfig config;
EGLSurface surface;
EGLContext context;
EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);[
eglInitialize(display, 0, 0);
/* Here, the application chooses the configuration it desires. In this
* sample, we have a very simplified selection process, where we pick
* the first EGLConfig that matches our criteria */
eglChooseConfig(display, attribs, &config, 1, &numConfigs);
/* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is
* guaranteed to be accepted by ANativeWindow_setBuffersGeometry().
* As soon as we picked a EGLConfig, we can safely reconfigure the
* ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */
eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);
surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);
context = eglCreateContext(display, config, NULL, NULL);
if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {
LOGW("Unable to eglMakeCurrent");
return -1;
}
eglQuerySurface(display, surface, EGL_WIDTH, &w);
eglQuerySurface(display, surface, EGL_HEIGHT, &h);
engine->display = display;
engine->context = context;
engine->surface = surface;
engine->width = w;
engine->height = h;
engine->state.angle = 0;
renderModule = RenderModule::getInstance();
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager;
glManager = GLManager::getInstance();
glManager->engin = engine;
glManager->setAssetManager(engine->app->activity->assetManager);
ScreenManager* screenManager = glManager->screen();
screenManager->scrWidth = engine->width;
screenManager->scrHeight = engine->height;
screenManager->engine = engine;
renderModule->setGLManager(glManager);
renderModule->rendererSetting();
...
return 0;
}
draw_frame function
static void engine_draw_frame(struct engine* engine) {
if (engine->display == NULL) {
// No display.
return;
}
renderModule->setupViewPort(engine->width, engine->height);
GLManager* glManager = GLManager::getInstance();
ScreenManager* screenManager = glManager->screen();
GLfloat x = screenManager->getOGLX(engine->state.x[0]);
GLfloat y = screenManager->getOGLY(engine->state.y[0]);
//GLuint scrX = glManager->screen()->getScrX(x);
//GLuint scrY = glManager->screen()->getScrY(y);
if (screenTouched) {
renderModule->setFakeMode(true);
}
renderModule->render();
if (screenTouched) {
screenTouched = false;
glReadPixels(engine->state.x[pointerIndex],
screenManager->scrHeight - engine->state.y[pointerIndex],
1, 1, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
renderModule->setFakeMode(false);
renderModule->render();
}
glClear(GL_DEPTH_BUFFER_BIT);
eglSwapBuffers(engine->display, engine->surface);
}
rendererSetting function
void RenderModule::rendererSetting() {
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
/*
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glEnable(GL_CULL_FACE);
glShadeModel(GL_SMOOTH);
*/
...
}
and setupViewPort function
void RenderModule::setupViewPort(GLuint width, GLuint height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat ratio = (GLfloat)((GLfloat)width * 1.0f) / ((GLfloat)height * 1.0f);
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 200, //eyeXYZ
0.0f, 0.0f, 0.0f, //centerXYZ
0.0f, 1.0f, 0.0f);
viewportSetUp = true;
}
finally
gluLookAt function
void gluLookAt(GLfloat eyeX, GLfloat eyeY, GLfloat eyeZ,
GLfloat centreX, GLfloat centreY, GLfloat centreZ, GLfloat upX, GLfloat upY, GLfloat upZ)
{
GLfloat f[3] =
{
centreX - eyeX,
centreY - eyeY,
centreZ - eyeZ,
};
// make f and the up vector have unit length
GLfloat lengthOfF = sqrt(f[0] * f[0] + f[1] * f[1] + f[2] * f[2]);
f[0] /= lengthOfF;
f[1] /= lengthOfF;
f[2] /= lengthOfF;
GLfloat lengthOfUp = sqrt(upX*upX + upY*upY + upZ*upZ);
upX /= lengthOfUp;
upY /= lengthOfUp;
upZ /= lengthOfUp;
// use the cross product of f and Up to get s,
// and the cross product of s and f to get u
GLfloat s[3] =
{
f[1] * upZ - upY*f[2],
f[2] * upX - upZ*f[0],
f[0] * upY - upX*f[1]
};
GLfloat u[3] =
{
s[1] * f[2] - f[1] * s[2],
s[2] * f[0] - f[2] * s[0],
s[0] * f[1] - f[0] * s[1]
};
// Fill the matrix as prescribed.
// Note: OpenGL is "column major"
GLfloat M[16] =
{
s[0], u[0], -f[0], 0.0f,
s[1], u[1], -f[1], 0.0f,
s[2], u[2], -f[2], 0.0f,
0.0f, 0.0f, 0.0f, 1.0f
};
glMultMatrixf(M);
glTranslatef(-eyeX, -eyeY, -eyeZ);
}
In my opinion, there is a problem with renderSeting function and setupViewPort function. I would like to ask for help to resolve this issue.
If the distance from the center of the object is 200, but the size of the radius of the object is 1000, you are inside the object.
The distance to the back of the object is 1200 (1000+200). Since the distance to the near plane is 1000,
glFrustumf(-ratio, ratio, -1.0f, 1.0f, 1.5f, 1000.0f);
the object by the far plane of the viewing frustum.

What is a render target and how am I creating an Invalid operation?

I'm trying to create a frameBuffer using a floating point texture and everything has been working fine, the frameBuffer lists as a complete attachment. But now I seem to be getting this error and can not draw into my texture.
07-22 21:00:15.180: W/Adreno-ES20(27203): <validate_render_targets:454>: GL_INVALID_OPERATION
I have no idea what it means by validate render targets or how it could be an invalid operation. Google does not appear to be my friend either in this matter as I return no results as to what it is. I cant seem to find anything within the opengl sdk docs either.
Heres the code for setting up my framebuffer
public void setup(Context context) {
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = 1.5f;
final float lookX = 0.0f;
final float lookY = 0.0f;
final float lookZ = -5.0f;
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER,
ShaderHelper.readShader(context, R.raw.vertex_shader));
int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER,
ShaderHelper.readShader(context, R.raw.fragment_shader));
mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
new String[] {"a_Position", "a_TexCoord"});
vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER,
ShaderHelper.readShader(context, R.raw.particle_vertex_shader));
fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER,
ShaderHelper.readShader(context, R.raw.particle_fragment_shader));
mPartProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
new String[] {"a_Position"});
// create the ints for the framebuffer, depth render buffer and texture
int[] fb, renderTex; // the framebuffer, the renderbuffer and the texture to render
fb = new int[1];
renderTex = new int[2];
// generate
GLES20.glGenFramebuffers(1, fb, 0);
GLES20.glGenTextures(2, renderTex, 0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, renderTex[0]);
// parameters - we have to make sure we clamp the textures to the edges
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
int GL_OES_texture_half_float = 0x8D61;
int GL_RGBA32F = 0x8814;
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GL_RGBA32F, texW, texH, 0, GLES20.GL_RGBA, GLES20.GL_FLOAT, null);
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fb[0]);
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, renderTex[0], 0);
// ShaderHelper.saveTexture(renderTex[0], texW, texH, "test");
posTex = renderTex[0];
posFb = fb[0];
final int buffers[] = new int[2];
GLES20.glGenBuffers(2, buffers, 0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[0]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, spriteBuffer.capacity() * BYTES_PER_FLOAT, spriteBuffer, GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, buffers[1]);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, renderQuad.capacity() * BYTES_PER_FLOAT, renderQuad, GLES20.GL_STATIC_DRAW);
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_MVPMatrix");
mTransformsHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Transforms");
mTexDataHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_TexData");
mTexUniformHandle = GLES20.glGetUniformLocation(mProgramHandle, "u_Texture");
mPositionHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_Position");
mTexCoordHandle = GLES20.glGetAttribLocation(mProgramHandle, "a_TexCoord");
mPosTexHandle = GLES20.glGetUniformLocation(mPartProgramHandle, "u_Positions");
mFBPositionHandle = GLES20.glGetAttribLocation(mPartProgramHandle, "a_Position");
spriteBufferIdx = buffers[0];
fbQuadBufferIdx = buffers[1];
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}
and here is the actual draw code
private void renderToTexture() {
GLES20.glViewport(0, 0, this.texW, this.texH);
// Bind the framebuffer
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, posFb);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, fbQuadBufferIdx);
// check status
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE)
throw new RuntimeException("FrameBuffer Error; Status = " + status);
transforms[0] = Fields.screen_width / 2;
transforms[1] = Fields.screen_height / 2;
transforms[2] = 0;
transforms[3] = Fields.screen_height;
pixels.put(transforms).position(0);
GLES20.glTexSubImage2D(GLES20.GL_TEXTURE_2D, 0, 0, 0, 1, 1, GLES20.GL_RGBA, GLES20.GL_FLOAT, pixels);
GLES20.glReadPixels(0, 0, 1, 1, GLES20.GL_RGBA, GLES20.GL_FLOAT, pixels);
Log.d("X", "X=" + pixels.get(0));
GLES20.glClearColor(.0f, .0f, .0f, 1.0f);
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
GLES20.glUseProgram(mPartProgramHandle);
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, posTex);
GLES20.glUniform1i(mPosTexHandle, 0);
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 2, GLES20.GL_FLOAT, false, 8, 0);
// Clear the currently bound buffer (so future OpenGL calls do not use this buffer).
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
// Draw the cubes.
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
// if (!saved)
// ShaderHelper.saveTexture(posTex, texW, texH, "test2");
// saved = true;
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
}
Any idea how I am generating this error or advice for debugging this in the future?

OpenGl2.0 Android displaying blank screen when rendering 3D model with texture

I am new to Android OpenGl2.0.and I have got one issue while creating 3D model from .obj file.
While rendering the 3D model,I am getting blank screen.
Sharing the code below,
#Override
public void onSurfaceCreated(GL10 glUnused, EGLConfig config)
{
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
// Enable texture mapping
GLES20.glEnable(GLES20.GL_TEXTURE_2D);
// Position the eye in front of the origin.
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = -0.5f;
// We are looking toward the distance
final float lookX = 0.0f;
final float lookY = 0.0f;
final float lookZ = -8.0f;
// Set our up vector. This is where our head would be pointing were we holding the camera.
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, lookX, lookY, lookZ, upX, upY, upZ);
final String vertexShader = RawResourceReader.readTextFileFromRawResource(mActivityContext, R.raw.per_pixel_vertex_shader_tex_and_light);
final String fragmentShader = RawResourceReader.readTextFileFromRawResource(mActivityContext, R.raw.per_pixel_fragment_shader_tex_and_light);
final int vertexShaderHandle = ShaderHelper.compileShader(GLES20.GL_VERTEX_SHADER, vertexShader);
final int fragmentShaderHandle = ShaderHelper.compileShader(GLES20.GL_FRAGMENT_SHADER, fragmentShader);
mProgramHandle = ShaderHelper.createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle,
new String[] {"a_Position", "a_Normal", "a_TexCoordinate"});
// Load the texture
mTextureDataHandle = TextureHelper.loadTexture(mActivityContext, R.drawable.bumpy_bricks_public_domain);
// Initialize the accumulated rotation matrix
Matrix.setIdentityM(mAccumulatedRotation, 0);
}
*Load texture Function()
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId, options);
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
OnDrawFrame() Of Renderer-
// Set the active texture unit to texture unit 0.
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
// Bind the texture to this unit.
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataHandle);
....
GLES20.glUniform1i(mTextureUniformHandle, 0);
// Pass in the position information
mCubePositions.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, POSITION_DATA_SIZE_IN_ELEMENTS, GLES20.GL_FLOAT, false, 0, mCubePositions);
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Pass in the normal information
mCubeNormals.position(0);
GLES20.glVertexAttribPointer(mNormalHandle, NORMAL_DATA_SIZE_IN_ELEMENTS, GLES20.GL_FLOAT, false, 0, mCubeNormals);
GLES20.glEnableVertexAttribArray(mNormalHandle);
// Pass in the texture information
mCubeTextureCoordinates.position(0);
GLES20.glVertexAttribPointer(mTextureCoordinateHandle, TEXTURE_DATA_SIZE_IN_ELEMENTS, GLES20.GL_FLOAT, false,
0, mCubeTextureCoordinates);
GLES20.glEnableVertexAttribArray(mTextureCoordinateHandle);
GLES20.glDrawElements(GLES20.GL_TRIANGLE_STRIP ,m3DModel.getIndicesLength(), GLES20.GL_UNSIGNED_SHORT, m3DModel.getIndices());
if anyone have an idea or knows this issue,Please give me reply,
I am not getting where i did the mistake in the code,

OpenGL ES 2.0 cuts the screen in landscape mode

I'm starting to learn OpenGL, and are using the following site: http://www.learnopengles.com/android-lesson-one-getting-started/
But it seems that I got a problem at this part (works fine in portrait mode):
private float[] mViewMatrix = new float[16];
/** Store the projection matrix. This is used to project the scene onto a 2D viewport. */
private float[] mProjectionMatrix = new float[16];
/** Allocate storage for the final combined matrix. This will be passed into the shader program. */
private float[] mMVPMatrix = new float[16];
/** This will be used to pass in the transformation matrix. */
private int mMVPMatrixHandle;
/** This will be used to pass in model position information. */
private int mPositionHandle;
/** This will be used to pass in model color information. */
private int mColorHandle;
/** How many bytes per float. */
private final int mBytesPerFloat = 4;
/** How many elements per vertex. */
private final int mStrideBytes = 7 * mBytesPerFloat;
/** Size of the position data in elements. */
private final int mPositionDataSize = 3;
/** Offset of the color data. */
private final int mColorOffset = 3;
/** Size of the color data in elements. */
private final int mColorDataSize = 4;
public void onSurfaceChanged(GL10 gl, int width, int height)
{
GLES20.glViewport(0, 0, width, height);
// Create a new perspective projection matrix. The height will stay the same
// while the width will vary as per aspect ratio.
final float ratio = (float) width / height;
final float left = -ratio;
final float right = ratio;
final float bottom = -1.0f;
final float top = 1.0f;
final float near = 1.0f;
final float far = 10.0f;
System.out.println("Height: " + height);
System.out.println("Width: " + width);
Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
}
public void onDrawFrame(GL10 gl)
{
final float eyeX = 0.0f;
final float eyeY = 0.0f;
final float eyeZ = 1.5f;
final float lookY = 0.0f; //Y direction of what user see
final float lookZ = -5.0f; //Z direction of what user see
// Set our up vector. This is where our head would be pointing were we holding the camera.
final float upX = 0.0f;
final float upY = 1.0f;
final float upZ = 0.0f;
GLES20.glClearColor(red, green, blue, clearcoloralpha);
GLES20.glClear(GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);
// Draw the triangle facing straight on.
for(int i = 0; i < Triangles.size(); i++)
{
Matrix.setIdentityM(Triangles.get(i).getModelMatrix(), 0);
if(Triangles.get(i).Rotate())
{
Triangles.get(i).rotation = (360.0f / 10000.0f) * ((int) Triangles.get(i).last);
Triangles.get(i).last+=20;
//Rotates the matrix by rotation degrees
Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);
}
else
Matrix.rotateM(Triangles.get(i).getModelMatrix(), 0, Triangles.get(i).rotation, 0.0f, 0.0f, 1.0f);
drawTriangle(Triangles.get(i).getFloatBuffer(),Triangles.get(i));
}
}
private void drawTriangle(final FloatBuffer aTriangleBuffer, Triangle tri)
{
aTriangleBuffer.position(0);
GLES20.glVertexAttribPointer(mPositionHandle, tri.DataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mPositionHandle);
aTriangleBuffer.position(3);
GLES20.glVertexAttribPointer(mColorHandle, tri.ColorDataSize, GLES20.GL_FLOAT, false, mStrideBytes, aTriangleBuffer);
GLES20.glEnableVertexAttribArray(mColorHandle);
Matrix.multiplyMM(mMVPMatrix, 0, mViewMatrix, 0, tri.getModelMatrix(), 0);
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mMVPMatrix, 0);
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 3);
}
But when I try to move a triangle (to the left or right) in landscape mode the triangles get "cut off" (does not display the whole triangle) when moving them to far to one of the sides. It seems that they are been acted on as if they were outside the screen when they actually are not. As mentioned it seems to work fine in portrait mode.
Height is 752 and Width 1280 in landscape mode (Galaxy Tab 2).
Does this have something to do with the Project Matrix which is set here?
Thanks for any help!
You were right, the problem was you were moving you camera :D
xax should have stayed as 0.0f
Matrix.setLookAtM(mViewMatrix, 0, eyeX, eyeY, eyeZ, xax, lookY, lookZ, upX, upY, upZ);

Categories

Resources