其实在Cg中是没有varying这种类型限制符的。
如何让程序不仅仅输出一个单一的颜色,而是可以随顶点改变的颜色或纹理坐标集呢?
Cg教程中给出如下范例:
- // This is C3E2v_varying from "The Cg Tutorial" (Addison-Wesley, ISBN
- // 0321194969) by Randima Fernando and Mark J. Kilgard. See page 65.
- struct C3E2v_Output {
- float4 position : POSITION;
- float3 color : COLOR;
- float2 texCoord : TEXCOORD0;
- };
- C3E2v_Output C3E2v_varying(float2 position : POSITION,
- float3 color : COLOR,
- float2 texCoord : TEXCOORD0)
- {
- C3E2v_Output OUT;
- OUT.position = float4(position,0,1);
- OUT.color = color;
- OUT.texCoord = texCoord;
- return OUT;
- }
COLOR和TEXCOORD0分别对应着程序指定的顶点颜色和纹理坐标集0。
在应用程序中这样指定颜色就好了:
- glBegin(GL_TRIANGLES);
- glColor3f(1, 0, 0); /* Red */
- glVertex2f(-0.8, 0.8);
- glColor3f(0, 1, 0); /* Green */
- glVertex2f(0.8, 0.8);
- glColor3f(0, 0, 1); /* Blue */
- glVertex2f(0.0, -0.8);
- glEnd();