Sign in to follow this  

Loading multiple images (java)

This topic is 2471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
What you want to do is something like this:
[code]
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..[/code]

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.

Share this post


Link to post
Share on other sites
[quote name='Erik Rufelt' timestamp='1298115653' post='4776287']
What you want to do is something like this:
[code]
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..[/code]

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.
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]int vertices[] = {
// FRONT (1)
-half, -half, half, half, -half, half,
-half, half, half, half, half, half,};[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2][color=#000000][size=3]
[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);[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]-----------[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]Thanks again for taking the time to help out.[/size][/color]
[color=#1C2837][size=2]
[/size][/color]
[color=#1C2837][size=2]int vertices[] = {[/size][/color][/size][/color][/size][/color]
[color=#1C2837][size=2]// BACK (2)
-half, -half, -half, -half, half, -half,
half, -half, -half, half, half, -half,};[/size][/color]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Alright, I made classes (bindFront, bindBack, ect). However, I'm having trouble calling them now. My class looks like this:

[quote]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...

[quote] 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.

Share this post


Link to post
Share on other sites
Bump with some updated code.

I've got my binding routines set up in the GLRenderer class

[code]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);
}
public void bindBack(GL10 gl){
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glActiveTexture(GL10.GL_TEXTURE0);
gl.glBindTexture(GL10.GL_TEXTURE_2D, back_tex);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);[/code]

I have my textures set

[code] front_tex = GLDice.loadTexture(gl, context, R.drawable.image1);
back_tex = GLDice.loadTexture(gl, context, R.drawable.image2);
[/code]

Now I want to call my binding methods from the draw method in my GLDice class

[code]public void draw(GL10 gl) {

GLRenderer.bindFront(gl);

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);
............
[/code]

However, the code isnt letting me make the call. What am I doing wrong? I'm getting the exception "non-static method bindFront cannot be referenced from a static context".
How can i fix this?
Thanks, everyone.

Share this post


Link to post
Share on other sites

This topic is 2471 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this