PDA

View Full Version : Question for the usage of attribute in shader ?



lookof
08-26-2011, 08:58 PM
Hi all,

I tried writing a practice program which is used to draw massive cubics using instance tech. I learnt it from here: http://sol.gfxile.net/instancing.html

I sent a matrix to vertex_shader as an attribute by using glVertexAttribPointer, and it works as expected. However, when I comment this matrix in the vertex shader , and instead used gl_ModelViewProjectionMatrix itself, it's failed to draw anything.

Actually the matrix I sent to the vertex_shader is an indentity matrix. It won't affect anything to the result.

So I want to ask why ? Must I use the attribute in the shader which is sent from the host program?

+++++vertex_shader+++++

attribute mat4 transMat;
void main(void)
{
mat4 mvp = gl_ModelViewProjectionMatrix * transMat ; // --works!
//mat4 mvp = gl_ModelViewProjectionMatrix ; // --failed!
gl_Position = mvp * gl_Vertex ;
gl_FrontColor = vec4(1, 0, 0, 0);
}

BionicBytes
08-27-2011, 06:21 AM
i can't see why the inclusion / exclusion of an identity matrix would affect your shader. That is odd.

However, your instancing looks wrong to me. Currently you are using instancing to transform the MVP matrix:

mat4 mvp = gl_ModelViewProjectionMatrix * transMat
The more usual way is to use the instancing data to create a unique modelview matrix, which is in turn used to calculate the MVP. The way your doing it is after you've already created a MVP.

Instead you need to supply your own separate projection, model and camera matricies. The model matrix is comming from your vertex attribute, per instance, so the code would be something like:

mat4 modelview = camera * model;
mat4 mvp = projection * modelview;
gl_Position = mvp * gl_Vertex;

//usual lighting bits and bobs
vec4 ecPos = modelview * gl_Vertex;
......

The key here then is to ensure your camera matrix contains just that - just the camera and nothing to do with models. In other words if your are making use of OpenGL's modelView matrix, then avoid glTranslatef,glRotatef commands as they will be adding model parts to the modelview matrix.