Clone of mesa.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

vp-tris.c 5.0KB

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