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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 GLuint fragShader;
  17. static GLuint vertShader;
  18. static GLuint program;
  19. static void usage(char *name)
  20. {
  21. fprintf(stderr, "usage: %s [ options ] shader_filename\n", name);
  22. #ifndef WIN32
  23. fprintf(stderr, "\n" );
  24. fprintf(stderr, "options:\n");
  25. fprintf(stderr, " -fps show frames per second\n");
  26. #endif
  27. }
  28. #ifndef WIN32
  29. void alarmhandler (int sig)
  30. {
  31. if (sig == SIGALRM) {
  32. printf("%d frames in 5.0 seconds = %.3f FPS\n", frame_cnt,
  33. frame_cnt / 5.0);
  34. frame_cnt = 0;
  35. }
  36. signal(SIGALRM, alarmhandler);
  37. alarm(5);
  38. }
  39. #endif
  40. static void load_and_compile_shader(GLuint shader, const char *text)
  41. {
  42. GLint stat;
  43. glShaderSource(shader, 1, (const GLchar **) &text, NULL);
  44. glCompileShader(shader);
  45. glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
  46. if (!stat) {
  47. GLchar log[1000];
  48. GLsizei len;
  49. glGetShaderInfoLog(shader, 1000, &len, log);
  50. fprintf(stderr, "fp-tri: problem compiling shader:\n%s\n", log);
  51. exit(1);
  52. }
  53. }
  54. static void read_shader(GLuint shader, const char *filename)
  55. {
  56. const int max = 100*1000;
  57. int n;
  58. char *buffer = (char*) malloc(max);
  59. FILE *f = fopen(filename, "r");
  60. if (!f) {
  61. fprintf(stderr, "fp-tri: Unable to open shader file %s\n", filename);
  62. exit(1);
  63. }
  64. n = fread(buffer, 1, max, f);
  65. printf("fp-tri: read %d bytes from shader file %s\n", n, filename);
  66. if (n > 0) {
  67. buffer[n] = 0;
  68. load_and_compile_shader(shader, buffer);
  69. }
  70. fclose(f);
  71. free(buffer);
  72. }
  73. static void check_link(GLuint prog)
  74. {
  75. GLint stat;
  76. glGetProgramiv(prog, GL_LINK_STATUS, &stat);
  77. if (!stat) {
  78. GLchar log[1000];
  79. GLsizei len;
  80. glGetProgramInfoLog(prog, 1000, &len, log);
  81. fprintf(stderr, "Linker error:\n%s\n", log);
  82. }
  83. }
  84. static void setup_uniforms()
  85. {
  86. {
  87. GLint loc1f = glGetUniformLocationARB(program, "Offset1f");
  88. GLint loc2f = glGetUniformLocationARB(program, "Offset2f");
  89. GLint loc4f = glGetUniformLocationARB(program, "Offset4f");
  90. GLfloat vecKer[] =
  91. { 1.0, 0.0, 0.0, 1.0,
  92. 0.0, 1.0, 0.0, 1.0,
  93. 1.0, 0.0, 0.0, 1.0,
  94. 0.0, 0.0, 0.0, 1.0
  95. };
  96. if (loc1f >= 0)
  97. glUniform1fv(loc1f, 16, vecKer);
  98. if (loc2f >= 0)
  99. glUniform2fv(loc2f, 8, vecKer);
  100. if (loc4f >= 0)
  101. glUniform4fv(loc4f, 4, vecKer);
  102. }
  103. {
  104. GLint loci = glGetUniformLocationARB(program, "KernelSizeInt");
  105. if (loci >= 0)
  106. glUniform1i(loci, 4);
  107. }
  108. {
  109. GLint loc1f = glGetUniformLocationARB(program, "KernelValue1f");
  110. GLint loc2f = glGetUniformLocationARB(program, "KernelValue2f");
  111. GLint loc4f = glGetUniformLocationARB(program, "KernelValue4f");
  112. GLfloat vecKer[] =
  113. { 1.0, 0.0, 0.0, 0.25,
  114. 0.0, 1.0, 0.0, 0.25,
  115. 0.0, 0.0, 1.0, 0.25,
  116. 0.0, 0.0, 0.0, 0.25,
  117. 0.5, 0.0, 0.0, 0.35,
  118. 0.0, 0.5, 0.0, 0.35,
  119. 0.0, 0.0, 0.5, 0.35,
  120. 0.0, 0.0, 0.0, 0.35
  121. };
  122. if (loc1f >= 0)
  123. glUniform1fv(loc1f, 16, vecKer);
  124. if (loc2f >= 0)
  125. glUniform2fv(loc2f, 8, vecKer);
  126. if (loc4f >= 0)
  127. glUniform4fv(loc4f, 4, vecKer);
  128. }
  129. }
  130. static void prepare_shaders()
  131. {
  132. static const char *fragShaderText =
  133. "void main() {\n"
  134. " gl_FragColor = gl_Color;\n"
  135. "}\n";
  136. static const char *vertShaderText =
  137. "void main() {\n"
  138. " gl_FrontColor = gl_Color;\n"
  139. " gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;\n"
  140. "}\n";
  141. fragShader = glCreateShader(GL_FRAGMENT_SHADER);
  142. if (filename)
  143. read_shader(fragShader, filename);
  144. else
  145. load_and_compile_shader(fragShader, fragShaderText);
  146. vertShader = glCreateShader(GL_VERTEX_SHADER);
  147. load_and_compile_shader(vertShader, vertShaderText);
  148. program = glCreateProgram();
  149. glAttachShader(program, fragShader);
  150. glAttachShader(program, vertShader);
  151. glLinkProgram(program);
  152. check_link(program);
  153. glUseProgram(program);
  154. setup_uniforms();
  155. }
  156. #define LEVELS 8
  157. #define SIZE (1<<LEVELS)
  158. static int TexWidth = SIZE, TexHeight = SIZE;
  159. static void
  160. ResetTextureLevel( int i )
  161. {
  162. GLubyte tex2d[SIZE*SIZE][4];
  163. {
  164. GLint Width = TexWidth / (1 << i);
  165. GLint Height = TexHeight / (1 << i);
  166. GLint s, t;
  167. for (s = 0; s < Width; s++) {
  168. for (t = 0; t < Height; t++) {
  169. tex2d[t*Width+s][0] = ((s / 16) % 2) ? 0 : 255;
  170. tex2d[t*Width+s][1] = ((t / 16) % 2) ? 0 : 255;
  171. tex2d[t*Width+s][2] = 128;
  172. tex2d[t*Width+s][3] = 255;
  173. }
  174. }
  175. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  176. glTexImage2D(GL_TEXTURE_2D, i, GL_RGB, Width, Height, 0,
  177. GL_RGBA, GL_UNSIGNED_BYTE, tex2d);
  178. }
  179. }
  180. static void
  181. ResetTexture( void )
  182. {
  183. int i;
  184. for (i = 0; i <= LEVELS; i++)
  185. {
  186. ResetTextureLevel(i);
  187. }
  188. }
  189. static void Init( void )
  190. {
  191. GLuint Texture;
  192. /* Load texture */
  193. glGenTextures(1, &Texture);
  194. glBindTexture(GL_TEXTURE_2D, Texture);
  195. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  196. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  197. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  198. if (!LoadRGBMipmaps(TEXTURE_FILE, GL_RGB)) {
  199. printf("Error: couldn't load texture image file %s\n", TEXTURE_FILE);
  200. exit(1);
  201. }
  202. glGenTextures(1, &Texture);
  203. glActiveTextureARB(GL_TEXTURE0_ARB + 1);
  204. glBindTexture(GL_TEXTURE_2D, Texture);
  205. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  206. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  207. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  208. {
  209. GLubyte data[32][32];
  210. int width = 32;
  211. int height = 32;
  212. int i;
  213. int j;
  214. for (i = 0; i < 32; i++)
  215. for (j = 0; j < 32; j++)
  216. {
  217. /**
  218. ** +-----------+
  219. ** | W |
  220. ** | +-----+ |
  221. ** | | | |
  222. ** | | B | |
  223. ** | | | |
  224. ** | +-----+ |
  225. ** | |
  226. ** +-----------+
  227. **/
  228. int i2 = i - height / 2;
  229. int j2 = j - width / 2;
  230. int h8 = height / 8;
  231. int w8 = width / 8;
  232. if ( -h8 <= i2 && i2 <= h8 && -w8 <= j2 && j2 <= w8 ) {
  233. data[i][j] = 0x00;
  234. } else if ( -2 * h8 <= i2 && i2 <= 2 * h8 && -2 * w8 <= j2 && j2 <= 2 * w8 ) {
  235. data[i][j] = 0x55;
  236. } else if ( -3 * h8 <= i2 && i2 <= 3 * h8 && -3 * w8 <= j2 && j2 <= 3 * w8 ) {
  237. data[i][j] = 0xaa;
  238. } else {
  239. data[i][j] = 0xff;
  240. }
  241. }
  242. glTexImage2D( GL_TEXTURE_2D, 0,
  243. GL_ALPHA8,
  244. 32, 32, 0,
  245. GL_ALPHA, GL_UNSIGNED_BYTE, data );
  246. }
  247. glGenTextures(1, &Texture);
  248. glActiveTextureARB(GL_TEXTURE0_ARB + 2);
  249. glBindTexture(GL_TEXTURE_2D, Texture);
  250. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
  251. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  252. glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  253. ResetTexture();
  254. glClearColor(.1, .3, .5, 0);
  255. }
  256. static void args(int argc, char *argv[])
  257. {
  258. GLint i;
  259. for (i = 1; i < argc; i++) {
  260. if (strcmp(argv[i], "-fps") == 0) {
  261. show_fps = 1;
  262. }
  263. else if (i == argc - 1) {
  264. filename = argv[i];
  265. }
  266. else {
  267. usage(argv[0]);
  268. exit(1);
  269. }
  270. }
  271. }
  272. static void Reshape(int width, int height)
  273. {
  274. glViewport(0, 0, (GLint)width, (GLint)height);
  275. glMatrixMode(GL_PROJECTION);
  276. glLoadIdentity();
  277. glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
  278. glMatrixMode(GL_MODELVIEW);
  279. }
  280. static void CleanUp(void)
  281. {
  282. glDeleteShader(fragShader);
  283. glDeleteShader(vertShader);
  284. glDeleteProgram(program);
  285. }
  286. static void Key(unsigned char key, int x, int y)
  287. {
  288. switch (key) {
  289. case 27:
  290. CleanUp();
  291. exit(1);
  292. default:
  293. break;
  294. }
  295. glutPostRedisplay();
  296. }
  297. static void Display(void)
  298. {
  299. glClear(GL_COLOR_BUFFER_BIT);
  300. glUseProgram(program);
  301. glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 0, 1.0, 1.0, 0.0, 0.0);
  302. glProgramLocalParameter4fARB(GL_FRAGMENT_PROGRAM_ARB, 1, 0.0, 0.0, 1.0, 1.0);
  303. glBegin(GL_TRIANGLES);
  304. glColor3f(0,0,1);
  305. glTexCoord3f(1,1,0);
  306. glVertex3f( 0.9, -0.9, -30.0);
  307. glColor3f(1,0,0);
  308. glTexCoord3f(1,-1,0);
  309. glVertex3f( 0.9, 0.9, -30.0);
  310. glColor3f(0,1,0);
  311. glTexCoord3f(-1,0,0);
  312. glVertex3f(-0.9, 0.0, -30.0);
  313. glEnd();
  314. glFlush();
  315. if (show_fps) {
  316. ++frame_cnt;
  317. glutPostRedisplay();
  318. }
  319. }
  320. int main(int argc, char **argv)
  321. {
  322. glutInit(&argc, argv);
  323. glutInitWindowPosition(0, 0);
  324. glutInitWindowSize(250, 250);
  325. glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE | GLUT_DEPTH);
  326. args(argc, argv);
  327. glutCreateWindow(filename ? filename : "fp-tri");
  328. glewInit();
  329. glutReshapeFunc(Reshape);
  330. glutKeyboardFunc(Key);
  331. glutDisplayFunc(Display);
  332. prepare_shaders();
  333. Init();
  334. #ifndef WIN32
  335. if (show_fps) {
  336. signal(SIGALRM, alarmhandler);
  337. alarm(5);
  338. }
  339. #endif
  340. glutMainLoop();
  341. return 0;
  342. }