Draw (shapes) to a texture/sprite in libGDX - libgdx

In my libGDX game I have several sprites that share the same texture. Now I want to "manually" draw onto some of the sprites (i.e. I want to change some of the pixels in some of the sprites).
How can I modify a texture that is shared amongst seveal sprites without affecting other sprites?
I guess I need to copy the texture before I set it to the sprite?

You can use custom shader to customize sprite texture.
Before drawing the sprite with spriteBatch, simply say:
spriteBatch.begin();
spriteBatch.useShader(shaderProgram1);
sprite1.draw(...);
spriteBatch.useShader(shaderProgram);
sprite2.draw(...);
...
spriteBatch.end();
If you aren't familiar with the shaders you can check this link:
https://github.com/libgdx/libgdx/wiki/Shaders
Also there is option to use frame buffer object, for texture customization, but I think if those texture difference aren't that huge, this is the best solution if you are looking for best performances.
Hope that this gives you an idea.

Related

Pixmaps and performance-LibGdx

In my game,I am planning to use Pixmap for drawing rectangle.Game logic is based on changing the size,angle,position etc of this rectangle object in a dynamic way.
Will Pixmap create any performance issues to the android game when I use it to implement the main game logic?
I have to change the properties of the rectangle object created by the pixmap frequently .Then how it will affect the entire game performance?
Do I have to find some other way to implement my requirements?Is it worth using Pixmaps ?
How it will differ if use sprite scale and rotation etc to manipulate image instead of using Pixmap?
According to your requirement As I think you have number of Rectangle. So create number of Sprite because one texture can be shared with number of Sprite.
Create one Texture and share with all of your Sprite(Rectangular object). Do your required manipulation on Sprite like color, size, rotation, scale.
And keep in mind :-
As a Pixmap resides in native heap memory it must be disposed of by calling dispose() when no longer needed to prevent memory leaks.

Drawing shapes by pixel in libgdx

I'm trying to draw shapes in libgdx that change constantly so I don't think using sprites will work, and I was wondering if it is possible to draw a shape using a function that is called for each pixel to determine if it should be drawn.
What I need to draw is part of a washer (an area bound by two concentric circles and two radiuses), with the circles and radiuses changing constantly.
What I want to know is wether here is a way to draw complex shapes that are determined by a function (the shape would consist of those (x,y) for wich theFunction(x,y)=true) instead of an image
Everything is possible. The best solution really depends on the details of what you want to create. Perhaps you can show an example of what you mean?
Without seeing an example, it looks you might want to start with ShapeRenderer. See the javadocs for detailed information on how to use it. That should get you started and if you find it to be insufficient in some way then you at least you have a more specific question.
Btw, using a Pixmap as suggested by #Ludevik is also possible, but since you want it to change constantly that would imply uploading the entire image each frame which is not very performant.
Would use of Pixmap help?
You can create a pixmap:
Pixmap pixmap = new Pixmap(300, 300, Pixmap.Format.RGBA8888);
and then draw pixels with specific color on that pixmap:
pixmap.drawPixel(x, y, color);
Then you can then create a texture from that pixmap and draw the texture. I'm not sure about the performance of such solution though.
See also Pixmaps in the libgdx wiki.

When should I decide to use Sprite over Texture and vice versa?

Say I have a main menu screen which has a Logo which is png. Would I draw it as a texture or convert it into a sprite then draw it? This logo would have no interactivity on it, it's just sitting there looking pretty.
Thanks!
It's up to you which you prefer. Sprite is a convenience class that allows a Texture or TextureRegion store its position, rotation and scale, etc. If you don't use Sprite, that just means you plan to store that info elsewhere to use when you call draw().
Typically, you should try to put a groups of 2D images into a single TextureAtlas using TexturePacker, from which you can extract TextureRegions or Sprites (which are TextureRegions with features) to draw as a batch with SpriteBatch. If you only have a couple images on screen, such as on the initial loading screen, then you might want to go with just a Texture, but generally, you want to minimize the number of unique Textures on screen at once for performance reasons.
For instance, the low-res 2D game I'm working on has only three image files. One is the logo that appears before anything else is loaded, so I just draw it as a Texture, centered on screen. The other two are TextureAtlases, containing hundreds of sprites. One that is loaded with nearest filtering, and the other that is loaded with linear filtering.

Is it possible to create a visualization like this in WebGL?

This visualization is created using Away3D (Flash):
www.guardian.co.uk/world/interactive/2011/mar/22/middle-east-protest-interactive-timeline
Would it be possible to create something of the same quality (re. interaction, animation, performance, pixel-perfection etc.) using WebGL?
Bonus: How would one set up the basic scene? (without interaction and animation)
Yes, it would be. The scene is not complex at all, so it would have good performance. Interaction, yes, depends how you implement it, but if you are doing project with, for example, Three.js it wouldn't be a problem. Pixel perfection, obviously, yes.
In the scene, you could have curved plane, with texture that is changing UVs of vertices when you use 'navigate'. Pins could be done with particle, or better, simple quads with transparent textures. To have pixel-precise pin picking, you could depth-test pins on ray trace, or use pin-shaped geometry with shader, which probably the best solution.
So, basic scene - curved plane with per-vertex-changing-UV, pin-shaped and texture pins.
Alternatively, you could do exactly the same thing with 2D canvas. All elements are just drawn and scaled, text would be much simpler, and picking would be with 2d calculations.
Hope this helps.

Drawing shapes on BitmapData, or geting its graphics context

what would be your recommendation for drawing shapes (rects, circles...) onto BitmapData, and how to effectively switch between colors.
Is there any way to get graphics context from BitmapData so I could easily paint shapes using graphics.draw...()?
Why do you want to use a BitmapData? Not sure what you're after, but after reading a couple of your questions it seems you're a kind of fighting against how flash works. I think you'll make things much easier for yourself if you use what's available already. BitmapData objects are meant mainly to manipulate pixels and don't expose methods for drawing shapes. A Graphics object (available through Sprite, Shape, etc) on the other hand, allows you to draw vector shapes easily.
So, the best approach for this, I guess, would be using the drawing API to do what you want and then, if needed, convert the graphic to a BitmapData.