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

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

## 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?

[CODE]
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
}
[/CODE]

In my redner function which draws the cube i tried doing this but nothing changes.
[CODE]
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

}
[/CODE]

##### 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

##### Share on other sites
[quote name='guptan' timestamp='1327497688' post='4906090']
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
[/quote]

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 on other sites
[quote name='MrDarkKnight' timestamp='1327500720' post='4906102']
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.
[/quote]

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 on other sites
thank you [url="../../user/110541-guptan/"]guptan[/url] 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 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 on other sites
[quote name='japro' timestamp='1327512934' post='4906146']
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.
[/quote]

oo ok thank you very much.

thank you guys for all your help.

##### Share on other sites
[quote name='Yours3!f' timestamp='1327503771' post='4906116']
To mention a few: GLM, CML, Eigen, libmymath
[/quote]

If you are allergic to templates or are looking for an online reference of copy-paste code, you can also have a look at [url="http://clb.demon.fi/MathGeoLib/"]MathGeoLib[/url].

• 10
• 9
• 14
• 16
• 10
• ### Similar Content

• i got error 1282 in my code.
#version 450 core layout(location=0) in vec3 inPos; layout(location=1) in vec2 inTexCoord; out vec2 TexCoord; void main() { gl_Position=vec4(inPos,1.0); TexCoord=inTexCoord; } and the fragment shader
#version 450 core in vec2 TexCoord; uniform sampler2D inTextureOne; uniform sampler2D inTextureTwo; out vec4 FragmentColor; void main() { FragmentColor=mix(texture(inTextureOne,TexCoord),texture(inTextureTwo,TexCoord),0.2); } I was expecting awesomeface.png on top of container.jpg

• By khawk
We've just released all of the source code for the NeHe OpenGL lessons on our Github page at https://github.com/gamedev-net/nehe-opengl. code - 43 total platforms, configurations, and languages are included.
Now operated by GameDev.net, NeHe is located at http://nehe.gamedev.net where it has been a valuable resource for developers wanting to learn OpenGL and graphics programming.

View full story
• By TheChubu
The Khronos™ Group, an open consortium of leading hardware and software companies, announces from the SIGGRAPH 2017 Conference the immediate public availability of the OpenGL® 4.6 specification. OpenGL 4.6 integrates the functionality of numerous ARB and EXT extensions created by Khronos members AMD, Intel, and NVIDIA into core, including the capability to ingest SPIR-V™ shaders.
SPIR-V is a Khronos-defined standard intermediate language for parallel compute and graphics, which enables content creators to simplify their shader authoring and management pipelines while providing significant source shading language flexibility. OpenGL 4.6 adds support for ingesting SPIR-V shaders to the core specification, guaranteeing that SPIR-V shaders will be widely supported by OpenGL implementations.
OpenGL 4.6 adds the functionality of these ARB extensions to OpenGL’s core specification:
GL_ARB_gl_spirv and GL_ARB_spirv_extensions to standardize SPIR-V support for OpenGL GL_ARB_indirect_parameters and GL_ARB_shader_draw_parameters for reducing the CPU overhead associated with rendering batches of geometry GL_ARB_pipeline_statistics_query and GL_ARB_transform_feedback_overflow_querystandardize OpenGL support for features available in Direct3D GL_ARB_texture_filter_anisotropic (based on GL_EXT_texture_filter_anisotropic) brings previously IP encumbered functionality into OpenGL to improve the visual quality of textured scenes GL_ARB_polygon_offset_clamp (based on GL_EXT_polygon_offset_clamp) suppresses a common visual artifact known as a “light leak” associated with rendering shadows GL_ARB_shader_atomic_counter_ops and GL_ARB_shader_group_vote add shader intrinsics supported by all desktop vendors to improve functionality and performance GL_KHR_no_error reduces driver overhead by allowing the application to indicate that it expects error-free operation so errors need not be generated In addition to the above features being added to OpenGL 4.6, the following are being released as extensions:
GL_KHR_parallel_shader_compile allows applications to launch multiple shader compile threads to improve shader compile throughput WGL_ARB_create_context_no_error and GXL_ARB_create_context_no_error allow no error contexts to be created with WGL or GLX that support the GL_KHR_no_error extension “I’m proud to announce OpenGL 4.6 as the most feature-rich version of OpenGL yet. We've brought together the most popular, widely-supported extensions into a new core specification to give OpenGL developers and end users an improved baseline feature set. This includes resolving previous intellectual property roadblocks to bringing anisotropic texture filtering and polygon offset clamping into the core specification to enable widespread implementation and usage,” said Piers Daniell, chair of the OpenGL Working Group at Khronos. “The OpenGL working group will continue to respond to market needs and work with GPU vendors to ensure OpenGL remains a viable and evolving graphics API for all its customers and users across many vital industries.“
The OpenGL 4.6 specification can be found at https://khronos.org/registry/OpenGL/index_gl.php. The GLSL to SPIR-V compiler glslang has been updated with GLSL 4.60 support, and can be found at https://github.com/KhronosGroup/glslang.
Sophisticated graphics applications will also benefit from a set of newly released extensions for both OpenGL and OpenGL ES to enable interoperability with Vulkan and Direct3D. These extensions are named:
GL_EXT_memory_object GL_EXT_memory_object_fd GL_EXT_memory_object_win32 GL_EXT_semaphore GL_EXT_semaphore_fd GL_EXT_semaphore_win32 GL_EXT_win32_keyed_mutex They can be found at: https://khronos.org/registry/OpenGL/index_gl.php
Industry Support for OpenGL 4.6
“With OpenGL 4.6 our customers have an improved set of core features available on our full range of OpenGL 4.x capable GPUs. These features provide improved rendering quality, performance and functionality. As the graphics industry’s most popular API, we fully support OpenGL and will continue to work closely with the Khronos Group on the development of new OpenGL specifications and extensions for our customers. NVIDIA has released beta OpenGL 4.6 drivers today at https://developer.nvidia.com/opengl-driver so developers can use these new features right away,” said Bob Pette, vice president, Professional Graphics at NVIDIA.
"OpenGL 4.6 will be the first OpenGL release where conformant open source implementations based on the Mesa project will be deliverable in a reasonable timeframe after release. The open sourcing of the OpenGL conformance test suite and ongoing work between Khronos and X.org will also allow for non-vendor led open source implementations to achieve conformance in the near future," said David Airlie, senior principal engineer at Red Hat, and developer on Mesa/X.org projects.

View full story
• By _OskaR
Hi,
I have an OpenGL application but without possibility to wite own shaders.
I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
• By xhcao
Does sync be needed to read texture content after access texture image in compute shader?
My simple code is as below,
glUseProgram(program.get());
glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
glDispatchCompute(1, 1, 1);
// Does sync be needed here?
glUseProgram(0);