Jump to content
  • Advertisement
Sign in to follow this  
Phil15

OpenGL avoid loosing Stacked Transformations on Load identity matrix

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

Hi guys,

When I use to code in regular old opengl it had glLoadIdentity() which would help me translate my object , load Identity matrix and then rotate it around its own axis.

when stacking translation and rotation together an object will not rotate around it`s own origin, but rather (around the initial point it was translated from).(which according to matrix math it is supposed to)

How do I translate an object and then rotatate it about it`s own origin using glm library or even better manually since I want to understand how it can be done. I presume loadIdentity resets the model matrix to identity matrix but then you will loose your previous translation .. I cannot seem to figure out how you could do it manually here is my code:

RenderScene

void renderScene()
{
glm::mat4 ModelViewProject;

	glm::mat4 View;

	glm::mat4 Model;
	
	glm::mat4 Projection = glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 10.0f);


	glm::mat4 rotation = glm::rotate(Model, angle, glm::vec3(1, 0, 0));

	Model *= rotation;

	rotation = loadIdentity(); 

	glm::mat4 transRotation = glm::translate(rotation, glm::vec3(xCh, yCh, zCh));
	

	Model *= transRotation;
View = glm::lookAt(glm::vec3(0, 0, 2), glm::vec3(0, 0, 0), glm::vec3(0.0, 1.0, 0.0));

ModelViewProject = Projection * View *  Model;

GLint transform = glGetUniformLocation(programId, "ModelViewProject");

	glUniformMatrix4fv(transform, 1, false, glm::value_ptr(ModelViewProject));;


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glClearColor(0.0, 0.0, 0.0, 1.0);
	
	glDrawArrays(GL_TRIANGLES, 0, 6);

	glutSwapBuffers();
	}


Vertex Initial position

 


void init()
{
	int z = 1.0f;

	GLfloat vertices[] =
	{
		
		0.999999f, 1.000000f, 1.0f,
		 0.0f, 0.0f, 1.0f,
		 -1.000000f, 1.000000f, 1.0f,
		1.0f, 1.0f, 0.0f,
		-1.000000f, -1.000000f, 1.0f,
		0.0f, 1.0f, 1.0f, // colors

		1.000000f, -1.000000f, 1.0f,
		1.0f, 1.0f, 0.4f,
		0.999999f, 1.000000f, 1.0f,
		1.0f, 0.8f, 0.4f,
		-1.000000f, -1.000000f, 1.0f,
		0.7f, 0.8f, 0.4f,
};

	GLint FragmentShaderColorId;
	GLint VertexId;

	VertexId = glGetAttribLocation(programId, "position");

	FragmentShaderColorId = glGetAttribLocation(programId, "ColorIn");
	glEnable(GL_DEPTH_TEST);


	GLuint myBuffId = 0;

	glGenBuffers(1, &myBuffId);

	glEnableVertexAttribArray(VertexId);

	glBindBuffer(GL_ARRAY_BUFFER, myBuffId);

	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	glVertexAttribPointer(VertexId, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);

	glEnableVertexAttribArray(FragmentShaderColorId);

	glVertexAttribPointer(FragmentShaderColorId, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char *)(sizeof(float) * 3));  // stride of 5 floats describing my colors starting at position 2


Share this post


Link to post
Share on other sites
Advertisement
instead of using loadIdentity() why when you do your translation like say
glm::mat4 translation = glm::translate(glm::mat4(1.0f), glm::vec3(x, y, z));
The mat4(1.0f) is an identity matrix so it leaves your rotation matrix intact

Share this post


Link to post
Share on other sites

Ok, when I try to multiply the translation with the (Model * View * Projection) and then the rotation with the translated (Model * View * Projection) it gives me the same result.

 

glm::mat4 ModelViewProject;

	glm::mat4 View;

	glm::mat4 Model;
	
	glm::mat4 Projection = glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 10.0f);

	glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(xCh, yCh, zCh));



	Model *= translate;


	glm::mat4 rotation = glm::rotate(glm::mat4(1.0), angle, glm::vec3(1, 0, 0));


	Model *= rotation;

View = glm::lookAt(glm::vec3(0, 0, 2), glm::vec3(0, 0, 0), glm::vec3(0.0, 1.0, 0.0));

ModelViewProject = Projection * View *  Model;
	

Share this post


Link to post
Share on other sites


glm::mat4 translate = glm::translate(glm::mat4(1.0), glm::vec3(xCh, yCh, zCh));



Model *= translate;


glm::mat4 rotation = glm::rotate(glm::mat4(1.0), angle, glm::vec3(1, 0, 0));


Model *= rotation;

View = glm::lookAt(glm::vec3(0, 0, 2), glm::vec3(0, 0, 0), glm::vec3(0.0, 1.0, 0.0));

You have a lot of mix and match values here i dont know if it has been improved but glm never liked that so it might be worth changing your 1 to a 1.0f or a 1.f the same with your other numbers such as 0 and 2 to 0.0f and 2.0f

 

With matrix math aswell i believe the order is Scale * Rotation * Translation so in c++ it would be

model = translation * rotation * scale;

 

Then after doing that do your

modelViewProject = projection * view * model;

Share this post


Link to post
Share on other sites

Hi, thanks for your reply, I followed what you said, now the object rotates but still it rotates in an arc sort of way( yet less of an arc than before) but not around its own axis.
The most surprising thing is it rotates in an arc way even if you do not apply translation (just rotation).....any ideas ?
 

void renderScene()
{
glm::mat4 ModelViewProject(1.0f);

	glm::mat4 View(1.0f);

	glm::mat4 Model(1.0f);
	
	glm::mat4 Projection = glm::perspective(45.0f, 1024.0f / 768.0f, 0.1f, 10.0f);

	glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(xCh, yCh, zCh));

glm::mat4 rotation = glm::rotate(glm::mat4(1.0), angle, glm::vec3(1.0f, 0.0f, 0.0f));


	Model = translate * rotation;

	View = glm::lookAt(glm::vec3(0.0f, 0.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

	ModelViewProject = Projection * View *  Model;

	GLint transform = glGetUniformLocation(programId, "ModelViewProject");

	glUniformMatrix4fv(transform, 1, false, glm::value_ptr(ModelViewProject));;


	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	
	glDrawArrays(GL_TRIANGLES, 0, 6);

	glutSwapBuffers();
}
	


 

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!