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.
Related
I am trying to download an image using this code in my unity project my an android game.
public static Texture2D userTexture=new Texture2D(64, 64, TextureFormat.DXT5, false); //TextureFormat must be DXT5
public static IEnumerator UserPictureCallback(){
Debug.Log("USerPicture called");
FbDebug.Log("USerPicture called");
WWW url = new WWW("http://i1.wp.com/a0.twimg.com/sticky/default_profile_images/default_profile_1_normal.png?resize=64%2C64");
yield return url;
//profilePic.renderer.material.mainTexture = textFb2;
Debug.Log(url);
url.LoadImageIntoTexture(userTexture);
Debug.Log("Working");
}
And then I am trying to draw it in GUI using DrawTexture.
GUI.DrawTexture(new Rect(5,0,50,50),FB.userTexture,ScaleMode.StretchToFill, false, 0.0f);
But the image texture is not getting downloaded or at least it is not showing in the GUI. Can somebody help me with this?
I recently faced the same problem. I was creating an scrollable image gallery in Unity, where images are downloaded in real-time from the a web server. On iOS devices I was reaching very fast the memory limit (with consequent app crash). That was caused due to a very big number of www objects leaved in the memory and never deleted or released. Also a very big number of Texture2D objects leaked.
The main misunderstanding is considering a Texture2D like a variable of an object that self-contained the image information. Texture2D only point to a referenced asset so we need to create one before assigning to our GUI object. Without creating one we will overwrite the referenced asset in our project, leading to very crazy behaviour.
So this the code that works for me on iOS and just use the minimum of memory
public void DownloadImage(string url)
{
StartCoroutine(coDownloadImage(url));
}
IEnumerator coDownloadImage(string imageUrl)
{
WWW www = new WWW( imageUrl );
yield return www;
thumbnail.mainTexture = new Texture2D(www.texture.width, www.texture.height, TextureFormat.DXT1, false);
www.LoadImageIntoTexture(thumbnail.mainTexture as Texture2D);
www.Dispose();
www = null;
}
A brief comment:
You receive a url you will download the image from.
Start the coroutine that manage the download
Create the www object in the local scope
yield waiting the download to complete
Create a new Texture2D object/asset with parameters and assign to your final object mainTexture or what do you want
Use the "www.LoadImageIntoTexture()" function to COPY the image inside the created asset (this is the fundamental part)
Dispose and set to null the www object to prevent memory leaking or orphans
Hope it helps who will face the same problem.
An interesting lecture is this website where they implement a WebImageCache system to avoid re-downloading the same image many times.
http://studiofive27.com/index.php/unity-cached-web-images/
I need a (really fast) way to check if a JPG file is in RGB format (or any other format that Android can show).
Actually, at this moment, I just know if a JPG file can be show when I try to convert it to Bitmap using BitmapFactory.
I think this should not be the fastest way. So I try to get it by using ExifInterface. Unfortunately, ExifInterface (from Android) does not have any tag that indicates that the jpg can be shown in Android (color space tag or something).
Then, I think I have 2 ways:
1) A fast way to get bitmap from jpg: any tip of how to do it?
2) Or try to read Exif tags by my self, but without adding any other lib to the project: I don't have any idea of how to do it!
Ok so I did some looking around and I may have a solution for you but it may require a little work. The link is a pure java library that I think you can use in your project or at least modify and include a few classes. I have not worked with this yet but it looks like it will work.
http://commons.apache.org/proper/commons-imaging
final ImageInfo imageInfo = Imaging.getImageInfo(File file);
if(imageInfo.getColorType() == ImageInfo.COLOR_TYPE_CMYK){
}
else {
}
I'm currently trying to make an android dicom app
Following code opens pictures drom res/drawable in "ussual" image formats, but it doesn't work with .dcm
public class BitmapView extends View
{
public BitmapView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.test);
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bmp, 10, 10, null);
}
}
in the main activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new BitmapView(this));
}
thanks in advance!
Dicom is a kind of generic container. Inside a Dicom file you can find a huge variety of image formats. From grayscale ones to RGB, from single frame to multiframe ones, with pixel value ranges not in the ordinary 8 bit (24/32 in RGB/RGBA) but also in 12 or 16 bit grayscales.
Dicom files include many elements (fields) indicating the type of the contents and even how such contents should be presented. It is not as simple as converting the Dicom image to BMP.
If you are retrieving Dicom images from a PACS, I would suggest using WADO service. This way, you can obtain Jpeg images (the results of having applied a presentation state to the contents of the Dicom file).
The other option is to use some utility to convert the Dicom file to a more conventional image format. There are some excellent open source tools, such as dcmj2pnm, from the DCMTK toolkit.
Dicom images are not recognized by Android and therefore you cannot open them using a BitmapFactory object.
Recently Imebra has been extended with Java wrappers that call the Imebra native code and comes with a pre-built JAR so you don't have to deal with JNI compilation.
In addition to the 2 options mentioned by jap1968 and Paolo, which are both good but require using some method for converting the image on a different platform then viewing it on the Android device and an internet connection, you can actually view the Dicom file itself on the Android device directly if you use a library that supports DCM and its different sub-types. One such toolkit has a free demo application on Google Play here:
https://play.google.com/store/apps/details?id=leadtools.datasetdemo
I am using libgdx 0.9.8 and trying to load .obj files as Mesh.But as specified in various tutorials and in the documentation its not taking Stream as paramtetr rather its asking for FileHandle. What should be passsed is it because of the version ?
I am currently using this code, but I am still struggling with texture loading:
ObjLoader model = new ObjLoader();
meshCar = model.loadObj(
Gdx.files.internal("data/car_white.obj"),
Gdx.files.internal("data/car_white"),
true
);
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)