OpenGL more textures for one rect

Problems with Canvas, OpenGL, etc...

OpenGL more textures for one rect

Postby afail » Mon Nov 01, 2010 6:57 pm

I want to be able to switch textures for a given Rectangle. I have one rect and I put two textures, depending on different conditions. I just want to switch between textures.

I followed this code to create the textures:
http://blog.poweredbytoast.com/loading- ... in-android

and they render fine, but then I don't know how to tell the given rect to use another texture. Do I do something like "setTexture" with the ID that is generated in loadTextures() ? Do I have to have an array with texture names (numbers) that I then change .. I just don't get it..
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

Re: OpenGL more textures for one rect

Postby mark@project8games.com » Mon Nov 01, 2010 10:43 pm

You bind the texture with the handle you receive from the loadTexture() method. Just load both textures, keeping track of the returned IDs. Bind the one you want to use and it will show up.

You may have to do some u/v calculations and update your texture buffer if you aren't drawing the whole texture, but doesn't seem like this is the case.
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: OpenGL more textures for one rect

Postby afail » Mon Nov 01, 2010 11:13 pm

mark@project8games.com wrote:You bind the texture with the handle you receive from the loadTexture() method. Just load both textures, keeping track of the returned IDs. Bind the one you want to use and it will show up.

You may have to do some u/v calculations and update your texture buffer if you aren't drawing the whole texture, but doesn't seem like this is the case.


Well I do exactly that. It goes like this:

I generate name for the texture:
Code: Select all
private static int newTextureID(GL10 gl) {
    int[] temp = new int[1];
    gl.glGenTextures(1, temp, 0);
    return temp[0];       
}


I load the texture:
Code: Select all
private int loadTexture(GL10 gl, Context context, int resource) {
 
   // field mId
    mId = newTextureID(gl);
    ...
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mId);
    ...
    return id;
}


then goes the drawing of the frame:
Code: Select all
void drawFrame() {
   ...
    gl.glBindTexture(GL10.GL_TEXTURE_2D, mId);
   ...
}


So then when I want tho change the texture for the applied rect, I call this:

Code: Select all
void setTexutreId(int id) {
   mId = id;
}


But it doesn't update the texture :/
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Re: OpenGL more textures for one rect

Postby mark@project8games.com » Mon Nov 01, 2010 11:43 pm

Ok, I think I see. How are you saving the texture handles for the two textures you are loading? Do you have an array or something. Who calls setTextureId()? Where does that id field come from?
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Re: OpenGL more textures for one rect

Postby afail » Tue Nov 02, 2010 7:09 am

mark@project8games.com wrote:Ok, I think I see. How are you saving the texture handles for the two textures you are loading? Do you have an array or something. Who calls setTextureId()? Where does that id field come from?


Well I don't keep it anywhere :/ I don't have an array..

setTextureId is called from the onTouchEvent (from the GLSurfaceView).
the id field is actually mId, sorry (wrote that by hand) :/
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Re: OpenGL more textures for one rect

Postby mark@project8games.com » Tue Nov 02, 2010 4:02 pm

Hmm...ok. So here's what I think you should be doing

1)App starts. Call loadTexture() twice. Once for each of the textures you want to display. Save the ids in some member variables. Psuedo -code like this

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int firstTexture = loadTexture(gl, context, R.texture1);
  2. int secondTexture = loadTexture(gl, context, R.texture2);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


2)On touch, call setTexture() with either firstTexture, or secondTexture.

You're probably doing something like this already, so I'm not sure. Also, does the quad you're texturing go blank or does it just not change?
User avatar
mark@project8games.com
Developer
Developer
 
Posts: 41
Joined: Tue Mar 02, 2010 8:33 pm

Top

Re: OpenGL more textures for one rect

Postby afail » Wed Nov 03, 2010 8:23 am

Yes, that way it works fine.

Thanks!
afail
Developer
Developer
 
Posts: 31
Joined: Sat Oct 02, 2010 8:43 pm

Top

Return to Android 2D/3D Graphics - OpenGL Problems

Who is online

Users browsing this forum: No registered users and 5 guests