I've created an Android application to show about a hundred point objects using Libgdx modelBuilder. It works fine and models are rendered without any problem, but I needed to add a marker for every point model. To do that I've extended from Actor and in this class, I've created two textures representing the marker in normal and the selected mode when the user clicked the objects. The following is the LabelActor class which has been instantiated for each point model.
public class LabelActor extends Actor {
Texture texture;
Texture selectedTexture;
Sprite sprite;
private Vector3 distVector = new Vector3();
public LabelActor( String markerColor){
String path = "markers/point_marker_" + markerColor + ".png"";
String selected_path = "markers/point_marker_select.png";
texture = new Texture(Gdx.files.internal(path));
selectedTexture = new Texture(Gdx.files.internal(selected_path));
sprite = new Sprite(texture);
setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
}
#Override
protected void positionChanged() {
sprite.setPosition(getX(),getY());
super.positionChanged();
}
#Override
public void draw(Batch batch, float parentAlpha) {
if (selected){
batch.draw(selectedTexture, getX()-sprite.getWidth()/2, getY());
} else {
batch.draw(texture, getX()-sprite.getWidth()/2, getY());
}
}
}
The problem will be arisen on using the marker for these objects which causes a huge amount of memory usage. Loading a sphere model for each point objects takes about 14M of graphics memory but loading texture markers take 500M of memory on the device. I've used png icons located in the asset folders to create my textures but not using a Libgdx atlas. Is there any chance to create markers for this number of point objects to consume a little amount of memory?
Below is the image representing the view of the markers and point models.
Edit 1:
I've used AssetManager to load textures and create HashMap of them in the main screen then pass an array of two textures to each LabelActor class as Retron advised, but the memory is still overloaded.
public class LabelActor extends Actor {
Texture texture;
Texture selectedTexture;
Sprite sprite;
private Vector3 distVector = new Vector3();
public LabelActor(Texture[] textures){
texture = textures[0];
selectedTexture = textures[1];
sprite = new Sprite(texture);
setBounds(sprite.getX(), sprite.getY(), sprite.getWidth(), sprite.getHeight());
}
#Override
protected void positionChanged() {
sprite.setPosition(getX(),getY());
super.positionChanged();
}
#Override
public void draw(Batch batch, float parentAlpha) {
if (selected){
batch.draw(selectedTexture, getX()-sprite.getWidth()/2, getY());
infoWindow.draw(batch, parentAlpha);
} else {
batch.draw(texture, getX()-sprite.getWidth()/2, getY());
}
}
}
Part of main screen to load textures using AssetManager:
#Override
public void create() {
...
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.0f));
pointLight = new PointLight().set(0.8f, 0.8f, 0.8f, 2f, 0f, 0f, 500f);
environment.add(pointLight);
assetManager = new AssetManager();
textureTitleList = new ArrayList<>();
for (FileHandle f: Gdx.files.internal("markers").list()){
assetManager.load("markers/" + f.name(), Texture.class);
textureTitleList.add(f.name());
}
}
private void loadModel() {
textureMap = new HashMap<>();
for (String fName: textureTitleList){
textureMap.put(fName, assetManager.get("markers/" + fName, Texture.class));
}
}
#Override
public void render() {
if (!isLoaded && assetManager.update()) {
loadModel();
isLoaded = true;
}
...
}
You must use AssetManager and load ONE texture from asset, not create new texture for every new actor.
Related
I am currently working on an android app, and I want to include ARToolKit to scan pictures and show 3d models.
So I added the ARToolKit 6 to my Android Studio and I created the program itself fast. Next I wanted to add markers with addMarker, so basically my code was this.
class ARTrackingRenderer extends ARRenderer {
private static final class Trackable{
String name;
float height;
Trackable(String name, float height)
{
this.name = name;
this.height = height;
}
}
private static final Trackable trackables[] = new Trackable[]{
new Trackable("Alterra_Ticket_1.jpg", 95.3f),
new Trackable("Alterra_Postcard_2.jpg", 95.3f),
new Trackable("Alterra_Postcard_3.jpg", 127.0f),
new Trackable("Alterra_Postcard_4.jpg", 95.3f)
};
private int trackableUIDs[] = new int[trackables.length];
private Cube cube;
private Cube cube2;
#Override
public boolean configureARScene() {
int i = 0;
for (Trackable trackable : trackables){
trackableUIDs[i] = ARToolKit.getInstance().addMarker("2d;Data/2d/" + trackable.name + ";" + trackable.height);
if (trackableUIDs[1] < 0) return false;
}
NativeInterface.arwSetTrackerOptionInt(NativeInterface.ARW_TRACKER_OPTION_2D_MAX_IMAGES, trackables.length);
return true;
}
//Shader calls should be within a GL thread. GL threads are onSurfaceChanged(), onSurfaceCreated() or onDravFrame()
//As the cube instantiates the shader during setShaderProgram call we need to create the cube here.
#Override
public void onSurfaceCreated(GL10 unused, javax.microedition.khronos.egl.EGLConfig config){
this.shaderProgram = new SimpleShaderProgram(new SimpleVertexShader(), new SimpleFragmentShader());
cube = new Cube(30.0f, 0.0f, 0.0f, 0.0f); //Das erste ist die Größe, das zweite die horizontale Ausrichtung, das dritte die vertikale und das vierte der Abstand vom boden aus
cube2 = new Cube(15.0f, 50.0f, 0.0f, 20.0f);
cube.setShaderProgram(shaderProgram);
cube2.setShaderProgram(shaderProgram);
super.onSurfaceCreated(unused, config);
}
#Override
public void draw(){
super.draw();
GLES20.glEnable(GLES20.GL_CULL_FACE);
GLES20.glEnable(GLES20.GL_DEPTH_TEST);
GLES20.glFrontFace(GLES20.GL_CCW);
for (int trackableUID:trackableUIDs){
if (ARToolKit.getInstance().queryMarkerVisible(trackableUID)){
float[] projectionMatrix = ARToolKit.getInstance().getProjectionMatrix();
float[] modelVievMatrix = ARToolKit.getInstance().queryMarkerTransformation(trackableUID);
cube.draw(projectionMatrix, modelVievMatrix);
cube2.draw(projectionMatrix, modelVievMatrix);
}
}
}
}
I got the code from a Tutorial on YouTube. I want to add new markers. How can I do this? Or basically just how can I add new pictures, so it wont scan everytime the alterra pictures?
Thanks for all help.
This code:
private static final Trackable trackables[] = new Trackable[]{
new Trackable("Alterra_Ticket_1.jpg", 95.3f),
new Trackable("Alterra_Postcard_2.jpg", 95.3f),
new Trackable("Alterra_Postcard_3.jpg", 127.0f),
new Trackable("Alterra_Postcard_4.jpg", 95.3f)
};
Is responsible for adding the 'markers' called Trackable to ARToolKit. You can add your markers to the trackables array.
The .jpg files need to be available in the assets directory parallel to the Alterra*.jpg files.
Only .jpg files are possible.
Most of the Libgdx tutorials I found show how to add 2D elements in a 3D world, but I would like to know how to the the opposite, adding 3D elements in a 2D Stage.
I tried adding a background image to the Stage, then adding to the Stage an Actor that renders the model batch and the 3D instances in its draw() method.
But instead, the image isn't drawn and part of the 3D object is hidden.
SimpleGame class
public class SimpleGame extends ApplicationAdapter {
Stage stage;
#Override
public void create () {
stage = new Stage();
InputMultiplexer im = new InputMultiplexer(stage);
Gdx.input.setInputProcessor( im );
Image background = new Image(new Texture("badlogic.jpg"));
background.setSize(stage.getWidth(), stage.getHeight());
stage.addActor(background);
setup();
}
private void setup() {
SimpleActor3D group = new SimpleActor3D();
group.setSize(stage.getWidth(), stage.getHeight());
group.setPosition(0, 0);
stage.addActor(group);
}
#Override
public void render () {
stage.act();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT );
stage.draw();
}
}
SimpleActor3D class
public class SimpleActor3D extends Actor {
public Environment environment;
public PerspectiveCamera camera;
public ModelBatch modelBatch;
public ModelInstance boxInstance;
public SimpleActor3D() {
environment = SimpleUtils.createEnvironment();
camera = SimpleUtils.createCamera();
boxInstance = SimpleUtils.createModelInstance(Color.GREEN);
modelBatch = new ModelBatch();
}
#Override
public void draw(Batch batch, float parentAlpha) {
Gdx.gl.glViewport((int)getX(), (int)getY(), (int)getWidth(), (int)getHeight());
modelBatch.begin(camera);
modelBatch.render( boxInstance, environment );
modelBatch.end();
super.draw(batch, parentAlpha);
}
}
SimpleUtils class
public class SimpleUtils {
public static Environment createEnvironment() {
Environment environment = new Environment();
environment.set( new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f) );
DirectionalLight dLight = new DirectionalLight();
Color lightColor = new Color(0.75f, 0.75f, 0.75f, 1);
Vector3 lightVector = new Vector3(-1.0f, -0.75f, -0.25f);
dLight.set( lightColor, lightVector );
environment.add( dLight ) ;
return environment;
}
public static PerspectiveCamera createCamera() {
PerspectiveCamera camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(10f, 10f, 10f);
camera.lookAt(0,0,0);
camera.near = 1f;
camera.far = 300f;
camera.update();
return camera;
}
public static ModelInstance createModelInstance(Color color) {
ModelBuilder modelBuilder = new ModelBuilder();
Material boxMaterial = new Material();
boxMaterial.set( ColorAttribute.createDiffuse(color) );
int usageCode = VertexAttributes.Usage.Position + VertexAttributes.Usage.ColorPacked + VertexAttributes.Usage.Normal;
Model boxModel = modelBuilder.createBox( 5f, 5f, 5f, boxMaterial, usageCode );
return new ModelInstance(boxModel);
}
}
What I would like :
What I have instead :
I have tried rendering the model batch directly in the ApplicationAdapter render() method and it works perfectly, so the problems must lie somewhere with the Stage but I can't find how.
I had the same problem but I needed to render 3d object only once so I came with an idea to render 3d model as a Sprite. In order to do that I rendered my model via modelBatch to frame buffer object instead of default screen buffer and then created a sprite from FBO color buffer.
Sample code below:
FrameBuffer frameBuffer = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight(), true);
Sprite renderModel(ModelInstance modelInstance) {
frameBuffer.begin(); //Capture rendering to frame buffer.
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT | (Gdx.graphics.getBufferFormat().coverageSampling ? GL20.GL_COVERAGE_BUFFER_BIT_NV : 0))
modelBatch.begin(camera);
modelBatch.render(modelInstance);
modelBatch.end();
frameBuffer.end();
return new Sprite(frameBuffer.getColorBufferTexture());
}
You can always update your sprite texture in a render loop with use of sprite.setTexture(); method. You can also create an Image from a texture -> new Image(frameBuffer.getColorBufferTexture()); and use it in Scene2d.
subjet in the title
loading textures, making
private static Map<String, Texture> textureAlreadyLoad = new HashMap<String, Texture>();
source of loading
String location = tagMap.get("location");
Texture texture;
if(textureAlreadyLoad.containsKey(location)) {
texture = textureAlreadyLoad.get(location);
} else {
texture = new Texture(Gdx.files.internal(location));
textureAlreadyLoad.put(location, texture);
}
graphic = new StaticGraphic(state, new TextureRegion(texture));
support class
public class GraphicStorage {
private Map<Pair<String, State>, ActorGraphic> ActorsGraphic;
public GraphicStorage() {
ActorsGraphic = ResourceProvider.getMapGraphic();
}
public ActorGraphic getGraphic(String className, State state) {
return ActorsGraphic.get(new Pair<String, State>(className, state));
}
}
And then I just draw it by the spriteBatch — only 64x64 white rectangles (size of image)
It is a point that I use one image.
Is it good to storage Textures in Map?
I am new in AndEngine. I am using following code to display an image of a ball.
private ITextureRegion mBallTextureRegion;
private Sprite ball1;
#Override
public void onCreateResources() {
ITexture ball = new BitmapTexture(this.getTextureManager(),
new IInputStreamOpener() {
#Override
public InputStream open() throws IOException {
return getAssets().open("gfx/ball.png");
}
});
this.mBallTextureRegion = TextureRegionFactory.extractFromTexture(ring1);
....................
....................
}
#Override
protected Scene onCreateScene() {
final Scene scene = new Scene();
scene.attachChild(backgroundSprite);
...........
ball1 = new Sprite(192, 63, this.mBallTextureRegion, getVertexBufferObjectManager());
scene.attachChild(ball1);
..............
...........
}
Now, depending on the game level I want to add multiple ball of different size in the scene. Is it possible to add ITextureRegion mBallTextureRegion multiple time in different size(using different magnifying it)? If it is, then how? Please help me this sample code.
if you want to resize a Sprite,AnimatedSprite,Text,etc...
//the original image x2, 2f because the parameter is float
youSprite.setScale(2f);
if you use a texture region in more sprites:
Sprite youSprite;
//set deepCopy() in you texture to optimized memory
youSprite= new Sprite(0,0,youTexture.deepCopy(),mEnginge.getVertexTextureManager());
and if you want generate random position of each ball use "Random" variable.
best regards.
I'm creating an android app using Andengine. One part of the app requires users to select a few sprites from a group of sprites on the screen, which causes the selected sprites to turn a different color (ie, moving to the next tile). I declared them all as animated sprites and I'm using the same texture for each one. The problem is that once I select a sprite, every sprite moves to the next tile, not just the one I selected. How do I make just the one sprite change?
Here's where I setup the textures and whatnot:
private Texture mGreenTextureAtlas;
private TiledTextureRegion mGreenBallFaceTextureRegion;
#Override
public void onLoadResources() {
/* Textures. */
...
this.mGreenTextureAtlas = new Texture(32, 32, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
...
TextureRegionFactory.setAssetBasePath("gfx/");
/* TextureRegions. */
...
this.mGreenBallFaceTextureRegion = TextureRegionFactory.createTiledFromAsset(this.mGreenTextureAtlas, this, "green_ball.png", 0, 16, 2, 1); // 64x32
this.mEngine.getTextureManager().loadTextures(this.mCueTextureAtlas, this.mGreenTextureAtlas , this.mBackgroundTexture, this.mPocketTexture);
}
Here's where I actually create the sprites and apply the textures:
face = new AnimatedSprite(pX, pY, this.mGreenBallFaceTextureRegion);
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
encapsed = new Encapsulator(body, face, Encapsulator.AVOID_BALL, mFaceCount);
ballsList.add(encapsed);
I encapsulate each sprite, it's body, and some other data into an object that I made, and then add that object into an ArrayList.
Here is the onTouch event handler.
#Override
public boolean onAreaTouched( final TouchEvent pSceneTouchEvent, final ITouchArea pTouchArea,final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
if(pSceneTouchEvent.isActionDown()) {
final AnimatedSprite face = (AnimatedSprite) pTouchArea;
for(int i=0; i<ballsList.size(); i++)
{
if(face.equals(ballsList.get(i).animatedFace))
{
ballsList.get(i).toggleType(face);
System.out.println("Ball " + ballsList.get(i).id + " is now " + ballsList.get(i).type);
}
}
return true;
}
return false;
}
Finally, here is the toggleType method in the Encapsulator class that's responsible for moving to the next tile:
public void toggleType(AnimatedSprite face)
{
if(this.type == AVOID_BALL)
{
this.type = HIT_BALL;
face.nextTile();
}
else if(this.type == HIT_BALL)
{
this.type = AVOID_BALL;
face.setCurrentTileIndex(0);
}
}
Sorry if this is a bit long-winded. Any help is appreciated.
I did some more googling and came across a solution. I had to use the textureregion.clone() method when creating the sprites. I found the solution at this link:
http://www.andengine.org/forums/development/two-sprites-sharing-the-same-tiledtextureregion-t4339.html