Jump to content
  • Advertisement
Sign in to follow this  
Prot

OpenGL Rotating object around its own center

This topic is 583 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

 

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):

 

7xhvk.png

Instead the actual result looks something like this:

 

7qQrc.png

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);

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?

 4. If I change the rotation axis from y to z the object rotates around its own center just like expected.

 5. 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 this position to something else than the origin also with no success.

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.

Share this post


Link to post
Share on other sites
Advertisement

There are a lot of things that "could" be wrong here. But this stands out to me:

 


 4. If I change the rotation axis from y to z the object rotates around its own center just like expected.

 

#5 would have been my first guess, or change the order of multiplication for the matrices.

 

But if you're on the wrong axis, the only thing I can think of is that the view matrix is wrong. In #4, you're basically saying that z is "up" as you've defined it. That's fine as long as you are consistent. However, since you're not getting what you want, I would check the view matrix to make certain it is oriented the way you expect. I would set it manually so that Y is the "up" vector and set it back a couple of units using LookAt for a fixed position for testing.

Share this post


Link to post
Share on other sites
Your vertices all have a value of negative -0.5 in the z axis. That means that the center of the quad is not the center of the model. The rotate function as you are using it rotates around the center of the model. Set the values of the -z axis to 0 to make the quad and model center the same and it should be rotating around the center of the quad Edited by HappyCoder

Share this post


Link to post
Share on other sites

Your vertices all have a value of negative -0.5 in the z axis. That means that the center of the quad is not the center of the model. The rotate function as you are using it rotates around the center of the model. Set the values of the -z axis to 0 to make the quad and model center the same and it should be rotating around the center of the quad

 

This did the trick thank you!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!