PDA

View Full Version : Vertex Shader help



noahn567
11-08-2014, 12:01 PM
Okay, so currently i have 3 attributes being used by the vertex shader. Position, Normal and texture cords. Recently i've added a Rig object to each rawmodel object. If i have these attributes,

List<Matrix4f> boneMatrixs;
ArrayList<ArrayList<Integer>> boneChains;
private float vertexWeights[];
private int vertexBoneCount[];
private float BoneVertexAttatch[][][];

How would i go about adding these attributes to effect each vertex? Thanks in advance!

Vertex Shader:

in vec3 position;
in vec2 textureCoords;
in vec3 normal;

out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;


void main(void){

vec4 worldPosition = (transformationMatrix * vec4(position,1.0));
gl_Position = projectionMatrix * viewMatrix * worldPosition;
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal,0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
}




Loader:


in vec3 position;
in vec2 textureCoords;
in vec3 normal;

out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec3 toLightVector;
out vec3 toCameraVector;

uniform mat4 transformationMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 lightPosition;


void main(void){

vec4 worldPosition = (transformationMatrix * vec4(position,1.0));
gl_Position = projectionMatrix * viewMatrix * worldPosition;
pass_textureCoords = textureCoords;
surfaceNormal = (transformationMatrix * vec4(normal,0.0)).xyz;
toLightVector = lightPosition - worldPosition.xyz;
toCameraVector = (inverse(viewMatrix) * vec4(0.0,0.0,0.0,1.0)).xyz - worldPosition.xyz;
}

Please ask if more information is needed.

GClements
11-08-2014, 09:12 PM
Okay, so currently i have 3 attributes being used by the vertex shader. Position, Normal and texture cords. Recently i've added a Rig object to each rawmodel object. If i have these attributes,


List<Matrix4f> boneMatrixs;
ArrayList<ArrayList<Integer>> boneChains;
private float vertexWeights[];
private int vertexBoneCount[];
private float BoneVertexAttatch[][][];

How would i go about adding these attributes to effect each vertex?
You don't state how those values are supposed to be interpreted.

But as a rough guide, what you need to pass to the vertex shader is:

A 1D array of matrices representing absolute bone transformations.
A 2D array of vertex-bone attachments, i.e. each attachment[v][i] is the index of one of the bones attached to vertex v.
A 2D array of weights, , i.e. each weight[v][i] is the weight corresponding to attachment[v][i].

Note that GLSL doesn't directly support 2D arrays; you can either
[list=1]
pass a flattened array of length num_vertices * num_attachments and translate attachments[v][i] to attachments[v*num_attachments+i], or
use an array of vectors; this limits the maximum number of bones attached to a sinlge vertex to 4.
[list]

Example:


#define NUM_BONES 50
uniform mat4 bones[NUM_BONES];
uniform vec4 weights[NUM_BONES];
uniform ivec4 attachments[NUM_BONES];

in vec4 position;

void main() {
vec4 position4 = vec4(position, 1.0);
vec4 worldPosition = vec4(0.0);
for (int i = 0; i < 4; i++)
worldPosition += weights[attachments[gl_VertexID][i]] * (bones[attachments[gl_VertexID][i]] * position);
gl_Position = gl_ModelViewProjectionMatrix * worldPosition;
}

noahn567
11-08-2014, 10:50 PM
Hey, first off, thank you for the very helpful information :D. This will really get me on my way. You can completely shut down this idea if you'd like, but could we exchange emails? I just think that you are very Intelligent in this field and would love to learn what you know :D. My email: noahn567@gmail.com. I dont really have time to work with the info you've given me right now (Its 2 am here). But i plan on working on this tommorow! I'll update this thread with questions later :D. Thanks again!

DragonForce99
11-09-2014, 01:30 AM
1- bondeindex and bonweight are attributes in other world max bone each vertex will be 4
each vertex most has 0-4 bone effect on it

2- to do it in vertex shader

in vec3 position;
in vec2 textureCoords;
in vec3 normal;
in vec4 BoneIndex;
in vec4 Weights;
uniform mat4 bones[10];

void main()
{
vec4 newvertex;
vev3 newNormal;
new newvertex += bones[int(BoneIndex.x)]*gl_Vertex*Weights.x;
new newNormal+= mat3(bones[int(BoneIndex.x)])*gl_Normal*Weights.x;

new newvertex += bones[int(BoneIndex.y)]*gl_Vertex*Weights.y;
new newNormal+= mat3(bones[int(BoneIndex.y)])*gl_Normal*Weights.y;

new newvertex += bones[int(BoneIndex.z)]*gl_Vertex*Weights.z;
new newNormal+= mat3(bones[int(BoneIndex.z)])*gl_Normal*Weights.z;

new newvertex += bones[int(BoneIndex.w)]*gl_Vertex*Weights.w;
new newNormal+= mat3(bones[int(BoneIndex.w)])*gl_Normal*Weights.w;

gl_Position = Projection*View*Transform*newvertex ;
newNormal = mat4(Transform)*newNormal ;
}