Jump to content
  • Advertisement
Sign in to follow this  
MrDarkKnight

OpenGL need help with rotation, transformation and scaling in opengl mathematics

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

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

}

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!