abcdefgqwerty
Lurker
I could use some help on two things.
One is when I decode the bitmap and draw it then it works right, but when I try flipping the bitmap it draws nothing. What would cause this?
Two is how to draw the first image 32x32 out of this 64x64 image with texture coordinates. It seems .5 would draw half the texture so 32 out of the 64 pixels and 1 would draw the whole texture. Why is this or how to draw first 32 pixels of 64 pixel texture?
One is when I decode the bitmap and draw it then it works right, but when I try flipping the bitmap it draws nothing. What would cause this?
Two is how to draw the first image 32x32 out of this 64x64 image with texture coordinates. It seems .5 would draw half the texture so 32 out of the 64 pixels and 1 would draw the whole texture. Why is this or how to draw first 32 pixels of 64 pixel texture?
Code:
class Quad {
private FloatBuffer vertexbuffer;
private FloatBuffer texturebuffer;
private int[] textures = new int[1];
private float vertices[] = {
0.0f, 0.0f, // 0. top left
32.0f, 0.0f, // 1. top right
0.0f, 32.0f, // 2. bottom left
32.0f, 32.0f // 3. bottom right
};
//.5 should give half of texture but it draws the whole 64x64 texture?
private float texture[] = {
0.0f, 0.0f,
0.5f, 0.0f,
0.0f, 0.5f,
0.5f, 0.5f
};
void SetVertices(int x,int y,int z)
{
}
public Quad(){
ByteBuffer bytebuffer = ByteBuffer.allocateDirect(vertices.length * 4);
bytebuffer.order(ByteOrder.nativeOrder());
vertexbuffer = bytebuffer.asFloatBuffer();
vertexbuffer.put(vertices);
vertexbuffer.position(0);
bytebuffer = ByteBuffer.allocateDirect(texture.length * 4);
bytebuffer.order(ByteOrder.nativeOrder());
texturebuffer = bytebuffer.asFloatBuffer();
texturebuffer.put(texture);
texturebuffer.position(0);
}
public void loadGLTexture(GL10 gl, Context context) {
//Get the texture from the Android resource directory
//Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.herowalkback);
BitmapFactory.Options bitmapOptions = new
[B]///This part draws nothing, but removing this and just using bitmap above it works but is upside down.[/B]
[B]BitmapFactory.Options();
bitmapOptions.inScaled = false;
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),R.drawable.herowalkback);
Matrix flip = new Matrix();
flip.postScale(1f, -1f);[/B]
bitmap=Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flip, true);
if (bitmap == null)
{
Log.i ("info", "bitmap could not be decoded");
}
//Generate one texture pointer...
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_NEAREST);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
//gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT );
// gl.glTexParameterf( GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
//Use the Android GLUtils to specify a two-dimensional texture image from our bitmap
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0,bitmap, 0);
//Clean up
bitmap.recycle();
}
public void Draw(GL10 gl)
{
// set the color for the triangle
gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);
//Enable the vertex and texture state
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texturebuffer);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertexbuffer);
// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 2);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
myquad.loadGLTexture(gl, this.context);
//turn on alpha blending
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(gl.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
gl.glDisable(GL10.GL_DEPTH_TEST);
x=y=0;
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
width=w;
height=h;
gl.glViewport(0, 0, w, h);
gl.glMatrixMode(GL10.GL_PROJECTION); //Select The Projection Matrix
gl.glLoadIdentity();
gl.glOrthof(0.0f, 640, 0.0f, 480, 1.0f, -1.0f);
}
public void onDrawFrame(GL10 gl) {
startframe=SystemClock.elapsedRealtime();
// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
switch (move_direction)
{
case UP:
gl.glTranslatef(0, 1, 0);
y=y+1;
break;
case DOWN:
gl.glTranslatef(0, -1, 0);
y= y-1;
break;
case LEFT:
gl.glTranslatef(-1, 0, 0);
x=x-1;
break;
case RIGHT:
gl.glTranslatef(1, 0, 0);
x=x+1;
break;
case NONE:
//gl.glTranslatef(0, 0, 0);
break;
}
myquad.Draw(gl);