Jump to content

  • Log In with Google      Sign In   
  • Create Account


eCustic

Member Since 25 Oct 2011
Offline Last Active Jul 28 2012 04:43 PM
-----

Topics I've Started

[Solved] Applying Color Palette to Texture

06 July 2012 - 08:17 PM

Hi all,

Does anyone know how I could replace the colors on a texture in run-time using OpenGL (more specifically LWJGL)? All the sprites I am loading for my game consist only of 8 shades of grey, and what I want to do is replace each of those shades of grey with a specific color which is retrieved from another 1 x 8 pixel texture. The following picture demonstrates exactly what I want to be able to do.

Posted Image

The reason I want to do this is to be able to reuse the same sprite for multiple things in the game. A sword for example might have the same sprite but have a different color palette applied to it depending on which material it is made of.

I've implemented a system for this once in Java2D, which I obviously can't use now that I'm using OpenGL, but I was wondering whether this could be achieved with shaders or something. Please note that in this case I can't simply use color tinting or changing the hue, since I want to be able to control the specific color for each shade of grey (so that shadows can be bluish/purplish while highlights can be more yellow/orange etc.). In my Java2D game I had implemented it so that I had 4 channels on each sprite, grey, red, green and blue, and each channel could have its own color palette applied.

Thanks in advance. Posted Image

[Solved] Spritesheet Bleeding

06 July 2012 - 07:43 AM

I have been trying to implement animated sprites using a spritesheet in LWJGL, but I'm having some issues with my sprites bleeding into each other. My sprite sheet is 256 x 512 pixels, with 4 x 4 sprites which are each 64 x 128 pixels, for a walking animation in 4 directions. The problem is that each sprite is positioned so that the pixels of the feet are on the edge of the sprite which causes lines to appear above the head on the sprite below it. However, this doesn't happen consistently, only on certain parts of the screen apparently, and not always the same colour even though it should be the same colour. All of my sprites also seem to blur slightly sometimes.

Here is the code for the draw method of my Sprite class:

public void draw(float x, float y) {
  GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());

  float currentOffsetX = (float)(offsetX + currentFrame) * spriteTextureWidthRatio;
  float currentOffsetY = (float)offsetY * spriteTextureHeightRatio;

  GL11.glBegin(GL11.GL_QUADS);

  GL11.glTexCoord2f(currentOffsetX, currentOffsetY);
  GL11.glVertex2f(x,y);

  GL11.glTexCoord2f(currentOffsetX + spriteTextureWidthRatio, currentOffsetY);
  GL11.glVertex2f(x + width, y);

  GL11.glTexCoord2f(currentOffsetX + spriteTextureWidthRatio, currentOffsetY + spriteTextureHeightRatio);
  GL11.glVertex2f(x + width, y + height);

  GL11.glTexCoord2f(currentOffsetX, currentOffsetY + spriteTextureHeightRatio);
  GL11.glVertex2f(x, y + height);

  GL11.glEnd();
}

The spriteTextureWidthRatio and spriteTextureHeightRatio variables are spriteWidth / textureWidth and spriteHeight / textureHeight respectively.

I've read somewhere that the issue might be caused because I'm using floating point values for the texture coordinates. In this case the values for the coordinates will be either 0.0, 0.25, 0.50, 0.75 or 1.0 depending on which part of the sprite sheet is drawn, which would make OpenGL do some weird stuff where it offsets by a half pixel or something, causing the bleeding and the blurriness in some cases. I've heard that I could scale the texture instead using a texture matrix and then access the different parts of the spritesheet with integer values which in this case would be 0 to 4. I don't know how I would go about doing this (I'm fairly new to OpenGL) or if this would solve the problem at all.

I've seen other places where people recommend using a 1 pixel transparent border around every sprite. I think the problem with this solution is that it doesn't work for tilesets where you don't want a border between your tiles.

Either way, I'm hoping someone here can tell me what the right way to do it is. I'm probably only having this problem because I'm such a noob when it comes to OpenGL. Posted Image

Java - References

25 October 2011 - 07:09 AM

So I'm mainly a C# developer and I'm used to objects being passed by reference in methods. However I am currently attempting to make a small-medium sized game in Java and I've hit a bit of a bump.
Usually when I create my game objects I like each object to have a reference to my main Game object. So currently I have the following (only the relevant part of the code is shown here):

public abstract class Entity {
	private Game game;

	public Entity(Game game) {
		this.game = game;
	}
}

So when I instantiate a new Entity object from my Game class I just pass this in the constructor. I expected any changes made to the Game object to be reflected by the game variable of the Entity class, but due to Java's pass-by-value property they aren't. This however is not the main problem as I can live without having a Game reference in each entity. The problem is that I had the same thoughts when making my input handler. I'm currently using Java 2D for the rendering and I'm just drawing on a canvas (my Game class extends Canvas) so I'm just extending the KeyAdapter class with my KeyInputHandler and passing it to the addKeyListener() method in my Game class. However I'd also like to be able to pass the KeyInputHandler object to my Player object so that I can handle all movement of the player within the Player class. However, since the KeyInputHandler is just copied into the Player object it doesn't update the instance variables when the KeyEvents are fired.

I might be going about this the wrong way (this is how I usually do it with XNA in C#) or I might just be missing something in the way I've implemented it, but if anyone could help point me in the right direction I'd be really grateful. :)


I'll just paste some of the relevant pieces of code from my KeyInputHandler class and my Player class, so that it might be a bit clearer what I'm trying to do.


public class KeyInputHandler extends KeyAdapter {
	private boolean rightDown;
	private boolean leftDown;
	private boolean downDown;
	private boolean upDown;
	
	public boolean isRightDown() { return rightDown; }	
	public boolean isLeftDown() { return leftDown; }	
	public boolean isUpDown() { return upDown; }
	public boolean isDownDown() { return downDown; }	
	
	public void keyPressed(KeyEvent e) { ... } // here the rightDown, leftDown, etc. variables are changed.
	public void keyReleased(KeyEvent e) { ... }
}



public class Player extends Actor {
	public void updateInput(KeyInputHandler keyHandler) {
		if(keyHandler.isLeftDown() && !keyHandler.isRightDown()) { this.setHorizontalVelocity(-1); }
		if(keyHandler.isRightDown() && !keyHandler.isLeftDown()) { this.setHorizontalVelocity(1); }
		...
	}
}

PARTNERS