Hello Monkeys,
tl;dr: If you have little time, I am happy with your idea: Is that a major problem, such as a problem in maths or general understanding, or is it just a little typing mistake that I have to find somewhere? Thanks!
I am currently experimenting with different generated planes, as a foundation for a subsequent generated terrain.
That plane I want to generate (numbers are vertex indexes):
I think I’ve made code that is well understandable, through the using of named variables and console outputs that make transparent what the code did there.
The last change however needs a comment line: As I want the plane to generate from 0 towards -z direction, I’ve added “-1 * …” to the z-Coordinates of vertex position generation, and reversed the triangle indexes (so now the triangles are specified clockwise). Just to minimize confusion
Oh, also the green numbers are the “outer” vertices and the brown ones the “inner” vertices.
public void initialize(AppStateManager stateManager, Application app) {
this.app = (SimpleApplication) app;
this.assetManager = this.app.getAssetManager();
this.rootNode = this.app.getRootNode();
this.flyCam = this.app.getFlyByCamera();
this.cam = this.app.getCamera();
this.inputManager = this.app.getInputManager();
/** Specify variables for the mesh: */
float vertDist = 1f;
int nX = 4; // Quads count
int nZ = 3; // Quads count
int nOuter = (nX+1) * (nZ+1); // outer verts are always one more
int nInner = nX * nZ;
int nGes = nOuter + nInner;
int n;
/** --- Create the mesh --- */
// The mesh object
Mesh mesh = new Mesh();
// Vertex Coordinates
Vector3f[] outerVerts = new Vector3f[nOuter];
Vector3f[] innerVerts = new Vector3f[nInner];
Vector3f[] vertices = new Vector3f[nGes];
for (int i = 0; i < nOuter; i++) {
outerVerts[i] = new Vector3f((i%(nX+1)) * vertDist, 0f * vertDist, -1 * ((int)(i/(nX+1))) * vertDist);
}
for (int i = 0; i < nInner; i++) {
innerVerts[i] = new Vector3f(((i%nX) + 0.5f) * vertDist, 0f * vertDist, -1 * (((int)(i/nX)) + 0.5f) * vertDist);
}
n = 0;
for (Vector3f vert : outerVerts) {
vertices[n] = vert;
n++;
}
for (Vector3f vert : innerVerts) {
vertices[n] = vert;
n++;
}
if (logMode == LogMode.All || logMode == LogMode.Vertices) {
for (Vector3f vert : vertices) {
System.out.println(vert);
}
System.out.println(outerVerts.length);
System.out.println(innerVerts.length);
System.out.println(vertices.length);
}
System.out.println("Vertices created successfully.");
// Triangles: now clockwise
int[] triangles = new int[12*4*3]; // 12 Quads à 4 Triangles à 3 Vertices
n = 0;
for (int i = 0; i < triangles.length; ) {
// Quad by Quad: 4 Triangles consisting of 5 Vertices
if((n+1)%(nX+1) == 0)
n++; // bc of this outer-vert-one-more-thing n has to be one additionally more per line
int nCenter = nOuter+n - ((int)(n/(nX+1))); // unfortunately, we have to subtract that, to access the innerVert-index
int nTL = n; // top-left
int nTR = n+1; // top-right
int nBL = n+(nX+1); // bottom-left
int nBR = nBL+1; // bottom-right
triangles[i++] = nTL;
triangles[i++] = nTR;
triangles[i++] = nCenter;
triangles[i++] = nBL;
triangles[i++] = nTL;
triangles[i++] = nCenter;
triangles[i++] = nBR;
triangles[i++] = nBL;
triangles[i++] = nCenter;
triangles[i++] = nTR;
triangles[i++] = nBR;
triangles[i++] = nCenter;
n++;
}
if (logMode == LogMode.All || logMode == LogMode.Triangles) {
n = 1;
for (int triangle : triangles) {
System.out.print(triangle + " ");
if (n%3 == 0)
System.out.println();
n++;
}
}
System.out.println("Triangles created successfully.");
// Texture Coordinates
Vector2f[] texCoord = new Vector2f[vertices.length];
for (int i = 0; i < vertices.length; i++) {
// texCoord[count] = new Vector2f(x*(1/vert_xmaxf), z*(1/vert_zmaxf));
texCoord[i] = new Vector2f(vertices[i].x/(nX), vertices[i].z/(nZ));
}
if (logMode == LogMode.All || logMode == LogMode.TexCoords) {
for (Vector2f texCoord1 : texCoord) {
System.out.println(texCoord1);
}
}
System.out.println("TexCoords created successfully.");
// Normals
float[] normals = new float[3*vertices.length];
for (int i = 0; i < vertices.length; ) {
normals[i++] = 0;
normals[i++] = 1;
normals[i++] = 0;
}
System.out.println("Normals created successfully.");
// Set the Mesh Buffer
mesh.setBuffer(VertexBuffer.Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
mesh.setBuffer(VertexBuffer.Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(VertexBuffer.Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
mesh.setBuffer(VertexBuffer.Type.Index, 3, BufferUtils.createIntBuffer(triangles));
mesh.updateBound(); // pre-calculate the bounding volume
/** --- Using the Mesh in a Scene --- */
Geometry geo = new Geometry("OurMesh", mesh); // using our custom mesh object
mat = new Material(assetManager, "Common/MatDefs/Light/Lighting.j3md");
mat.setTexture("DiffuseMap", assetManager.loadTexture("Textures/Anno2070_TitleScreen.jpg"));
// mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
// mat.setColor("Color", ColorRGBA.Blue);
geo.setMaterial(mat);
rootNode.attachChild(geo);
And the console output:
(0.0, 0.0, 0.0)
(1.0, 0.0, 0.0)
(2.0, 0.0, 0.0)
(3.0, 0.0, 0.0)
(4.0, 0.0, 0.0)
(0.0, 0.0, -1.0)
(1.0, 0.0, -1.0)
(2.0, 0.0, -1.0)
(3.0, 0.0, -1.0)
(4.0, 0.0, -1.0)
(0.0, 0.0, -2.0)
(1.0, 0.0, -2.0)
(2.0, 0.0, -2.0)
(3.0, 0.0, -2.0)
(4.0, 0.0, -2.0)
(0.0, 0.0, -3.0)
(1.0, 0.0, -3.0)
(2.0, 0.0, -3.0)
(3.0, 0.0, -3.0)
(4.0, 0.0, -3.0)
(0.5, 0.0, -0.5)
(1.5, 0.0, -0.5)
(2.5, 0.0, -0.5)
(3.5, 0.0, -0.5)
(0.5, 0.0, -1.5)
(1.5, 0.0, -1.5)
(2.5, 0.0, -1.5)
(3.5, 0.0, -1.5)
(0.5, 0.0, -2.5)
(1.5, 0.0, -2.5)
(2.5, 0.0, -2.5)
(3.5, 0.0, -2.5)
20
12
32
Vertices created successfully.
0 1 20
5 0 20
6 5 20
1 6 20
1 2 21
6 1 21
7 6 21
2 7 21
2 3 22
7 2 22
8 7 22
3 8 22
3 4 23
8 3 23
9 8 23
4 9 23
5 6 24
10 5 24
11 10 24
6 11 24
6 7 25
11 6 25
12 11 25
7 12 25
7 8 26
12 7 26
13 12 26
8 13 26
8 9 27
13 8 27
14 13 27
9 14 27
10 11 28
15 10 28
16 15 28
11 16 28
11 12 29
16 11 29
17 16 29
12 17 29
12 13 30
17 12 30
18 17 30
13 18 30
13 14 31
18 13 31
19 18 31
14 19 31
Triangles created successfully.
(0.0, 0.0)
(0.25, 0.0)
(0.5, 0.0)
(0.75, 0.0)
(1.0, 0.0)
(0.0, -0.33333334)
(0.25, -0.33333334)
(0.5, -0.33333334)
(0.75, -0.33333334)
(1.0, -0.33333334)
(0.0, -0.6666667)
(0.25, -0.6666667)
(0.5, -0.6666667)
(0.75, -0.6666667)
(1.0, -0.6666667)
(0.0, -1.0)
(0.25, -1.0)
(0.5, -1.0)
(0.75, -1.0)
(1.0, -1.0)
(0.125, -0.16666667)
(0.375, -0.16666667)
(0.625, -0.16666667)
(0.875, -0.16666667)
(0.125, -0.5)
(0.375, -0.5)
(0.625, -0.5)
(0.875, -0.5)
(0.125, -0.8333333)
(0.375, -0.8333333)
(0.625, -0.8333333)
(0.875, -0.8333333)
TexCoords created successfully.
Normals created successfully.
Toggle Wireframe
BUILD SUCCESSFUL (total time: 13 seconds)
As I can see, everything generated right, including the texCoords. However, because of the following problem I suppose the error has to be somewhere there.
If I use the unshaded material, the result looks like that:
However if I use the lighting.j3md, the result is that:
(Edit: To see the lines I may suggest to click and open the picture in fullscreen - the lines are thin and, when scaled down, only barely seen).
There aren’t even triangles there anymore, just lines and squares. And If I check the console output, everything should be fine. Hmmmm.
So what is your general perception? Is that a major problem, such as a problem in maths or general understanding, or is it just a little typing mistake that I have to find somewhere?
Thanks!