Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  LWJGL - Made my first 3D world! Running into texture problems!  (Read 3056 times)
0 Members and 1 Guest are viewing this topic.
Offline saucecode

Senior Newbie


Medals: 1



« Posted 2012-04-18 00:16:03 »

So I wrote my first 3D world in LWJGL (YEAH!), which I am very proud of, and I've just started adding textures to my lifeless world. I've had to rewrite this about 6 times now as i discover many new things/bugs about my problem. After a long analysis, it seems that my textures are binding themselves after being loaded.

1  
2  
3  
4  
5  
6  
try {
         textureFloor = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("img/floor.png"));
         textureWall = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("img/tileWall.jpg"));
      } catch (IOException e) {
         e.printStackTrace();
      }


So when the floor is rendered, textureWall is already being drawn onto it! If I comment out textureWall, and load only textureFloor, then the floor texture is drawn onto the floor. This isn't right at all! Nothing is happening to these texture objects, other than being loaded from the above code.

My Floor's class is available here

I've got another problem aswell. When I add a box (and the textures still aren't bound), it renders really weird, and makes the floor render weird aswell!



In this picture, we see the floor, and a box. As you can see, they have textures on them, that are extremely off.

Will add code if requested.

Thanks!!
Online Riven
« League of Dukes »

JGO Overlord


Medals: 782
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2012-04-18 00:17:56 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      GL11.glVertex3f(width,0, length);
      GL11.glTexCoord2f(width, length);
     
      GL11.glVertex3f(-width,0, length);
      GL11.glTexCoord2f(-width, length);
     
      GL11.glVertex3f(-width,0,-length);
      GL11.glTexCoord2f(-width, -length);
     
      GL11.glVertex3f(width,0,-length);
      GL11.glTexCoord2f(width, -length);


texcoords are normalized, so you typically use the range 0.0 .. 1.0


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      GL11.glVertex3f(width,0, length);
      GL11.glTexCoord2f(1.0, 1.0);
     
      GL11.glVertex3f(-width,0, length);
      GL11.glTexCoord2f(0.0, 1.0);
     
      GL11.glVertex3f(-width,0,-length);
      GL11.glTexCoord2f(0.0, 0.0);
     
      GL11.glVertex3f(width,0,-length);
      GL11.glTexCoord2f(1.0, 0.0);

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline saucecode

Senior Newbie


Medals: 1



« Reply #2 - Posted 2012-04-18 00:24:57 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      GL11.glVertex3f(width,0, length);
      GL11.glTexCoord2f(width, length);
     
      GL11.glVertex3f(-width,0, length);
      GL11.glTexCoord2f(-width, length);
     
      GL11.glVertex3f(-width,0,-length);
      GL11.glTexCoord2f(-width, -length);
     
      GL11.glVertex3f(width,0,-length);
      GL11.glTexCoord2f(width, -length);


texcoords are normalized, so you typically use the range 0.0 .. 1.0


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
      GL11.glVertex3f(width,0, length);
      GL11.glTexCoord2f(1.0, 1.0);
     
      GL11.glVertex3f(-width,0, length);
      GL11.glTexCoord2f(0.0, 1.0);
     
      GL11.glVertex3f(-width,0,-length);
      GL11.glTexCoord2f(0.0, 0.0);
     
      GL11.glVertex3f(width,0,-length);
      GL11.glTexCoord2f(1.0, 0.0);


(This is all done without rendering the box)

Before:


After:


I don't really know what normalized texcoords are, but from context I think it means they rangle from 0-1 (possibly -1 too).
What ever it means, it seems to just stretch the texture out over the whole quad, rather than repeating it. I think the later looks nicer.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-04-18 00:34:04 »

Slick converts textures to a power-of-two (POT) size.

Ideally you should convert your assets to a power-of-two size.

Alternatively, you could use Texture.getWidth/getHeight to determine the normalized width/height, e.g.
1  
glTextCoord2f(tex.getWidth(), tex.getHeight());

Offline saucecode

Senior Newbie


Medals: 1



« Reply #4 - Posted 2012-04-18 00:40:32 »

Slick converts textures to a power-of-two (POT) size.

Ideally you should convert your assets to a power-of-two size.

Alternatively, you could use Texture.getWidth/getHeight to determine the normalized width/height, e.g.
1  
glTextCoord2f(tex.getWidth(), tex.getHeight());


I like your thinking! Thanks for this.
I think I made it even better!
1  
GL11.glTexCoord2f(World.textureFloor.getWidth()*repeats, World.textureFloor.getHeight()*repeats);


int repeats = 1;


int repeats = 8;


While all this is great, it still doesn't solve my texture pre-binding themselves!
Offline davedes
« Reply #5 - Posted 2012-04-18 01:03:50 »

Call floorTex.bind() before rendering the floor geometry, and wallTex.bind() before rendering the wall geometry. etc.

Offline saucecode

Senior Newbie


Medals: 1



« Reply #6 - Posted 2012-04-18 01:33:10 »

Call floorTex.bind() before rendering the floor geometry, and wallTex.bind() before rendering the wall geometry. etc.

Textures are right on each quad, but are still skewed!

Offline JESTERRRRRR

Senior Member


Medals: 7
Exp: 1 year



« Reply #7 - Posted 2012-04-18 04:11:13 »

Can you show any more code? I'd like to run it and have a stab at fixing
Offline saucecode

Senior Newbie


Medals: 1



« Reply #8 - Posted 2012-04-18 04:49:30 »

Can you show any more code? I'd like to run it and have a stab at fixing

Oh please, stab away!

You'll need LWJGL 2.8.3 (lwjgl.jar and lwjgl_util.jar) and slick-utils.
Thanks!
Offline davedes
« Reply #9 - Posted 2012-04-18 17:30:03 »

You have some problems with your code:
a) Don't call glTranslate within glBegin/glEnd
b) If you translate to centerX/centerY then you don't need to use centerX/centerY in glVertex! Right now you are translating twice.
c) Call glTexCoord2f before glVertex2f
d) Your texture coords for the box have some bugs. On once face, you forget to multiply the value by "repeats". On another face, you use the coordinate (0, 0) twice. etc.
c) Avoid duplicate code and learn some basic design patterns. e.g. You might want to create a Quad class/utility so that you have a "single source of responsibility". Right now you are repeating the same stuff over and over again, which is leading to typos and a more difficult program to debug.
d) Start reading up on OpenGL to learn more.

Cheers, good luck. Smiley

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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!