I am trying to implement Blades of Waving Grass 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

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

Instead the actual result looks something like this:

enter image description here

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 and here) 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.
  • 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 Dec 7 '16 at 12:12
  • @SimonF could you elaborate a little more on your suggestion? Like I mentioned, still in the learning process. – チーズパン Dec 7 '16 at 12:14
  • Sorry.... hit return by accident while typing the reply – Simon F Dec 7 '16 at 12:15
  • @SimonF So this arbitrary point X in my case would be the center of my texture. How could I determine it? – チーズパン Dec 7 '16 at 12:40
  • But if you are placing blades of grass, don't you know where they are being placed? – Simon F Dec 7 '16 at 16:23
up vote 3 down vote accepted

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.

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. 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.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.