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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #ifndef WIN32
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #endif
  8. #include <GL/glew.h>
  9. #include <GL/glut.h>
  10. #include "readtex.c"
  11. #define TEXTURE_FILE "../images/bw.rgb"
  12. unsigned show_fps = 0;
  13. unsigned int frame_cnt = 0;
  14. void alarmhandler(int);
  15. static const char *filename = NULL;
  16. static void usage(char *name)
  17. {
  18. fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
  19. #ifndef WIN32
  20. fprintf(stderr, "\n" );
  21. fprintf(stderr, "options:\n");
  22. fprintf(stderr, " -fps show frames per second\n");
  23. #endif
  24. }
  25. #ifndef WIN32
  26. void alarmhandler (int sig)
  27. {
  28. if (sig == SIGALRM) {
  29. printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
  30. frame_cnt / 5.0);
  31. frame_cnt = 0;
  32. }
  33. signal(SIGALRM, alarmhandler);
  34. alarm(5);
  35. }
  36. #endif
  37. static void args(int argc, char *argv[])
  38. {
  39. GLint i;
  40. for (i = 1; i < argc; i++) {
  41. if (strcmp(argv[i], "-fps") == 0) {
  42. show_fps = 1;
  43. }
  44. else if (i == argc - 1) {
  45. filename = argv[i];
  46. }
  47. else {
  48. usage(argv[0]);
  49. exit(1);
  50. }
  51. }
  52. if (!filename) {
  53. usage(argv[0]);
  54. exit(1);
  55. }
  56. }
  57. static void Init( void )
  58. {
  59. GLuint Texture;
  60. GLint errno;
  61. GLuint prognum;
  62. char buf[50000];
  63. GLuint sz;
  64. FILE *f;
  65. if ((f = fopen(filename, "r")) == NULL) {
  66. fprintf(stderr, "Couldn't open %s\n", filename);
  67. exit(1);
  68. }
  69. sz = fread(buf, 1, sizeof(buf), f);
  70. if (!feof(f)) {
  71. fprintf(stderr, "file too long\n");
  72. exit(1);
  73. }
  74. fprintf(stderr, "%.*s\n", sz, buf);
  75. if (!GLEW_ARB_fragment_program) {
  76. printf("Error: GL_ARB_fragment_program not supported!\n");
  77. exit(1);
  78. }
  79. printf("GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  80. /* Setup the fragment program */
  81. glGenProgramsARB(1, &prognum);
  82. glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, prognum);
  83. glProgramStringARB(GL_FRAGMENT_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB,
  84. sz, (const GLubyte *)buf);
  85. errno = glGetError();
  86. printf("glGetError = 0x%x\n", errno);
  87. if (errno != GL_NO_ERROR) {
  88. GLint errorpos;
  89. glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos);
  90. printf("errorpos: %d\n", errorpos);
  91. printf("glError(GL_PROGRAM_ERROR_STRING_ARB) = %s\n",
  92. (char *) glGetString(GL_PROGRAM_ERROR_STRING_ARB));
  93. }
  94. glEnable(GL_FRAGMENT_PROGRAM_ARB);
  95. /* Load texture */
  96. glGenTextures(1, &Texture);
  97. glBindTexture(GL_TEXTURE_2D, Texture);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  101. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  102. printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
  103. exit(1);
  104. }
  105. glGenTextures(1, &Texture);
  106. glActiveTextureARB(GL_TEXTURE0_ARB + 1);
  107. glBindTexture(GL_TEXTURE_2D, Texture);
  108. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  109. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  110. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  111. {
  112. GLubyte data[32][32];
  113. int width = 32;
  114. int height = 32;
  115. int i;
  116. int j;
  117. for (i = 0; i < 32; i++)
  118. for (j = 0; j < 32; j++)
  119. {
  120. /**
  121. ** +-----------+
  122. ** | W |
  123. ** | +-----+ |
  124. ** | | | |
  125. ** | | B | |
  126. ** | | | |
  127. ** | +-----+ |
  128. ** | |
  129. ** +-----------+
  130. **/
  131. int i2 = i - height / 2;
  132. int j2 = j - width / 2;
  133. int h8 = height / 8;
  134. int w8 = width / 8;
  135. if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
  136. data[i][j] = 0x00;
  137. } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
  138. data[i][j] = 0x55;
  139. } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
  140. data[i][j] = 0xaa;
  141. } else {
  142. data[i][j] = 0xff;
  143. }
  144. }
  145. glTexImage2D( GL_TEXTURE_2D, 0,
  146. GL_ALPHA8,
  147. 32, 32, 0,
  148. GL_ALPHA, GL_UNSIGNED_BYTE, data );
  149. }
  150. {
  151. const float Ambient[4] = { 0.0, 1.0, 0.0, 0.0 };
  152. const float Diffuse[4] = { 1.0, 0.0, 0.0, 0.0 };
  153. const float Specular[4] = { 0.0, 0.0, 1.0, 0.0 };
  154. const float Emission[4] = { 0.0, 0.0, 0.0, 1.0 };
  155. glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, Ambient);
  156. glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Diffuse);
  157. glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, Specular);
  158. glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
  159. }
  160. glClearColor(.1, .3, .5, 0);
  161. fclose(f);
  162. }
  163. static void Reshape(int width, int height)
  164. {
  165. glViewport(0, 0, (GLint)width, (GLint)height);
  166. glMatrixMode(GL_PROJECTION);
  167. glLoadIdentity();
  168. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  169. glMatrixMode(GL_MODELVIEW);
  170. }
  171. static void Key(unsigned char key, int x, int y)
  172. {
  173. switch (key) {
  174. case 27:
  175. exit(1);
  176. default:
  177. break;
  178. }
  179. glutPostRedisplay();
  180. }
  181. static void Display(void)
  182. {
  183. glClear(GL_COLOR_BUFFER_BIT);
  184. glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
  185. glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
  186. glBegin(GL_TRIANGLES);
  187. glColor3f(0,0,1);
  188. glTexCoord3f(1,1,0);
  189. glVertex3f( 0.9, -0.9, -30.0);
  190. glColor3f(1,0,0);
  191. glTexCoord3f(1,-1,0);
  192. glVertex3f( 0.9, 0.9, -30.0);
  193. glColor3f(0,1,0);
  194. glTexCoord3f(-1,0,0);
  195. glVertex3f(-0.9, 0.0, -30.0);
  196. glEnd();
  197. glFlush();
  198. if (show_fps) {
  199. ++frame_cnt;
  200. glutPostRedisplay();
  201. }
  202. }
  203. int main(int argc, char **argv)
  204. {
  205. glutInit(&argc, argv);
  206. glutInitWindowPosition(0, 0);
  207. glutInitWindowSize(250, 250);
  208. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  209. args(argc, argv);
  210. glutCreateWindow(filename);
  211. glewInit();
  212. glutReshapeFunc(Reshape);
  213. glutKeyboardFunc(Key);
  214. glutDisplayFunc(Display);
  215. Init();
  216. #ifndef WIN32
  217. if (show_fps) {
  218. signal(SIGALRM, alarmhandler);
  219. alarm(5);
  220. }
  221. #endif
  222. glutMainLoop();
  223. return 0;
  224. }