libgdx android doesn't show model (black screen) - java

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.

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.

Gdx.input.getY is flipped

I have an issue in LibGDX where when i call upon Gdx.input.getY(), it selects a pixel that's on the other side of the application relative to the center of the screen.
public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private Texture img;
private OrthographicCamera camera;
int xPos;
int yPos;
private Vector3 tp = new Vector3();
BitmapFont font;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("crosshair.png");
camera = new OrthographicCamera();
camera.setToOrtho(false, 1280, 720);
font = new BitmapFont();
}
#Override
public void render () {
yPos = Gdx.input.getY();
xPos = Gdx.input.getX();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.unproject(tp.set(xPos, yPos, 0));
batch.begin();
font.draw(batch,xPos + " , " + yPos, Gdx.input.getX() - 25, Gdx.input.getY() - 5);
batch.draw(img, xPos, yPos);
batch.end();
}
#Override
public void dispose () {
batch.dispose();
img.dispose();
}
Subtracting the viewport height with the touch location won't work, because that would be subtracting world coordinates with touch coordinates. (and even for a pixel perfect projection it would be height - 1 - y). Instead use the unproject method to convert touch coordinates to world coordinates.
There are two problems with your code:
You are never setting the batch projection matrix.
Even though you are using the unproject method, you are never using its result.
So instead use the following:
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
camera.unproject(tp.set(Gdx.input.getX(), Gdx.input.getY(), 0));
font.draw(batch,tp.x+ " , " + tp.y, tp.x - 25, tp.y - 5);
batch.draw(img, tp.x, tp.y);
batch.end();
}
I would suggest to read the following pages, which describe this and the reasoning behind it in detail:
https://github.com/libgdx/libgdx/wiki/Coordinate-systems
https://xoppa.github.io/blog/pixels/
https://github.com/libgdx/libgdx/wiki/Viewports
It's better to try this
yPos = camera.viewportHeight - Gdx.input.getY();

Fit Viewport Black Bars

I'm currently developing a game where you have to avoid Asteroids. To make the Game look same on every device I use the FitViewport. Unfortunately I somehow get White Bars on the top and on the Bottom instead of Black ones. My Game Background is also white, so it looks a bit weird.
GameScreen:
#Override
public void create()
{
float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
cam = new OrthographicCamera();
viewport = new FitViewport(MyGdxGame.WIDTH * aspectRatio, MyGdxGame.HEIGHT, cam);
[...]
}
#Override
public void render(SpriteBatch batch)
{
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
em.render(batch); //render Ship and Asteroids
[...]
}
#Override
public void resize(int width, int height)
{
viewport.update(width, height);
cam.position.set(MyGdxGame.WIDTH / 2, MyGdxGame.HEIGHT /2, 0);
}
I dragged the Ship into the white Bar.
LibGDX provides viewports as a more convenient way of dealing with different aspect ratios. You don't have to multiply MyGdxGame.WIDTH with aspectRatio. Just initialize it with MyGdxGame.WIDTH and MyGdxGame.HEIGHT.
Also, in resize function, you can change the cam position using viewport values (instead of using constants):
cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2, 0);
I found some issues in your code. for best practice while handling with the different screen ratio just try with the fill viewPort. Here is simply editing your code with the fill viewport . Just try it once.
#Override
public void create()
{
float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.position.set(0, 0, 0);
camera.update();
//1280 is the screen width and 800 is screen height
camera.setToOrtho(false, 1280, 800);
viewPort = new FillViewport(1280, 800, camera);
}
#Override
public void render(SpriteBatch batch)
{
batch.setProjectionMatrix(camera.combined);
batch.begin();
em.render(batch); //render Ship and Asteroids
[...]
}
#Override
public void resize(int width, int height)
{
viewPort.update(width, height);
}
just try with the above code . it will definitely work

Displaying 3D models with an OrthographicCamera in LibGDX

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();
}
}

Categories

Resources