<p>Am I right if I understand</p>

<blockquote>
<pre><code>GLfloat vertices[] = {
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
    0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
    0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
    0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
    -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
    -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
};
</code></pre>
</blockquote>

<p>as being: </p>

<blockquote>
  <p>x, y, z, texX, texY</p>
</blockquote>

<p>for 6 vertices in total?</p>

<p>If so, then it seems like you do not use indexing yet (vertices 3 and 4 are the same), but nevermind.</p>

<p>Not being sure what order your coordinate system is in, the plane you are constructing here is perpendicular to an axis, but does not go through your origin. </p>

<p>An object that gets rotated by a rotation matrix rotates around its origin. The origin of your model matrix is (0, 0, 0), but this is <strong>not</strong> the center of your object. </p>

<p>In a right handed coordinate system, the following vertices give you a plane that is standing on the x-z-plane (perpendicular to it) and goes through your origin. Rotating that around y should rotate it like you wish to do:</p>

<pre><code> GLfloat vertices[] = {
     -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
     0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
     0.5f, 1.0f, 0.0f, 1.0f, 1.0f,
     0.5f, 1.0f, 0.0f, 1.0f, 1.0f,
     -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
     -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
 };
</code></pre>

<p>Notice the third value for each vertex.</p>

CC BY-SA 3.0
2016-12-13T15:37:01.320Z
2016-12-13T17:26:43.167Z
2016-12-13T17:26:43.167Z
Djindjidj
<p>I see you are rendering one quad of grass at the time. Many draw calls each for small number of vertices aren't very efficient and would make massive CPU bottlenecks especially if you want to render thousand of grass blades. This isn't an answer to your question about order of transformation but you might consider how I implemented this grass technique.</p>

<p>I stored each grass instance (3 quads with 60° degrees between each as in picture) in single VAO which contains buffers for vertices, texcoords, indices:</p>

<pre><code>//first quad
vertices.push_back(glm::vec3(-0.25, 0.5, 0.0));
vertices.push_back(glm::vec3(0.25, 0.5, 0.0));
vertices.push_back(glm::vec3(-0.25, 0.0, 0.0));
vertices.push_back(glm::vec3(0.25, 0.0, 0.0));

//second quad
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.5, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.5, 0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.0, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.0, 0.25 * glm::sin(60.0f)));

//third quad
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.5, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.5, 0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.0, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.0, 0.25 * glm::sin(60.0f)));
</code></pre>

<p><em>sin</em> and <em>cos</em> are from <a href="http://www.mathopenref.com/coordparamcircle.html" rel="nofollow noreferrer">circle equation</a>. You might need to change these angles to radians if you use glm.</p>

<pre><code>//texcoords
for (int i = 0; i &lt; 3; ++i)
{
    texCoords.push_back(glm::vec2(1.0, 0.0)); 
    texCoords.push_back(glm::vec2(0.0, 0.0));
    texCoords.push_back(glm::vec2(1.0, 1.0)); 
    texCoords.push_back(glm::vec2(0.0, 1.0));
}

//indices
    for(int i = 0; i &lt; 3; ++i)
    {
        for(int j = 0; j &lt; 4; ++j)
        {
            indices.push_back(4*i + j);
            if(j == 3 &amp;&amp; i != 2)
            {
                indices.push_back(4*i+j);
                indices.push_back(4*i+j+1);
            }
        }
    }
</code></pre>

<p>then draw call for single instance with triangle strip:</p>

<pre><code>glDrawElements(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0);
</code></pre>

<p>But the power lies in drawing many instances with single draw call as you can do it easily with so filled buffer. </p>

<pre><code>glDrawElementsInstanced(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0, totalGrassInstances);
</code></pre>

<p>Each instance will have its position somewhere on the terrain which is <code>glm::vec3</code> type. This is the additional buffer you need to bind with VAO to do instancing. Then you only need <code>model</code> matrix you use for terrain, for example which scales your terrain multiplied by <code>viewProjection</code>, or just <code>viewProjection</code> matrix if your positions generated by CPU are in world space, which you multiply by the position of each instance in vertex shader. thats all.</p>

<p>In case you need some random scale or rotation matrix for each instance to look more realistic you can generate on the fly based on some random values in vertex shader.</p>

CC BY-SA 3.0
2016-12-07T14:03:26.197Z
2016-12-07T14:40:03.687Z
2016-12-07T14:40:03.687Z
mdkdy
<p>I am trying to implement <a href="http://http.developer.nvidia.com/GPUGems/gpugems_ch07.html" rel="nofollow noreferrer">Blades of Waving Grass</a> and I am still stuck at the point where I need to achieve texture arrangements like in the figure below (the first one).</p>

<p><a href="https://i.sstatic.net/QVbKy.jpg" rel="nofollow noreferrer"><img src="https://i.sstatic.net/QVbKy.jpg" alt="enter image description here"></a></p>

<p>So my plan is to draw the first texture, then draw the second and rotate it by 30°, then another and rotate it by 60° etc. Drawing the first texture is not a problem but I have some issues rotating the second one. I would expect it to look something like this (view from above):</p>

<p><a href="https://i.sstatic.net/7xhvk.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7xhvk.png" alt="enter image description here"></a></p>

<p>Instead the actual result looks something like this:</p>

<p><a href="https://i.sstatic.net/7qQrc.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7qQrc.png" alt="enter image description here"></a></p>

<p>I suspect that this deviation occurs due to the object being rotated around the world-space axes and not around its own.</p>

<p>Vertex and position data:</p>

<pre><code>GLfloat vertices[] = {
    -0.5f, -0.5f, -0.5f,  0.0f,  0.0f,
     0.5f, -0.5f, -0.5f,  1.0f,  0.0f,
     0.5f,  0.5f, -0.5f,  1.0f,  1.0f,
     0.5f,  0.5f, -0.5f,  1.0f,  1.0f,
    -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,
    -0.5f, -0.5f, -0.5f,  0.0f,  0.0f,
};

glm::vec3 texturePositions[] = {
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 0.0f)
};
</code></pre>

<p>Camera/View transformations:</p>

<pre><code>// Camera/View transformation
glm::mat4 view;
view = camera.GetViewMatrix();
// Projection
glm::mat4 projection;
projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f);
// Get the uniform locations
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
GLint projectionLoc = glGetUniformLocation(ourShader.Program, "projection");
// Pass the matrices to the shader
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
</code></pre>

<p>Drawing the first two textures:</p>

<pre><code>//Texture 1
glm::mat4 model;
model = glm::translate(model, texturePositions[0]);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);

//Texture 2
GLfloat angle = glm::radians(30.0f);
model = glm::translate(model, texturePositions[1]);
model = glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);
</code></pre>

<p>Online search showed that this might have several reasons:</p>

<ol>
<li>Order of the applied transformations are crucial</li>
<li>One solution might be to first move the "object" until its center
matches the world-space origin, apply the rotation and then move it
to the desired position.</li>
</ol>

<p>As I am still in the very early learning process so I am kind of clueless here.</p>

<ol>
<li>I think that the order of transformations for the second texture is fine in my code, isn't it?</li>
<li>Also how would I determine the right position for the object where
the rotation would be applied correctly? (see above point 2)</li>
<li>Might it be more efficient to apply the rotation in the objects
local space? Is that possible in my case?</li>
</ol>

<p>I do not expect anybody to write code for me (though some snippet would be nice) but I would appreciate some hints or somebody pointing me in the right direction. I think that I have a basic fallacy here.</p>

<p>Thanks in advance.</p>

<p><strong>Edit:</strong></p>

<ol>
<li>If I change the rotation axis from <code>y</code> to <code>z</code> the object rotates
around its own center just like expected.</li>
<li>I also tried to apply the following solutions (taken from <a href="https://gamedev.stackexchange.com/questions/18854/rotating-an-object-in-opengl-about-its-centre">here</a>
and <a href="https://www.opengl.org/discussion_boards/showthread.php/169200-Rotating-an-Object-around-its-center" rel="nofollow noreferrer">here</a>) where I moved the object to <code>glm::vec3(-x,-y,-z)</code>
rotate it and then moving it back to <code>glm::vec3(x,y,z)</code>,
unfortunately with no success. Which makes sence as the position
vector is set to (0,0,0) anyway. I also tried to change thisposition
to something else than the origin also with no success.</li>
</ol>

CC BY-SA 3.0
2016-12-07T12:12:41.387Z
If you want to do a rotation, R, around an arbitrary point, X, then the matrix is simply formed by
 Translate(-X)*RotateAroundOrigin(R)*Translate(X)
Simon F
CC BY-SA 3.0
2016-12-07T12:14:27.473Z
@SimonF could you elaborate a little more on your suggestion? Like I mentioned, still in the learning process.
チーズパン
CC BY-SA 3.0
2016-12-07T12:15:06.797Z
Sorry.... hit return by accident while typing the reply
Simon F
CC BY-SA 3.0
2016-12-07T12:40:13.513Z
@SimonF So this arbitrary point X in my case would be the center of my texture. How could I determine it?
チーズパン
CC BY-SA 3.0
2016-12-07T16:23:33.583Z
But if you are placing blades of grass, don't you know where they are being placed?
Simon F
CC BY-SA 3.0
2016-12-07T16:25:38.527Z
@SimonF I do but is there a way to determine the textures width/height in world-space?
チーズパン
CC BY-SA 3.0
2016-12-07T19:22:36.620Z
I'm not sure what this is supposed to do model = glm::translate(model, texturePositions[0]); since texturePositions is filled with 0 vectors. But you are translating before rotating.
Matthew Woo
CC BY-SA 3.0
2016-12-08T08:25:23.700Z
@MatthewWoo as far as I am concerned it is filled with two vec3-objects, which are both set to (0,0,0). Am I worng?
チーズパン
<opengl><transformations>
Rotating object around its own center
チーズパン
# Rotating object around its own center

(Asked by: username_0 on 2016-12-07)

I am trying to implement [Blades of Waving Grass](http://http.developer.nvidia.com/GPUGems/gpugems_ch07.html) and I am still stuck at the point where I need to achieve texture arrangements like in the figure below (the first one).

[![enter image description here](https://i.sstatic.net/QVbKy.jpg)](https://i.sstatic.net/QVbKy.jpg)

So my plan is to draw the first texture, then draw the second and rotate it by 30°, then another and rotate it by 60° etc. Drawing the first texture is not a problem but I have some issues rotating the second one. I would expect it to look something like this (view from above):

[![enter image description here](https://i.sstatic.net/7xhvk.png)](https://i.sstatic.net/7xhvk.png)

Instead the actual result looks something like this:

[![enter image description here](https://i.sstatic.net/7qQrc.png)](https://i.sstatic.net/7qQrc.png)

I suspect that this deviation occurs due to the object being rotated around the world-space axes and not around its own.

Vertex and position data:

```
GLfloat vertices[] = {
    -0.5f, -0.5f, -0.5f,  0.0f,  0.0f,
     0.5f, -0.5f, -0.5f,  1.0f,  0.0f,
     0.5f,  0.5f, -0.5f,  1.0f,  1.0f,
     0.5f,  0.5f, -0.5f,  1.0f,  1.0f,
    -0.5f,  0.5f, -0.5f,  0.0f,  1.0f,
    -0.5f, -0.5f, -0.5f,  0.0f,  0.0f,
};

glm::vec3 texturePositions[] = {
    glm::vec3(0.0f, 0.0f, 0.0f),
    glm::vec3(0.0f, 0.0f, 0.0f)
};

```

Camera/View transformations:

```
// Camera/View transformation
glm::mat4 view;
view = camera.GetViewMatrix();
// Projection
glm::mat4 projection;
projection = glm::perspective(glm::radians(camera.Zoom), (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 1000.0f);
// Get the uniform locations
GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
GLint projectionLoc = glGetUniformLocation(ourShader.Program, "projection");
// Pass the matrices to the shader
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));

```

Drawing the first two textures:

```
//Texture 1
glm::mat4 model;
model = glm::translate(model, texturePositions[0]);
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);

//Texture 2
GLfloat angle = glm::radians(30.0f);
model = glm::translate(model, texturePositions[1]);
model = glm::rotate(model, angle, glm::vec3(0.0f, 1.0f, 0.0f));
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glDrawArrays(GL_TRIANGLES, 0, 6);

```

Online search showed that this might have several reasons:

1. Order of the applied transformations are crucial
2. One solution might be to first move the "object" until its center
   matches the world-space origin, apply the rotation and then move it
   to the desired position.

As I am still in the very early learning process so I am kind of clueless here.

1. I think that the order of transformations for the second texture is fine in my code, isn't it?
2. Also how would I determine the right position for the object where
   the rotation would be applied correctly? (see above point 2)
3. Might it be more efficient to apply the rotation in the objects
   local space? Is that possible in my case?

I do not expect anybody to write code for me (though some snippet would be nice) but I would appreciate some hints or somebody pointing me in the right direction. I think that I have a basic fallacy here.

Thanks in advance.

**Edit:**

1. If I change the rotation axis from `y` to `z` the object rotates
   around its own center just like expected.
2. I also tried to apply the following solutions (taken from [here](https://gamedev.stackexchange.com/questions/18854/rotating-an-object-in-opengl-about-its-centre)
   and [here](https://www.opengl.org/discussion_boards/showthread.php/169200-Rotating-an-Object-around-its-center)) where I moved the object to `glm::vec3(-x,-y,-z)`
   rotate it and then moving it back to `glm::vec3(x,y,z)`,
   unfortunately with no success. Which makes sence as the position
   vector is set to (0,0,0) anyway. I also tried to change thisposition
   to something else than the origin also with no success.

**Tags:** opengl, transformations

> **Comments:**
> **username_1**: If you want to do a rotation, R, around an arbitrary point, X, then the matrix is simply formed by  Translate(-X)*RotateAroundOrigin(R)*Translate(X)  
> **username_0**: @username_1 could you elaborate a little more on your suggestion? Like I mentioned, still in the learning process.  
> **username_1**: Sorry.... hit return by accident while typing the reply  
> **username_0**: @username_1 So this arbitrary point X in my case would be the center of my texture. How could I determine it?  
> **username_1**: But if you are placing blades of grass, don't you know where they are being placed?  
> **username_0**: @username_1 I do but is there a way to determine the textures width/height in world-space?  
> **username_2**: I'm not sure what this is supposed to do model = glm::translate(model, texturePositions[0]); since texturePositions is filled with 0 vectors. But you are translating before rotating.  
> **username_0**: @username_2 as far as I am concerned it is filled with two vec3-objects, which are both set to (0,0,0). Am I worng?  

---

## 2 answers

### Answer by username_3 on 2016-12-13. Score: 2 | Accepted answer

Am I right if I understand

> ```
> GLfloat vertices[] = {
>     -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
>     0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
>     0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
>     0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
>     -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
>     -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
> };
>
> ```

as being:

> x, y, z, texX, texY

for 6 vertices in total?

If so, then it seems like you do not use indexing yet (vertices 3 and 4 are the same), but nevermind.

Not being sure what order your coordinate system is in, the plane you are constructing here is perpendicular to an axis, but does not go through your origin.

An object that gets rotated by a rotation matrix rotates around its origin. The origin of your model matrix is (0, 0, 0), but this is **not** the center of your object.

In a right handed coordinate system, the following vertices give you a plane that is standing on the x-z-plane (perpendicular to it) and goes through your origin. Rotating that around y should rotate it like you wish to do:

```
 GLfloat vertices[] = {
     -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
     0.5f, 0.0f, 0.0f, 1.0f, 0.0f,
     0.5f, 1.0f, 0.0f, 1.0f, 1.0f,
     0.5f, 1.0f, 0.0f, 1.0f, 1.0f,
     -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,
     -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,
 };

```

Notice the third value for each vertex.


---

### Answer by username_4 on 2016-12-07. Score: 1

I see you are rendering one quad of grass at the time. Many draw calls each for small number of vertices aren't very efficient and would make massive CPU bottlenecks especially if you want to render thousand of grass blades. This isn't an answer to your question about order of transformation but you might consider how I implemented this grass technique.

I stored each grass instance (3 quads with 60° degrees between each as in picture) in single VAO which contains buffers for vertices, texcoords, indices:

```
//first quad
vertices.push_back(glm::vec3(-0.25, 0.5, 0.0));
vertices.push_back(glm::vec3(0.25, 0.5, 0.0));
vertices.push_back(glm::vec3(-0.25, 0.0, 0.0));
vertices.push_back(glm::vec3(0.25, 0.0, 0.0));

//second quad
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.5, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.5, 0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.0, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.0, 0.25 * glm::sin(60.0f)));

//third quad
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.5, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.5, 0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(0.25 * glm::cos(60.0f), 0.0, -0.25 * glm::sin(60.0f)));
vertices.push_back(glm::vec3(-0.25 * glm::cos(60.0f), 0.0, 0.25 * glm::sin(60.0f)));

```

*sin* and *cos* are from [circle equation](http://www.mathopenref.com/coordparamcircle.html). You might need to change these angles to radians if you use glm.

```
//texcoords
for (int i = 0; i < 3; ++i)
{
    texCoords.push_back(glm::vec2(1.0, 0.0)); 
    texCoords.push_back(glm::vec2(0.0, 0.0));
    texCoords.push_back(glm::vec2(1.0, 1.0)); 
    texCoords.push_back(glm::vec2(0.0, 1.0));
}

//indices
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 4; ++j)
        {
            indices.push_back(4*i + j);
            if(j == 3 && i != 2)
            {
                indices.push_back(4*i+j);
                indices.push_back(4*i+j+1);
            }
        }
    }

```

then draw call for single instance with triangle strip:

```
glDrawElements(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0);

```

But the power lies in drawing many instances with single draw call as you can do it easily with so filled buffer.

```
glDrawElementsInstanced(GL_TRIANGLE_STRIP, indices.size(), GL_UNSIGNED_INT, 0, totalGrassInstances);

```

Each instance will have its position somewhere on the terrain which is `glm::vec3` type. This is the additional buffer you need to bind with VAO to do instancing. Then you only need `model` matrix you use for terrain, for example which scales your terrain multiplied by `viewProjection`, or just `viewProjection` matrix if your positions generated by CPU are in world space, which you multiply by the position of each instance in vertex shader. thats all.

In case you need some random scale or rotation matrix for each instance to look more realistic you can generate on the fly based on some random values in vertex shader.


---

