2 * Warp a triangle mesh with a vertex program.
13 static float Xrot
= -60.0, Yrot
= 0.0, Zrot
= 0.0;
14 static GLboolean Anim
= GL_TRUE
;
15 static GLfloat Phi
= 0.0;
18 static void Idle( void )
25 static void DrawMesh( int rows
, int cols
)
27 static const GLfloat colorA
[3] = { 0, 1, 0 };
28 static const GLfloat colorB
[3] = { 0, 0, 1 };
29 const float dx
= 2.0 / (cols
- 1);
30 const float dy
= 2.0 / (rows
- 1);
35 #define COLOR3FV(c) glVertexAttrib3fvNV(3, c)
36 #define VERTEX2F(x, y) glVertexAttrib2fNV(0, x, y)
38 #define COLOR3FV(c) glColor3fv(c)
39 #define VERTEX2F(x, y) glVertex2f(x, y)
43 for (i
= 0; i
< rows
- 1; i
++) {
44 glBegin(GL_QUAD_STRIP
);
46 for (j
= 0; j
< cols
; j
++) {
61 static void Display( void )
63 glClear( GL_COLOR_BUFFER_BIT
| GL_DEPTH_BUFFER_BIT
);
66 glRotatef(Xrot
, 1, 0, 0);
67 glRotatef(Yrot
, 0, 1, 0);
68 glRotatef(Zrot
, 0, 0, 1);
70 /* Position the gravity source */
72 GLfloat x
, y
, z
, r
= 0.5;
76 glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV
, 30, x
, y
, z
, 1);
77 glDisable(GL_VERTEX_PROGRAM_NV
);
84 glEnable(GL_VERTEX_PROGRAM_NV
);
92 static void Reshape( int width
, int height
)
94 float ar
= (float) width
/ (float) height
;
95 glViewport( 0, 0, width
, height
);
96 glMatrixMode( GL_PROJECTION
);
98 glFrustum( -1.0 * ar
, 1.0 * ar
, -1.0, 1.0, 5.0, 25.0 );
99 glMatrixMode( GL_MODELVIEW
);
101 glTranslatef( 0.0, 0.0, -12.0 );
106 static void Key( unsigned char key
, int x
, int y
)
135 static void SpecialKey( int key
, int x
, int y
)
137 const GLfloat step
= 3.0;
158 static void Init( void )
161 * c[0..3] = modelview matrix
162 * c[4..7] = inverse modelview matrix
163 * c[30] = gravity source location
164 * c[31] = gravity source strength
166 * c[35] = diffuse color
168 static const char prog
[] =
171 "# Compute distance from vertex to gravity source\n"
172 "ADD R1, c[30], -v[OPOS]; # vector from vertex to gravity\n"
173 "DP3 R2, R1, R1; # dot product\n"
174 "RSQ R2, R2.x; # square root = distance\n"
175 "MUL R2, R2, c[31].xxxx; # scale by the gravity factor\n"
177 "# Displace vertex by gravity factor along R1 vector\n"
178 "MAD R3, R1, R2, v[OPOS];\n"
180 "# Continue with typical modelview/projection\n"
181 "DP4 o[HPOS].x, c[0], R3 ; # object x MVP -> clip\n"
182 "DP4 o[HPOS].y, c[1], R3 ;\n"
183 "DP4 o[HPOS].z, c[2], R3 ;\n"
184 "DP4 o[HPOS].w, c[3], R3 ;\n"
186 "MOV o[COL0], v[COL0];\n # copy input color to output color\n"
190 if (!glutExtensionSupported("GL_NV_vertex_program")) {
191 printf("Sorry, this program requires GL_NV_vertex_program\n");
195 glLoadProgramNV(GL_VERTEX_PROGRAM_NV
, 1,
196 strlen(prog
), (const GLubyte
*) prog
);
197 assert(glIsProgramNV(1));
198 glBindProgramNV(GL_VERTEX_PROGRAM_NV
, 1);
200 /* Load the program registers */
201 glTrackMatrixNV(GL_VERTEX_PROGRAM_NV
, 0, GL_MODELVIEW_PROJECTION_NV
, GL_IDENTITY_NV
);
202 glTrackMatrixNV(GL_VERTEX_PROGRAM_NV
, 4, GL_MODELVIEW
, GL_INVERSE_TRANSPOSE_NV
);
205 glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV
, 32, 2, 2, 4, 1);
206 /* Diffuse material color */
207 glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV
, 35, 0.25, 0, 0.25, 1);
209 /* Gravity strength */
210 glProgramParameter4fNV(GL_VERTEX_PROGRAM_NV
, 31, .5, 0, 0, 0);
212 glEnable(GL_DEPTH_TEST
);
213 glClearColor(0.3, 0.3, 0.3, 1);
214 glShadeModel(GL_FLAT
);
216 printf("glGetError = %d\n", (int) glGetError());
220 int main( int argc
, char *argv
[] )
222 glutInit( &argc
, argv
);
223 glutInitWindowPosition( 0, 0 );
224 glutInitWindowSize( 250, 250 );
225 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
226 glutCreateWindow(argv
[0]);
228 glutReshapeFunc( Reshape
);
229 glutKeyboardFunc( Key
);
230 glutSpecialFunc( SpecialKey
);
231 glutDisplayFunc( Display
);