0
\$\begingroup\$

In my libGDX 2D RPG-Game (like Zelda or something) I want to display some items as 3D-Objects. Since I'm using an OrthographicCamera like this:

this.gameWorldCam = new OrthographicCamera();
this.hudCam = new OrthographicCamera();
gameWorldCameraViewport = new ExtendViewport(FRUSTUM_WIDTH, FRUSTUM_HEIGHT, gameWorldCam);

I don't know how to render 3D-Object on it. In my render loop I call this:

// 2D
spritebatch.begin();
...
spritebatch.end();

// ----------------------------------------------
// Just some hardcoded model creation for testing
// ----------------------------------------------
Model model;
ModelInstance instance;
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(
  5f,
  5f,
  5f,
  new Material(ColorAttribute.createDiffuse(Color.GREEN)),
  Usage.Position | Usage.Normal
);
instance = new ModelInstance(model);
// ---------------------------------------------

// 3D
modelBatch.begin(gameWorldCam);
modelBatch.render(instance);
modelBatch.end();

// 2D
spritebatch.begin();
...
spritebatch.end();

What do I have to do, to get the proper result? Right now, nothing of the green 3D cube is visible.

\$\endgroup\$
  • \$\begingroup\$ It might be because of the place where the camera is and the place where that 3d object is rendered. \$\endgroup\$ – Joza100 Jul 31 '18 at 18:00
1
\$\begingroup\$

I don't have the reputation to post a comment, but it looks like it's a problem with the camera as the code looks fine. If the camera is inside the object it will not render properly. if your camera it's on the plane Z on the position 0 and your model it's also on that position your are not going to see it. Lower the z on the object or up the camera. Also the camera should have a far and a near values that will render the object even if you move the camera far away.

It also needs the material ambient set to more than > 0 to be visible or and environment added to the render that has it's own light.

In this last case you can add environments like this:

Environment environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f, 0.2f, 1f)); //20% of minimum light for all objects on this environment
modelBatch.render(instance, environment);

Using environments you can also use an environment with point of ligh to give a dynamic shadow to the object (note, it will not cast a shadow on his own):

//cast a light from a point. If the object has this enviroment it will be affected by it.
environmentSun.add(new PointLight().set(1f, 1f, 1f, posX, posY, posZ, 1000));

Please note that in this last case the point of light should be outside of the model or it will not work for the model.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.