I cannot print any message in the LogCat with libGDX using the method:
Gdx.app.log(tag, message);
In my onCreate method I've set:
Gdx.app.setLogLevel(Application.LOG_INFO); //debug or error, same story
Render method:
public void render() {
if (loading && assets.update())
doneLoading();
Gdx.app.log("application", "loaded"); //not showed
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instances, environment);
modelBatch.end();
}
doneLoading() method:
private void doneLoading() {
I_model = assets.get("data/male_teen.g3db", Model.class);
I_instance = new ModelInstance(I_model);
instances.add(I_instance);
loading = false;
}
Suggestions?
I've deleted the filter that is created automatically by Eclipse. Cleaned the project and reinstalled on the device. Now it works.
Related
I'm using libGdx 1.9.6 and I've got an issue. I've searched multiple forums, read various tutorials but nothing fits. I've created a simple cube in blender, textured it and export it to FBX (using fbx-conv). I've also downloaded the BDX-Blender-Exporter. I've tested Blender 2.69 and 2.76b without any changes to the result.
The model is loaded and shown :
blended cube
If I change the background color to (0,0,0,0) or (0,0,0,1) then only a black screen appears.
Here's the code (libGDX 1.9.6, Android-Studio 2.3.3)
public class modelloader implements ApplicationListener {
private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Model model;
private ModelInstance modelInstance;
private Environment environment;
private CameraInputController camController;
private AssetManager as;
#Override
public void create() {
camera = new PerspectiveCamera(
75,
Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
camera.position.set(0f,0f,7f);
camera.lookAt(0f,0f,0f);
camera.near = 0.1f;
camera.far = 300.0f;
modelBatch = new ModelBatch();
as = new AssetManager();
as.load("moon.g3db",Model.class);
as.finishLoading();
model = as.get("moon.g3db",Model.class);
model.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));
modelInstance = new ModelInstance(model);
modelInstance.transform.rotate(1, 0, 0, 0);
modelInstance.transform.translate(0, 0, -2);
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f));
environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -0.8f, 0.3f, 1f));
camController = new CameraInputController(camera);
camController.forwardTarget = true;
Gdx.input.setInputProcessor(camController);
}
#Override
public void dispose() {
modelBatch.dispose();
model.dispose();
}
#Override
public void render() {
camController.update();
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl20.glClearColor(1,1,1,0);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
camera.update(true);
modelBatch.begin(camera);
modelBatch.render(modelInstance, environment);
modelBatch.end();
}
The model is also only shown when the line
model.materials.get(0).set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA));
exists.
Here's the blender file :
cube.blend
Where could be the problem?
Holy mackerel, it was the texture file... it was 1024x1024 png... but somehow corruptet... once opened and resaved and all works.... countless hours and this is the only thing I've not checked... Thanks for the help guys! –
Android Studio-libgdx making a 2d game.
I have a Main which creates the MenuState, in the render method I made this
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
myGame.batch.begin();
stage.draw();
myGame.batch.end();
}
I checked some tutorials on how to make a menu and that's pretty much what I'm testing in here.
But when I run the app the button appears and nothing happens if I click it.
I've tried testing with the ClickListener but still doesn't work. Any ideas? Or am I doing it wrong or it should be in another method?
I'm new with libgdx. Thanks in advance.
You are creating new instances of the stage, skin and batch in render method? Move them away from your render method ;)
Render should look more like this:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0.2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
myGame.batch.setProjectionMatrix(camera.combined);
myGame.batch.begin();
stage.draw();
myGame.batch.end();
}
And in constructor:
batch = new SpriteBatch();
skin = new Skin(Gdx.files.internal("data/uiskin.json"));
stage = new Stage();
final TextButton button = new TextButton("Click me", skin, "default");
button.setWidth(400f);
button.setHeight(100f);
button.setPosition(Gdx.graphics.getWidth() / 2 - 100f, Gdx.graphics.getHeight() / 2 - 10f);
button.addListener(new ChangeListener() {
#Override
public void changed(ChangeEvent event, Actor actor) {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
});
stage.addActor(button);
Gdx.input.setInputProcessor(stage);
Im using libGDX and this is a Desktop project.
I have 2 models, one is the character and the other is the map, as you can see in the image below:
My question is:
How do I project the shadow of the character on the floor?
As you can see the character doesn't have a shadow, thus the Ambience Light. What do I have to use or how do I achieve this? Should I fake a shadow or is there a real way to project shadows?
Any comments or sugestions are welcome.
You can use following code:
Environment environment;
DirectionalShadowLight shadowLight;
#Override
public void show() {
modelBatch = new ModelBatch();
environment = new Environment();
environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1f, .6f, 1f));
environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 60f, 60f, .1f, 50f))
.set(1f, 1f, 1f, 40.0f, -35f, -35f));
environment.shadowMap = shadowLight;
shadowBatch = new ModelBatch(new DepthShaderProvider());
}
#Override
public void render(float delta) {
//create shadow texture
shadowLight.begin(Vector3.Zero, camera.direction);
shadowBatch.begin(shadowLight.getCamera());
shadowBatch.render(instances);
shadowBatch.end();
shadowLight.end();
//render scene
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(instances, environment); //environment has shadowMap!
modelBatch.end();
}
Sometime in 2015, a better shadow system was added to the libGDX testbed, separately from the "experimental, do not use" stuff mentioned in #Nolesh's answer.
The code is in the libGDX tests, so you'd need to copy it to your own project. A short description of how it works was posted on the libGDX forums by the author, realitix. Note that there doesn't seem to be a fully runnable example, but the JavaDoc for the ShadowSystem interface contains the following:
// Init system:
Array<ModelBatch> passBatches = new Array<ModelBatch>();
ModelBatch mainBatch;
ShadowSystem system = new XXXShadowSystem();
system.init();
for (int i = 0; i < system.getPassQuantity(); i++) {
passBatches.add(new ModelBatch(system.getPassShaderProvider(i)));
}
mainBatch = new ModelBatch(system.getShaderProvider());
// Render scene with shadows:
system.begin(camera, instances);
system.update();
for (int i = 0; i < system.getPassQuantity(); i++) {
system.begin(i);
Camera camera;
while ((camera = system.next()) != null) {
passBatches.get(i).begin(camera);
passBatches.get(i).render(instances, environment);
passBatches.get(i).end();
}
camera = null;
system.end(i);
}
system.end();
HdpiUtils.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
mainBatch.begin(cam);
mainBatch.render(instances, environment);
mainBatch.end();
It's not as good as having a proper API for shadowing, but better than having to implement the entire thing from scratch.
This problem only occurs in Windows. Runs fine in Linux.
I have a stack based state management system. When I switch from one state to the other, the display will flicker between the previous state and the current one. Its almost like one of the buffers are still displaying the previous state and alternating with the current state.
Here is my first state init, draw, and exit code:
init is called when the state first loads
draw is called each loop
pause is called before the new state is put on top of the stack.
private void init()
{
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,Game.getGameConfig().getDisplayWidth(),Game.getGameConfig().getDisplayHeight());
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Game.getGameConfig().getDisplayWidth(), Game.getGameConfig().getDisplayHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public void draw() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
titleFont.drawString((Game.getGameConfig().getDisplayWidth()/2)-(titleFont.getWidth(titleText)/2),
(Game.getGameConfig().getDisplayHeight()/2) - (titleFont.getHeight()/2) ,
titleText,Color.orange);
if(showStartText)
{
pressStartFont.drawString((Game.getGameConfig().getDisplayWidth()/2)-(pressStartFont.getWidth(pressStartText)/2),
(Game.getGameConfig().getDisplayHeight()/2) - (titleFont.getHeight() - 100),
pressStartText, Color.orange);
}
}
public void pause() {
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
And here is the next gameState code:
private void init()
{
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,Game.getGameConfig().getDisplayWidth(),Game.getGameConfig().getDisplayHeight ());
glMatrixMode(GL_MODELVIEW);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,Game.getGameConfig().getDisplayWidth(), Game.getGameConfig().getDisplayHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
public void draw() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
mainMenu.draw();
Display.update();
}
public void pause() {
glDisable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
}
This is how the new state is loaded.
The first state calls:
super.sm.push(new MainMenu(super.sm));
Here is the StateManager (sm) code:
public void push(GameState state)
{
if(!states.empty())
{
states.peek().pause();
}
states.push(state);
state.enter();
}
The main game loop is calling this method on the StateManager:
public void draw()
{
if(!states.empty())
{
states.peek().draw();
}
Display.update();
}
What causes the flickering between states happen on Windows?
You have a Display.update(); In both your StateManager Draw method, and in your gameState Draw method. Remove the one in your game state.
So I have a splash image that is not rendering and I'm not really sure why. I have done the correct process, creating anew sprite and then in my render method rendering it with the batch like so:
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
#Override
public void show() {
splashSprite = new Sprite(new Texture("data/xidstudios_splash.png"));
batch = new SpriteBatch();
}
As you can see, I have done the correct process of steps, am I missing one?
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
splashSprite.draw(batch);
batch.end();
}
You must provide the viewports to opengl either using a camera or directly calling the opengl func so that your projection are currectly set to view the image