Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (769)
Games in Android Showcase (230)
games submitted by our members
Games in WIP (855)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  (libGDX | Help) Rotate & zoom camera in/out and around a sphere (planet viewer)  (Read 108 times)
0 Members and 1 Guest are viewing this topic.
Offline unenergizer

Junior Devvie


Medals: 5
Exp: 5 years


Trying to figure it all out


« Posted 2018-10-08 19:02:51 »

Hello everyone!

As the title states I'm trying to make a simple 3d planet viewer. In this you can zoom in/out and rotate around the sphere object.  The sphere will be mapped with a texture that represents a world.

So far I was able to add the sphere and rotate, kinda, but my approach is not working. This is my first time working with 3d and I thought a 3d plant viewer would be a good and small introduction into 3d, but it's harder than I thought. xD

Here is my code:

WorldViewerMain.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
package world.viewer;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
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.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class WorldViewerMain extends Game {

    private PerspectiveCamera camera;
    private Model model;
    private ModelInstance instance;
    private ModelBatch modelBatch;
    private Environment environment;
    private BitmapFont font;
    private Batch batch;

    private float mainWorldSize = 10f;
    private float cameraX, cameraY, cameraZ;

    @Override
    public void create() {
        Gdx.input.setInputProcessor(new InputKeyboard(this));

        font = new BitmapFont();
        batch = new SpriteBatch();

        cameraX = cameraY = cameraZ = 10f;

        camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        camera.position.set(cameraX, cameraY, cameraZ);
        camera.lookAt(0, 0, 0);
        camera.near = 1f;
        camera.far = 300f;
        camera.update();

        ModelBuilder modelBuilder = new ModelBuilder();
        Material material = new Material(ColorAttribute.createDiffuse(Color.RED));
        final long attributes = VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates;
        model = modelBuilder.createSphere(mainWorldSize, mainWorldSize, mainWorldSize, 24, 24, material, attributes);
        instance = new ModelInstance(model);

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

    @Override
    public void dispose() {
        if (screen != null) screen.hide();
        model.dispose();
        font.dispose();
        modelBatch.dispose();
    }

    @Override
    public void pause() {
        if (screen != null) screen.pause();
    }

    @Override
    public void resume() {
        if (screen != null) screen.resume();
    }

    @Override
    public void render() {
        if (screen != null) screen.render(Gdx.graphics.getDeltaTime());

        Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

        camera.position.set(cameraX, cameraY, cameraZ);

        camera.rotateAround(Vector3.Zero, new Vector3(1, 1, 1), 1f);
        camera.update();

        modelBatch.begin(camera);
        modelBatch.render(instance, environment);
        modelBatch.end();

        batch.begin();
//        font.draw(batch, "MOUSE X: " + mouseX + ", Y: " + mouseY, 10, 60);
        font.draw(batch, "ZOOM: " + camera.position.x + " ," + camera.position.y + " ," + camera.position.z, 10, 40);
        font.draw(batch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 10, 20);
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
        if (screen != null) screen.resize(width, height);
    }

}


InputKeyboard.java
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
package world.viewer;

import com.badlogic.gdx.InputProcessor;

public class InputKeyboard implements InputProcessor {

    private WorldViewerMain worldViewerMain;

    public InputKeyboard(WorldViewerMain worldViewerMain) {
        this.worldViewerMain = worldViewerMain;
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return false;
    }

    @Override
    public boolean scrolled(int amount) {

        float x = worldViewerMain.getCamera().position.x;
        float y = worldViewerMain.getCamera().position.y;
        float z = worldViewerMain.getCamera().position.z;

        if (amount == -1) {
            worldViewerMain.setCameraX(x - 1);
            worldViewerMain.setCameraY(y - 1);
            worldViewerMain.setCameraZ(z - 1);
        } else if (amount == 1) {
            worldViewerMain.setCameraX(x + 1);
            worldViewerMain.setCameraY(y + 1);
            worldViewerMain.setCameraZ(z + 1);
        }

        return false;
    }
}


Thank you for any and all help! Please forgive me as I have tried to do some research.

Before I posted I purchased and read some of:
https://www.packtpub.com/game-development/building-3d-game-libgdx

I have also gone to:
https://github.com/libgdx/libgdx/wiki/3D-Graphics
http://www.gamefromscratch.com/post/2014/01/10/LibGDX-minimal-3D-app.aspx

If I have missed something I apologize!

JGO is Awesome!
Steam: unenergizer
Offline KaiHH

JGO Kernel


Medals: 614



« Reply #1 - Posted 2018-10-08 19:29:43 »

Search for "arcball camera".
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (1567 views)
2018-06-10 19:43:48

EgonOlsen (1538 views)
2018-06-10 19:43:44

EgonOlsen (1141 views)
2018-06-10 19:43:20

DesertCoockie (1567 views)
2018-05-13 18:23:11

nelsongames (1171 views)
2018-04-24 18:15:36

nelsongames (1548 views)
2018-04-24 18:14:32

ivj94 (2305 views)
2018-03-24 14:47:39

ivj94 (1514 views)
2018-03-24 14:46:31

ivj94 (2601 views)
2018-03-24 14:43:53

Solater (878 views)
2018-03-17 05:04:08
Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45

Deployment and Packaging
by philfrei
2018-08-20 02:33:38

Deployment and Packaging
by philfrei
2018-08-20 02:29:55

Deployment and Packaging
by philfrei
2018-08-19 23:56:20

Deployment and Packaging
by philfrei
2018-08-19 23:54:46
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!