<p>When drawing in OpenGL and related APIs you usually have one or more models. In your case, this would be the cylinder and the icosahedron. The models are generally generated at some default size which may or may not suit your needs for a particular task. You move them, scale them, and rotate them by setting up a model matrix which describes how you want to transform them.</p>

<p>So how would this work in your case? I recommend having 2 functions. One which draws a unit cylinder and another which draws a unit icosahedron. Say you have a data structure for your coordinates like this (I'm assuming a C-like language):</p>

<pre><code>typedef struct Position3D {
    float x;
    float y;
    float z;
} Position3D;
</code></pre>

<p>You can then construct a triangle by making a structure for one:</p>

<pre><code>typedef struct Triangle3D {
    Position3D p0;
    Position3D p1;
    Position3D p2;
} Triangle3D;
</code></pre>

<p>Then you'd have functions for generating the vertices you need. Something like this:</p>

<pre><code>void generateCylinder (Triangle3D* triangles) // &lt;-assumes you allocated triangles before calling and that you have enough space for all of them!
{
    int i = 0;
    const float kRadius = 1.0;
    const float kDeltaH = 0.1; // &lt;- change this to make the mesh more or less dense
    const float kDeltaAngle = M_PI / 10.0; // &lt;- change this to make the mesh more or less curved
    for (float h = -1.0; h &lt; 1.0; h += kDeltah)
    {
        for (float angle = 0; angle &lt; 2.0 * M_PI; angle += kDeltaAngle)
        {
            float x = kRadius * cos(angle);
            float y = h;
            float z = kRadius * sin(angle);
            //... add points to triangles array here...
            // p0 = &lt;x,y,z&gt;
            // p1 = &lt;x, y + kDeltaH, z&gt;
            // p1 = &lt;x + radius * cos(angle + kDeltaAngle), y, z  + radius * sin(angle + kDeltaAngle)&gt;
        }
    }
}
</code></pre>

<p>And a similar function for the icosahedron. </p>

<p>Next, you'll create a matrix for every object you draw and send that to your shader before drawing it. So you'll have the translations for the vertices of the (big) icosahedron, and you want to draw a (small) icosahedron at each one. You'd do the following:</p>

<pre><code>float translationMatrix [ 16 ] = {
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
};
translationMatrix [ 12 ] = xPos;
translationMatrix [ 13 ] = yPos;
translationMatrix [ 14 ] = zPos;
GLuint modelMatrixLoc = glGetUniformLocation(program, "modelMatrix");
glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, translationMatrix);
</code></pre>

<p>Then you'd draw your icosahedron. For the cylinders you would need to scale vertically, rotate to the appropriate angle and translate it to the position between 2 icosahedrons.</p>

CC BY-SA 3.0
2017-02-24T04:13:10.053Z
2017-02-24T04:13:10.053Z
user1118321
<p>You could calculate the midpoints of the segments between the vertices (atoms) of the icosahedron. These are the positions of the cylinders. You would then need to determine the orientation and scale of the cylinders. This information should then be encoded into a transformation matrix. Doing this for every cylinder would mean multiple transformation matrices.</p>

<p>An effective way to draw many copies of the same object is to use instanced drawing. Instead of defining the position of the vertices of each cylinder separately, you only define the geometry for one cylinder and send this to a buffer. You can then draw a number of copies of this cylinder and transforming each copy of this cylinder with its respective transformation matrix. The matrices should be put into a buffer on the GPU to make it efficient. Alternatively, it is possible to do it with uniforms but this would mean multiple draw calls and no instancing.</p>

<p>A couple of things you would need to look up documentation for is <a href="https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glDrawElementsInstanced.xhtml" rel="nofollow noreferrer">instanced drawing</a>. Next, would be to investigate <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribDivisor.xhtml" rel="nofollow noreferrer">attribute divisors</a>. Attribute divisors regulate the rate for which vertex attributes are read from the buffer. If you upload the transformation matrices for each cylinder with 40 vertices to the buffer. You would want to have one transformation matrix for each batch of 40 vertices. Therefore, the attribute divisor for the transformation matrix should be set to 1, such that it advances once per batch of vertices.</p>

<p>Using this approach it is best to define you cylinder as an axis aligned unit length and width cylinder centered at the origin. This will simplify the transformations a whole lot.</p>

CC BY-SA 3.0
2017-11-28T10:42:09.517Z
2017-11-28T12:20:27.940Z
2017-11-28T12:20:27.940Z
Reynolds
<p>I have drawn a cylinder and an icosahedron using OpenGL.</p>

<p>Now, I need to use the icosahedron and the open cylinder to build a structure motivated by crystal lattices, where I use an icosahedron for each atom and a cylinder to connect each 2 icosahedrons. </p>

<p>I would have to use 12 icosahedrons for atoms and about 20 cylinders for the lines connecting the atoms, as I need to place them all to form the shape of an icosahedron itself.</p>

<p>Is there anybody that could give me any ideas regarding how to go about this? I can arrange 12 icosahedrons to simulate the 12 vertices of an icosahedron, but I'm not sure how to place all of the cylinders around properly.</p>

CC BY-SA 3.0
2017-02-23T08:13:40.710Z
billboards, flat squares rotated to always face the camera
ratchet freak
<opengl>
OpenGL - drawing icosahedrons and cylinders and combining them
gambit20088
# OpenGL - drawing icosahedrons and cylinders and combining them

(Asked by: username_0 on 2017-02-23)

I have drawn a cylinder and an icosahedron using OpenGL.

Now, I need to use the icosahedron and the open cylinder to build a structure motivated by crystal lattices, where I use an icosahedron for each atom and a cylinder to connect each 2 icosahedrons.

I would have to use 12 icosahedrons for atoms and about 20 cylinders for the lines connecting the atoms, as I need to place them all to form the shape of an icosahedron itself.

Is there anybody that could give me any ideas regarding how to go about this? I can arrange 12 icosahedrons to simulate the 12 vertices of an icosahedron, but I'm not sure how to place all of the cylinders around properly.

**Tags:** opengl

> **Comments:**
> **username_1**: billboards, flat squares rotated to always face the camera  

---

## 2 answers

### Answer by username_2 on 2017-02-24. Score: 1

When drawing in OpenGL and related APIs you usually have one or more models. In your case, this would be the cylinder and the icosahedron. The models are generally generated at some default size which may or may not suit your needs for a particular task. You move them, scale them, and rotate them by setting up a model matrix which describes how you want to transform them.

So how would this work in your case? I recommend having 2 functions. One which draws a unit cylinder and another which draws a unit icosahedron. Say you have a data structure for your coordinates like this (I'm assuming a C-like language):

```
typedef struct Position3D {
    float x;
    float y;
    float z;
} Position3D;

```

You can then construct a triangle by making a structure for one:

```
typedef struct Triangle3D {
    Position3D p0;
    Position3D p1;
    Position3D p2;
} Triangle3D;

```

Then you'd have functions for generating the vertices you need. Something like this:

```
void generateCylinder (Triangle3D* triangles) // <-assumes you allocated triangles before calling and that you have enough space for all of them!
{
    int i = 0;
    const float kRadius = 1.0;
    const float kDeltaH = 0.1; // <- change this to make the mesh more or less dense
    const float kDeltaAngle = M_PI / 10.0; // <- change this to make the mesh more or less curved
    for (float h = -1.0; h < 1.0; h += kDeltah)
    {
        for (float angle = 0; angle < 2.0 * M_PI; angle += kDeltaAngle)
        {
            float x = kRadius * cos(angle);
            float y = h;
            float z = kRadius * sin(angle);
            //... add points to triangles array here...
            // p0 = <x,y,z>
            // p1 = <x, y + kDeltaH, z>
            // p1 = <x + radius * cos(angle + kDeltaAngle), y, z  + radius * sin(angle + kDeltaAngle)>
        }
    }
}

```

And a similar function for the icosahedron.

Next, you'll create a matrix for every object you draw and send that to your shader before drawing it. So you'll have the translations for the vertices of the (big) icosahedron, and you want to draw a (small) icosahedron at each one. You'd do the following:

```
float translationMatrix [ 16 ] = {
    1, 0, 0, 0,
    0, 1, 0, 0,
    0, 0, 1, 0,
    0, 0, 0, 1
};
translationMatrix [ 12 ] = xPos;
translationMatrix [ 13 ] = yPos;
translationMatrix [ 14 ] = zPos;
GLuint modelMatrixLoc = glGetUniformLocation(program, "modelMatrix");
glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, translationMatrix);

```

Then you'd draw your icosahedron. For the cylinders you would need to scale vertically, rotate to the appropriate angle and translate it to the position between 2 icosahedrons.


---

### Answer by username_3 on 2017-11-28. Score: 1

You could calculate the midpoints of the segments between the vertices (atoms) of the icosahedron. These are the positions of the cylinders. You would then need to determine the orientation and scale of the cylinders. This information should then be encoded into a transformation matrix. Doing this for every cylinder would mean multiple transformation matrices.

An effective way to draw many copies of the same object is to use instanced drawing. Instead of defining the position of the vertices of each cylinder separately, you only define the geometry for one cylinder and send this to a buffer. You can then draw a number of copies of this cylinder and transforming each copy of this cylinder with its respective transformation matrix. The matrices should be put into a buffer on the GPU to make it efficient. Alternatively, it is possible to do it with uniforms but this would mean multiple draw calls and no instancing.

A couple of things you would need to look up documentation for is [instanced drawing](https://www.khronos.org/registry/OpenGL-Refpages/es3.0/html/glDrawElementsInstanced.xhtml). Next, would be to investigate [attribute divisors](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glVertexAttribDivisor.xhtml). Attribute divisors regulate the rate for which vertex attributes are read from the buffer. If you upload the transformation matrices for each cylinder with 40 vertices to the buffer. You would want to have one transformation matrix for each batch of 40 vertices. Therefore, the attribute divisor for the transformation matrix should be set to 1, such that it advances once per batch of vertices.

Using this approach it is best to define you cylinder as an axis aligned unit length and width cylinder centered at the origin. This will simplify the transformations a whole lot.


---

