ANZGC FlyingFalcon
ANZGC FlyingFalcon

Reputation: 101

Render a shape upon mouse click using Libgdx (Android)

Is it possible to render a sphere using Libgdx after the user clicks/taps on the screen, at the position chosen by the user? No matter what I try, it doesn't seem to work and either crashes the program or just does nothing.

I'd like a short demo code if possible - thanks!

Here's my entire class. I'm probably doing numerous things wrong :

package com.mygdx.game;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.FPSLogger;


/**
 * See: http://blog.xoppa.com/basic-3d-using-libgdx-2/
 * @author Xoppa
 */
public class Render implements ApplicationListener {
    public Environment environment;
    public PerspectiveCamera cam;
    public CameraInputController camController;
    public ModelBatch modelBatch;
    private FPSLogger fps;

    public Model model;

    @Override
    public void create() {
        Gdx.graphics.setContinuousRendering(false);
        DataBase.r = this;

        environment = new Environment();
        environment.set(new ColorAttribute(ColorAttribute.Specular, 0.4f, 0.4f, 0.4f, 0.6f));
        environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 0f, 0f, -1f));

        modelBatch = new ModelBatch();

        cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        cam.position.set(0f, 0f, 10f);
        cam.lookAt(0,0,0);
        cam.near = 1f;
        cam.far = 300f;
        cam.update();

        ModelBuilder modelBuilder = new ModelBuilder();
        model = modelBuilder.createSphere(1f, 1f, 1f, 30, 30,
                new Material(), Usage.Position | Usage.Normal);
        ModelInstance instance = new ModelInstance(model);
        instance.transform.setToTranslation(1, 1, 1);
        ModelInstance instance2 = new ModelInstance(model);
        camController = new CameraInputController(cam);
        Gdx.input.setInputProcessor(camController);
        DataBase.array.add(instance);
        DataBase.array.add (instance2);

        Gdx.input.setInputProcessor(new InputAdapter());

        if (Gdx.input.justTouched()) {
            System.out.println ("yes");
            ModelInstance Instance = new ModelInstance (model);
            Instance.transform.setToTranslation(Gdx.input.getX(),Gdx.input.getY(), 0);
            DataBase.array.add(Instance);
            Gdx.graphics.requestRendering();
        }

        fps = new FPSLogger();
    }

    @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);
        for (ModelInstance instance: DataBase.array) {
            modelBatch.render(instance, environment);
        }
        modelBatch.end();




        //fps.log();
    }


    @Override
    public void dispose() {
        modelBatch.dispose();
        model.dispose();
    }




    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

DataBase.array is just an ArrayList<ModelInstance>.

Upvotes: 0

Views: 136

Answers (1)

Alexios
Alexios

Reputation: 367

I don't understand what are you doing there , my style of writing maybe is different, but still I can provide a solution . I dont suggest to use Touch events and Texture creating on render method. But for the example :

//defined out of method
//OrthographicCamera cam , Boolean drawBoolean
//Texture texture ,SpriteBatch sb , Vector3 tmp
if (Gdx.input.justTouched()) {
      tmp = new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0);

      //your camera obj 
      cam.unproject(tmp);

      //Your sphere or rectangle object
      texture =  new Texture("yourTexture.png");
      drawBoolean=true;          
 }
 if(drawBoolean)
     //sprite batch , use your render() thing
     sb.draw(texture , tmp.x ,tmp.y);     
   }

I hope you can adapt it to your code.

Upvotes: 2

Related Questions