Create texture geometry - 1 2D coordinate per vertex, so for your 4
verts, you could use this:
float texLeft = 0f;
float texTop = 0f;
float texRight = 1.0f;
float texBottom = 1.0f;
float texArray[] = { texLeft, texTop, texLeft, texBottom , texRight,
texBottom, texRight, texTop};
put texArray into a FloatBuffer and use it when you draw. This
geometry will map the full texture image onto your quad. If you
wanted to use the top corner of the texture, you'd put right and
bottom as .5 instead of 1.0. If you want your texture to repeat and
have the parameters set to repeat (see below), you use a value over
1. 2.0, for instance, will repeat twice, 3.0 will do 3, etc..
Loading textures:
Note - Must be done after every time the surface is changed (Because
memory is cleared)
// create pointers for textures
int[] textures = new int[1];
gl.glGenTextures(1, textures, 0);
myTextureId= textures[0];
// load bitmap texture into vram
// bind to which texture ID we want to load
gl.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
// learn about these from an OpenGL book, this is just to get you
started.
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.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER,
GL10.GL_LINEAR);
gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER,
GL10.GL_LINEAR);
gl.glTexEnvf(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
GL10.GL_MODULATE);
// actually upload (this is a very high-cost operation so don't do it
every frame unless you know what you're doing)
Bitmap bitmap = loadSomePowerof2Bitmap()
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);
Now finally draw it all:
assuming you have
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); in your
initialization code,
draw(GL10 gl) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, myTextureId);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
gl.glDrawElements(GL10.GL_TRIANGLES, vertexCount,
GL10.GL_UNSIGNED_SHORT, indexBuffer);
}
On Feb 17, 2:35 pm, joshbeck <[email protected]> wrote:
> Hello All,
>
> I'm learning OpenGL on my own atm and I have a question about
> texturing a surface:
>
> I can create a simple polygon like this:
>
> public class Square {
> // Our vertices.
> private FloatBuffer colorBuffer;
> float[] colors = {
> .5f, .5f, 0f, .2f, // vertex 0 red
> 0f, 1f, 0f, .2f, // vertex 1 green
> 0f, 0f, 1f, .2f, // vertex 2 blue
> 1f, 0f, 1f, .2f, // vertex 3 magenta
> };
> private float vertices[] = {
> -.7f, .04f, -.5f, // 0, Top Left
> -.7f, -.04f, -0.5f, // 1, Bottom Left
> .7f, -.04f, 0.05f, // 2, Bottom Right
> .7f, .04f, 0.05f, // 3, Top Right
> };
> private short[] indices = { 0, 1, 2, 0, 2, 3 };
>
> Then I load all of that info into the approriate buffers and draw it
> to the screen with a call to:
>
> gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,
> GL10.GL_UNSIGNED_SHORT, indexBuffer);
>
> My question:
>
> I'm looking for a good example of how to take an image and texture it
> to the surface.
>
> (I'm looking for a good book on JOGL with examples.)
>
> Any suggestions are appreciated.
>
> Thanks,
> Josh Beck
> Northeast ISD
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en