Loading multiple images (java)

Started by
10 comments, last by Scourged Soul 13 years, 1 month ago
I'm a fair hand at coding, but java is new to me. I've been trying to work my way through some of the Open GL lessons available here on this site. So far, the code has been flawless, however, as I was drawing the cube, I got curious. I started trying to load a different texture to each side of the cube, but my ignorance in the language is getting the better off me. I was just hoping that someone could give me a nudge in the right direction. I'm having trouble setting up the coords so that a different image appears on the backside of the cube. Is there a way to call the loadTexture, while giving the coord/vertex? I would really appreciate it whatever guidance you good people can offer. Thanks in advance.
Advertisement
PS. I'm a true newbie at this, so if you could just explain it in the simplest of terms, it would probably help my get my head around it. Thanks again.
What you want to do is something like this:

glBindTexture(GL_TEXTURE_2D, firstTexture);
glBegin(GL_QUADS);
... vertices for first face
glEnd();

glBindTexture(GL_TEXTURE_2D, secondTexture);
glBegin(..);
... vertices for second face
glEnd();

etc..


As for actually loading firstTexture and secondTexture it depends, are you referring to the NeHe tutorials?
If you post your current code it's easier to suggest how to change it.

What you want to do is something like this:

glBindTexture(GL_TEXTURE_2D, firstTexture);
glBegin(GL_QUADS);
... vertices for first face
glEnd();

glBindTexture(GL_TEXTURE_2D, secondTexture);
glBegin(..);
... vertices for second face
glEnd();

etc..


As for actually loading firstTexture and secondTexture it depends, are you referring to the NeHe tutorials?
If you post your current code it's easier to suggest how to change it.


I appreciate the quick reply. I'm actually at school right now and I don't have my laptop with me. I was referring to the NeHe tutorials though.
Alright, I'm back at my pc. I'm working on making an android program, so I'm writing this in GL 10.

----------
front_tex = GLCube.loadTexture(gl, context, R.drawable.image_1);
back_tex = GLCube.loadTexture(gl, context, R.drawable.image_2);



gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glClientActiveTexture(GL10.GL_TEXTURE0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, front_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);

gl.glActiveTexture(GL10.GL_TEXTURE1);
gl.glClientActiveTexture(GL10.GL_TEXTURE1);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, back_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);

------------------
---Here is my loadTexture---

static int loadTexture(GL10 gl, Context context, int resource){
Bitmap bmp = BitmapFactory.decodeResource(
context.getResources(), resource);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
gl.glTexParameterx(GL10.GL_TEXTURE_2D,
GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

------------------

---Here are my vertices----
int one = 65536;
int half = one / 2;

int vertices[] = {
// FRONT (1)
-half, -half, half, half, -half, half,
-half, half, half, half, half, half,
// BACK (2)
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,
// LEFT (3)
-half, -half, half, -half, half, half,
-half, -half, -half, -half, half, -half,
// RIGHT (4)
half, -half, -half, half, half, -half,
half, -half, half, half, half, half,
// TOP (5)
-half, half, half, half, half, half,
-half, half, -half, half, half, -half,
// BOTTOM (6)
-half, -half, half, -half, -half, -half,
half, -half, half, half, -half, -half, };

int TexCoordsf [] = {
0, one, one, one, 0, 0, one, 0,
};

What I can't figure out is where in this process do I have an opportunity to specify the vertices of my front_tex and back_tex. Am I going about this all the wrong way? Should I split up my vertices somehow? Thanks again for the help. And remember, this is an android app, so my GL is limited.
Don't set back-texture to texture GL_TEXTURE1, but keep using GL_TEXTURE0 for both textures. The different texture units are needed when you want to use both textures at the same time (blend them in shaders for example), but not when you just want to switch between them. Then when you actually draw your vertices (glDrawElements for example), only draw those vertices that want to use the currently active texture. Then call glBindTexture to set the other texture to active (still on the same GL_TEXTURE0), and draw the vertices that use that texture.
So, for instance, if I'm spinning a box and I want it to look like a dice, and I want the single dot to appear on the front, and the six dots to appear on the back; would I want my drawDice() to look like this:

-------------------------

[color=#1C2837][size=2]gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glClientActiveTexture(GL10.GL_TEXTURE0);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glBindTexture(GL10.GL_TEXTURE_2D, 1_dot_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
[color=#1C2837][size=2]

[color=#1C2837][size=2]int vertices[] = {
// FRONT (1)
-half, -half, half, half, -half, half,
-half, half, half, half, half, half,};
[color=#1C2837][size=2]

[color=#1C2837][size=2][color=#000000]


[color=#1C2837][size=2]gl.glBindTexture(GL10.GL_TEXTURE_2D, 6_dot_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
[color=#1C2837][size=2]

[color=#1C2837][size=2]-----------
[color=#1C2837][size=2]

[color=#1C2837][size=2]Thanks again for taking the time to help out.
[color=#1C2837][size=2]

[color=#1C2837][size=2]int vertices[] = {
[color=#1C2837][size=2]// BACK (2)
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,};

I started to edit my code to display a second image. My current problem lies in the fact that I have my cube split into two classes. GLDice and GLRenderer. My current code appearing in the GLDice class looks like this...


-------------------------
---Appearing in GLDice class---

public void draw(GL10 gl) {

gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, front_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, mTextureBuffer1);
gl.glVertexPointer(3, GL10.GL_FIXED, 0, mVertexBuffer1);
gl.glColor4f(1, 1, 1, 1);
gl.glNormal3f(0, 0, 1);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
.....

----------
However, "front_tex" is giving me trouble, because it is declared in the GLRenderer class. The reason that it's declared in GLRenderer is because that is where I load the textures from a bitmap. I'm not sure if I'm making this more complicated than it needs to be, but here is the code.

-----------
---Appearing in the GLRenderer class onSurfaceCreated()---

// Enable textures
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);

// Load the cube's texture from a bitmap
front_tex = GLDice.loadTexture(gl, context, R.drawable.image_1);
back_tex = GLDice.loadTexture(gl, context, R.drawable.image_2);

-----------

Should I condense my program down to a single class, or am I missing a simple solution to my problem? Can I initialize front_tex within the GLDice class, and if so, how? Like I said, I only know the basics of java, so I need help with some simple questions. I appreciate people taking the time to help.
You can for example add a method in the GLRenderer class that sets the texture, like bindFront() and bindBack(), or bindTexture(int index) or even bindTexture(String name).
Alright, I made classes (bindFront, bindBack, ect). However, I'm having trouble calling them now. My class looks like this:

public void bindFront(GL10 gl){
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, front_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}[/quote]

And this is my attempt to call it...

GLRenderer.bindFront(gl);[/quote]

I also tried declaring

GLRenderer rend;

as a global variable, but it said i wasn't initializing it. When I tried to intialize it as

private final GLRenderer rend = new GLRenderer();

but it gave me an error saying that it wasn't a constructor.

Sorry that I need so much help.

This topic is closed to new replies.

Advertisement