Libgdx Fog of War (Texture blending), Framebuffer bug - java

i try to get a simple fog of war for my strategy game.
I fail at the moment on framebuffer drawing.
Im an absolute openGL beginner.
New Problem 1;
Contructor:
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
this.fbo = new FrameBuffer(Pixmap.Format.RGB565, width, height, false);
this.fboRegion = new TextureRegion(fbo.getColorBufferTexture(), 0, 0, 1000, 1000); // 1000,1000 = Test mapsize
fboRegion.flip(false, true);
Units.camera.setCam(new OrthographicCamera(fbo.getWidth(), fbo.getHeight()));
OrthographicCamera cam = Units.camera.getCam();
cam.position.set(fbo.getWidth() / 2, fbo.getWidth() / 2, 0);
cam.update();
Render part:
public void draw(float delta)
{
Gdx.gl.glClearColor(0, 0, 0, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Units.camera().update();
fbo.begin();
Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(Units.camera().getCam().combined);
batch.begin();
background.draw(batch);
this.unitDraw.draw(batch, delta);
this.shotDraw.draw(batch, delta);
batch.end();
fbo.end();
batch.begin();
batch.draw(fboRegion, 0, 0);
batch.end();
}
Problem:
The camera move wrong. The framebuffer and the textureregion move different speed.
My objects have a low resolution on the texture region because its scaling. But i need scaling my object pictures. How increase the resolution without scaling images to original size ? Scaling Batch before write in framebuffer ? or is this a camera problem ?
See: https://www.youtube.com/watch?v=sMGe1Sgh5JA&feature=youtu.be
The last post in this thread (http://badlogicgames.com/forum/viewtopic.php?f=11&t=4207) has the same problem.

Related

LibGDX FrameBufferObject and SpriteBatch layering and transparency issue

I have the following issue: When I render the fob to the screen it won't clear itself from the last time. But when I add the clear screen code, the background is not visible.
Code without clearing:
public void render(float delta) {
// Temporary variable
float zoom = camera.zoom;
frameBufferObject.begin();
{
// Render
sb.setProjectionMatrix(camera.combined);
sb.begin();
groundManager.render(sb);
sb.end();
}
frameBufferObject.end();
// Clear the screen
Gdx.gl.glClearColor(0.25f, 0.25f, 0.3f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// Render the images
sb.setProjectionMatrix(camera.combined);
sb.begin();
{
sb.draw(backgroundTexture,
camera.position.x - camera.viewportWidth * zoom / 2,
camera.position.y - camera.viewportHeight * zoom / 2,
camera.viewportWidth * zoom,
camera.viewportHeight * zoom);
sb.draw(frameBufferObject.getColorBufferTexture(),
camera.position.x - camera.viewportWidth * zoom / 2,
camera.position.y - camera.viewportHeight * zoom / 2,
camera.viewportWidth * zoom,
camera.viewportHeight * zoom,
0, 0, 1, 1);
// Render the ground
surfaceFragmentManager.render(sb);
}
sb.end();
}
Has this output:
But when I add
Gdx.gl.glClearColor(0.25f, 0.25f, 0.3f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
It just clears the screen and draws the background with a solid color.
How could I make fob clear itself, and have the background visible?
Thank you.
After painful 3 hours, the answer was as simple as adding the following fragment at the beginning of the FBO
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

Multiple cameras and ScissorStack?

I'm currently trying to make a game, and I'm still novice with using cameras, and I'm thinking that two OrthographicCameras may be necessary, but I'm not sure if that's the most efficient way, or even how to do so.
Basically, I want this to be the layout for it:
The Main Area is where the main stuff is, which is a Server Interface. The Game Level is where the actual game part is in. I am currently using a ScissorStack to cut the region, but with this demo, results make me question how to do this:
public class TestScissorStackAndCamera extends ApplicationAdapter {
private SpriteBatch batch;
private OrthographicCamera camera;
private Sprite sprite;
private int width, height;
#Override
public void create() {
Gdx.gl.glClearColor(0, 0, 0, 0);
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
batch = new SpriteBatch();
camera = new OrthographicCamera(width, height);
camera.position.set(width / 2, height / 2, 0);
camera.update();
createSprite();
}
private void createSprite() {
Pixmap map = new Pixmap(width, height, Format.RGBA8888);
map.setColor(Color.RED);
map.fillRectangle(0, 0, width, height);
map.setColor(Color.BLUE);
map.drawLine(width / 2, 0, width / 2, height);
map.drawLine(0, height / 2, width, height / 2);
Texture texture = new Texture(map);
sprite = new Sprite(texture);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined); // The Question!
batch.begin();
{
Rectangle scissors = new Rectangle();
Rectangle area = new Rectangle(10, 10, width - 20, height - 20);
ScissorStack.calculateScissors(camera, batch.getTransformMatrix(), area, scissors);
ScissorStack.pushScissors(scissors);
batch.draw(sprite, 0, 0);
batch.flush();
ScissorStack.popScissors();
}
batch.end();
}
public static void main(String[] args) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = "ScissorStack & Camera Test";
config.resizable = false;
new LwjglApplication(new TestScissorStackAndCamera(), config);
}
}
Questioning batch.setProjectionMatrix(camera.combined)
I marked a line in the code with a comment, The Question!, which is what is affecting the results. If I don't have it, using the camera.translate(...) method, the image is drawn at (0, 0) but what it does is it moves what part is viewed. If I do have that line, when I use the camera.translate(...) method, the image is drawn respectively to the position of the camera.
In respect to the game that I'm currently developing, it behaves awkwardly without the projectionMatrix not being set, but when I do set it, it messes up the positioning of the rest of the game. I even added some testing features, and it's not rendering inside of the correct ScissorStack
How could I go about setting up two cameras, or what could I do to set up what I'm trying to correctly and efficiently?
With my actual game (not the mock-up) this is what it is doing. It should be rendering inside of the red lines, but it's not:
If you'd like to see my current code for my GameLevel that is handling the ScissorStack and OrthographicCamera:
public GameLevel(int x, int y, int displayWidth, int displayHeight) {
this.x = x; // x = 10
this.y = y; // y = 10
this.displayWidth = displayWidth; // displayWidth = Gdx.graphics.getWidth() - x - 10
this.displayHeight = displayHeight; // displayHeight = Gdx.graphics.getHeight() - y - 120
camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(displayWidth / 2, displayHeight / 2, 0);
// FBLAGame.batch.setProjectionMatrix(camera.combined);
camera.update();
init();
}
...
#Override
public void render() {
Rectangle area = new Rectangle(x, y, displayWidth, displayHeight);
Rectangle scissor = new Rectangle();
Matrix4 matrix = FBLAGame.batch.getTransformMatrix();
ScissorStack.calculateScissors(camera, matrix, area, scissor);
ScissorStack.pushScissors(scissor);
renderLevel();
FBLAGame.batch.flush();
ScissorStack.popScissors();
Pixmap map = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Format.RGBA8888);
map.setColor(Color.RED);
map.drawRectangle((int) area.x, (int) area.y, (int) area.width, (int) area.height);
Texture t = new Texture(map);
map.dispose();
FBLAGame.batch.draw(t, 0, 0);
}

LWJGL Texture not stretching to Quads

I have been trying for hours to get a Texture in LWJGL to stretch to a quad.
Here is the code I am using for the quad:
private static void renderLoad() {
glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
texture.bind();
glPushMatrix();{
glBegin(GL_QUADS);
{
glTexCoord2f(0, 1);
glVertex2f(0, 0); //Upper-left
glTexCoord2f(1, 1);
glVertex2f(Display.getWidth(), 0); //Upper-right
glTexCoord2f(1, 0);
glVertex2f(Display.getWidth(), Display.getHeight()); //Bottom-right
glTexCoord2f(0, 0);
glVertex2f(0, Display.getHeight()); //Bottom-left
}
glEnd();
}glPopMatrix();
}
This is what the display looks like when I run it:
http://gyazo.com/376ddb0979c55226d2f63c26215a1e12
I am trying to make the image expand to the size of the window. The Quad is at the size of the window, but the texture seems to not stretch.
Here is what it looks like if I do not use a texture and I simple make the quad a color:
http://gyazo.com/65f21fe3efa2d3948de69b55d5c85424
If it helps here is my main loop:
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, displaySizeX, displaySizeY);
glLoadIdentity();
glOrtho(0, displaySizeX, 0, displaySizeY, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
texture = loadLoadingImage();
//This is the main loop for the game.
while(!Display.isCloseRequested()){
delta = getDelta();
updateFPS();
if(Display.wasResized()){
displaySizeX = Display.getWidth();
displaySizeY = Display.getHeight();
glViewport(0, 0, displaySizeX, displaySizeY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, displaySizeX, 0, displaySizeY, -1, 1);
}
render();
checkInput();
Display.update();
Display.sync(sync);
}
cleanUp();
return true;
How do I make the image stretch to the quad?
public void stretch() {
Color.white.bind();
texture.bind
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(100,100);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(100+texture.getTextureWidth(),100);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(100+texture.getTextureWidth(),100+character.getTextureHeight());
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(100,100+texture.getTextureHeight());
GL11.glEnd(); // all the 0's were originally 100 but it was off centered
}
texture = TextureLoader.getTexture("PNG",ResourceLoader.getResourceAsStream("res/texture.png"));
Try using this. This is usually how I do this.
Perhaps something is modifying the texture matrix. You could try adding a
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
to see if that affects anything.

Coordinate Issue with Libgdx and TiledMap

I have a problem with coordinates between Libgdx and TiledMap.
I created a map by TiledMap and on it I added a object layer(rectangle) and I want ,when I render in Libgdx the map,to add a font in the same position of rectangle.
For this reason in render method I make this:
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
renderer.setView(camera);
renderer.render();
MapObjects collisionObjects = map.getLayers().get("Rectangle").getObjects();
for(MapObject object : collisionObjects) {
if (object instanceof RectangleMapObject) {
RectangleMapObject rect = ((RectangleMapObject) object);
batch.setProjectionMatrix(camera.combined);
batch.begin();
font.setUseIntegerPositions(false);
font.draw(batch, "Test Position",rect.getRectangle().x,rect.getRectangle().y);
batch.end();
}
}
}
This is How I set camera and other in the create method:
#Override
public void create () {
font = new BitmapFont();
font.setColor(Color.BLACK);
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Gdx.input.setInputProcessor(this);
camera = new OrthographicCamera(w,h);
camera.translate(w/2, h/2);
camera.update();
batch = new SpriteBatch();
map = new TmxMapLoader().load("TestRectangleMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
}
My problem is that the font is drawn in incorrect position respect to the position of the rectangle that is in the tile map.
Could you help me to understand where is the problem.
Thank you very much for the time you spent.
Coordinates output
I done this:
System.out.println("X: "+rect.getRectangle().x);
System.out.println("Y: "+rect.getRectangle().y);
I get this:
X: 450.0
Y: 232.0
while the object's coordinates in TiledMap editor are:
X: 14,062
Y: 5,156
Let me try to adjust this just to clean things up a bit. I'm pretty sure you only need to begin one batch before drawing. So embedding then into you for loop I don't think is necessary.
There is a couple of things that should be on the console display to debug this further,
You can try to add the Gdx.app.log() method whenever you need something to output to the console.
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
renderer.setView(camera);
renderer.render();
batch.setProjectionMatrix(camera.combined);
batch.begin();
MapObjects collisionObjects = map.getLayers().get("Rectangle").getObjects();
for(MapObject object : collisionObjects) {
if (object instanceof RectangleMapObject) {
RectangleMapObject rect = ((RectangleMapObject) object);
font.setUseIntegerPositions(false);
font.draw(batch, "Test Position",rect.getRectangle().x,rect.getRectangle().y);
Gdx.app.log("My Game", "Rect X: "+rect.getRectangle().x+" Y : "+rect.getRectangle().y);
}
}
batch.end();
}
Adding logs to your code will help out when your trying to find out where an object is drawn.

JOGL - Texture shows blue scale

I'm trying to make a game in Java OpenGL (JOGL), but I have a problem with textures.
When I draw a quad with a texture, the only thing I see is the image in blue scale.
The following is my code:
Texture grass;
// public void init() in Base class that implements GLEventListener
try {
grass = TextureIO.newTexture(new File("src/com/jeroendonners/main/grass.png"), false);
} catch(Exception e) {
e.printStackTrace();
}
To render a quad with this texture I use the following code:
int x = 0;
int y = 0;
gl.glColor3f(1f, 1f, 1f);
this.grass.bind(gl);
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2d(0, 0);
gl.glVertex3f(0, 0, 0);
gl.glTexCoord2d(1, 0);
gl.glVertex3f(1, 0, 0);
gl.glTexCoord2d(1, 1);
gl.glVertex3f(1, 1, 0);
gl.glTexCoord2d(0, 1);
gl.glVertex3f(0, 1, 0);
gl.glEnd();
I have read here that I have to use GL_BGR instead of the default GL_RGB, but since that question initializes textures in a different way, I don't know what to do with it.
Maybe a note: I am using an old version of JOGL, 1.0 I think. That's because I had a course on school with this version.

Categories