Jump to content
  • Advertisement
Sign in to follow this  
Suen

Rotation around own axis

This topic is 2282 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

So after playing around a bit with using my own matrices I've decided to go with GLM for a more comfortable (and optimized) interface when it comes to handling the common transformations. However I've stumbled upon a problem and while it really seems (and probably is) a very basic problem that I should notice immideatly I can't seem to figure it out at all.

I've built a pyramid and I just want it to rotate around it's own y-axis. The problem is that it instead seems to rotate around some other axis , basically it's orbiting around it. I thought that it might be the wrong order of doing operations (since I'm also translating it several units in the negative z-direction) but that's not the problem. Besides if the problem was there then the further I translate the larger the orbit but this is not the case, the size of the orbit stays the same regardless of how much I translate. So...I'm kind of clueless on where the problem is.

Any help or suggestion on this will be appreciated. I'll post the display function I use for rendering and the vertex shader as I feel those are the most relevant for now:

Display

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(theProgram);
glBindVertexArray(vao);

//ModelView matrix
glm::mat4 modelViewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -30.0f));
modelViewMatrix = glm::rotate(modelViewMatrix, -20.0f, glm::vec3(1.0f, 0.0f, 0.0f));
modelViewMatrix = glm::rotate(modelViewMatrix, rot, glm::vec3(0.0f, 1.0f, 0.0f));

glUniform4fv(locTriangleColor, 1, white);
glDrawArrays(GL_TRIANGLES, 0, 3);

glUniform4fv(locTriangleColor, 1, red);
glDrawArrays(GL_TRIANGLES, 3, 3);

glUniform4fv(locTriangleColor, 1, green);
glDrawArrays(GL_TRIANGLES, 6, 3);

glUniform4fv(locTriangleColor, 1, blue);
glDrawArrays(GL_TRIANGLES, 9, 3);

glUniform4fv(locTriangleColor, 1, special);
glDrawArrays(GL_QUADS, 12, 4);

glUniformMatrix4fv(locModelToViewMatrix, 1, GL_FALSE, glm::value_ptr(modelViewMatrix));

glUseProgram(0);
glutSwapBuffers();
glutPostRedisplay();

rot++;
}


Vertex Shader
#version 330

layout(location = 0) in vec4 position;

uniform mat4 modelToViewMatrix;
uniform mat4 viewToClipMatrix;

void main()
{
vec4 viewPos = modelToViewMatrix*position;

gl_Position = viewToClipMatrix*viewPos;
}

Share this post


Link to post
Share on other sites
Advertisement
I should add that it actually is rotating around it's own y-axis...but it's doing so as it orbits. I want to get rid of the latter one.

Share this post


Link to post
Share on other sites
It looks like you're rotating twice there, once around X and once around Y (but with a fixed rotation around X). I think your translation should be done last, however, rather than first.

Share this post


Link to post
Share on other sites

It looks like you're rotating twice there, once around X and once around Y (but with a fixed rotation around X). I think your translation should be done last, however, rather than first.


That is right, I want it to rotate around Y with a fixed slope of 20 degress in the X-axis. Should have mentioned that. So you mean that I should do more like this (ignoring the fixed rotation for the time being) :

glm::mat4 modelViewMatrix = glm::rotate(glm::mat4(1.0f), rot, glm::vec3(0.0f, 1.0f, 0.0));
modelViewMatrix = glm::translate(modelViewMatrix, glm::vec3(0.0f, 0.0f, -10.0f));


If this is what you meant then I get what I mentioned in my first post (if I did my transformations in the wrong order) which is that my object starts to orbit around the Y-axis (without rotating around it's own Y axis).

Share this post


Link to post
Share on other sites
This is the problem with using custom packages. If you stick with opengl, everyone here could answer you. I dont know how that library works. If it orbits I assume it is not in openGL stack order and that the translation is happening first. Did you change the order of the 3 matrices? It looks 100% like its an order issue since the way you are multiplying matrices in those glm functions.

Share this post


Link to post
Share on other sites

This is the problem with using custom packages. If you stick with opengl, everyone here could answer you. I dont know how that library works. If it orbits I assume it is not in openGL stack order and that the translation is happening first. Did you change the order of the 3 matrices? It looks 100% like its an order issue since the way you are multiplying matrices in those glm functions.


I've tried changing the orders of them. In fact I even removed the translation and merely had a rotation around y-axis only. It still orbits. I've checked the matrix resulting from calling the function and it's correct as well according to the matrix formula for rotating around Y. Furthermore I've seen people using this library and not encountering such problems as far as I know.

I just tried out my own function now (rotation only) and it gave me the same result. So I assume there's a wrong somewhere else rather than the custom package. It can't be the order of operations since even a single rotation gives the same result. Not sure exactly where it might be (or what to post of my code)

I'd like to point out again. If I use the custom package AND put the translation before a rotation then YES it does orbit as anyone would expect, it would exactly be like the OpenGL stack order. However IF I change the order then it still orbits but in a much much smaller orbit.

In case someone is still confused I just recorded a small demo of it. Found in the link below

Demo

Share this post


Link to post
Share on other sites
I might as well post the rest of the code. The program is not particularly big. This is not all functions in the program but reading/loading shaders, the main function are not relevant to this, neither is the reshape function I assume.

void setVAO()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glEnableVertexAttribArray(0); //Enable generic vertex attribute array
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, colorBufferObject);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, 0);
glBindVertexArray(0);
}


void init()
{
GLuint vertexShaderObject = CreateShader(GL_VERTEX_SHADER, "pyramidStack.vert");
GLuint fragmentShaderObject = CreateShader(GL_FRAGMENT_SHADER, "pyramidStack.frag");
theProgram = CreateProgram(vertexShaderObject, fragmentShaderObject);

locTriangleColor = glGetUniformLocation(theProgram, "triangleColor");
locModelToViewMatrix = glGetUniformLocation(theProgram, "modelToViewMatrix");
locViewToClipMatrix = glGetUniformLocation(theProgram, "viewToClipMatrix");


glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);

//Vertex source data
const float vertexPositions[] = {

//Front side
+0.0f, +1.0f, -4.0f, +1.0f,
+1.0f, -1.0f, -3.0f, +1.0f,
-1.0f, -1.0f, -3.0f, +1.0f,

//Left side
+0.0f, +1.0f, -4.0f, +1.0f,
-1.0f, -1.0f, -3.0f, +1.0f,
-1.0f, -1.0f, -5.0f, +1.0f,

//Right side
+0.0f, +1.0f, -4.0f, +1.0f,
+1.0f, -1.0f, -5.0f, +1.0f,
+1.0f, -1.0f, -3.0f, +1.0f,

//Back side
+0.0f, +1.0f, -4.0f, +1.0f,
-1.0f, -1.0f, -5.0f, +1.0f,
+1.0f, -1.0f, -5.0f, +1.0f,

//Bottom
+1.0f, -1.0f, -3.0f, +1.0f,
+1.0f, -1.0f, -5.0f, +1.0f,
-1.0f, -1.0f, -5.0f, +1.0f,
-1.0f, -1.0f, -3.0f, +1.0f,
};

const float pyramidColors[] = {
//Color info in this array...
};

//Buffer objects
glGenBuffers(1, &positionBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, positionBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexPositions), vertexPositions, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glGenBuffers(1, &colorBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, colorBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(pyramidColors), pyramidColors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

//VAO
setVAO();
}

Share this post


Link to post
Share on other sites
So after looking into this for a few hours today I've managed to locate the error. But I have absolutely NO idea to why this happens. What I did was to construct an object with the following vertices from a tutorial on the net:


const float vertexData2[] =
{
+1.0f, +1.0f, +1.0f, +1.0,
-1.0f, -1.0f, +1.0f, +1.0,
-1.0f, +1.0f, -1.0f, +1.0,

-1.0f, -1.0f, +1.0f, +1.0,
+1.0f, +1.0f, +1.0f, +1.0,
+1.0f, -1.0f, -1.0f, +1.0,

-1.0f, +1.0f, -1.0f, +1.0,
+1.0f, -1.0f, -1.0f, +1.0,
+1.0f, +1.0f, +1.0f, +1.0,

+1.0f, -1.0f, -1.0f, +1.0,
-1.0f, +1.0f, -1.0f, +1.0,
-1.0f, -1.0f, +1.0f, +1.0,

+1.0f, +1.0f, -1.0f, +1.0,
-1.0f, -1.0f, -1.0f, +1.0,
+1.0f, -1.0f, +1.0f, +1.0,

-1.0f, -1.0f, -1.0f, +1.0,
+1.0f, +1.0f, -1.0f, +1.0,
-1.0f, +1.0f, +1.0f, +1.0,

-1.0f, +1.0f, +1.0f, +1.0,
+1.0f, -1.0f, +1.0f, +1.0,
-1.0f, -1.0f, -1.0f, +1.0,

+1.0f, -1.0f, +1.0f, +1.0,
-1.0f, +1.0f, +1.0f, +1.0,
+1.0f, +1.0f, -1.0f, +1.0,
};


When rendering this objects it works just as it should. It only rotate around it's own axis...just like I want it to be. But when I use my own data for building a pyramid (see init() function in the post above) then I get the problem I mentioned.

In other words the problem seems to have something to do in the way I am constructing my mesh. But I can't really say what the problem from that might be so yeah....any kind of help would be nice here to give me a better understanding of this in case someone know what's wrong. My guess is that it might have something to do with the fact that I'm not defining it at the origin. But I'm kind of lost on how to approach that problem.

Share this post


Link to post
Share on other sites
Just want to say that I just solved the problem with this. It had to do with the fact that I did not declare my object around what I assume to be the origin of the world. Thus it's as if it had already been translated and as been mentioned a few times now if we translate something and then rotate it orbits. In this case the cause wasn't by the transformations I made but because of how I declared the positions of my triangles. I will post code later for my vertex array data in case someone with a similar problem might find it helpful in the future.

Share this post


Link to post
Share on other sites
const float vertexPositions[] = {

//Front
+0.0f, +7.0f, +0.0f, +1.0f,
+3.0f, +0.0f, +3.0f, +1.0f,
-3.0f, +0.0f, +3.0f, +1.0f,

//Left
+0.0f, +7.0f, +0.0f, +1.0f,
-3.0f, +0.0f, +3.0f, +1.0f,
-3.0f, +0.0f, -3.0f, +1.0f,

//Right
+0.0f, +7.0f, +0.0f, +1.0f,
+3.0f, +0.0f, -3.0f, +1.0f,
+3.0f, +0.0f, +3.0f, +1.0f,

//Back
+0.0f, +7.0f, +0.0f, +1.0f,
-3.0f, +0.0f, -3.0f, +1.0f,
+3.0f, +0.0f, -3.0f, +1.0f,

//Bottom
-3.0f, +0.0f, -3.0f, +1.0f,
-3.0f, +0.0f, +3.0f, +1.0f,
+3.0f, +0.0f, +3.0f, +1.0f,
+3.0f, +0.0f, -3.0f, +1.0f,
};

Vertex data code for those who wondered. Thanks for the suggestions in this thread either way.

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!