Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (482)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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 trouble (new to opengl all together)  (Read 5904 times)
0 Members and 1 Guest are viewing this topic.
Offline QuicK

Junior Member





« Posted 2012-03-28 04:23:20 »

I am working on recreating the space invaders game and it shouldn't be too difficult if I can get all this working haha.  I have read through the tutorials on the main page of the lwjgl wiki.

So the idea is I have an abstract class called Entity which needs a sprite (which includes a Texture) and x and y coordinates.
That works fine, so I created the EntityPlayer class which will be the space ship.  It is loading the .png file fine (no errors), but it is only displaying as a single pixel.

I'll include the code I think you'll need to help me solve this issue below:

The texture loading in the Sprite class:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public void init(String path) {
      if (path != null) {
         try {
            texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path));
         } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
         }
      }
   }


The draw method in the Sprite class:
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  
public void draw(int x, int y) {
      // store the current model matrix
     glPushMatrix();

         // bind to the appropriate texture for this sprite
        texture.bind();
   
         // translate to the right location and prepare to draw
        glTranslatef(x, y, 0);
   
         // draw a quad textured to match the sprite
        glBegin(GL_QUADS);
         {
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
   
            glTexCoord2f(0, texture.getHeight());
            glVertex2f(0, height);
   
            glTexCoord2f(texture.getWidth(), texture.getHeight());
            glVertex2f(width, height);
   
            glTexCoord2f(texture.getWidth(), 0);
            glVertex2f(width, 0);
         }
         glEnd();

      // restore the model view matrix to prevent contamination
     glPopMatrix();
   }


The draw method in the Entity class:

1  
2  
3  
public void draw() {
      sprite.draw((int)x, (int)y);
   }


The initialization code for the only instance of EntityPlayer:

1  
2  
3  
4  
5  
public static final EntityPlayer player;

static {
      player = new EntityPlayer(new Sprite("res/player.png"), 100, 100);
   }


Hopefully there is just something with lwjgl I'm doing wrong.  If not, maybe I can pastebin my code for someone to help me.  I'm pretty familiar with java, so It shouldn't be anything outside of lwjgl.

Please let me know if you see anything wrong with the above code Smiley or if you have any tips, that would be much appreciated as well!
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2012-03-28 04:26:24 »

You have to use getTextureWidth/Height when giving the TexCoords Wink

Offline QuicK

Junior Member





« Reply #2 - Posted 2012-03-28 04:30:10 »

I'm assuming you mean to change the code here to this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
glBegin(GL_QUADS);
         {
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
   
            glTexCoord2f(0, texture.getTextureHeight());
            glVertex2f(0, height);
   
            glTexCoord2f(texture.getTextureWidth(), texture.getTextureHeight());
            glVertex2f(width, height);
   
            glTexCoord2f(texture.getTextureWidth(), 0);
            glVertex2f(width, 0);
         }
         glEnd();


if so, that did not work, it has the same result.
Thanks for the hasty reply!

Edit: I changed all width and height variables in the above code to "getWidth()" and "getHeight()" and that seems to get the right scale, however the image is still not showing up, and is just white.  I understand white is the default color for lwjgl if none is set. 

I'm still not sure why this is happening.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2012-03-28 06:03:55 »

Note the differences:
1  
2  
3  
Texture.getImageWidth() -> the width of the loaded image in pixels for glVertex2f (e.g. 384 px)
Texture.getTextureWidth() -> the converted power-of-two size (e.g. 512 px)
Texture.getWidth() -> the normalized width of the texture for glTexCoord2f (e.g. 0.75)


Also there's no real need for push/pop/translate matrix. Read here for details on Slick's texture loader and how to place the texcoords properly:
http://slick.javaunlimited.net/viewtopic.php?p=25911#p25911

If it's still white you could try replacing bind() with:
1  
GL11.glBindTexture(texture.getTextureID());

Offline loom_weaver

JGO Coder


Medals: 17



« Reply #4 - Posted 2012-03-28 07:22:17 »

snip...

Basically what davedes said... looking at the code in my game my texture coordinates ranged from 0.0f to 1.0f as 1.0f indicates the length of the entire texture.
Offline QuicK

Junior Member





« Reply #5 - Posted 2012-03-28 15:14:35 »

Thanks for the reply.

My new code looks like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
glBegin(GL_QUADS);
         {
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
   
            glTexCoord2f(0, texture.getHeight());
            glVertex2f(0, texture.getImageHeight());
   
            glTexCoord2f(texture.getWidth(), texture.getHeight());
            glVertex2f(texture.getImageWidth(), texture.getImageHeight());
   
            glTexCoord2f(texture.getWidth(), 0);
            glVertex2f(texture.getImageWidth(), 0);
         }
         glEnd();


This should be exactly the same as what you wrote on http://slick.javaunlimited.net/viewtopic.php?p=25911#p25911
The image is still white, and from my understanding...the code in TextureLoader from lwjgl creates a black/completely transparent rectangle g.setColor(new Color(0f,0f,0f,0f));
I'm not sure why this white rectangle is even visible at all.

Also: the method glBindTexture takes two parameters...glBindTexture(target, textureID)...both integers.  I'm confused as to what target is.
but...bind() is a built-in method in the Texture class of lwjgl, so I can't see that being the issue when so many people can use it without any problems.
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #6 - Posted 2012-03-28 15:42:33 »

Have you enabled textures? GL11.glEnable(GL11.GL_TEXTURE_2D)

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline sproingie

JGO Kernel


Medals: 202



« Reply #7 - Posted 2012-03-28 16:35:53 »

1  
glTexCoord2f(0, texture.getHeight());


Texture coordinates range from (0.0,0.0) to (1.0,1.0).   You need to divide your coordinates by the width and height of your texture.
Offline davedes
« Reply #8 - Posted 2012-03-28 17:16:59 »

Looks like a slight bug in Slick in that it doesn't enable GL_TEXTURE_2D when loading...

Solution A: (best)
1  
2  
3  
4  
5  
6  
      try {
         GL11.glEnable(GL11.GL_TEXTURE_2D);
         tex = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("res/clouds.jpg"));
      } catch (IOException e) {
         e.printStackTrace();
      }


Solution B:
1  
2  
3  
4  
5  
6  
      try {
         tex = TextureLoader.getTexture("JPG", ResourceLoader.getResourceAsStream("res/clouds.jpg"));
         TextureImpl.unbind(); //'unbind' the texture after loading, forcing it to be re-bound next draw
     } catch (IOException e) {
         e.printStackTrace();
      }



EDIT: Also note that Texture and TextureLoader are not "built in classes of LWJGL" -- they are small utility classes that are extracted from the core of Slick2D, a 2D LWJGL library with a Java2D-like design. Other texture loaders exist (many that are better than Slick's) if you feel like taking a different approach, e.g. Matthias Mann's pure java decoders.

Offline QuicK

Junior Member





« Reply #9 - Posted 2012-03-28 17:29:24 »

Thank you.  (glEnable(GL_TEXTURE_2D))This got my texture loaded and showing up on the box that it's supposed to, not properly though.  
It is flipped on the x-axis, and it still has the white background...I'm using a PNG image with a transparent background.  

How can I get the image to only show up as what's drawn (without the white background)?

Edit: I was able to fix the flipped image by inverting the texture height.
and another question: how can I get a 16x16 image to show up across...say 64x64 pixels rather than 16x16?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #10 - Posted 2012-03-28 17:32:18 »

Make sure alpha blending is on:
1  
2  
        GL11.glEnable(GL11.GL_BLEND);
        GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);


If that is still not working it may be because of your PNG file. Test out some other transparent PNG files to see if it changes things.

You might like to visit a higher level library (like Slick2D or LibGDX) if you are still having trouble with these basic things. Wink

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #11 - Posted 2012-03-28 17:34:04 »

It is flipped on the x-axis, and it still has the white background...I'm using a PNG image with a transparent background.
Invert your 'v' texture coords (replace texture.getHeight() with 0 and vice versa).

Quote
How can I get the image to only show up as what's drawn (without the white background)?
Enable blending.

GL11.glBlendMode(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glEnable(GL11.GL_BLEND);

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline QuicK

Junior Member





« Reply #12 - Posted 2012-03-28 17:36:55 »

again thank you.  I probably should learn more about slick's stuff...I'm only having issues because if I had coded what he made, I would have made all of this automatic rather than having to enable every functionality that you want to use.  I'm also dissatisfied that these enabling functions weren't included in the texture tutorial on the lwjgl wiki :/

Once that last question is answered I can close this post Cheesy thanks everyone!

question: how can I get a 16x16 image to show up across...say 64x64 pixels rather than 16x16?
Offline davedes
« Reply #13 - Posted 2012-03-28 17:38:51 »

Quote
and another question: how can I get a 16x16 image to show up across...say 64x64 pixels rather than 16x16?
Do you want to tile it across the space? Or do you want to stretch it?

For the former: repeat the image drawing multiple times within glBegin/glEnd, using different x/y positions for the vertices.

For the latter: use the filter parameter in TextureLoader.getTexture -- GL_LINEAR (smooth; default) or GL_NEAREST (hard-edge). Then scale the value that you pass to glVertex -- i.e. instead of texture.getWidth() you would might use texture.getWidth()*4 or simply '64'.

Offline QuicK

Junior Member





« Reply #14 - Posted 2012-03-28 17:42:05 »

My goal is to stretch it, basically making 1 pixel into 4, so that I can use small images but make them any size I want (of course, not caring how they look graphically as long as they're not blurry)
Offline davedes
« Reply #15 - Posted 2012-03-28 17:43:52 »

As for the x-axis flip, it shouldn't happen with the code you posted earlier (i.e. the code I posted in the slick forums) if you've set your ortho mode like so:
1  
2  
3  
4  
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, width, height, 0, 1, -1);
      GL11.glMatrixMode(GL11.GL_MODELVIEW);

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #16 - Posted 2012-03-28 17:50:49 »

I'm also dissatisfied that these enabling functions weren't included in the texture tutorial on the lwjgl wiki :/

I think you're confusing yourself because you're learning three things at once (LWJGL, OpenGL and Slick) and getting confused about what does what. Because using LWJGL and Slick you're expected to have a certain amount of OpenGL knowledge.

OpenGL is the lowest-level graphics drawing commands (GL11, GL12, etc.). I'd suggest reading the red book which will teach you about things like texturing and blending.

LWJGL gives you a display (eg. Display.create()) and input, as well as letting you talk to OpenGL. LWJGL docs are here.

Slick sits on top of LWJGL and OpenGL and provides convenience classes like texture loading which you can do yourself but make things easier. Slick docs are here.

Perhaps try doing some more basic stuff from the red book so you understand the basics first.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline QuicK

Junior Member





« Reply #17 - Posted 2012-03-28 17:55:59 »

thanks for the suggestion. I'll be sure to take a look at the book!
Offline sproingie

JGO Kernel


Medals: 202



« Reply #18 - Posted 2012-03-28 19:31:52 »

You should keep in mind that most tutorial code using Slick assumes an origin at on the upper left with Y increasing downward, but most OpenGL tutorials will assume your origin is at the center with Y increasing upward.  The first is standard for most 2D graphics, while the second is for 3D.  So when you look at the red book, don't be surprised.

The gist is that for any tutorial, you should ask "what is the projection being used" if it doesn't say otherwise.
Offline QuicK

Junior Member





« Reply #19 - Posted 2012-03-28 23:17:22 »

@davedes --

I tried what you suggested, and created my texture using the following code:
1  
texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path), false, GL_NEAREST);


I also scaled the texture placement:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
glTexCoord2f(0, 0);
            glVertex2f(0, 0);
   
            glTexCoord2f(0, -texture.getHeight()*4);
            glVertex2f(0, texture.getImageHeight()*4);
   
            glTexCoord2f(texture.getWidth()*4, -texture.getHeight()*4);
            glVertex2f(texture.getImageWidth()*4, texture.getImageHeight()*4);
   
            glTexCoord2f(texture.getWidth()*4, 0);
            glVertex2f(texture.getImageWidth()*4, 0);


The underlying quad is the right size, but the texture is being tiled, not scaled.

Any further suggestions?
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #20 - Posted 2012-03-29 00:03:04 »

@davedes --

I tried what you suggested, and created my texture using the following code:

Just scale the positions, not the texture coordinates.

Seriously, just go and read the red book. It covers texture coordinates and they're pretty fundamental to doing anything with textures.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
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 (14 views)
2014-08-22 19:31:30

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

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

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

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

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

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

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

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

Norakomi (37 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!