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).
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):
Instead the actual result looks something like this:
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:
- Order of the applied transformations are crucial
- 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.
- I think that the order of transformations for the second texture is fine in my code, isn't it?
- Also how would I determine the right position for the object where the rotation would be applied correctly? (see above point 2)
- 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:
- If I change the rotation axis from
y
toz
the object rotates around its own center just like expected. - 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 toglm::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.