Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
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 1015 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK

Junior Member





« 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

JGO Kernel


Medals: 201



« 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!
Legends of Yore - The Casual Retro Roguelike
Offline QuicK

Junior Member





« 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.

 

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

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!