How would you get a texture to always face the camera in LWJGL?
-siD-
This is called billboarding. And there are essentially two methods or doing it.
-
Don’t use the camera rotation as part of the view transform when you are billboarding. The translation part yes but not the rotation method.
-
You can create the geometry you’re rendering using the camera’s left and up axis for the geometry’s left and up axes. Example:
To render a quad of width “w” and height “h” at position “P” with a camera left axis “L” and up axis “U”, then the 4 points of the quad would be:
P,
P + wL,
P + wL + hU,
p + hU
For more help, ask a more specific question and give some information about your current setup.
I will be using it for a 2D sun texture in my world, for my setup I have a world class that stores the position of both the player position and the sun position and handles where the sun should in the world be based on the current time of day and the current location of the player in the world.
With the points P, P + wL, P + wL + hU, p + hU how would I plot them to get the texture to billboard?
Something like this (You should use a VBO or something though instead of direct mode, but at least it gives you a point of reference)
private void drawSun(final Point3DFloat upVector, final Point3DFloat rightVector) {
float sunSize = 7;
float sunDistance = Config.getRenderDistance() / 2;
GL11.glBindTexture(GL11.GL_TEXTURE_2D, TextureContainer.getTexture(PictureContainer.SUN[0]));
GL11.glBegin(GL11.GL_TRIANGLES);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
- (rightVector.getX() + upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
- (rightVector.getY() + upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
- (rightVector.getZ() + upVector.getZ())
* sunDistance
/ sunSize);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
+ (rightVector.getX() - upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
+ (rightVector.getY() - upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
+ (rightVector.getZ() - upVector.getZ())
* sunDistance
/ sunSize);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
+ (rightVector.getX() + upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
+ (rightVector.getY() + upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
+ (rightVector.getZ() + upVector.getZ())
* sunDistance
/ sunSize);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
+ (rightVector.getX() + upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
+ (rightVector.getY() + upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
+ (rightVector.getZ() + upVector.getZ())
* sunDistance
/ sunSize);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
- (rightVector.getX() - upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
- (rightVector.getY() - upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
- (rightVector.getZ() - upVector.getZ())
* sunDistance
/ sunSize);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(
Camera.getPosition().getX()
+ LIGHTPOSITION.get(0)
* sunDistance
- (rightVector.getX() + upVector.getX())
* sunDistance
/ sunSize, Camera.getPosition().getY()
+ LIGHTPOSITION.get(1)
* sunDistance
- (rightVector.getY() + upVector.getY())
* sunDistance
/ sunSize, Camera.getPosition().getZ()
+ LIGHTPOSITION.get(2)
* sunDistance
- (rightVector.getZ() + upVector.getZ())
* sunDistance
/ sunSize);
GL11.glEnd();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
}
Mike
For rendering, I am currently using display list for everything.
where would I get upVector and rightVector from and what is Config.getRenderDistance()?
Config.getRenderDistance() in the code above is the far perspective. You can just try out values that works out well for you.
The up and the right vector is calculated as such (you won’t have the crossproduct and such functions but you can find that very easily by googling).
Point3DFloat normalizedLookVector =
normalizeVector(new Point3DFloat(Camera.getPosition().getX() - Camera.getLookingAt().getX(), Camera.getPosition().getY()
- Camera.getLookingAt().getY(), Camera.getPosition().getZ() - Camera.getLookingAt().getZ()));
rightVector.setLocation(crossProduct(upVector, normalizedLookVector));
normalizeVector(rightVector);
upVector.setLocation(crossProduct(rightVector, normalizedLookVector));
normalizeVector(upVector);
Good luck!
Mike
Thanks ;D