Im pretty new to opengl, and I just cant figure out how to rotate this vbo/vao in 2d space. This is how I bind my coordinates:

float points[] =
    0.0f,  0.10f,  0.0f,
    -0.10f, -0.10f,  0.0f,
    0.10f, -0.10f,  0.0f

glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);

I place the vbo at vao[0]

glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

And I enable the array


and call to draw in the cpu, with

glDrawArrays(GL_TRIANGLES, 0, 3);

In my shader, I load the array by:

//glsl shader
layout ( location = 0 ) in vec3 vertex_position ;
void main()
 gl_Position = vec4(vertex_position, 1.0);

Ive edited out the coloring/fragment to save space, but this is my problem: I can make a uniform to move my triangle around, but idk how to apply a transformation matrix to a vec4.

It seems the shader reads my vertex_position VAO one vec/element at a time, and trying to apply any matrix just gives an error.

How do you make or apply a rotation matrix on vao, am I inputting it wrong? All the uniforms I apply seem to only stretch, zoom, or move the overall triangle.

Sorry if the question is bad, or partially confused, shading is new to me overall, so thanks in advance.'

my shader:

#version 410 core
layout ( location = 0 ) in vec3 vertex_position ;
layout ( location = 1 ) in vec3 vertex_colour ;

out vec3 colour;
uniform vec3 fade=vec3(0.0,0.0,0.0); //fade
uniform float zoom=1.0;
uniform mat4 transform = mat4(vec4(1.0,0.0,0.0,0.0),
vec4(0.0,1.0,0.0,0.0), vec4(0.0,0.0,1.0,0.0), vec4(0.0,0.0,0.0,1.0));

void main()

this example https://open.gl/transformations and the answer made it doable.

  • \$\begingroup\$ You aren't declaring the mat4 properly. It wants 4 consecutive vec4s rather than floats. Further your first float just ends with a period, I don't know if that would parse right. However, here's the modified code: uniform mat4 transform = mat4(vec4(1.0,0.0,0.0,0.0), vec4(0.0,1.0,0.0,0.0), vec4(0.0,0.0,1.0,0.0), vec4(1.0,1.0,1.0,1.0)); \$\endgroup\$ – Yattabyte Oct 12 '15 at 17:53
  • \$\begingroup\$ aha! how on earth was I supposed to figure that out? edit: the last vec was wrong, should be 0,0,0,1, not 1,1,1,1, that puts it back midscreen. uve helped me so much \$\endgroup\$ – Charlie Oct 12 '15 at 18:03
  • \$\begingroup\$ hehe yeah, just have to figure out the transformation values now, maybe u could give me a tip where to start? \$\endgroup\$ – Charlie Oct 13 '15 at 0:18
  • 1
    \$\begingroup\$ Well you can start by looking into seeing if you have classes in your existing libraries for matrices and vertices, and whether or not they have the appropriate functions for translating/rotating/scaling. If you don't for either of those, there are many websites that can teach you the math, one of which I supplied for quaternions in my answer. One library I suggest is GLM, also in my answer. \$\endgroup\$ – Yattabyte Oct 13 '15 at 1:21
  • \$\begingroup\$ im interested in understanding whats going on tho, not just finding a quick glm::rotate or w/e, besides, that approach seems to be cpu, i want this to be in gpu memory. maybe I need to read more before making another question, thanks again dude \$\endgroup\$ – Charlie Oct 13 '15 at 1:37

Alright, so transformations in general are best done with matrices. Translation, Rotation, and Scaling can all be done using a **single matrix*. *Since you will be working with vector 3's and 4's, what you end up using is a 4x4 matrix.

There are several libraries out there such as GLM for handling the mathematics generally used in opengl, but whatever library you're using may already have the appropriate vector and matrix related operations.

Secondly, the technique you may use really depends on what you are trying to do. For instance, in 3D software, the position, rotation, and scale of an object is held within a single "Model Matrix", instead of modifying the entire object's vertices. However, when the model gets rendered it's underlying vertices then get transformed by that matrix every frame. It is much better to do it like this as GPU's are incredibly fast and are optimized for doing it like this, rather than doing this CPU side.

To rotate a vector

  • the order of operations is:

    vector4 newVector4 = TransformationMatrix * oldVector4;

To prepare the matrix

  • most libraries have functions for their matrices such as the "lookAt()" function for rotating the matrix to look at a particular point in space. This can sometimes be problematic as Euler rotations can lead to gimbal locking, but are a much easier starting point.

  • Additionally, Quaternions may be supported in the library used for rotating a matrix. Even if no class exists, one can still write in the functionality themselves, however the math can be very off-putting to some.

In your shader, you want to provide it with a uniform mat4. You will need to bind the correct data to it in your application, but the implementation may vary if you have a library which aids you with shader logic. The implementation will look something like this:

layout ( location = 0 ) in vec3 vertex_position ;
uniform mat4 ModelMatrix;

void main()
   gl_Position = ModelMatrix * vec4(vertex_position, 1.0);
  • \$\begingroup\$ Hm, does the order u multiply make a difference? like vec * mat as opposed to mat * vec? I think ive tried this at one point, only backwards \$\endgroup\$ – Charlie Oct 12 '15 at 16:06
  • \$\begingroup\$ Yes it does matter, matrix * vector gives a vector transformed by the matrix. See opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices \$\endgroup\$ – Yattabyte Oct 12 '15 at 16:21
  • \$\begingroup\$ it doesnt work tho. im getting error c7011: "implicit cast from "float" to "vec4", and:error c1058: too much data in init. could you edit in something more elaborate? \$\endgroup\$ – Charlie Oct 12 '15 at 16:53
  • \$\begingroup\$ Show us your shader code. \$\endgroup\$ – Yattabyte Oct 12 '15 at 16:55
  • \$\begingroup\$ check original entry, put it there \$\endgroup\$ – Charlie Oct 12 '15 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.