LibGDX FrameBufferObject and SpriteBatch layering and transparency issue - java

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);

Related

How to fix weird object stretching while rotating in OpenGL in x or y axis?

When I am using rotate matrix to rotate my square in openGL and when I want to rotate it around X or Y axis the whole object dissapears and stretches.Z axis is working fine. What could the problem be?
I have tried loading uniformMatrix4f before DrawArrays and it is not working.Still the same problem. Translation is working fine and scaling.
Render method
public void render(Player model, Cube cube_model, StaticShader shader) {
GL30.glBindVertexArray(model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(move_X,-Display.getHeight(),0f), 0f,0f,tilt_Y,1f));
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(0);
GL30.glBindVertexArray(0);
GL30.glBindVertexArray(cube_model.vaoID);
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
shader.loadModelMatrix(Maths.createModelMatrix(new Vector3f(0f,Display.getHeight() - 200f,0f), 0f,increaseRotation,0f,0.5f));
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 6);
increaseRotation += 1f;
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
}
Matrix math:
public static Matrix4f createModelMatrix(Vector3f t, float rx, float ry, float rz, float s) {
Matrix4f modelMatrix = new Matrix4f();
modelMatrix.setIdentity();
Matrix4f.translate(t, modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(rx), new Vector3f(1,0,0), modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(ry), new Vector3f(0,1,0), modelMatrix, modelMatrix);
Matrix4f.rotate((float) Math.toRadians(rz), new Vector3f(0,0,1), modelMatrix, modelMatrix);
Matrix4f.scale(new Vector3f(s,s,s), modelMatrix, modelMatrix);
return modelMatrix;
}
Projection matrix setup:
public StaticShader(String vertexShaderFileName, String fragmentShaderFileName) {
super(vertexShaderFileName, fragmentShaderFileName);
enableProgram();
loadProjectionMatrix(Maths.createOrthoMatrix(-Display.getWidth(), Display.getWidth(), -Display.getHeight(), Display.getHeight(), 0.1f, 1000));
disableProgram();
}
Cube vertices:
private static final float vertices[] = {
-Display.getWidth() / 5, 0.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, 0.0f, 0.0f, 0.0f,1.0f,1.0f,1.0f,
-Display.getWidth() / 5, Display.getHeight() / 5, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, 0.0f, 0.0f, 1.0f,1.0f,1.0f,1.0f,
Display.getWidth() / 5, Display.getHeight() /5, 0.0f, 0.0f,1.0f,1.0f,1.0f,
-Display.getWidth() / 5, Display.getHeight() / 5, 0.0f, 1.0f,1.0f,1.0f,1.0f
};
I am expecting the cube to rotate in place in Y axis normally but it is actually rotating and dissapears then a line of it appears stretched in middle and dissapears.
I want to rotate it around X or Y axis the whole object disapears and stretches.Z axis is working fine.
Of course.
Your object is a 2 dimensional object. It is just a plane with in the 2 dimensions x and y. The x and y axis are parallel to the viewport. The z axis points out of the viewport.
If you do a rotation around the z axis (in the xy plane), the object keeps its size and rotates.
But if you do a rotation around the x axis, then object seems to be stretched along y and disappears at an angle of 90° respectively 270° degrees.
if you do a rotation around the y axis, then object seems to be stretched along x.
The rotation around the x and y axis looks "flat", because of the Orthographic projection. Actually you've no projection matrix, so the scene is projected in parallel (orthographic).
If you want to "see" a 3 dimensional rotation, then you've to use a Perspective projection

Java Game: Changing the brightness or transparency of a image

Hello Everyone,
I am working on a 2D tile game and I want to work on light so at night it'll be darker and at night brighter. If I can't change the brightness, I could have it fade from a brighter texture to a darker. Here is the image drawing function I have:
public static void drawRectTexRot(Texture tex, float x, float y, float width, float height, float angle) {
tex.bind();
glTranslatef(x + width / 2, y + height / 2, 0);
glRotatef(angle, 0, 0, 1);
glTranslatef(- width / 2, - height / 2, 0);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(0,0);
glTexCoord2f(1,0);
glVertex2f(width,0);
glTexCoord2f(1,1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
}
Any help would be great. :)
-Ekrcoaster
In java you cange transparency of Graphics2D object by method :
float opacity = 0.5f;
g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
Value 0 is for transparent and 1 is opaque. In this case whatever you draw next
will be half transparent.
To change brightness of Image use RescaleOp. Example :
RescaleOp rescaleOp = new RescaleOp(1.3f, 0, null);
rescaleOp.filter(image, image);
The first argument of RescaleOp constructor determines brightness scale. Value of 1f means the same brightness, value 0.75f means image is darker by 25%. Value of 1.3f makes image brighter by 30 %.

Libgdx Fog of War (Texture blending), Framebuffer bug

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.

OpenGL 1.1 Changing Color changes previous color?

I am trying to render 2 2d rectangles after each other, I have the height and the width of the 2 rectangles together. Now when I set the color for the 2nd quad the first quad inherits my first color?
I have tried to use popmatrix along with pushmatrix but that makes no difference. I have also tried resetting the colors with glColor4f(1,1,1,1).
Here is my code:
protected void renderComponent(Frame component) {
Rectangle area = new Rectangle(component.getArea());
int fontHeight = theme.getFontRenderer().FONT_HEIGHT;
int titleHeight = 25;
translateComponent(component, false);
glEnable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
if(component.isMinimized()){
}
glBegin(GL_QUADS);
{
RenderUtil.setColor(titleColor);
glVertex2d(0, 0);
glVertex2d(area.width, 0);
glVertex2d(area.width, titleHeight);
glVertex2d(0, titleHeight);
}
glEnd();
glBegin(GL_QUADS);
{
RenderUtil.setColor(component.getBackgroundColor());
glVertex2d(0, 0);
glVertex2d(area.width, 0);
glVertex2d(area.width, area.height + titleHeight);
glVertex2d(0, area.height + titleHeight);
}
glEnd();
glEnable(GL_TEXTURE_2D);
theme.getFontRenderer().func_175063_a(component.getTitle(), getCenteredX(area.width, component.getTitle()), 6, RenderUtil.toRGBA(component.getForegroundColor()));
glEnable(GL_CULL_FACE);
glDisable(GL_BLEND);
}
And my util setcolor method:
public static void setColor(Color c) {
glColor4f(c.getRed() / 255f, c.getGreen() / 255f, c.getBlue() / 255f, c.getAlpha() / 255f);
}
You appear to be drawing the second rectangle over top of the first, thus making it appear you've changed the colour of the first.
use the coordinates below for the second cube instead
glVertex2d(0, titleHeight);
glVertex2d(area.width, titleHeight);
glVertex2d(area.width, area.height + titleHeight);
glVertex2d(0, area.height + titleHeight);
This will place the second rectangle below the first, and give it a height of area.height.

Billboard in LWJGL

I was trying to make a billboard using LWJGL and I succeeded partially. The quad appears and does face the camera when I strafe. However, as soon as I rotate the camera on any of the axises the quad also moves along the axises in different manners. Here is the code I use:
/***************************************************************************************************************************************************************************************************
* All rendering happens here...
**************************************************************************************************************************************************************************************************/
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
GL11.glLoadIdentity(); // Reset The View
//GL11.glLight(GL11.GL_LIGHT1, GL11.GL_POSITION, lightPosition); // Position The Light
GL11.glRotatef(360.0f - yrot, 0, 1.0f, 0);
GL11.glRotatef(lookupdown, 1.0f, 0, 0);
GL11.glTranslatef(-xpos, 0, -zpos);
GL11.glCallList(blocksList);
GL11.glCallList(tilesList);
GL11.glCallList(roofList);
/* Render billboards */
Billboard bb = lvLoader.currentLevel.billboards[0];
GL11.glPushMatrix();
GL11.glRotatef(360.0f - yrot, 0, -1.0f, 0);
GL11.glRotatef(lookupdown, -1.0f, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(bb.origin.x + 0.5f, bb.origin.y + 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x - 0.5f, bb.origin.y + 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x - 0.5f, bb.origin.y - 0.5f, bb.origin.z);
GL11.glVertex3f(bb.origin.x + 0.5f, bb.origin.y - 0.5f, bb.origin.z);
GL11.glEnd();
GL11.glPopMatrix();
Here are the functions controlling lookupdown and yrot.
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) { // Is PageUp Being Pressed?
lookupdown -= 1.0f; // Rotate The Secene Downwards
}
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) { // Is PageDown Being Pressed?
lookupdown += 1.0f; // Rotate The Scene Upwards
}
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
{
yrot -= 1.5f; // Rotate The Scene To The Left
}
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
{
yrot += 1.5f; // Rotate The Scene To The Left
}
if (Keyboard.isKeyDown(Keyboard.KEY_W))
{
adjustXZ((float) Math.sin(yrot * piover180) * 0.05f, (float) Math.cos(yrot * piover180) * 0.05f, false, false);
}
if (Keyboard.isKeyDown(Keyboard.KEY_S))
{
adjustXZ((float) Math.sin(yrot * piover180) * 0.05f, (float) Math.cos(yrot * piover180) * 0.05f, true, true);
}
if (Keyboard.isKeyDown(Keyboard.KEY_D))
{
adjustXZ((float) Math.sin((yrot-90) * piover180) * 0.05f, (float) Math.cos((yrot-90) * piover180) * 0.05f, false, false);
}
if (Keyboard.isKeyDown(Keyboard.KEY_A))
{
adjustXZ((float) Math.sin((yrot+90) * piover180) * 0.05f, (float) Math.cos((yrot+90) * piover180) * 0.05f, false, false);
}
private void adjustXZ(float x, float z, boolean xAdd, boolean zAdd)
{
if (collisionCheck(x, z, xAdd, zAdd))
return;
if (xAdd)
xpos += x;
else if (!xAdd)
xpos -= x;
if (zAdd)
zpos += z;
else if (!zAdd)
zpos -= z;
}
I'm not familiar with LWJGL so I'm not 100% sure what it expects, but based on my experience with other 3D API's it sounds very much like you've got your matrix transforms in the wrong order.
Try changing your code as follows:
GL11.glPushMatrix();
GL11.glTranslatef(bb.origin.x, bb.origin.y, bb.origin.z); // Translation here
GL11.glRotatef(360.0f - yrot, 0, -1.0f, 0);
GL11.glRotatef(lookupdown, -1.0f, 0, 0);
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex3f(0.5f, 0.5f, 0); // No translation here anymore
GL11.glVertex3f(0.5f, 0.5f, 0;
GL11.glVertex3f(0.5f, 0.5f, 0);
GL11.glVertex3f(0.5f, 0.5f, 0);
GL11.glEnd();
On a separate note, you're not using a conventional billboarding technique. Rather than having the billboards always face the camera, you're having them always be perpendicular to the camera. This will be noticeable for billboard objects near the edges of your view frustrum. I'll leave it up to you to look into this further though!

Categories