How to transform vertices by GLSL..

Started by
7 comments, last by FireInDark 11 years, 9 months ago
typedef struct _vertex
{
float x,y,z;
}vertex;

typedef struct _matrix
{
float value[16];
}
vertex vArray[1000];
vertex mArray[32];

the matrixes will transform the vertices..
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
OK..We send the datas in vArray to a VBO...And we really know witch matrix in mArray will transform a vertex in vArray as a index..
and may be we can send the matrix datas to GPU as a texture or as a buffer object..

we can code GLSL like :
void main ()
{
gl_Position = M1 * M2 * Vertex;//Vertex is an enement in VBO source from vArray;
}
but I don't know the true index of the vertex in vbo.... any way ?

And I've think about to send all the datas - the matrixes and the vertices - as texture to GPU but maybe it'll be foolish..


I'm just finding the right way to act the Skeletal animation with GPU..But not with CPU...

Thanks for any help..
Advertisement
I've learned that we could use Geometry Shader ..
Any sample ?
you can pass an attribute(or in) vertex as vertex data. you don't only have to pass x/y/z, lookup information about attributes with shaders.

for skinning, you can do it as a series of uniform matrix's, but your limited by the number of uniform names the gpu can have. personally, i use a texture buffer sampler, which is essentially a large unformatted array, to hold my skeleton matrix's, and the vertex's have both an ivec4 and vec4 for index/weight information passed.

still, i'd recommend first learning about attributes, then move onto texture buffers/uniform buffers.

also, off topic, but i somehow activated firefox's 3D view when i was responding, it's pretty sick.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

you can pass an attribute(or in) vertex as vertex data. you don't only have to pass x/y/z, lookup information about attributes with shaders.


I notice that you've said :the vertex's have both an ivec4 and vec4 for index/weight information passed.
Yes it's really the right way .. But I don't know how to get the value for the index and weight in GLSL..
All of glVertexPointer and glBufferData can submit data to GPU include the X Y Z , index and weight and other value we want to submit..
Then we call the Scence render funcs of OpenGL but I want to know how should the GLSL separate them as X Y Z and index and weight or other data..?
If the data in vertex buffer is like this:
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight

then how the OpenGL pass the data to our shader process ?
here's the vertex shader:
glVertexAttribPointer is what you need, to be fair, i'd recommend forgetting glVertexPointer, or it's affiliates exist. using glVertexAttribPointer you can pass any data you need to the shader, you just have to bind the attribute's name to a bind index, and pass the proper offset's.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

glVertexAttribPointer is what you need, to be fair, i'd recommend forgetting glVertexPointer, or it's affiliates exist. using glVertexAttribPointer you can pass any data you need to the shader, you just have to bind the attribute's name to a bind index, and pass the proper offset's.


Thank you ..I'll try it latter..biggrin.png

But still I can't understand your last words "the vertex's have both an ivec4 and vec4 for index/weight information passed"
How will you get vertex part ,the index part and the weight part frome a "vertex data buffer" .

I've google glVertexAttribPointer and I noticed that there's a parameter "stride " for it ,just as the glVertexPointer.
It's means that we can pass extra datas like:

"attribute in use" "extra data"
"attribute in use" "extra data"
"attribute in use" "extra data"
"attribute in use" "extra data"
"attribute in use" "extra data"
I got it's useful by your words "bind the attribute's name to a bind index, and pass the proper offset's."

But just like what I express as you pass the data to GPU :
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight
x y z index weight

how will you use the index and the weight part ...
Any way to bind index part and the weight part as we bind the attribute datas ?
you really should be looking up some tutorials, i've given you quite a few keywords to be searching though.

this is a rather old, but goodie for introduction to shaders: http://www.lighthous.../glsl-tutorial/

but let's look at your problem, you want to pass just a single index/weight.

so, in c++/openGL:

struct Vertex{
float x, y, z;
int index;
float weight;
};


void DrawObject(Vertex *Verts, int Count){
glUseProgram(ShaderID); //ShaderID is defined elsewhere
//Also bind buffer here if using vbo
//Upload uniform's here as well for bone matrix's/view/proj matrix's.
int i_VetexLoc=glGetAttribLocation(ShaderID, "i_Vertex"); //get attib locations
int i_IndexLoc=glGetAttribLocation(ShaderID, "i_Index");
int i_WeightLoc=glGetAttribLocation(ShaderID, "i_Weight");

glEnableVertexAttribArray(i_VertexLoc); //Enable them so the appropiate pointer is also called.
glEnableVertexAttribArray(i_IndexLoc);
glEnableVertexAttribArray(i_WeightLoc); //remember to disable later!

glVertexAttribPointer(i_VertexLoc, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), &Verts[0].x); //use offset's if using vbo.
glVertexAttribPointer(i_IndexLoc, 1, GL_INT, GL_FALSE, sizeof(Vertex), &Verts[0].index);
glVertexAttribPointer(i_WeightLoc, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), &Vertes[0].weight);
glDrawArrays(GL_TRIANGLES, 0, Count);
return;
}


note: my engine abstracts this away, so if i got something wrong, someone please correct me.

vertex shader:

#version 150 //i like to use 1.50, but you can use whatever you want.

uniform mat4 ViewMatrix; //My view matrix, and/or viewmodel
uniform mat4 ProjMatirx; //My projection matrix.
uniform mat4 BoneMatrix[30]; //My array of bone matrix's.

in vec3 i_Vertex; //In vertex.
in int i_Index; //Index to BoneMatrix;
in float i_Weight; //Weight of influence.

void main(){
gl_Position = ProjMatrix*ViewMatrix*BoneMatrix[i_Index]*vec4(i_Vertex, 0.0)*i_Weight; //over-simplified, but get's the point across...
return;
}


edit: also:

I've google glVertexAttribPointer and I noticed that there's a parameter "stride " for it ,just as the glVertexPointer.
It's means that we can pass extra datas like:


[quote="docs"]
Stride:
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
[/quote]
it has nothing to do with "extra data", you could encode all the data in diffrent arrays for all gl cares, all stride does it tell each attribute how far to move before reading the next element.
Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

you really should be looking up some tutorials, i've given you quite a few keywords to be searching though.


Yes,that's true ....I just have read the "red book " about OpenGL and I know little about shader language ...
Thus I've got http://www.lighthouse3d.com many days ago ,but still I learned little about GLSL..cause I know little about english too..

But I know how should I code my shader now by reading your codes above..So thank you very much..

This topic is closed to new replies.

Advertisement