Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vp-tris.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /* Test glGenProgramsNV(), glIsProgramNV(), glLoadProgramNV() */
  2. #include <assert.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #define GL_GLEXT_PROTOTYPES
  8. #include <GL/glut.h>
  9. static const char *filename = NULL;
  10. static GLuint nr_steps = 4;
  11. static void usage( char *name )
  12. {
  13. fprintf( stderr, "usage: %s [ options ] shader_filename\n", name );
  14. fprintf( stderr, "\n" );
  15. fprintf( stderr, "options:\n" );
  16. fprintf( stderr, " -f flat shaded\n" );
  17. fprintf( stderr, " -nNr subdivision steps\n" );
  18. }
  19. static void args(int argc, char *argv[])
  20. {
  21. GLint i;
  22. for (i = 1; i < argc; i++) {
  23. if (strncmp(argv[i], "-n", 2) == 0) {
  24. nr_steps = atoi((argv[i]) + 2);
  25. }
  26. else if (strcmp(argv[i], "-f") == 0) {
  27. glShadeModel(GL_FLAT);
  28. }
  29. else if (i == argc - 1) {
  30. filename = argv[i];
  31. }
  32. else {
  33. usage(argv[0]);
  34. exit(1);
  35. }
  36. }
  37. if (!filename) {
  38. usage(argv[0]);
  39. exit(1);
  40. }
  41. }
  42. static void Init( void )
  43. {
  44. GLint errno;
  45. GLuint prognum;
  46. char buf[4096];
  47. GLuint sz;
  48. FILE *f;
  49. if ((f = fopen(filename, "r")) == NULL) {
  50. fprintf(stderr, "couldn't open %s\n", filename);
  51. exit(1);
  52. }
  53. sz = fread(buf, 1, sizeof(buf), f);
  54. if (!feof(f)) {
  55. fprintf(stderr, "file too long\n");
  56. exit(1);
  57. }
  58. fprintf(stderr, "%.*s\n", sz, buf);
  59. glGenProgramsARB(1, &prognum);
  60. glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum);
  61. glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  62. sz, (const GLubyte *) buf);
  63. errno = glGetError();
  64. printf("glGetError = %d\n", errno);
  65. if (errno != GL_NO_ERROR)
  66. {
  67. GLint errorpos;
  68. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  69. printf("errorpos: %d\n", errorpos);
  70. printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  71. }
  72. assert(glIsProgramARB(prognum));
  73. }
  74. union vert {
  75. struct {
  76. GLfloat color[3];
  77. GLfloat pos[3];
  78. } v;
  79. GLfloat f[6];
  80. };
  81. static void make_midpoint( union vert *out,
  82. const union vert *v0,
  83. const union vert *v1)
  84. {
  85. int i;
  86. for (i = 0; i < 6; i++)
  87. out->f[i] = v0->f[i] + .5 * (v1->f[i] - v0->f[i]);
  88. }
  89. static void subdiv( union vert *v0,
  90. union vert *v1,
  91. union vert *v2,
  92. GLuint depth )
  93. {
  94. if (depth == 0) {
  95. glColor3fv(v0->v.color);
  96. glVertex3fv(v0->v.pos);
  97. glColor3fv(v1->v.color);
  98. glVertex3fv(v1->v.pos);
  99. glColor3fv(v2->v.color);
  100. glVertex3fv(v2->v.pos);
  101. }
  102. else {
  103. union vert m[3];
  104. make_midpoint(&m[0], v0, v1);
  105. make_midpoint(&m[1], v1, v2);
  106. make_midpoint(&m[2], v2, v0);
  107. subdiv(&m[0], &m[2], v0, depth-1);
  108. subdiv(&m[1], &m[0], v1, depth-1);
  109. subdiv(&m[2], &m[1], v2, depth-1);
  110. subdiv(&m[0], &m[1], &m[2], depth-1);
  111. }
  112. }
  113. /** Assignment */
  114. #define ASSIGN_3V( V, V0, V1, V2 ) \
  115. do { \
  116. V[0] = V0; \
  117. V[1] = V1; \
  118. V[2] = V2; \
  119. } while(0)
  120. static void Display( void )
  121. {
  122. glClearColor(0.3, 0.3, 0.3, 1);
  123. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  124. glEnable(GL_VERTEX_PROGRAM_NV);
  125. glBegin(GL_TRIANGLES);
  126. {
  127. union vert v[3];
  128. ASSIGN_3V(v[0].v.color, 0,0,1);
  129. ASSIGN_3V(v[0].v.pos, 0.9, -0.9, 0.0);
  130. ASSIGN_3V(v[1].v.color, 1,0,0);
  131. ASSIGN_3V(v[1].v.pos, 0.9, 0.9, 0.0);
  132. ASSIGN_3V(v[2].v.color, 0,1,0);
  133. ASSIGN_3V(v[2].v.pos, -0.9, 0, 0.0);
  134. subdiv(&v[0], &v[1], &v[2], nr_steps);
  135. }
  136. glEnd();
  137. glFlush();
  138. }
  139. static void Reshape( int width, int height )
  140. {
  141. glViewport( 0, 0, width, height );
  142. glMatrixMode( GL_PROJECTION );
  143. glLoadIdentity();
  144. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  145. glMatrixMode( GL_MODELVIEW );
  146. glLoadIdentity();
  147. /*glTranslatef( 0.0, 0.0, -15.0 );*/
  148. }
  149. static void Key( unsigned char key, int x, int y )
  150. {
  151. (void) x;
  152. (void) y;
  153. switch (key) {
  154. case 27:
  155. exit(0);
  156. break;
  157. }
  158. glutPostRedisplay();
  159. }
  160. int main( int argc, char *argv[] )
  161. {
  162. glutInit( &argc, argv );
  163. glutInitWindowPosition( 0, 0 );
  164. glutInitWindowSize( 250, 250 );
  165. glutInitDisplayMode( GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH );
  166. glutCreateWindow(argv[0]);
  167. glutReshapeFunc( Reshape );
  168. glutKeyboardFunc( Key );
  169. glutDisplayFunc( Display );
  170. args( argc, argv );
  171. Init();
  172. glutMainLoop();
  173. return 0;
  174. }