2 * Warp a triangle mesh with a vertex program.
11 #include "glut_wrap.h"
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) glVertexAttrib3fvARB(3, c)
36 #define VERTEX2F(x, y) glVertexAttrib2fARB(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 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 30, x
, y
, z
, 1);
77 glDisable(GL_VERTEX_PROGRAM_ARB
);
84 glEnable(GL_VERTEX_PROGRAM_ARB
);
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 )
164 * c[0..3] = modelview matrix
165 * c[4..7] = inverse modelview matrix
166 * c[30] = gravity source location
167 * c[31] = gravity source strength
169 * c[35] = diffuse color
171 static const char prog
[] =
175 "# Compute distance from vertex to gravity source\n"
176 "ADD R1, program.local[30], -vertex.position; # vector from vertex to gravity\n"
177 "DP3 R2, R1, R1; # dot product\n"
178 "RSQ R2, R2.x; # square root = distance\n"
179 "MUL R2, R2, program.local[31].xxxx; # scale by the gravity factor\n"
181 "# Displace vertex by gravity factor along R1 vector\n"
182 "MAD R3, R1, R2, vertex.position;\n"
184 "# Continue with typical modelview/projection\n"
185 "DP4 result.position.x, state.matrix.mvp.row[0], R3 ; # object x MVP -> clip\n"
186 "DP4 result.position.y, state.matrix.mvp.row[1], R3 ;\n"
187 "DP4 result.position.z, state.matrix.mvp.row[2], R3 ;\n"
188 "DP4 result.position.w, state.matrix.mvp.row[3], R3 ;\n"
190 "MOV result.color, vertex.attrib[3];\n # copy input color to output color\n"
194 if (!glutExtensionSupported("GL_ARB_vertex_program")) {
195 printf("Sorry, this program requires GL_ARB_vertex_program\n");
199 glGenProgramsARB(1, &prognum
);
200 glBindProgramARB(GL_VERTEX_PROGRAM_ARB
, prognum
);
201 glProgramStringARB(GL_VERTEX_PROGRAM_ARB
, GL_PROGRAM_FORMAT_ASCII_ARB
,
202 strlen(prog
), (const GLubyte
*)prog
);
203 errno
= glGetError();
204 printf("glGetError = %d\n", errno
);
206 if (errno
!= GL_NO_ERROR
)
210 glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB
, &errorpos
);
211 printf("errorpos: %d\n", errorpos
);
212 printf("%s\n", glGetString(GL_PROGRAM_ERROR_STRING_ARB
));
216 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 32, 2, 2, 4, 1);
217 /* Diffuse material color */
218 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 35, 0.25, 0, 0.25, 1);
220 /* Gravity strength */
221 glProgramLocalParameter4fARB(GL_VERTEX_PROGRAM_ARB
, 31, .5, 0, 0, 0);
223 glEnable(GL_DEPTH_TEST
);
224 glClearColor(0.3, 0.3, 0.3, 1);
225 glShadeModel(GL_FLAT
);
230 int main( int argc
, char *argv
[] )
232 glutInit( &argc
, argv
);
233 glutInitWindowPosition( 0, 0 );
234 glutInitWindowSize( 250, 250 );
235 glutInitDisplayMode( GLUT_RGB
| GLUT_DOUBLE
| GLUT_DEPTH
);
236 glutCreateWindow(argv
[0]);
238 glutReshapeFunc( Reshape
);
239 glutKeyboardFunc( Key
);
240 glutSpecialFunc( SpecialKey
);
241 glutDisplayFunc( Display
);