PDA

View Full Version : How to write commands in Vertex Shader to rotate points?



reader1
04-03-2015, 12:15 AM
for example: #version 330
in vec3 VertexPosition;


How to rotate the VertexPosition in vertex shader?

Agent D
04-03-2015, 04:04 AM
As has been suggested to you many times before: Read a beginner tutorial.

You need to get at least some basic knowledge of linear algebra.

Rotating a vector around the origin is a linear transformation and can be expressed by a multiplication with a 3x3 matrix.

You might have noticed that many sample programs pass a 4x4 transformation matrix to the vertex shader that the
vertex shader then multiplies the vertex positions with.

reader1
04-03-2015, 05:20 AM
As has been suggested to you many times before: Read a beginner tutorial.

You need to get at least some basic knowledge of linear algebra.

Rotating a vector around the origin is a linear transformation and can be expressed by a multiplication with a 3x3 matrix.

You might have noticed that many sample programs pass a 4x4 transformation matrix to the vertex shader that the
vertex shader then multiplies the vertex positions with.
Thank you, I have read and find nothing about it.
Im familiar with linear algebra, I know how to operate matrix, verious transformations, and it stores in different form than that of hlsl etc. As to 3x3 or 4x4 matrix, this does not strick me as odd. merely homogeneous coordinate expression. but how to write statements in shader is the issue.
would you pls show me an example?
I have a vertexPosition and say 3x3 rotation matrix, A;
1]I find if I use not VertexPositoin, we will not get correct result. silmilarly, what built-in function shall I use for A?
2]jthe A contains triangle fuctions, say sin, cos, How shall I transmit arguements in vertex shader in order that it can calculate by gpui, not cpu.

this may be only three or five substances.

Agent D
04-03-2015, 05:37 AM
This is probably the simplest vertex shader applying a transformation:


#version 330

uniform mat4 matrix;

layout(location=0) in vec4 V_POSITION;

void main( )
{
gl_Position = matrix * V_POSITION;
}


How to get the transformation matrix into the shader?


GLfloat matrix[16];
GLint location;

location = glGetUniformLocation( program, "matrix" );
glUniformMatrix4fv( location, 1, GL_FALSE, matrix );


Every beginner tutorial on modern GL covers this in pretty much the very first chapter.
It is impossible not to come accross this.

reader1
04-03-2015, 06:23 AM
This is probably the simplest vertex shader applying a transformation:


#version 330

uniform mat4 matrix;

layout(location=0) in vec4 V_POSITION;

void main( )
{
gl_Position = matrix * V_POSITION;
}


How to get the transformation matrix into the shader?


GLfloat matrix[16];
GLint location;

location = glGetUniformLocation( program, "matrix" );
glUniformMatrix4fv( location, 1, GL_FALSE, matrix );


Every beginner tutorial on modern GL covers this in pretty much the very first chapter.
It is impossible not to come accross this.

Thank you very much for your prompt reply, wonderful, however, I may have a little different idea,
In this case, it should be gl_Position =v_position * matrix, otherwise, you may not get correct result according to linear algebra,
2]I want to get arguments transmit the matrix at side of GPU not cpu part.

reader1
04-03-2015, 06:36 AM
for example
mat3 A = { cos(w), sin(w),
-sin(w), cos(w),
0 0 };
I wish this calculation takes place at GPU. WHEN give const int w = 36...in vertex shader.
do I have to declare this matrix at vertex shader?

Agent D
04-03-2015, 06:43 AM
In this case, it should be gl_Position =v_position * matrix, otherwise, you may not get correct result according to linear algebra,

No. Since v_position is a column vector, we right-multiply it with the matrix. If it was a row vector, we would left multiply it with the
transpose matrix, to get the transpose of the result (i.e. the same result as a row vector).



2]I want to get arguments transmit the matrix at side of GPU not cpu part.
....
for example
mat3 A = { cos(w), sin(w),
-sin(w), cos(w),
0 0 };
I wish this calculation takes place at GPU. WHEN give const int w = 36...in vertex shader.
do I have to declare this matrix at vertex shader?

Of course, you can send the 'w' value to the GPU and set the matrix up in the vertex shader, however this would by very
inefficient. If you calculate the matrix on the CPU, you do that once and send the matrix to the GPU. If you calculate it in the
vertex shader, you do the matrix setup once per vertex.

reader1
04-03-2015, 07:24 AM
No. Since v_position is a column vector, we right-multiply it with the matrix. If it was a row vector, we would left multiply it with the
transpose matrix, to get the transpose of the result (i.e. the same result as a row vector).


Of course, you can send the 'w' value to the GPU and set the matrix up in the vertex shader, however this would by very
inefficient. If you calculate the matrix on the CPU, you do that once and send the matrix to the GPU. If you calculate it in the
vertex shader, you do the matrix setup once per vertex.
I modify it as follow,

#version 330

in vec3 VertexPosition;
in vec3 VertexColor;
out vec3 Color;
const int w = 30;
float matt3 A = ( cos(w), sin(w),0,
-sin(w), cos(w),0,
0, 0, 0 );
// */
void main()
{
Color =VertexColor;
//gl_Position = vec4(VertexPosition,1);
gl_Position = vec4(VertexPosition*A,1); }

set up directly at vtertex shader without load from system memory in way of vbo.
but get error like this,
c0000, syntax error, unexpected identifier,...link shader program failed.

reader1
04-03-2015, 07:57 AM
Since v_position is a column vector, we right-multiply it with the ma...
I remember that glsl stores the matrix in way of column vector, whereas v_position is an array, thus should be stored normally, in way of row, so that
(x, y, z, h) * M; opposite to that in hlsl.

If you calculate it in the
vertex shader, you do the matrix setup once per vertex.
that's a matter, indeed, when define at vertex shader, it will be calculated once per vertex, however, I wish to get a correct result at present without taken account of efficiency.

Alfonse Reinheart
04-03-2015, 08:17 AM
Every beginner tutorial on modern GL covers this in pretty much the very first chapter.
It is impossible not to come accross this.

To be fair to him, transformation is usually not a first chapter lesson. It generally happens later.

Granted, if he actually read any tutorial, rather than just skimming through the first page of it for what he's looking for, he'd still learn far more than this current scheme he has of wasting everyone else's time rather than his own.


I remember that glsl stores the matrix in way of column vector, whereas v_position is an array, thus should be stored normally, in way of row

No, v_position is a vector, not an "array". It is therefore interpreted in whatever convention the system wants to interpret it in. OpenGL uses column vectors, and therefore v_position is a column vector.

How something is stored and how it is interpreted are different issues.

reader1
04-03-2015, 06:56 PM
To be fair to him, transformation is usually not a first chapter lesson. It generally happens later.

nt issues.

To be frank, how about nine lessions? then, lets say 31rd, it doesn't matter. as if you'd have learned it as a dish of cake. cong.

Granted, if he actually read any tutorial, rather than just skimming through the first page of it for what he's looking for, he'd still learn far more than this current scheme he has of wasting everyone else's time rather than his own.
as it were, imagine is often not real. tell, where, when, how did you learn tis language?

No, v_position is a vector, not an "array". It is therefore interpreted in whatever convention the system wants to interpret it in. OpenGL uses column vectors, and therefore v_position is a column vector.

Sounds like you are beyond the concept of matrix. How do you represent a vector? you may have to review math and physics, don't limit in type of data. you may know relationship amid m,v and a.
well, what is element of M[2][1]?

How something is stored and how it is interpreted are differen
correct. how to store and read out in order, then?

reader1
04-12-2015, 08:24 AM
No. Since v_position is a column vector, we right-multiply it with the matrix. If it was a row vector, we would left multiply it with the
transpose matrix, to get the transpose of the result (i.e. the same result as a row vector).

vertex.
I browse and come across this, Im afraid I have to insiste on my idea.
mat3 * v_position^T should be matrix left-multify, and v_positon * mat3 should be right_multify.
where v_position^T is column vector arrangement.

if mat3 was stored in buffers according to collumn arrangemtn, it will be,
v_position * (mat3)^T to get correct result.
you may typo.

Agent D
04-12-2015, 08:36 AM
How the vectors are stored in memory and how they are accessed is irrelevant.

GLSL interprets the vec2, vec3 and vec4 data types as column vectors.

Try it out. If you flip it around, your shader won't work anymore.

reader1
04-12-2015, 09:48 AM
How the vectors are stored in memory and how they are accessed is irrelevant.

GLSL interprets the vec2, vec3 and vec4 data types as column vectors.

Try it out. If you flip it around, your shader won't work anymore.
So it is. as it is interpreted a column vector, the matrix must be left multify. (pro), otherwise the law of matrix operation will be broken.

Agent D
04-12-2015, 09:57 AM
"left multiplying" a vetor with a matrix is when I write the vector to the left of the opartor and the matrix to the right.
In this case, the vector must be a row vector and the result is a row vector. Each component of the result is the product
of the row vector with the coresponding matrix column, i.e. the dot product of the transposed column and the vector.

"right multiplying" a vector with a matrix is when I write the vector to the right of the opartor and the matrix to the left.
In this case, the vector and the result are column vectors. Each component of the result is the dot product of the
coresponding matrix row with the transpose input vector.

This is basic linear algebra and has nothing to do with how the matrix and the vector are stored in memory. GLSL
vectors are column vectors. They are right multiplied.

But as I already suggested, why don't you take a sample program and try it out? Take a sample program from a tutorial,
look at the vertex shader and try what happens when you swap the operands.


EDIT: According to the GLSL spec, when multiplying a vector with a matrix as in "vector * matrix", the vector is interpreted as a row
vector. So actually, you can do both in GLSL, but then you have to transpose the matrix.

reader1
04-12-2015, 07:20 PM
"left multiplying" a vetor with a matrix is when I write the vector to the left of the opartor and the matrix to the right.
In this case, the vector must be a row vector and the result is a row vector. Each component of the result is the product
of the row vector with the coresponding matrix column, i.e. the dot product of the transposed column and the vector.

Oh, I see, Rnglish is an interesting one, which has to be read carefully and feel. if you put down formula, that will be more clear.
Im very familiar with matrix theory, but when read the doc about this description of opengl, soon got confusion.
I just want to make sense of how their data stored,

EDIT: According to the GLSL spec, when multiplying a vector with a matrix as in "vector * matrix", the vector is interpreted as a row
vector. So actually, you can do both in GLSL, but then you have to transpose the matrix.
This is very clear. but make sense of data pisition in memory may be a chaos to read some doc.

well, both of us are correct.