Shouldn't this work?
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.png"));
AtlasRegion region = atlas.findRegion("ape_glad");
Sprite ape= new Sprite(region);
Instead I get: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: data/texture.png at the first line above O.o
Thanks for helping!
First, you need to create a texture atlas, using TexturePacker is the recommended way for libgdx. It results in the texture image and another file (containing the required information for libgdx TextureAtlas).
In your code, you need to provide the atlas file to the constructor, see TextureAtlas() documentation, instead of the image itself:
TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("data/texture.atlas"));
(Notice the use of the 'atlas'-file instead of the image file)
Related
Currently I'm working on a android program using Mobile Vision. I am using the "TextRecognizer" class and one of the methods is .detect(Frame frame). Right now I have a image I want to input into it, however, the image is the file type "Bitmap". I have tried to convert it to "Frame" by casting it but that hasn't worked. If anyone has any suggesting it would be much appreciated.
Use the setBitmap method in the Frame.Builder class:
Frame outputFrame = new Frame.Builder().setBitmap(myBitmap).build();
I'm new to libgdx, and I get this exception:
com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: C:/Users/myname/Desktop/myLibgdxGame/android/assets/sprites.png/sprites.png
on the following function on the textureAtlas = new TextureAtlas("sprites.txt"); line and I can't understand what's wrong.
#Override
public void create() {
batch = new SpriteBatch();
textureAtlas = new TextureAtlas("sprites.txt");
banana = textureAtlas.createSprite("spr");
}
Anyone knows what is the problem?
Thank you!
The path which you have given for texture atlas is wrong . In your case, Run time exception occurs because the java is unable to locate your texture atlas. So make sure that the texture atlas path is correct.
I want to upload an internal png image to my backend, the API supplied with the backend only allows for byte[] data to be uploaded.
But so far, I haven't found a way of extracting byte[] data from a texture. If it's an internal resource or not, I'm not sure matters?
So what ways are there to achieve this using Libgdx framework?
The image I want to use is loaded with the AssetManager.
Before trying to do this, make sure to understand the following:
A Texture is an OpenGL resource which resides in video memory (VRAM). The texture data itself is not (necessarily) available in RAM. So you can not access it directly. Transferring that data from VRAM to RAM is comparable to taking a screenshot. In general it is something you want to avoid.
However, if you load the image using AssetManager then you are loading it from file and thus have the data available in RAM already. In that case it is not called a Texture but a Pixmap instead. To get the data from the pixmap goes like this:
Pixmap pixmap = new Pixmap(Gdx.files.internal(filename));
ByteBuffer nativeData = pixmap.getPixels();
byte[] managedData = new byte[nativeData.remaining()];
nativeData.get(managedData);
pixmap.dispose();
Note that you can load the Pixmap using AssetManager as well (in that case you would unload instead of dispose it). The nativeData contains the raw memory, most API's can use that also, so check if you can use that directly. Otherwise you can use the managedData managed byte array.
I normally load my spritesheet bundled with my app this way:
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile( "myspritesheet.plist");
CCSprite *pSprite = CCSprite::createWithSpriteFrame( CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("sprite_monster.png");
pSprite->setPosition(ccp(100.0f,100.0f));
this->addChild(pSprite);
However, as the number of textures are growing and to accomodate easier updates, I am planning to place some of the spritesheets to the server and download it to the sdcard (or Cache folder in iOS). However, I don't see ways of loading it in game.
This link show how to load the png texture. But if I have multiple sprite packed into a single texture, I'll need to load the .plist file and feed it into CCSpriteFrameCache. Anybody knows how to do that? or any other solution how to load the individual CCSprite from the texture that has multiple sprites?
Thanks in advance!
try this code:
CCSpriteBatchNode *batchNode = CCSpriteBatchNode::create("myspritesheet.png");
this->addChild(batchNode);
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("myspritesheet.plist");
CCSprite *pSprite = CCSprite::createWithSpriteFrameName("sprite_monster.png");
pSprite->setPosition(ccp(100.0f,100.0f));
this->addChild(pSprite);
I am trying to load .obj files into an Android project with LibGDX. The files have no texture file, but include materials in .mtl files. I'm using the latest official nightly, and rendering the object file only results in the object appearing white. How do I get the ObjLoader to use the .mtl file?
#Override
public void create() {
objLoader = new ObjLoader();
model = objLoader.loadObj( Gdx.files.internal("data/obj.obj"), true);
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
model.render();
batch.end();
}
This is how the code to render the object is called.
Here is a link to the ObjLoader class
https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g3d/loaders/wavefront/ObjLoader.java
What am I doing wrong? And why won't it load the .mtl file? From what I can understand, it should load a .mtl file that's in the same folder and same name as the .obj file.
EDIT
I have messed around a bit, putting some lines into the ObjLoader class to log what it is and isn't loading. It looks like it's loading the mtl file, and assigning each mtl to a Material instance, and it also looks like the obj is correctly asking for those materials.
Is there something I need to enable or otherwise do on the OpenGL end to make sure it's using these materials properly?
The ObjLoader and especially the MtlLoader it uses is very limited. Try using the new 3D api with fbx instead. Here's explained how to load a model: http://blog.xoppa.com/loading-models-using-libgdx/.
I found the very same issue, and that's exactly why I both reported it here:
https://github.com/libgdx/libgdx/issues/2441
and committed a fix to it here:
https://github.com/libgdx/libgdx/commit/d7e716351d26ddfba19ce9e0b3bdfb449dbc81b7
, supporting virtually all MTL parameters out there. Note that this is a WIP, and (hopefully) will get into trunk once it's finished.