Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (416)
games submitted by our members
Games in WIP (306)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texturing a cube with multiple textures (slick)  (Read 641 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK

Senior Newbie





« Posted 2012-10-07 07:45:50 »

I have a cube class that can render a textured cube fine, however when I try to bind separate textures to different faces of the cube, it always seems to draw all of them with the last bound texture (rather than the relative bound texture).

I believe my problem is coming from a lack of understanding of what Texture.bind() actually does in the slick.opengl library. (if anyone could provide me a link with actual source code, that would be great!)

So far, I have tried two ways of binding the textures. The first being with a for loop:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
for (int i = 0; i < pointlist.length; i++) {
           
            if (texture != null) {
               if (i%4 == 3) {
                  glTexCoord2f(0, 0);
               }
               if (i%4 == 0) {
                  if (i == 0) {
                     relative_texture[0].bind();
                  } else if (i == 4) {
                     relative_texture[1].bind();
                  } else if (i == 8) {
                     relative_texture[2].bind();
                  } else if (i == 12) {
                     relative_texture[3].bind();
                  } else if (i == 16) {
                     relative_texture[4].bind();
                  } else if (i == 20) {
                     relative_texture[5].bind();
                  }
                  glTexCoord2f(0, 1);
               }
               if (i%4 == 1) {
                  glTexCoord2f(1, 1);
               }
               if (i%4 == 2) {
                  glTexCoord2f(1, 0);
               }
            }
            glVertex3f(pointlist[i].x, pointlist[i].y, pointlist[i].z);
         }

and the second being without a for loop, and just written out (should be going through the same method):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
if (relative_texture.length == 6) {
            relative_texture[0].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[0].x, pointlist[0].y, pointlist[0].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[1].x, pointlist[1].y, pointlist[1].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[2].x, pointlist[2].y, pointlist[2].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[3].x, pointlist[3].y, pointlist[3].z);
         if (relative_texture.length == 6) {
            relative_texture[1].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[4].x, pointlist[4].y, pointlist[4].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[5].x, pointlist[5].y, pointlist[5].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[6].x, pointlist[6].y, pointlist[6].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[7].x, pointlist[7].y, pointlist[7].z);
         if (relative_texture.length == 6) {
            relative_texture[2].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[8].x, pointlist[8].y, pointlist[8].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[9].x, pointlist[9].y, pointlist[9].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[10].x, pointlist[10].y, pointlist[10].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[11].x, pointlist[11].y, pointlist[11].z);
         if (relative_texture.length == 6) {
            relative_texture[3].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[12].x, pointlist[12].y, pointlist[12].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[13].x, pointlist[13].y, pointlist[13].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[14].x, pointlist[14].y, pointlist[14].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[15].x, pointlist[15].y, pointlist[15].z);
         if (relative_texture.length == 6) {
            relative_texture[4].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[16].x, pointlist[16].y, pointlist[16].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[17].x, pointlist[17].y, pointlist[17].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[18].x, pointlist[18].y, pointlist[18].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[19].x, pointlist[19].y, pointlist[19].z);
         if (relative_texture.length == 6) {
            relative_texture[5].bind();
         }
         glTexCoord2f(0, 1);
         glVertex3f(pointlist[20].x, pointlist[20].y, pointlist[20].z);
         glTexCoord2f(1, 1);
         glVertex3f(pointlist[21].x, pointlist[21].y, pointlist[21].z);
         glTexCoord2f(1, 0);
         glVertex3f(pointlist[22].x, pointlist[22].y, pointlist[22].z);
         glTexCoord2f(0, 0);
         glVertex3f(pointlist[23].x, pointlist[23].y, pointlist[23].z);


the whole draw method looks like this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
public void draw() {
      glPushMatrix();
      if (texture != null) {
         texture.bind();
      }
      glEnable(GL_TEXTURE_2D);
      if (cullEnabled()) {
         glEnable(GL_CULL_FACE);
         glCullFace(cullface);
      }
      glBegin(GL_QUADS);
      {
         glColor3f(color.getR(), color.getG(), color.getB());
          <INSERT ABOVE CODE EXAMPLES HERE>
      }
      glEnd();
      glDisable(GL_TEXTURE_2D);
      if (cullEnabled()) {
         glDisable(GL_CULL_FACE);
      }
      glPopMatrix();
   }



The way I presume Texture.bind() to work (with limited knowledge of how openGL works) is that it changes the texture state of openGL to this texture using glBindTexture(GL_TEXTURE_2D, <intofTexture>) to draw the next shape(s) and since there is no Texture.unbind(), it would make sense that the next texture bound would be the new texture state and so forth.

Does Texture.bind() only work outside of glBegin(GL_QUADS) and similar methods?

Any help on this subject would be appreciated Smiley
Offline sproingie
« Reply #1 - Posted 2012-10-08 18:41:06 »

Does Texture.bind() only work outside of glBegin(GL_QUADS) and similar methods?

Indeed, you cannot bind a texture while inside glBegin/glEnd

GL_INVALID_OPERATION is generated if glBindTexture is executed between the execution of glBegin and the corresponding execution of glEnd.

Keep those man pages bookmarked.
Offline davedes
« Reply #2 - Posted 2012-10-08 20:13:09 »

For something like this you should use a single texture atlas and specify different texture coordinates for each face.

I'd also highly recommend using an object-oriented approach instead of endless if-else statements. Right now you have lots of repeated code.

Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline QuicK

Senior Newbie





« Reply #3 - Posted 2012-10-08 22:58:30 »

I'd also highly recommend using an object-oriented approach instead of endless if-else statements. Right now you have lots of repeated code.

Could you give me an example of how how would do it? (pseudo-code is fine)

From this statement: this comes to mind.
1  
2  
create a "Face" class for each face with a texture variable (with texture coordinates) and a draw method. (this will contain texture.bind())
create a "Cube" class that has an array of 6 faces that each call the Face.draw() method.


If you can think of a better way to do it, please let me know! Smiley

P.S. - thank you sproingie, I've been looking for those docs...
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Browse for soundtracks for your game!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
BrassApparatus (10 views)
2013-06-19 08:52:37

NegativeZero (17 views)
2013-06-19 03:31:52

NegativeZero (19 views)
2013-06-19 03:24:09

Jesse_Attard (20 views)
2013-06-18 22:03:02

HeroesGraveDev (62 views)
2013-06-15 23:35:23

Vermeer (61 views)
2013-06-14 20:08:06

davedes (61 views)
2013-06-14 16:03:55

alaslipknot (55 views)
2013-06-13 07:56:31

Roquen (77 views)
2013-06-12 04:12:32

alaslipknot (61 views)
2013-06-10 19:30:18
Smoothing Algorithm Question
by UprightPath
2013-05-28 02:58:26

Smoothing Algorithm Question
by UprightPath
2013-05-28 02:57:33

Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!