Ich beschäftige mit gerade etwas mit OpenGL und versuche eine Kugel zu erzeugen.
Die Idee ist es einen Würfel zu erstellen und diesen anschließen "aufzublasen".
Dazu habe ich eine Klasse Quad, die eine Fläche mit einer bestimmten Orientierung erzeugt und eine Klasse Sphere, die für jede Richtung eine entsprechende Fläche erzeugen soll und diese dann in je ein Array mit Vertices und Indices für den Mesh kopieren soll.
Solange ich nur eine einzelne Seite erzeuge scheint das auch zu funktionieren. Sobald eine weitere Seite dazukommt, wird es konfus.
Das Ergebnis mit einer Seite (die andere funktioniert entgegengesetzt):
Und mit zwei Seiten:
Die Idee ist es einen Würfel zu erstellen und diesen anschließen "aufzublasen".
Dazu habe ich eine Klasse Quad, die eine Fläche mit einer bestimmten Orientierung erzeugt und eine Klasse Sphere, die für jede Richtung eine entsprechende Fläche erzeugen soll und diese dann in je ein Array mit Vertices und Indices für den Mesh kopieren soll.
Solange ich nur eine einzelne Seite erzeuge scheint das auch zu funktionieren. Sobald eine weitere Seite dazukommt, wird es konfus.
Java:
import org.joml.Vector3f;
public class Sphere {
private static final int MIN_RESOLUTION = 2;
private final static Vector3f[] DIRECTIONS = {
//new Vector3f(1f, 0f, 0f),
new Vector3f(-1f, 0f, 0f)
};
private final int resolution;
private float[] vertices;
private int[] indices;
public Sphere(final int resolution) {
this.resolution = resolution < MIN_RESOLUTION ? MIN_RESOLUTION : resolution;
create();
}
public Mesh getMesh() {
return new Mesh(vertices, indices);
}
private void create() {
vertices = new float[resolution * resolution * 3 * (DIRECTIONS.length)];
indices = new int[(resolution - 1) * (resolution - 1) * 6 * (DIRECTIONS.length)];
for(int ixDirection = 0; ixDirection < DIRECTIONS.length; ixDirection++) {
// create a new Quad for a given direction
Quad quad = new Quad(resolution, DIRECTIONS[ixDirection]);
// normalize and copy all vertices to the vertices array
Vector3f[] quadVertices = quad.getVertices();
int ixVertices = ixDirection * quadVertices.length;
for (int ixQuadVertices = 0; ixQuadVertices < quadVertices.length; ixQuadVertices++) {
Vector3f vertex = quadVertices[ixQuadVertices].normalize();
vertices[ixVertices++] = vertex.x;
vertices[ixVertices++] = vertex.y;
vertices[ixVertices++] = vertex.z;
}
// copy all indices to the indices array
int[] quadIndices = quad.getIndices();
int ixIndices = ixDirection * quadIndices.length;
for(int index : quadIndices) {
indices[ixIndices++] = index;
}
}
}
}
Das Ergebnis mit einer Seite (die andere funktioniert entgegengesetzt):
Java:
private final static Vector3f[] DIRECTIONS = {
new Vector3f(1f, 0f, 0f),
//new Vector3f(-1f, 0f, 0f)
};
Und mit zwei Seiten: