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.

fp-tri.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define GL_GLEXT_PROTOTYPES
  5. #include <GL/glut.h>
  6. #include <unistd.h>
  7. #include <signal.h>
  8. unsigned show_fps = 0;
  9. unsigned int frame_cnt = 0;
  10. void alarmhandler(int);
  11. static const char *filename = NULL;
  12. static void usage(char *name)
  13. {
  14. fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
  15. fprintf(stderr, "\n" );
  16. fprintf(stderr, "options:\n");
  17. fprintf(stderr, " -fps show frames per second\n");
  18. }
  19. void alarmhandler (int sig)
  20. {
  21. if (sig == SIGALRM) {
  22. printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
  23. frame_cnt / 5.0);
  24. frame_cnt = 0;
  25. }
  26. signal(SIGALRM, alarmhandler);
  27. alarm(5);
  28. }
  29. static void args(int argc, char *argv[])
  30. {
  31. GLint i;
  32. for (i = 1; i < argc; i++) {
  33. if (strcmp(argv[i], "-fps") == 0) {
  34. show_fps = 1;
  35. }
  36. else if (i == argc - 1) {
  37. filename = argv[i];
  38. }
  39. else {
  40. usage(argv[0]);
  41. exit(1);
  42. }
  43. }
  44. if (!filename) {
  45. usage(argv[0]);
  46. exit(1);
  47. }
  48. }
  49. static void Init( void )
  50. {
  51. GLint errno;
  52. GLuint prognum;
  53. char buf[4096];
  54. GLuint sz;
  55. FILE *f;
  56. if ((f = fopen(filename, "r")) == NULL) {
  57. fprintf(stderr, "Couldn't open %s\n", filename);
  58. exit(1);
  59. }
  60. sz = fread(buf, 1, sizeof(buf), f);
  61. if (!feof(f)) {
  62. fprintf(stderr, "file too long\n");
  63. exit(1);
  64. }
  65. fprintf(stderr, "%.*s\n", sz, buf);
  66. if (!glutExtensionSupported("GL_ARB_fragment_program")) {
  67. printf("Error: GL_ARB_fragment_program not supported!\n");
  68. exit(1);
  69. }
  70. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  71. /* Setup the fragment program */
  72. glGenProgramsARB(1, &prognum);
  73. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum);
  74. glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  75. sz, (const GLubyte *)buf);
  76. errno = glGetError();
  77. printf("glGetError = 0x%x\n", errno);
  78. if (errno != GL_NO_ERROR) {
  79. GLint errorpos;
  80. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  81. printf("errorpos: %d\n", errorpos);
  82. printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
  83. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  84. }
  85. glEnable(GL_FRAGMENT_PROGRAM_ARB);
  86. glClearColor(.3, .3, .3, 0);
  87. }
  88. static void Reshape(int width, int height)
  89. {
  90. glViewport(0, 0, (GLint)width, (GLint)height);
  91. glMatrixMode(GL_PROJECTION);
  92. glLoadIdentity();
  93. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  94. glMatrixMode(GL_MODELVIEW);
  95. }
  96. static void Key(unsigned char key, int x, int y)
  97. {
  98. switch (key) {
  99. case 27:
  100. exit(1);
  101. default:
  102. return;
  103. }
  104. glutPostRedisplay();
  105. }
  106. static void Display(void)
  107. {
  108. glClear(GL_COLOR_BUFFER_BIT);
  109. glBegin(GL_TRIANGLES);
  110. glColor3f(0,0,1);
  111. glVertex3f( 0.9, -0.9, -30.0);
  112. glColor3f(1,0,0);
  113. glVertex3f( 0.9, 0.9, -30.0);
  114. glColor3f(0,1,0);
  115. glVertex3f(-0.9, 0.0, -30.0);
  116. glEnd();
  117. glFlush();
  118. if (show_fps) {
  119. ++frame_cnt;
  120. glutPostRedisplay();
  121. }
  122. }
  123. int main(int argc, char **argv)
  124. {
  125. glutInit(&argc, argv);
  126. glutInitWindowPosition(0, 0);
  127. glutInitWindowSize(250, 250);
  128. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  129. args(argc, argv);
  130. glutCreateWindow(filename);
  131. glutReshapeFunc(Reshape);
  132. glutKeyboardFunc(Key);
  133. glutDisplayFunc(Display);
  134. Init();
  135. if (show_fps) {
  136. signal(SIGALRM, alarmhandler);
  137. alarm(5);
  138. }
  139. glutMainLoop();
  140. return 0;
  141. }