Displaying 3D models with an OrthographicCamera in LibGDX - java

So basically I'm trying to display a .obj 3D Model with an OrthographicCamera in 2D rather than in 3D with a PerspectiveCamera.
Everything works fine when I render with the PerspectiveCamera, however if I switch it to Orthographic, nothing displays, just the blank background.
Below is the code I'm using with the PerspectiveCamera stuff commented out. I'm not sure if there is a different way that I need to setup the OrthographicCamera, or if I need to use a PerspectiveCamera with some modified to views to achieve this. At this point it really doesn't matter which axis I "eliminate".
Essentially what I want to achieve is being able to use Blender for my 2D animation. I've figured this out on the Blender side and it works very well, however they must be imported as .obj(or .fbx) into LibGDX, these are loaded into LibGDX as Models, and as such must be rendered with a ModelBatch using a ModelInstance and an Environment. I need a way to view this object using a 2D Orthographic projection rather than a 3D Perspective projection.
Anyone got any suggestions for this?
public class Test extends ApplicationAdapter {
//public PerspectiveCamera cam;
public OrthographicCamera cam;
public CameraInputController camController;
public ModelBatch modelBatch;
public ModelInstance ship;
public Environment environment;
#Override
public void create() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
/*
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 10f;
cam.update();
*/
cam = new OrthographicCamera();
cam.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.update();
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
ObjLoader loader = new ObjLoader();
Model model = loader.loadModel(Gdx.files.internal("data/invader.obj"));
ship = new ModelInstance(model);
}
#Override
public void render() {
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(ship, environment);
modelBatch.end();
}
#Override
public void dispose() {
modelBatch.dispose();
}
}

Related

Rendered 3d object using libgdx does not have colors as used to create using blender

I have created a 3d object using a blender. and I exported it as a g3db and gsdj types and used with libgdx. everything works fine but the colors of the object are not rendering as expected.
I tried using various ways to create an object and exporting with a blender. and In the past, I’ve tried libgdx-fbx-conv to convert fbx to g3db. and its also not working.
public class experiments extends ApplicationAdapter {
private ModelBatch modelBatch;
private Environment environment;
private PerspectiveCamera cam;
private Model model;
private ModelInstance instance;
private CameraInputController camController;
#Override
public void create() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
float color = 0.0001f;
environment.add(new DirectionalLight().set(color, color, color, -1f, -0.8f, -0.2f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(3f, 3f, 3f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 300f;
cam.update();
G3dModelLoader loader = new G3dModelLoader(new UBJsonReader());
model = loader.loadModel(Gdx.files.internal("test.g3db"));
instance = new ModelInstance(model);
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
#Override
public void render() {
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instance, environment);
modelBatch.end();
}
#Override
public void dispose() {
}
}
this is what blender shows
https://drive.google.com/open?id=1WAjrP_Z4IVjohk-CZSFeLk5st8PNOQGz
and this is what I have
https://drive.google.com/open?id=1AbRGLathCuESesTpcTFvKue49V1k533Z
That has most likely just something to do with the way it is being rendered. Blender renders it with either Cycles or Eevee (with Belender 2.8), while LibGDX uses OpenGl. It's like you make a photograph of the same object with two different cameras.

Libgdx doesn't load model.g3db on Android

It doesn't give any fatal exception or error, I just can see it on my device or in an emulator. The project is in Android Studio. I also tried to load the model with .g3dj extension.
#Override
public void create () {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(1f, 1f, 1f);
cam.lookAt(0,0,0);
cam.near = 1f;
cam.far = 300f;
cam.update();
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
assets = new AssetManager();
assets.load("piramid.g3db", Model.class);
loading = true;
}
private void doneLoading() {
Model ship = assets.get("piramid.g3db", Model.class);
ModelInstance shipInstance = new ModelInstance(ship);
instances.add(shipInstance);
loading = false;
}
#Override
public void render () {
if (loading && assets.update()) {
doneLoading();
}
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instances, environment);
modelBatch.end();
}
#Override
public void dispose () {
modelBatch.dispose();
instances.clear();
assets.dispose();
}
Does anyone know how can I render correctly the model?? Thanks.

LibGDX creating ModelInstance after create() has ended

Ok, I'm making a monopoly-like game where in the game-loop activity I have a fragment with a LibGDX inside, which represents the 3D board with pieces. I've been struggling in the last few days figuring out how can I create additional ModelInstances after the activity has loaded and the create() method in the LibGDX class has ended. Here is the Demo3D.java holding the board and pieces:
#Override
public void create() {
pieces = new ArrayList<ModelInstance>();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1f, 1f, 1f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.position.set(0f, 1.6f, -4f);
camera.lookAt(0f, 0f, 0f);
camera.near = 0.01f;
camera.far = 12f;
mapTexture = new Texture("monopoly_board.jpg");
mapSprite = new Sprite(mapTexture, 1024, 1024);
modelBatch = new ModelBatch();
modelBuilder= new ModelBuilder();
board = new Board3D(mapSprite, mapSprite);
board.worldTransform.rotate(Vector3.X, 270);
board.worldTransform.rotate(Vector3.Z, 180);
board.worldTransform.translate(0, 0, 0);
createPiece("blue");
createPiece("green");
Gdx.input.setInputProcessor(this);
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
camera.update();
modelBatch.begin(camera);
modelBatch.render(board);
for (ModelInstance piece: pieces) {
modelBatch.render(piece, environment);
}
modelBatch.end();
}
// Some other methods...
public void createPiece(String pieceColor){
// Some logic...
ModelInstance mi = new ModelInstance(modelBuilder.createCone(0.2f, 0.4f, 0.2f, 20,
pieceMat,
VertexAttributes.Usage.Position|VertexAttributes.Usage.Normal), -1.95f, 0.1f, -2.2f);
pieces.add(mi);
render();
}
So I basically need to be able to call a method from the activity/model which would result in invoking
createPiece(String color) in the Demo3D class, resulting in a new piece shown on the board.
I tested calling the createPiece() from the outside and it creates a new piece and shows it but the board disappears. I tried calling render() at the end of createPiece() and in this case the board doesn't disappear but the newly created piece is not present... Couldn't dig anything up so I'd be glad if someone helps..
Cheers
So I decided to make a button "Start" which when pressed gets all the players and creates pieces for every player. This is the Listener:
((Button) findViewById(R.id.populate)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boardFragment.populatePieces(currentGame.getPlayers());
rollDice.setVisibility(View.VISIBLE);
v.setVisibility(View.INVISIBLE);
}
});
Where the boardFragment.populatePieces(Arrl<Players>) is:
for (Player p: players){
instance.createPiece(p.getTokenColor());
}
The odd thing is that yesterday I decided to test this out without any changes and in Genymotion it runs perfectly, adding and removing pieces with no problems at all /Running HTC One X in Geny/. But just few hours ago I decided to test it out on my physical device and there it is the problem again... My device is Huawei Mate 7 with Lolipop 5.1
Update: It turns out the app doesn't work properly on Lollipop devices haven't tested it out on physical devices with Android 4/6, but in Genymotion everything is smooth with 4/6 devices..
I believe you need to make sure that in your drawScene() method, you iterate over the pieces collection and draw/render each component.

libgdx android doesn't show model (black screen)

I'm new in LibGDX.
I've tried to apply some tutorial I've read online to load a model in libGDX.
The problem is that I have a black screen when the app is loaded on my Galaxy Nexus (Android 4.3), no error according to LogCat.
The code is this:
#Override
public void create() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(7f, 7f, 7f);
cam.lookAt(0,0,0);
cam.near = 1f;
cam.far = 300f;
cam.update();
assets = new AssetManager();
assets.load("data/skeleton.g3db", Model.class);
loading = true;
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
private void doneLoading() {
Model I_model = assets.get("data/skeleton.g3db", Model.class);
ModelInstance I_instance = new ModelInstance(I_model);
I_instance.transform.setToTranslation(-5f, 0, -5f);
instances.add(I_instance);
loading = false;
}
#Override
public void render() {
if (loading && assets.update())
doneLoading();
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instances, environment);
modelBatch.end();
}
#Override
public void dispose() {
modelBatch.dispose();
model.dispose();
assets.dispose();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
The model "skeleton" is taken online as .fbx and converted in .g3db with fbx-conv.
Is a code or model error?
Any help is appreciated, thanks.
I think the problem is the direction of the light, because I think two points of lighting here telling that the model has textures appear.
is possible that the model is getting bad light,
use;
PointLight set (float r, float g, float b, float x, float y, float z, float intensity)
float intensity = 1f;
environment.add(new PointLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.8f, intensity));
and tray change variable intesity in test for example 100;
Hello and sorry for my English, I have not much experience with models, but when you assign
cam.position.set (7f, 7f, 7f);
cam.lookAt (0,0,0);
and then you say
I_instance.transform.setToTranslation (-5F, 0, -5F);
is not far outside the range of vision of the camera? it's just a question maybe this error out there
Edit;
I_instance.transform.setToTranslation(-5f, 0, -5f);
DirectionalLight().set(float r, float g, float b, float dirX, float dirY, float dirZ)
Dirz your not is beyond the scope of the model, -5F changes by aver 1f think if you see that the lighting is not arrived
I had the same problem and my problem was that the model loaded really big, and the camera was in the model. So try either scaling your model down, or moving the camera back a lot.

LIBGDX keeps printing "DefaultShaderProvider: Creating new shader" to terminal

I am creating a 3d game in libGDX. For some reason my game keeps printing
DefaultShaderProvider: Creating new shader
in the terminal. This is driving me absolutely insane because I can not debug my program with the terminal because of the spamming of that line. I checked my code for anything that would print this but I can not find anything that would.
Could anyone tell me why this is happening?
Here is my code:
public class puppetDemo implements ApplicationListener {
public PerspectiveCamera camera;
public ModelBatch modelBatch;
public ModelInstance box;
public ModelInstance sphere;
public Array<ModelInstance> instances = new Array<ModelInstance>();
public AssetManager assets;
public Lights lights;
public CameraInputController camController;
public boolean loading = true;
#Override
public void create() {
camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
camera.position.set(10f, 0f, -100f);
camera.lookAt(0, 0, 0);
camera.near = 0.1f;
camera.far = 300f;
camera.update();
lights = new Lights();
lights.ambientLight.set(0.4f, 0.4f, 0.4f, 1f);
lights.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f,
-0.2f));
assets = new AssetManager();
assets.load("data/box.obj", Model.class);
assets.load("data/sphere.obj", Model.class);
}
#Override
public void resize(int width, int height) {
}
#Override
public void render() {
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
modelBatch = new ModelBatch();
modelBatch.begin(camera);// Begin Rendering
modelBatch.render(instances, lights);
modelBatch.end();// End Rendering
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
modelBatch.dispose();
}
}
When you call new ModelBatch() in your render method, this is creating a new instance of the DefaultShaderProvider:
public ModelBatch() {
this(new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.ROUNDROBIN, 1)),
new DefaultShaderProvider(),
new DefaultRenderableSorter());
}
Looking at the source for DefaultShaderProvider you'll notice logging output in the createShader method:
#Override
protected Shader createShader(final Renderable renderable) {
Gdx.app.log("DefaultShaderProvider", "Creating new shader");
// ...
}
Instantiate modelBatch in the create method instead of in render and I suspect you'll only see the output once. If not, it might be worth filing an issue to have logging statement removed as it seems unnecessary.

Categories

Resources