smart-fun / smartGL

SmartGL is a Graphic Engine for creating Android Games and Apps. It is based on OpenGL and handles 2D Sprites and 3D Textured Objects.
Apache License 2.0
109 stars 24 forks source link

Collada DAE loader #27

Open creek23 opened 5 years ago

creek23 commented 5 years ago

In my quest to add skeletal 3D animation for smartGL, I made a Collada/DAE file loader

Simply download this ColladaModel.java, then place it in directory of {yourproject}/app/src/main/java/fr/arnaudguyon/smartgl/tools/{here}

DISCLAIMER: it is NOT rendering textured DAE just yet, nor animating 3D model -- I'm still working on it. ^UPDATE 1: it is already rendering textured DAE, BUT NO skeleton-animated 3D model, yet -- still working on it.

~creek23

creek23 commented 5 years ago

download this DAE file that was tested to work on my Collada loader.

I patterned its usage to WavefrontModel's

ColladaModel daeModel = new ColladaModel.Builder(getApplicationContext(), R.raw.spaceship_collada).create();
Object3D myDAE = daeModel.toObject3D();

~creek23

creek23 commented 5 years ago

Was able to finally render textured Collada (DAE)! -- current limitation is that mesh should be one geometry.

Usage is still same with WavefrontModel's

Texture mShipTexture = new Texture(getApplicationContext(), R.drawable.space_cruiser_4_color);
ColladaModel daeModel = new ColladaModel.Builder(getApplicationContext(), R.raw.space_cruiser_collada_textured)
                .addTexture("space_cruiser_4_color_png", mShipTexture)  // "space_cruiser_4_color_png" is defined in the space_cruiser_collada_textured
                .create();
Object3D myDAE = daeModel.toObject3D();

Still need to implement skeletal animation. ~creek23

creek23 commented 5 years ago

Had to quick fix the rendering of a single-geometry Collada but with multiple triangles.

Now, I tested it with a model which contains 4881 triangles, my framerate went down to 5fps. As per 3DMark (opengl benchmarking app), my phone is just that piece of crap. But I wonder, how come I could play Mobile Legends, PUBG quite well. I know about lowpoly but I'm pretty sure all the 3D objects in ML/PUBG would come close or even greater than 4881 triangles.

@smart-fun , is it so that the renderer is that slow with rendering? Screenshot_20190506-225702

~creek23

creek23 commented 5 years ago

So I was able to fix the FPS issue -- somehow, I generated an Object3D with multiple Face3D (one per triangle). I realized WavefrontModel was generating a single Face3D with all of the model's triangle inside as float array -- the class name Face3D is quite confusing to actually contain multiple triangles which in 3D terminology, a single triangle is a face; so, I assumed multiple Face3D should be contained inside an Object3D.

Anyway, my problem now is that rendering these Face3D with multiple triangles is causing to have some of the triangles misplaced somehow -- will upload the picture later.

@smart-fun was this the case when you first wrote WavefrontModel? if so, how were you able to fix it? If not, can you please review my ColladaModel?

~creek23

smart-fun commented 5 years ago

Hi @creek23 !

you did an incredible job! Sorry I did not have time to follow what you did and did not help, but it seems all is OK now.

I also had framerate issues, and the solution was to use triangle strips when possible. So triangles are "merged" in a bigger face. It was not so easy to do triangle strips, but I have no bug so far (I did have some at the beginning).

I'll have a quick look to your model and code.

Arnaud.

smart-fun commented 5 years ago

This is not very easy to navigate your code and see what is wrong. But you seem to say that the issue appeared when you did the fps optimization (from triangles to triangle strips). This part should be more or less the same as in my code. it is possible that there is a bug in my code but the wavefront faces are not built the same way as the dae faces and so I don't see the bug with my objects. Or there is a bug in your code when you do the triangle strips. are you sure all was perfectly displayed before the optimization?

regarding the architecture of the code, it would be probably better to split your code in 2 steps / files: the xml parsing which basically construct an object or scene which reflects exactly what is in the xml, and then you parse this object or scene to build a 3DObject. The usage from the app would remain the same, but inside the ColladaModel you could call a ColladaParser or something. (I know that I did not split nothing in WavefrontModel ^^ but it could also be done)

And probably it would be great that ColladaModel and WavefrontModel extend from the same abstract parent class (no idea of the name, let's say BaseModel) and put all the common code / classes in here. OK it does not solve your issue ;)

creek23 commented 5 years ago

are you sure all was perfectly displayed before the optimization?

Yes. even tried reverting, and the rendering was okay as shown in earlier post, and framerate went back to 5fps.

regarding the architecture of the code, it would be probably better to split your code in 2 steps / files

yeah, I plan to do further clean-up with the code as it soon as it gets the job done -- I try to avoid early optimization.

I know that I did not split nothing in WavefrontModel ^^ but it could also be done

was planning to extract Strip/IndexInfo and other common classes which is used by WavefrontModel and ColladaModel -- but, like I said, later. :)

Although, code-readability is a hindrance for you to help with my problem :(

Still, debugging my code now -- writing an ObjectToXML to check if I could generate the same XML data that was parsed. Somehow, using float-array seems to be the culprit -- still investigating, though.

~creek23

creek23 commented 5 years ago

And probably it would be great that ColladaModel and WavefrontModel extend from the same abstract parent class (no idea of the name, let's say BaseModel) and put all the common code / classes in here

So, here's the BaseModel, and updated WavefrontModel. As well as ColladaModel with its ColladaParser.

Still haven't solved the abnormal, misplaced triangles though. :(

~creek23