OpenGL on Android - Texture Mapping - Only top left pixel visible - android

I've been playing with OpenGL ES Android and somehow my textures are always filled with the color of the the top left-most pixel. I suppose the flags are wrong, but I've tried a many logical combinations and it does not seem to make any difference.
Why would the texture behave like so? What's the bit (flag bit indeed) that am I missing? Thanks
public void loadGLTexture(GL10 gl, Context context) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), resourceId);
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
bitmap.recycle();
}
public void draw(GL10 gl) {
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
((GL11Ext) gl).glDrawTexfOES(position.getX(), position.getY(), 0, width, height);
}
the surface is set to
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

Obviously texture crop rectangle has to be explicitly set:
int cropWorkspace[] = new int[4];
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
cropWorkspace[0] = 0;
cropWorkspace[1] = height;
cropWorkspace[2] = width;
cropWorkspace[3] = -height;
((GL11) gl).glTexParameteriv(GL10.GL_TEXTURE_2D, GL11Ext.GL_TEXTURE_CROP_RECT_OES, cropWorkspace, 0);

Related

Opengl ES 1.0 2D only last drawn texture is displayed

Setup: OpenGL ES 1.0, Android
Term background image here means a full screen texture which is drawn over two triangles which make the full screen rectangle.
If I draw full scene, including background image, I get black background color instead of clear color background, and I do not see my texture for background displayed
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
drawParticles(scene.getNumDots());
Render image attached:
However, if I draw only the background image, I see it.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
drawBackground();
// drawParticles(scene.getNumDots()); commented out
Render image attached:
Implementations:
// how it was setup
public void setupGl(#NonNull final GL10 gl) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
}
// This was called
public void setDimensions(#NonNull final GL10 gl, final int width, final int height) {
gl.glViewport(0, 0, width, height);
gl.glOrthof(0, width, 0, height, 1, -1);
}
// textures were loaded like this
private void loadTexture(
#NonNull final GL10 gl,
#NonNull final Bitmap texture,
final int handleOffset) {
gl.glGenTextures(1, textureHandle, handleOffset);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[handleOffset]);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
}
private void drawBackground() {
backgroundTextureCoordinates.position(0);
backgroundCoordinates.position(0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_BACKGROUND]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, backgroundTextureCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, backgroundCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
private void drawParticles(final int count) {
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
particlesTexturesCoordinates.position(0);
particlesTrianglesCoordinates.position(0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureHandle[TEXTURE_PARTICLE]);
gl.glTexCoordPointer(2, GL10.GL_BYTE, 0, particlesTexturesCoordinates);
gl.glVertexPointer(2, GL10.GL_SHORT, 0, particlesTrianglesCoordinates);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, count * VERTICES_PER_PARTICLE);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
public void setClearColor(
#NonNull final GL10 gl,
#ColorInt int color) {
gl.glClearColor(
Color.red(color) / 255f,
Color.green(color) / 255f,
Color.blue(color) / 255f, 0f);
}
The problem was in the way I used to draw a background
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 2);
There, I specified the number of triangles: 2.
Whereas I had to specify the number of vertices for the two triangles: 6.
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 6);

Bitmap drawn using Open GL has a black square and color overlay (Android)

Following is my Sprite code. The image drawn using this code has a black background (which covers the transparent background) and has a color overlay making the image look distorted.
How can I improve transparent image drawing on open gl es.
class Ship {
public int life = 5;
public FloatBuffer ShipVertexBuffer;
public FloatBuffer ShipTextureBuffer;
public PieceCluster cluster;
//! TEXTURES
private int[] textures = new int[1];
//! TEXTURES
public float ShipVerticles[] = {
0, 0, // лево низ
0, 30, // лево вверх
30, 0, // право низ
30, 30 // право вверх
};
//! TEXTURES
public float ShipTextures[] = {
0.0f, 1.0f,
1.0f, 1.0f,
0.0f, 0.0f,
1.0f, 0.0f
};
//! TEXTURES
public Ship(PieceCluster c) {
//! Буффер вертексов
cluster = c;
ByteBuffer bb = ByteBuffer.allocateDirect(36);
bb.order(ByteOrder.nativeOrder());
ShipVertexBuffer = bb.asFloatBuffer();
ShipVertexBuffer.put(ShipVerticles);
ShipVertexBuffer.position(0);
//! TEXTURES
bb = ByteBuffer.allocateDirect(ShipTextures.length * 4);
bb.order(ByteOrder.nativeOrder());
ShipTextureBuffer = bb.asFloatBuffer();
ShipTextureBuffer.put(ShipTextures);
ShipTextureBuffer.position(0);
//! TEXTURES
}
public void loadGLTexture(GL10 gl) {
// loading texture
// generate one texture pointer
Bitmap bitmap = cluster.Picture;
gl.glGenTextures(1, textures, 0);
// ...and bind it to our array
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
// create nearest filtered texture
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
// gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
// Use Android GLUtils to specify a two-dimensional texture image from our bitmap
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
// Clean up
bitmap.recycle();
}
public void draw(GL10 gl, float x, float y) {
//! TEXTURE
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//! TEXTURE
gl.glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
gl.glTranslatef(x, y, 0.0f);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, ShipVertexBuffer);
//! TEXTURE
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, ShipTextureBuffer);
//! TEXTURE
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
//! TEXTURE
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
//! TEXTURE
}
It doesn't look like you've enabled blending.
Try adding glEnable(GL_BLEND) before your draw call. Use glBlendFunc() and glBlendEquation() to change the blending style to suit what effect you want.

OpenGL- Render texture with transapare

i have a png image which draw it into my rectangular mesh in opengl, the problem is the image background is transparent but, when i bind it to my mesh, its background gets white; i want the image background to still be transparent.
i tried glEnableClientState(GLES10.GL_COLOR_ARRAY); and glColorPointer but didn't work as i want.
UPDATE:
the render openGL code:
void render(GL10 gl){
// First allocate texture if there is not one yet.
if (DRAW_TEXTURE && mTextureIds == null) {
// Generate texture.
mTextureIds = new int[2];
gl.glGenTextures(2, mTextureIds, 0);
for (int textureId : mTextureIds) {
// Set texture attributes.
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureId);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE);
}
}
if (DRAW_TEXTURE && mPage.getTexturesChanged()) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
Bitmap texture = mPage.getTexture(mTextureRectFront,
CurlPage.SIDE_FRONT);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
texture.recycle();
mTextureBack = mPage.hasBackTexture();
if (mTextureBack) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
texture = mPage.getTexture(mTextureRectBack,
CurlPage.SIDE_BACK);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, texture, 0);
texture.recycle();
} else {
mTextureRectBack.set(mTextureRectFront);
}
mPage.recycle();
reset();
}
// Some 'global' settings.
gl.glEnableClientState(GLES10.GL_VERTEX_ARRAY);
if (DRAW_TEXTURE) {
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mBufTexCoords);
}
gl.glVertexPointer(3, GLES10.GL_FLOAT, 0, mBufVertices);
//inja khasiate transparent ro faAl mikonim ke betoonim too CurlPage be kaaghaz alpha bedim
gl.glEnable(GLES10.GL_BLEND);
gl.glBlendFunc(GLES10.GL_SRC_ALPHA, GLES10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDisable(GLES10.GL_LIGHTING);
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glDrawArrays(GLES10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
// Draw front facing texture.
if (DRAW_TEXTURE) {
gl.glEnable(GL10.GL_BLEND);
gl.glEnable(GL10.GL_TEXTURE_2D);
if (!mFlipTexture || !mTextureBack) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[0]);
} else {
gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureIds[1]);
}
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, mVerticesCountFront);
gl.glDisable(GL10.GL_BLEND);
gl.glDisable(GL10.GL_TEXTURE_2D);
}
int backStartIdx = Math.max(0, mVerticesCountFront - 2);
//Log.d("mVerticesCountFront", ""+mVerticesCountFront+"");
int backCount = mVerticesCountFront + mVerticesCountBack - backStartIdx;
// Draw back facing blank vertices.
//gl.glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
gl.glDrawArrays(GLES10.GL_TRIANGLE_STRIP, backStartIdx, backCount);
// Disable textures and color array.
gl.glDisableClientState(GLES10.GL_TEXTURE_COORD_ARRAY);
gl.glDisableClientState(GLES10.GL_VERTEX_ARRAY);
}
OnSurfaceCreated Method:
#Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// mCurlMesh.setup(gl);
gl.glClearColor(0f, 0f, 0f, 0f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST);
//gl.glHint(GL10.GL_POLYGON_SMOOTH_HINT, GL10.GL_NICEST);
gl.glEnable(GL10.GL_LINE_SMOOTH);
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_DITHER);
}

loading a drawable resource into a texture and rendering it

I'm trying to understand OpenGL ES 2.0 in android. I got a code from a tutorial, but I do not understand how the texture works. I can't even understand where the rendering took place. Here is the code I got.
#Override
public void surfaceCreated(GL10 gl) {
// set up the surface
gl.glDisable(GL10.GL_DITHER);
gl.glHint(
GL10.GL_PERSPECTIVE_CORRECTION_HINT,
GL10.GL_FASTEST);
gl.glClearColor(0.4f, 0.2f, 0.2f, 0.5f);
gl.glShadeModel(GL10.GL_SMOOTH);
gl.glEnable(GL10.GL_DEPTH_TEST);
// fetch the checker-board
initImage(gl);
}
#Override
public void drawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// apply the checker-board to the shape
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glTexEnvx(
GL10.GL_TEXTURE_ENV,
GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_S,
GL10.GL_REPEAT);
gl.glTexParameterx(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T,
GL10.GL_REPEAT);
// animation
int t = (int) (SystemClock.uptimeMillis() % (10 * 1000L));
gl.glTranslatef(6.0f - (0.0013f * t), 0, 0);
// draw
gl.glFrontFace(GL10.GL_CCW);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuf);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuf);
gl.glDrawElements(
GL10.GL_TRIANGLE_STRIP,
5,
GL10.GL_UNSIGNED_SHORT, indexBuf);
}
private void initImage(GL10 gl) {
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
gl.glTexParameterf(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_NEAREST);
gl.glTexParameterf(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_S,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf(
GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_WRAP_T,
GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf(
GL10.GL_TEXTURE_ENV,
GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_REPLACE);
InputStream in = context.getResources().openRawResource(R.drawable.cb);
Bitmap image;
try { image = BitmapFactory.decodeStream(in); }
finally {
try { in.close(); } catch(IOException e) { }
}
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0);
image.recycle();
}
Can anyone explain to me where the image is loaded from a drawable file, where the conversion to texture, and where the rendering took place? I just can't seem to understand this. I'm familiar with how to do it in OpenGL but it seems OpenGL ES 2.0 uses a different technique?
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, image, 0); is a utility function which converts an android Bitmap into an OpenGL texture.
When this is called, it stores the bitmap in image into the currently bound texture, which in your case is textures[0].
glDrawElements is the command that actually fires off the rendering call. If you want to know what any of the other functions do, I suggest taking a look at their online help pages.

Android OpenGL ES Texturing - alphablending

I d like to "kill" the black color of my texture. Or, I want to show png files, without background (transparent bg)
How i can do this?
Any example?
it's not that difficult. This are the main pieces:
public static Bitmap loadBitmapFromId(Context context, int bitmapId) {
InputStream is = context.getResources().openRawResource(bitmapId);
try {
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inPreferredConfig = Bitmap.Config.RGB_565;
return BitmapFactory.decodeStream(is, null, bitmapOptions);
} catch (Exception ex) {
Log.e("bitmap loading exeption", ex.getLocalizedMessage());
return null;
}
}
The Bitmap.Config.RGB_565 is important here. Then add the bitmap and get your texture id as usual.
Now in the onSurfaceCreated(GL10 gl, EGLConfig config) of your renderer add this:
// Transparancy
// important: transparent objects have to be drawn last!
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
And the drawing part (you are probably already doing this):
// first disable color_array for save:
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
// Enabled the vertices buffer for writing and to be used during
// rendering.
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// Specifies the location and data format of an array of vertex
// coordinates to use when rendering.
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);
gl.glDrawArrays(drawMode, 0, verticesCount);
gl.glDisable(GL10.GL_TEXTURE_2D);
// Disable the vertices buffer.
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

Categories

Resources