Jump to content

  • Log In with Google      Sign In   
  • Create Account

need help with rotation, transformation and scaling in opengl mathematics


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 MrDarkKnight   Members   -  Reputation: 98

Like
0Likes
Like

Posted 25 January 2012 - 03:30 AM

Hello guys

OpenGL 4.2 is basically killing me, I can't rotation, transformation or scaling, I have to use another library to do it, which is OpenGL mathematics. And I can't find any tutorials on how to do this.

I miss OpenGL 2.1.

Can you please guys help me out here.

I have this cube and I want to rotate it how can I do that in OpenGL mathematics?

void Engine::CreateCube()
{
float* Vertices = new float[72];  // Vertices for our square

Vertices[0]  = -0.3f; Vertices[1]  = -0.3f; Vertices[2]  = 0.3f;
Vertices[3]  =  0.3f; Vertices[4]  = -0.3f; Vertices[5]  = 0.3f;
Vertices[6]  =  0.3f; Vertices[7]  =  0.3f; Vertices[8]  = 0.3f;
Vertices[9]  = -0.3f; Vertices[10] =  0.3f; Vertices[11] = 0.3f;

Vertices[12] = -0.3f; Vertices[13] = -0.3f; Vertices[14] = -0.3f;
Vertices[15] = -0.3f; Vertices[16] =  0.3f; Vertices[17] = -0.3f;
Vertices[18] =  0.3f; Vertices[19] =  0.3f; Vertices[20] = -0.3f;
Vertices[21] =  0.3f; Vertices[22] = -0.3f; Vertices[23] = -0.3f;

Vertices[24] = -0.3f; Vertices[25] = 0.3f;  Vertices[26] = -0.3f;
Vertices[27] = -0.3f; Vertices[28] = 0.3f;  Vertices[29] =  0.3f;
Vertices[30] =  0.3f; Vertices[31] = 0.3f;  Vertices[32] =  0.3f;
Vertices[33] =  0.3f; Vertices[34] = 0.3f;  Vertices[35] = -0.3f;

Vertices[36] = -0.3f; Vertices[37] = -0.3f; Vertices[38] = -0.3f;
Vertices[39] =  0.3f; Vertices[40] = -0.3f; Vertices[41] = -0.3f;
Vertices[42] =  0.3f; Vertices[43] = -0.3f; Vertices[44] =  0.3f;
Vertices[45] = -0.3f; Vertices[46] = -0.3f; Vertices[47] =  0.3f;

Vertices[48] = 0.3f;  Vertices[49] = -0.3f; Vertices[50] = -0.3f;
Vertices[51] = 0.3f;  Vertices[52] =  0.3f; Vertices[53] = -0.3f;
Vertices[54] = 0.3f;  Vertices[55] =  0.3f; Vertices[56] =  0.3f;
Vertices[57] = 0.3f;  Vertices[58] = -0.3f; Vertices[59] =  0.3f;

Vertices[60] = -0.3f; Vertices[61] = -0.3f; Vertices[62] = -0.3f;
Vertices[63] = -0.3f; Vertices[64] = -0.3f; Vertices[65] =  0.3f;
Vertices[66] = -0.3f; Vertices[67] =  0.3f; Vertices[68] =  0.3f;
Vertices[69] = -0.3f; Vertices[70] =  0.3f; Vertices[71] = -0.3f;

glGenVertexArrays(1, &VAO_ID[0]); // Create our Vertex Array Object
glBindVertexArray(VAO_ID[0]); // Bind our Vertex Array Object so we can use it  

glGenBuffers(1, VBO_ID); // Generate our Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, VBO_ID[0]); // Bind our Vertex Buffer Object
glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), Vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW

glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer
glEnableVertexAttribArray(0); // Disable our Vertex Array Object
glBindVertexArray(0); // Disable our Vertex Buffer Object

delete [] Vertices; // Delete our vertices from memory
}

In my redner function which draws the cube i tried doing this but nothing changes.
void Engine::Render()
{
glViewport(0, 0, WindowWidth, WindowHeight);

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

ViewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -5.f)); // Create our view matrix which will translate us back 5 units
ModelMatrix = glm::scale(glm::mat4(1.0f), glm::vec3(0.5f));  // Create our model matrix which will halve the size of our model  

glm::mat4 View = glm::mat4(1.);
View = glm::rotate(View, angle * -1.0f, glm::vec3(1.f, 0.f, 0.f));

glBindVertexArray(VAO_ID[0]); // Bind our Vertex Array Object
	
glDrawArrays(GL_QUADS, 0, 24); // Draw our cube
	
glBindVertexArray(0); // Unbind our Vertex Array Object

}


Sponsor:

#2 guptan   Members   -  Reputation: 128

Like
1Likes
Like

Posted 25 January 2012 - 07:21 AM

You must be knowing how to write shaders for opengl 4.2 and make it running.
Try a port of the following code(most probably no change), for matrix calculations (translation, rotation, scaling etc.) to get
the uniform variables that you pass to your shader.
http://opengles-book-samples.googlecode.com/svn/trunk/iPhone/Common/esTransform.c

#3 MrDarkKnight   Members   -  Reputation: 98

Like
0Likes
Like

Posted 25 January 2012 - 08:12 AM

You must be knowing how to write shaders for opengl 4.2 and make it running.
Try a port of the following code(most probably no change), for matrix calculations (translation, rotation, scaling etc.) to get
the uniform variables that you pass to your shader.
http://opengles-book...n/esTransform.c


so if i don't use shaders i can't do translation, rotation or scaling?
what is sharder? what is the use of them?

sorry for the newbie questions.

#4 Yours3!f   Members   -  Reputation: 1336

Like
1Likes
Like

Posted 25 January 2012 - 09:02 AM

so if i don't use shaders i can't do translation, rotation or scaling?
what is sharder? what is the use of them?

sorry for the newbie questions.


ummm... shaders aren't responsible for translation rotation etc. but rendering. Math libraries are supposed to handle this stuff. You can find many math libraries that replace the built-in opengl maths.
To mention a few: GLM, CML, Eigen, libmymath
A shader is a tiny program that runs on your video card. It supposed to replace the fixed pipeline rendering that you used in OGL 2.1.
It has many advantages over the old rendering one being the great flexibility it offers when it comes to rendering.

#5 V-man   Members   -  Reputation: 805

Like
1Likes
Like

Posted 25 January 2012 - 11:02 AM

http://en.wikipedia.org/wiki/Shader
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#6 MrDarkKnight   Members   -  Reputation: 98

Like
0Likes
Like

Posted 25 January 2012 - 11:10 AM

thank you guptan but i would like to know how to rotate the cube using OpenGL Mathematics (GLM) library.

can anyone tell me how can rotate my cube using OpenGL Mathematics (GLM) library?

#7 japro   Members   -  Reputation: 887

Like
1Likes
Like

Posted 25 January 2012 - 11:35 AM

OpenGL 4.2 pretty much deprecates the fixed pipeline so you have to write shaders. The way you do transforms then is by passing the transformation matrix to the shader which then applies it to the vertices during rendering.

#8 MrDarkKnight   Members   -  Reputation: 98

Like
0Likes
Like

Posted 25 January 2012 - 11:37 AM

OpenGL 4.2 pretty much deprecates the fixed pipeline so you have to write shaders. The way you do transforms then is by passing the transformation matrix to the shader which then applies it to the vertices during rendering.


oo ok thank you very much.
i'll start working on shaders.

thank you guys for all your help. :D

#9 clb   Members   -  Reputation: 1781

Like
1Likes
Like

Posted 25 January 2012 - 11:47 AM

To mention a few: GLM, CML, Eigen, libmymath


If you are allergic to templates or are looking for an online reference of copy-paste code, you can also have a look at MathGeoLib.
Me+PC=clb.demon.fi | C++ Math and Geometry library: MathGeoLib, test it live! | C++ Game Networking: kNet | 2D Bin Packing: RectangleBinPack | Use gcc/clang/emcc from VS: vs-tool | Resume+Portfolio | gfxapi, test it live!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS