<p>Don't copy around huge chunks of data if you don't have to.</p>

<p>I would delay collecting and building the final forest mesh for as long as possible.</p>

<p>However when you have multiple of the same mesh that just needs to be translated and rotated a bit you can use instancing. Which lets you draw thousands of copies of a tree with a single draw call.</p>

<p>The most efficient way of getting data to the gpu is to map the buffer and fill that. In java that means using a Buffer.</p>

CC BY-SA 3.0
2017-08-03T16:08:40.673Z
For instancing would that mean to use `glDrawElementsInstanced`?
clabe45
CC BY-SA 3.0
2017-08-03T16:10:56.333Z
@clabe45 yeah or glDrawArraysInstanced
ratchet freak
CC BY-SA 3.0
2017-08-03T16:07:23.753Z
2017-08-03T16:07:23.753Z
ratchet freak
<p>I am writing a large 3D game in Java with OpenGL.</p>
<h3>Question #1</h3>
<p>As of now, I am rendering my 3D objects using a <code>Model</code> class that I defined. This class contains vertex, color, model matrix, code (a custom attribute I defined), and index arrays. I defined a method <code>addModels</code> that can concatenate an array of <code>Model</code> instances into a new, larger <code>Model</code> (it concatenates all the attribute arrays using <code>System.arraycopy</code>). For instance, to draw a forest; for every tree, I create a <code>Model</code>; and at the end I combine them all into one forest <code>Model</code>, which I render. This procedure is a very <em>object-oriented</em> way of doing it, and consists of fitting together many bundles of attribute arrays into larger ones.
Simple example code:</p>
<pre><code>Model[] models = new Model[trees.length];
for (int i=0; i&lt;trees.length; i++) {
    models[i] = getTreeModel(trees[i]);
}
Model result = addModels(models);
return result;
</code></pre>
<p>However, this procedure may be bad practice memory-wise, since I create <em>many</em> <code>Model</code> instances per frame. (Nonetheless, I read in <a href="https://softwareengineering.stackexchange.com/a/149569/279723">this answer</a> that objects do not take much memory.) <strong>Would it be better practice to lean towards a more <em>data-oriented</em> approach; like first declaring large attribute arrays (vertex, color, etc.), and creating them individually?</strong> Simple example code:</p>
<pre><code>float[] vertices, colors;
for (int i=0; i&lt;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);
</code></pre>
<p><em>Please note that either option results in</em> one <em>model that is going to be rendered finally.</em></p>
<p>Are there any better ways of doing this?</p>
<h3>Question #2</h3>
<p>When I render a <code>Model</code>, I convert each attribute array into an attribute buffer (e.g. <code>float[]</code> into <code>FloatBuffer</code>). 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.)</p>
<h3>Question #3</h3>
<p>Would it be more efficient to call <code>glBufferSubData</code> then to concatenate all the attribute arrays into longer ones and then call <code>glBufferData</code> (or a similar method)?</p>
<h3>Question #4</h3>
<p><a href="https://learnopengl.com/#!Model-Loading/Model" rel="nofollow noreferrer">One tutorial</a> I read defined a <code>Model</code> class as containing multiple <code>Mesh</code>s. <em>Then, they drew each mesh separately to the screen.</em> 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 <code>glDraw*</code> calls.</p>

<opengl><mesh><model>
Most efficient way to render many OO meshes in OpenGL
clabe45
# Most efficient way to render many OO meshes in OpenGL

(Asked by: username_0 on 2017-08-03)

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](https://softwareengineering.stackexchange.com/a/149569/279723) 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](https://learnopengl.com/#!Model-Loading/Model) 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.

**Tags:** opengl, mesh, model


---

## 1 answers

### Answer by username_1 on 2017-08-03. Score: 3 | Accepted answer

Don't copy around huge chunks of data if you don't have to.

I would delay collecting and building the final forest mesh for as long as possible.

However when you have multiple of the same mesh that just needs to be translated and rotated a bit you can use instancing. Which lets you draw thousands of copies of a tree with a single draw call.

The most efficient way of getting data to the gpu is to map the buffer and fill that. In java that means using a Buffer.

> **Comments:**
> **username_0**: For instancing would that mean to use `glDrawElementsInstanced`?  
> **username_1**: @username_0 yeah or glDrawArraysInstanced  

---

