ARB_fragment_program(from beginners forum)

Okay, I have been having trouble with a vertex and fragment program I have been testing. So I tested a simplified version:

VP

!!ARBvp1.0

cgc version 1.3.0001, build date Aug 4 2004 10:01:10

command line args: -profile arbvp1

source file: reflectionLinesVP.cg

#vendor NVIDIA Corporation
#version 1.0.02
#profile arbvp1
#program main
#var float4 i.position : $vin.POSITION : POSITION : 0 : 1
#var float3 i.normal : $vin.NORMAL : NORMAL : 0 : 1
#var float3 i.texcoord0 : : : 0 : 0
#var float3 i.texcoord1 : : : 0 : 0
#var float4 main.position : $vout.HPOS : HPOS : -1 : 1
#var float3 main.texcoord0 : $vout.TEX0 : TEX0 : -1 : 1
#var float3 main.texcoord1 : $vout.TEX1 : TEX1 : -1 : 1
PARAM c[1] = { program.local[0] };
PARAM MVP[4] = { state.matrix.mvp }; # Modelview Projection Matrix.
PARAM IMV[4] = {state.matrix.modelview.invtrans }; #inverse modelview
TEMP Temp;

#Transform vertex to clip space
DP4 Temp.x, MVP[0], vertex.position;
DP4 Temp.y, MVP[1], vertex.position;
DP4 Temp.z, MVP[2], vertex.position;
DP4 Temp.w, MVP[3], vertex.position;
MOV result.position, Temp;
MOV result.texcoord[0].xyz, Temp;

DP3 Temp.x, IMV[0], vertex.normal;
DP3 Temp.y, IMV[1], vertex.normal;
DP3 Temp.z, IMV[2], vertex.normal;

MOV result.texcoord[1].xyz, Temp;
END

3 instructions, 0 R-regs

Fragment Program
PARAM c[1] = { { 0, 1, 0, 1 } };
TEMP R0;
MOV result.color, fragment.texcoord[1];
END

The results in a white background, instead of a multicolored model based on normal directions.

The point is I need to pass, both the normal and position interpolated for each pixel.

So I transform, the vertices, and the normal correctly, and then apply some operations to them in the fragment program.

Help.

Okay, so no one has a comment. Did I stump you with my fairly simplistic question? Please, if someone could help, I would appreciate it.

Try normalizing Temp before moving it into texcoord[1]. If you have any scaling in your
modelview matrix it’ll show up in your normal transform and you’ll get saturated colors.

You could also normalize in the fragment program…

I do normalize. And no I never scale. It is too much of a hassle(ie glScale->transform problems).

Any other ideas. Thanks for the help. Could it be my environment. Mesa(used for testing on my old obselete laptop).

I don’t see it written there, but the fragment program should have !!ARBfp1.0 at the very start of the program string. Check for errors from your gl implementation (ARB_fp and ARB_vp will give you error strings). All white or all black fragments are a common result when the fragment program is invalid.

Nope, it does have the header(bad copying). I do check for errors, and none are generated. I am stumped. Any more ideas?

Only other things I can think of:

  • Check vp for syntax errors
  • Try normalizing in the fragment program (would give more correct results anyway)
  • Try outputting a constant color in fp to make sure the rest of your render state is ok (i.e. MOV result.color, {1,0,0,0}; should make everything red).
  • If this is really all you need, then no fp is necessary; just store your normal in the vertex color in the vp and keep texturing off; then the fixed function pipe will/should render it properly.

The point is to calculate real time reflections of a simplified geometry(at a distance d) on my my mesh.
Simple code:
Hard coding the color values does work. The problem is when I pass in the normal.

As I mentioned no errors, and the vertices are transformed correctly. As are the normals in the vertex program.

Well, thanks for the help, anyways. I appreciate the effort.

try changing fragment program to:
MOV result.color.rgb,fragment.texcoord[1];
MOV result.color.a,{1,1,1,1};
(or set result.texcoord[1].w in the VP)

Your right that did it. I am such a numbnuts.
Thanks to everyone, you’re all very kind.

Okay, everything is working(sort of). For the full fragment + vertex programs. Question: Is there a way to force the video card to use higher precision? The only option is to simply set the precision hint as detailed in ARB_fragment_program extension document.

The problem is that I rely on slight deviations of normals, on the order of less than .0001. These deviations show up well in sphere mapping and regular specular shading. But are not visible in my shading. Thanks.

If I recall correctly, not all modern boards have the option of choosing between different precisions for fragment programs. I remember hearing that ATI operates in just one precision for everything all the time, but that NV has both 16 and 32 bit floating point operations. Hence OPTION ARB_precision_hint_* and not just a direct setting.