Help me understand this vbo/rotation, and how its done opengl c++

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

glEnableVertexAttribArray(0);


and call to draw in the cpu, with

glDrawArrays(GL_TRIANGLES, 0, 3);


//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.'

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

out vec3 colour;
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()
{
gl_Position=transform*vec4(vertex_position,1.0);
}


• 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)); – Yattabyte Oct 12 '15 at 17:53
• 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 – Charlie Oct 12 '15 at 18:03
• hehe yeah, just have to figure out the transformation values now, maybe u could give me a tip where to start? – Charlie Oct 13 '15 at 0:18
• 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. – Yattabyte Oct 13 '15 at 1:21
• 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 – 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);
}

• 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 – Charlie Oct 12 '15 at 16:06
• Yes it does matter, matrix * vector gives a vector transformed by the matrix. See opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices – Yattabyte Oct 12 '15 at 16:21
• 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? – Charlie Oct 12 '15 at 16:53
• Show us your shader code. – Yattabyte Oct 12 '15 at 16:55
• check original entry, put it there – Charlie Oct 12 '15 at 17:45