NullPointerException? Not sure why. LIBGDX - java

I keep getting a NullPointerException on line 39, which is
modelBatch.begin(cam);
I have no idea as to why it's doing this. If you notice why, please tell me. I've been struggling with this problem all day now. I am new to android development, and am prone to making silly mistakes. Thank you for your help.
public class Loading implements Screen {
private boolean AP;
private Chemistry chemistry;
public PerspectiveCamera cam;
public ModelBatch modelBatch;
public Model model;
public ModelInstance instance;
public Lights lights;
public Loading(boolean AP, Chemistry chemistry) {
this.AP = AP;
this.chemistry = chemistry;
}
#Override
public void render(float delta) {
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.begin(cam);
modelBatch.render(instance, lights);
modelBatch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
modelBatch = new ModelBatch();
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));
cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
}
...
}

Move this to your constructor:
modelBatch = new ModelBatch();
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));
cam = new PerspectiveCamera(70, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
cam.position.set(10f, 10f, 10f);
cam.lookAt(0, 0, 0);
cam.near = 0.1f;
cam.far = 300f;
cam.update();
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.GREEN)),
Usage.Position | Usage.Normal);
instance = new ModelInstance(model);
like:
public Loading(boolean AP, Chemistry chemistry) {
this.AP = AP;
this.chemistry = chemistry;
//here
}

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 does foreach create an object?

Im learning by these tutourials g3d: https://xoppa.github.io/blog/using-materials-with-libgdx/
Now I have heard that the foreach loop creates an object and thats bad to use in the render method.
Example:
private PerspectiveCamera cam;
private CameraInputController camController;
private Shader shader;
private Model model;
private Array<ModelInstance> instances = new Array<ModelInstance>();
private ModelBatch modelBatch;
#Override
public void show() {
cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
cam.position.set(2f, 2f, 2f);
cam.lookAt(0, 0, 0);
cam.near = 1f;
cam.far = 300f;
cam.update();
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
ModelBuilder modelBuilder = new ModelBuilder();
model = modelBuilder.createSphere(2f, 2f, 2f, 20, 20,
new Material(),
VertexAttributes.Usage.Position |
VertexAttributes.Usage.Normal |
VertexAttributes.Usage.TextureCoordinates);
for (int x = -5; x <= 5; x+=2) {
for (int z = -5; z <= 5; z+=2) {
instances.add(new ModelInstance(model, x, 0, z));
}
}
shader = new TestShader();
shader.init();
modelBatch = new ModelBatch();
}
#Override
public void render(float delta) {
camController.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
for (ModelInstance instance : instances) {
modelBatch.render(instance, shader);
}
modelBatch.end();
}
Is this true does it create an Object?
What would be a solution?
No, it does not create an Object. The for-each loop is simply a syntactical shortcut. See Does the Java foreach loop create a new object?

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.

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