View Full Version : Simple Vertex Program

11-14-2002, 04:55 AM

Can anyone paste or give me an URL to a simple vertex program that does the vertex calculation that OpenGL do per vertex.

Regards, Ninja

11-14-2002, 05:30 AM
Do you want an nvidia (NV_vertex_program), ati (EXT_vertex_shader), or ARB_vertex_program example? I think your best bet would be to look at the ARB_vertex_program extension since both nvidia and ati support it. Here is a simple example:

ATTRIB vPos = vertex.position;
ATTRIB vCol = vertex.color;
ATTRIB vTex0 = vertex.texcoord;
PARAM mvp[4] = {state.matrix.mvp};
OUTPUT oPos = result.position;
OUTPUT oCol = result.color;
OUTPUT oTex0 = result.texcoord;

#Transform vertex by modelview-proj
DP4 oPos.x, mvp[0], vPos;
DP4 oPos.y, mvp[1], vPos;
DP4 oPos.z, mvp[2], vPos;
DP4 oPos.w, mvp[3], vPos;

MOV oCol, vCol;
MOV oTex0, vTex0;


EDIT: added tex coord move. http://www.opengl.org/discussion_boards/ubb/smile.gif


[This message has been edited by SirKnight (edited 11-14-2002).]

11-15-2002, 12:24 AM
Ok, thanks.
But I want the code for the Blinn equation in OpenGL per vertex.


11-15-2002, 01:35 AM
Grab this presentation http://developer.nvidia.com/view.asp?IO=fixed_function_pipeline

It has vertex program equivalents for most of the standard fixed-function transform & lighting pipeline stage.

11-15-2002, 04:19 AM
Should have said that in the first place. http://www.opengl.org/discussion_boards/ubb/smile.gif

Anyway here is the ARB vertex program code that mimics opengl's fixed function lighting code.

ATTRIB iPos = vertex.position;
ATTRIB iNormal = vertex.normal;
PARAM mvinv[4] = { state.matrix.modelview.invtrans };
PARAM mvp[4] = { state.matrix.mvp };
PARAM lightDir = state.light[0].position;
PARAM halfDir = state.light[0].half;
PARAM specExp = state.material.shininess;
PARAM ambientCol = state.lightprod[0].ambient;
PARAM diffuseCol = state.lightprod[0].diffuse;
PARAM specularCol = state.lightprod[0].specular;
TEMP xfNormal, temp, dots;
OUTPUT oPos = result.position;
OUTPUT oColor = result.color;

# Transform the vertex to clip coordinates.
DP4 oPos.x, mvp[0], iPos;
DP4 oPos.y, mvp[1], iPos;
DP4 oPos.z, mvp[2], iPos;
DP4 oPos.w, mvp[3], iPos;

# Transform the normal to eye coordinates.
DP3 xfNormal.x, mvinv[0], iNormal;
DP3 xfNormal.y, mvinv[1], iNormal;
DP3 xfNormal.z, mvinv[2], iNormal;

# Compute diffuse and specular dot products and use LIT to compute
# lighting coefficients.
DP3 dots.x, xfNormal, lightDir;
DP3 dots.y, xfNormal, halfDir;
MOV dots.w, specExp.x;
LIT dots, dots;

# Accumulate color contributions.
MAD temp, dots.y, diffuseCol, ambientCol;
MAD oColor.xyz, dots.z, specularCol, temp;
MOV oColor.w, difuseCol.w;


11-19-2002, 06:44 AM
Are you sure this is the right program ? Where is the emissive component ?

11-19-2002, 07:10 AM
...and this is for a single *directional* light.

I haven't read the arb_vp spec, but it looks like you can use the opengl light and material parameters in the VP directly - this is very tidy....like it, like it a lot.
How do you tell if a particular opengl light is enabled/disabled in the vertex program? Can you have conditional statements?

11-19-2002, 08:04 AM
knacky, it's even more cool in an ARB_fragment_program http://www.opengl.org/discussion_boards/ubb/biggrin.gif using glLight's directly there in http://www.opengl.org/discussion_boards/ubb/biggrin.gif to.. replace the fixed function by.. uhm.. a bether one http://www.opengl.org/discussion_boards/ubb/biggrin.gif