Skip to main content

using opengl texture coordinates



From my understanding the texture coordinates are s,t and go from 0,0 to 1,1. So if you wanted half the texture you would use .5 instead of 1. No matter what I do I get the whole texture so let me show what I have and attach the png texture. How do I map the first half of the texture to the quad? Thanks for any help





!test image that has 4 sub images - http://www.sendspace.com/file/6b5y7x







///Main activity thread:

public void onSurfaceCreated(GL10 gl, EGLConfig config) {

myquad.loadGLTexture(gl, this.context);



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, 640f, 0.0f, 480.0f, -1.0f, 1.0f);

}



CLASS QUAD{

private float vertices[] = {

0.0f, 0.0f, 0.0f, // 0. top left

1.0f, 0.0f, 0.0f, // 1. top right

0.0f, 1.0f, 0.0f, // 2. bottom left

1.0f, 1.0f, 0.0f // bottom right

};



private float texture[] = {

0.0f, 0.0f,

.5f, 0.0f,

0.0f, 0.5f,

0.5f, 0.5f

};



public void loadGLTexture(GL10 gl, Context context) {

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),

R.drawable.herowalkback);

Matrix flip = new Matrix();

flip.postScale(1f,-1f);

Bitmap bmp=Bitmap.createBitmap(bitmap, 0, 0,

bitmap.getWidth(),

bitmap.getHeight(), flip, false);



if (bitmap == null)

{

Log.i ("info", "bitmap could not be decoded");

}

gl.glGenTextures(1, textures, 0);

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);



gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);

gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, 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);



public void Draw(GL10 gl)

{

// set the color for the triangle

gl.glColor4f(0.0f, 1.0f, 0.0f, 0.5f);



gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texturebuffer);

gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexbuffer);



gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, vertices.length / 3);



}







} ////end class quad







//GLsurfaceview main thread

public void onDrawFrame(GL10 gl) {

startframe=SystemClock.elapsedRealtime();



gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

gl.glLoadIdentity();

myquad.Draw(gl);

}




Comments

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?