I've been trying to learn libgdx's 3D functionality, because I want to make a turn-based game set on a 3D hexagonally tiled sphere.
As a simple starter project, I decided to try to create a simple icosahedron (in the future, I plan to subdivide it to create my map). This is my Java code:
package com.origin.hexasphere.screens;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.*;
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.glutils.ShaderProgram;
import com.badlogic.gdx.math.DelaunayTriangulator;
/** First screen of the application. Displayed after the application is created. */
public class GameScreen implements Screen
{
public float phi = (float)(1 + Math.sqrt(5d))/2f;
public float[] vertices = new float[]
{
0, 1, phi,
0, -1, phi,
0, 1, -phi,
0, -1, -phi,
phi, 0, 1,
-phi, 0, 1,
phi, 0, -1,
-phi, 0, -1,
1, phi, 0,
-1, phi, 0,
1, -phi, 0,
-1, -phi, 0
};
private Mesh mesh;
private Model model;
private ShaderProgram shader;
private ModelBatch batch;
private PerspectiveCamera cam;
private ModelInstance modelInstance;
@Override
public void show()
{
DelaunayTriangulator triangulator = new DelaunayTriangulator();
this.cam = new PerspectiveCamera();
cam.position.set(20f, 20f, 20f);
cam.lookAt(0f, 0f, 0f);
this.batch = new ModelBatch();
this.shader = new ShaderProgram(
Gdx.files.internal("shaders/vertex.glsl"),
Gdx.files.internal("shaders/fragment.glsl"));
short[] indices = triangulator.computeTriangles(vertices, true).toArray();
this.mesh = new Mesh(false, vertices.length, indices.length,
new VertexAttribute(VertexAttributes.Usage.Position, 3, ShaderProgram.POSITION_ATTRIBUTE ));
this.mesh.setVertices(vertices);
this.mesh.setIndices(indices);
model = new Model();
model.meshes.add(mesh);
modelInstance = new ModelInstance(model);
}
@Override
public void render(float delta)
{
cam.update();
batch.begin(cam);
batch.render(modelInstance);
batch.end();
}
}
This is my fragment shader:
void main()
{
gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}
And this is my vertex shader:
attribute vec4 a_position;
uniform mat4 u_projectionViewMatrix;
void main()
{
gl_Position = u_projectionViewMatrix * a_position;
}
(I don't actually understand much about shaders outside of the very basics, so I'm trying to use the most minimal shaders I can find to test around with before I move onto something more complex).
My output is a completely black screen.
