I am writing a large 3D game in Java with OpenGL.
Question #1
As of now, I am rendering my 3D objects using a Model
class that I defined. This class contains vertex, color, model matrix, code (a custom attribute I defined), and index arrays. I defined a method addModels
that can concatenate an array of Model
instances into a new, larger Model
(it concatenates all the attribute arrays using System.arraycopy
). For instance, to draw a forest; for every tree, I create a Model
; and at the end I combine them all into one forest Model
, which I render. This procedure is a very object-oriented way of doing it, and consists of fitting together many bundles of attribute arrays into larger ones.
Simple example code:
Model[] models = new Model[trees.length];
for (int i=0; i<trees.length; i++) {
models[i] = getTreeModel(trees[i]);
}
Model result = addModels(models);
return result;
However, this procedure may be bad practice memory-wise, since I create many Model
instances per frame. (Nonetheless, I read in this answer that objects do not take much memory.) Would it be better practice to lean towards a more data-oriented approach; like first declaring large attribute arrays (vertex, color, etc.), and creating them individually? Simple example code:
float[] vertices, colors;
for (int i=0; i<trees.length; i++) {
float[] treeVertices = getTreeVertices(trees[i]),
treeColors = getTreeColors(trees[i]);
// copy treeVertices into vertices, and treeColors into colors at appropriate positions
}
return new Model(vertices, colors);
Please note that either option results in one model that is going to be rendered finally.
Are there any better ways of doing this?
Question #2
When I render a Model
, I convert each attribute array into an attribute buffer (e.g. float[]
into FloatBuffer
). Would it make my game run faster to, first, load data into a buffer without using the array at all? (I'm using arrays because they're easier for me to manipulate.)
Question #3
Would it be more efficient to call glBufferSubData
then to concatenate all the attribute arrays into longer ones and then call glBufferData
(or a similar method)?
Question #4
One tutorial I read defined a Model
class as containing multiple Mesh
s. Then, they drew each mesh separately to the screen. Is this good practice if I have a lot of meshes (trees) per model (forest)? I heard it isn't good to make a lot of glDraw*
calls.