I am trying to figure out how to flip a Texture in libgdx using Java. I can't find any methods of doing this and I can't just do some code to flip the Texture itself. Is there any way that I can flip the texture some way in libgdx?
You can make a Sprite object and when you initialize it, you can pass in the Texture object. Then you can call methods such as rotate, flip, etc. Here is a code example
Texture tex = new Texture("path");
Sprite sprite = new Sprite(tex);
sprite.flip(true, false);
true as the first parameter for flipping over x-axis and false for y-axis, which is what you are looking for in your specific example.
Related
It's my first time attempting to create a basic light system that uses a black texture with a white circle on top. I read various threads about the issue but I just don't know what I am doing wrong.
What I want is the ambient to be dark and the light to be well white but changing the spritebatch color to something darker will cause the light to be darker to EVEN if I reset the color when drawing the light texture
So this is what I want (did this by forcing the light texture draw 5 times but that isn't a solution, it's an hack):
This is what I get (only 1 light texture is drawn but isn't very visible):
This is what I get without darkening the spritebatch:
Main code:
Game.sb.begin();
//Make stuff darker
Game.sb.setColor(0.1f, 0.1f, 0.1f,1f);
sb.setBlendFunction(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
lvl.render();
//Reset color
Game.sb.setColor(1f, 1f, 1f,1f);
sb.setBlendFunction(GL20.GL_DST_COLOR, GL20.GL_SRC_ALPHA);
//This draws all lights by drawing a texture with the above blending function
lightM.render();
Game.sb.end();
Light object draw method:
Game.sb.setColor(c.r,c.b,c.g, 1f);
Utils.drawTexture(Assets.get("sprites/lightcircle2.png", Texture.class), pos, size, true);
Game.sb.setColor(1,1,1,1);
Am I making some kind of error with the setcolor? I been considering using a FrameBuffer but I am not sure if it will give me the light effect I want
By the way this is my light texture (it's CC0):
You can achieve your requirement by these ways :
By using Shaders. Here is a small video and article on that video.
By use FBO and Blending, Here is one of my answer on this topic.
You can use box2dlight, even without using box2dbody(if you don't want any shadows)
World world = new World(new Vector2(0,0),false);
RayHandler rayHandler = new RayHandler(world);
rayHandler.setCombinedMatrix(stage.getCamera().combined); //<-- pass your camera combined matrix
new PointLight(rayHandler,1000, Color.WHITE,radius,x_position,y_position);
And at last call rayHandler.updateAndRender(); after all your rendering in your render() method.
now I'm working at a depth of field shader.
Here's how it works:
I render the whole scene to a FrameBuffer then apply the shader on it.
But, there's a problem: How do I pass the depthbuffer as an uniform to the shader? I couldn't find it anywhere in FrameBuffer's methods.
Any help is appreciated.
Not a LibGDX user (I use LWJGL), but in OpenGL you need to pass framebuffers as textures to a shader. In LibGDX, FrameBuffer extends GLFrameBuffer. In order to get the depth texture of a FrameBuffer, you need to call getDepthBufferHandle(), then treat it as you would a texture.
is there a possibility to save the framebuffer to texture. So that i can reuse the framebuffer to create a new texture?
com.badlogic.gdx.graphics.glutils.FrameBuffer.getColorBufferTexture()
will return the same texture everytime.
Edit:
I calculate shadow and don't wanna recalculate the static lights. So i want to save the static lights / shadow calculation in a texture. Currently i have to make a new framebuffer for each light to save them. And thats inperformant.
You can override the setupTexture method of the FrameBuffer class to provide a different texture object. Look at the default implementation of setupTexture in the libgdx code.
I am extremely new to JOGL, and I have two main questions about JOGL, the first is about sprites and animations and the next is about textures.
(I am not doing 3d, this is all in the contexts of 2d)
1.) I have been able to create quads, polygons, and so forth in JOGL, but I am very curious, is that the only way to get a type of image that you can manipulate out there? For instance if you want a sprite of a man to appear in JOGL, and when you move the sprite it will move in the direction you make it but also display an animation. Is there a way to do this efficiently in JOGL, or do I have to scalp a shape out of polygons, one vertex at a time, and then paint a texture over it? Or is there a simpler method of just loading the sprite and displaying it right then and there and of course changing the image depending on user input. If there is a way to do it without creating a vertex for every corner of the sprite could you display some code, or a tutorial so I can grasp the idea of what you are doing.
2.) I have been searching around for a way of displaying textures in JOGL, but I am still confused on how to do so. Let me first show you the way I found of loading and display a texture on a shape. First I create 7 variables:
Texture image;
private String textureFileName = "images/crate.png";
private String textureFileType = ".png";
private float textureTop, textureBottom, textureLeft, textureRight;
Next in my init() method I load my image by doing the following:
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL_SMOOTH);
image = TextureIO.newTexture(getClass().getClassLoader().getResource(textureFileName),false, textureFileType);
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
TextureCoords textureCoords = image.getImageTexCoords();
textureTop = textureCoords.top();
textureBottom = textureCoords.bottom();
textureLeft = textureCoords.left();
textureRight = textureCoords.right();
Then in the display method I do the following
GL2 gl = drawable.getGL().getGL2();
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
image.enable(gl);
image.bind(gl);
gl.glBegin(gl.GL_QUADS);
gl.glTexCoord2f(textureLeft,textureTop);
gl.glVertex2d(c,a); // vertex 1
gl.glTexCoord2f(textureLeft, textureBottom);
gl.glVertex2d(c,b); // vertex 2
gl.glTexCoord2f(textureRight, textureBottom);
gl.glVertex2d(d,b); // vertex 3
gl.glTexCoord2f(textureRight, textureTop);
gl.glVertex2d(d,a); // vertex 4
gl.glEnd();
//Variables a,b,c,d are just doubles with certain values for each point
Now I just wish to know if this is an effective way to load images and display them if I am going to be having multiple textures. Seeing this way I have devised a plan on how to do that using this method but I want to know if this is the best way of doing it. I have see people talk about bmp loaders but hearing of what they do, mapping everything single integer value the image has, seems really complex. Is there no simple way to do it? But if not could someone show me how or point me into a direction of the most correct, and efficient way to load textures and display those textures.
One last thing, when I put the line:
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT);
Isnt this just basically just clearing out an integers for pixels, etc. in the buffer? But what does the | do anyways? Does it shift the bits?
Also what do these two lines of code do? Im confused by the comments. These two lines appear in my init() method for loading a texture:
// Use linear filter for texture if image is larger than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Use linear filter for texture if image is smaller than the original texture
gl.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Thank you for taking the time to read and respond to this message, Thank you.
-Dan
I recently started to work with box2d,
and currently I am having a problem with drawing box2d.
Right now, my boxes are drawn with only boundary lines. But I want to draw Textures at where the box exists.
Though, I am not sure if the right way is to assign texture to box2d, or draw texture using Sprite over the box.
Is there a way to assign texture to box2d? or another way to render textures when using box2d?
There are basically two approaches to this that I've used.
In a GameManager/GameWorld class implement a update/tick method that gets called every frame. After calling world->Step(delta, 10, 10); loop through all the bodies in the world and update their Sprites position. Note that when you create the bodies many will use the body.userData member to store a pointer to the Sprite "attached" to this body.
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext())
{
if (b->GetUserData() != NULL) {
CCSprite *spriteData = (CCSprite *)b->GetUserData();
spriteData.position = ccp(b->GetPosition().x * PTM_RATIO,b->GetPosition().y * PTM_RATIO);
spriteData.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
Say that you have an object that has a class called player. You can implement a tick/update method in this class that updates it's own sprite with the position of the body... like:
[self setPosition:ccp(body->GetPosition().x * PTM_RATIO, body->GetPosition().y * PTM_RATIO)];
You'll need to have a pointer to the b2_body and add this instance to the CCLayer where the main update/tick/step is implemented.
You have to find actual body position
for example, in render method you implement following things to find body position and attach texture to it.
ballPosition = ballBody.getPosition();
and apply position to texture drawn on screen