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.

convolutions.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /**
  2. * Convolution with GLSL.
  3. * Note: uses GL_ARB_shader_objects, GL_ARB_vertex_shader, GL_ARB_fragment_shader,
  4. * not the OpenGL 2.0 shader API.
  5. * Author: Zack Rusin
  6. */
  7. #include <GL/glew.h>
  8. #define GL_GLEXT_PROTOTYPES
  9. #include "readtex.h"
  10. #include <GL/glut.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <assert.h>
  14. #include <math.h>
  15. enum Filter {
  16. GAUSSIAN_BLUR,
  17. SHARPEN,
  18. MEAN_REMOVAL,
  19. EMBOSS,
  20. EDGE_DETECT,
  21. NO_FILTER,
  22. LAST
  23. };
  24. #define QUIT LAST
  25. struct BoundingBox {
  26. float minx, miny, minz;
  27. float maxx, maxy, maxz;
  28. };
  29. struct Texture {
  30. GLuint id;
  31. GLfloat x;
  32. GLfloat y;
  33. GLint width;
  34. GLint height;
  35. GLenum format;
  36. };
  37. static const char *textureLocation = "../images/girl2.rgb";
  38. static GLfloat viewRotx = 0.0, viewRoty = 0.0, viewRotz = 0.0;
  39. static struct BoundingBox box;
  40. static struct Texture texture;
  41. static GLuint program;
  42. static GLint menuId;
  43. static enum Filter filter = GAUSSIAN_BLUR;
  44. static void checkError(int line)
  45. {
  46. GLenum err = glGetError();
  47. if (err) {
  48. printf("GL Error %s (0x%x) at line %d\n",
  49. gluErrorString(err), (int) err, line);
  50. }
  51. }
  52. static void loadAndCompileShader(GLuint shader, const char *text)
  53. {
  54. GLint stat;
  55. glShaderSource(shader, 1, (const GLchar **) &text, NULL);
  56. glCompileShader(shader);
  57. glGetShaderiv(shader, GL_COMPILE_STATUS, &stat);
  58. if (!stat) {
  59. GLchar log[1000];
  60. GLsizei len;
  61. glGetShaderInfoLog(shader, 1000, &len, log);
  62. fprintf(stderr, "Problem compiling shader: %s\n", log);
  63. exit(1);
  64. }
  65. else {
  66. printf("Shader compiled OK\n");
  67. }
  68. }
  69. static void readShader(GLuint shader, const char *filename)
  70. {
  71. const int max = 100*1000;
  72. int n;
  73. char *buffer = (char*) malloc(max);
  74. FILE *f = fopen(filename, "r");
  75. if (!f) {
  76. fprintf(stderr, "Unable to open shader file %s\n", filename);
  77. exit(1);
  78. }
  79. n = fread(buffer, 1, max, f);
  80. printf("Read %d bytes from shader file %s\n", n, filename);
  81. if (n > 0) {
  82. buffer[n] = 0;
  83. loadAndCompileShader(shader, buffer);
  84. }
  85. fclose(f);
  86. free(buffer);
  87. }
  88. static void
  89. checkLink(GLuint prog)
  90. {
  91. GLint stat;
  92. glGetProgramiv(prog, GL_LINK_STATUS, &stat);
  93. if (!stat) {
  94. GLchar log[1000];
  95. GLsizei len;
  96. glGetProgramInfoLog(prog, 1000, &len, log);
  97. fprintf(stderr, "Linker error:\n%s\n", log);
  98. }
  99. else {
  100. fprintf(stderr, "Link success!\n");
  101. }
  102. }
  103. static void fillConvolution(GLint *k,
  104. GLfloat *scale,
  105. GLfloat *color)
  106. {
  107. switch(filter) {
  108. case GAUSSIAN_BLUR:
  109. k[0] = 1; k[1] = 2; k[2] = 1;
  110. k[3] = 2; k[4] = 4; k[5] = 2;
  111. k[6] = 1; k[7] = 2; k[8] = 1;
  112. *scale = 1./16.;
  113. break;
  114. case SHARPEN:
  115. k[0] = 0; k[1] = -2; k[2] = 0;
  116. k[3] = -2; k[4] = 11; k[5] = -2;
  117. k[6] = 0; k[7] = -2; k[8] = 0;
  118. *scale = 1./3.;
  119. break;
  120. case MEAN_REMOVAL:
  121. k[0] = -1; k[1] = -1; k[2] = -1;
  122. k[3] = -1; k[4] = 9; k[5] = -1;
  123. k[6] = -1; k[7] = -1; k[8] = -1;
  124. *scale = 1./1.;
  125. break;
  126. case EMBOSS:
  127. k[0] = -1; k[1] = 0; k[2] = -1;
  128. k[3] = 0; k[4] = 4; k[5] = 0;
  129. k[6] = -1; k[7] = 0; k[8] = -1;
  130. *scale = 1./1.;
  131. color[0] = 0.5;
  132. color[1] = 0.5;
  133. color[2] = 0.5;
  134. color[3] = 0.5;
  135. break;
  136. case EDGE_DETECT:
  137. k[0] = 1; k[1] = 1; k[2] = 1;
  138. k[3] = 0; k[4] = 0; k[5] = 0;
  139. k[6] = -1; k[7] = -1; k[8] = -1;
  140. *scale = 1.;
  141. color[0] = 0.5;
  142. color[1] = 0.5;
  143. color[2] = 0.5;
  144. color[3] = 0.5;
  145. break;
  146. case NO_FILTER:
  147. k[0] = 0; k[1] = 0; k[2] = 0;
  148. k[3] = 0; k[4] = 1; k[5] = 0;
  149. k[6] = 0; k[7] = 0; k[8] = 0;
  150. *scale = 1.;
  151. break;
  152. default:
  153. assert(!"Unhandled switch value");
  154. }
  155. }
  156. static void setupConvolution()
  157. {
  158. GLint *kernel = (GLint*)malloc(sizeof(GLint) * 9);
  159. GLfloat scale = 0.0;
  160. GLfloat *vecKer = (GLfloat*)malloc(sizeof(GLfloat) * 9 * 4);
  161. GLuint loc;
  162. GLuint i;
  163. GLfloat baseColor[4];
  164. baseColor[0] = 0;
  165. baseColor[1] = 0;
  166. baseColor[2] = 0;
  167. baseColor[3] = 0;
  168. fillConvolution(kernel, &scale, baseColor);
  169. /*vector of 4*/
  170. for (i = 0; i < 9; ++i) {
  171. vecKer[i*4 + 0] = kernel[i];
  172. vecKer[i*4 + 1] = kernel[i];
  173. vecKer[i*4 + 2] = kernel[i];
  174. vecKer[i*4 + 3] = kernel[i];
  175. }
  176. loc = glGetUniformLocationARB(program, "KernelValue");
  177. glUniform4fv(loc, 9, vecKer);
  178. loc = glGetUniformLocationARB(program, "ScaleFactor");
  179. glUniform4f(loc, scale, scale, scale, scale);
  180. loc = glGetUniformLocationARB(program, "BaseColor");
  181. glUniform4f(loc, baseColor[0], baseColor[1],
  182. baseColor[2], baseColor[3]);
  183. free(vecKer);
  184. free(kernel);
  185. }
  186. static void createProgram(const char *vertProgFile,
  187. const char *fragProgFile)
  188. {
  189. GLuint fragShader = 0, vertShader = 0;
  190. program = glCreateProgram();
  191. if (vertProgFile) {
  192. vertShader = glCreateShader(GL_VERTEX_SHADER);
  193. readShader(vertShader, vertProgFile);
  194. glAttachShader(program, vertShader);
  195. }
  196. if (fragProgFile) {
  197. fragShader = glCreateShader(GL_FRAGMENT_SHADER);
  198. readShader(fragShader, fragProgFile);
  199. glAttachShader(program, fragShader);
  200. }
  201. glLinkProgram(program);
  202. checkLink(program);
  203. glUseProgram(program);
  204. /*
  205. assert(glIsProgram(program));
  206. assert(glIsShader(fragShader));
  207. assert(glIsShader(vertShader));
  208. */
  209. checkError(__LINE__);
  210. {/*texture*/
  211. GLuint texLoc = glGetUniformLocationARB(program, "srcTex");
  212. glUniform1iARB(texLoc, 0);
  213. }
  214. {/*setup offsets */
  215. float offsets[] = { 1.0 / texture.width, 1.0 / texture.height,
  216. 0.0 , 1.0 / texture.height,
  217. -1.0 / texture.width, 1.0 / texture.height,
  218. 1.0 / texture.width, 0.0,
  219. 0.0 , 0.0,
  220. -1.0 / texture.width, 0.0,
  221. 1.0 / texture.width, -1.0 / texture.height,
  222. 0.0 , -1.0 / texture.height,
  223. -1.0 / texture.width, -1.0 / texture.height };
  224. GLuint offsetLoc = glGetUniformLocationARB(program, "Offset");
  225. glUniform2fv(offsetLoc, 9, offsets);
  226. }
  227. setupConvolution();
  228. checkError(__LINE__);
  229. }
  230. static void readTexture(const char *filename)
  231. {
  232. GLubyte *data;
  233. texture.x = 0;
  234. texture.y = 0;
  235. glGenTextures(1, &texture.id);
  236. glBindTexture(GL_TEXTURE_2D, texture.id);
  237. glTexParameteri(GL_TEXTURE_2D,
  238. GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  239. glTexParameteri(GL_TEXTURE_2D,
  240. GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  241. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
  242. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
  243. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
  244. data = LoadRGBImage(filename, &texture.width, &texture.height,
  245. &texture.format);
  246. if (!data) {
  247. printf("Error: couldn't load texture image '%s'\n", filename);
  248. exit(1);
  249. }
  250. printf("Texture %s (%d x %d)\n",
  251. filename, texture.width, texture.height);
  252. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  253. texture.width, texture.height, 0, texture.format,
  254. GL_UNSIGNED_BYTE, data);
  255. }
  256. static void menuSelected(int entry)
  257. {
  258. switch (entry) {
  259. case QUIT:
  260. exit(0);
  261. break;
  262. default:
  263. filter = (enum Filter)entry;
  264. }
  265. setupConvolution();
  266. glutPostRedisplay();
  267. }
  268. static void menuInit()
  269. {
  270. menuId = glutCreateMenu(menuSelected);
  271. glutAddMenuEntry("Gaussian blur", GAUSSIAN_BLUR);
  272. glutAddMenuEntry("Sharpen", SHARPEN);
  273. glutAddMenuEntry("Mean removal", MEAN_REMOVAL);
  274. glutAddMenuEntry("Emboss", EMBOSS);
  275. glutAddMenuEntry("Edge detect", EDGE_DETECT);
  276. glutAddMenuEntry("None", NO_FILTER);
  277. glutAddMenuEntry("Quit", QUIT);
  278. glutAttachMenu(GLUT_RIGHT_BUTTON);
  279. }
  280. static void init()
  281. {
  282. if (!glutExtensionSupported("GL_ARB_shader_objects") ||
  283. !glutExtensionSupported("GL_ARB_vertex_shader") ||
  284. !glutExtensionSupported("GL_ARB_fragment_shader")) {
  285. fprintf(stderr, "Sorry, this program requires GL_ARB_shader_objects, GL_ARB_vertex_shader, and GL_ARB_fragment_shader\n");
  286. exit(1);
  287. }
  288. fprintf(stderr, "GL_RENDERER = %s\n", (char *) glGetString(GL_RENDERER));
  289. fprintf(stderr, "GL_VERSION = %s\n", (char *) glGetString(GL_VERSION));
  290. fprintf(stderr, "GL_VENDOR = %s\n", (char *) glGetString(GL_VENDOR));
  291. menuInit();
  292. readTexture(textureLocation);
  293. createProgram("convolution.vert", "convolution.frag");
  294. glEnable(GL_TEXTURE_2D);
  295. glClearColor(1.0, 1.0, 1.0, 1.0);
  296. /*glShadeModel(GL_SMOOTH);*/
  297. glShadeModel(GL_FLAT);
  298. }
  299. static void reshape(int width, int height)
  300. {
  301. glViewport(0, 0, width, height);
  302. glMatrixMode(GL_PROJECTION);
  303. glLoadIdentity();
  304. box.minx = 0;
  305. box.maxx = width;
  306. box.miny = 0;
  307. box.maxy = height;
  308. box.minz = 0;
  309. box.maxz = 1;
  310. glOrtho(box.minx, box.maxx, box.miny, box.maxy, -999999, 999999);
  311. glMatrixMode(GL_MODELVIEW);
  312. }
  313. static void keyPress(unsigned char key, int x, int y)
  314. {
  315. switch(key) {
  316. case 27:
  317. exit(0);
  318. default:
  319. break;
  320. }
  321. glutPostRedisplay();
  322. }
  323. static void
  324. special(int k, int x, int y)
  325. {
  326. switch (k) {
  327. case GLUT_KEY_UP:
  328. viewRotx += 2.0;
  329. break;
  330. case GLUT_KEY_DOWN:
  331. viewRotx -= 2.0;
  332. break;
  333. case GLUT_KEY_LEFT:
  334. viewRoty += 2.0;
  335. break;
  336. case GLUT_KEY_RIGHT:
  337. viewRoty -= 2.0;
  338. break;
  339. default:
  340. return;
  341. }
  342. glutPostRedisplay();
  343. }
  344. static void draw()
  345. {
  346. GLfloat center[2];
  347. GLfloat anchor[2];
  348. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  349. glLoadIdentity();
  350. glPushMatrix();
  351. center[0] = box.maxx/2;
  352. center[1] = box.maxy/2;
  353. anchor[0] = center[0] - texture.width/2;
  354. anchor[1] = center[1] - texture.height/2;
  355. glTranslatef(center[0], center[1], 0);
  356. glRotatef(viewRotx, 1.0, 0.0, 0.0);
  357. glRotatef(viewRoty, 0.0, 1.0, 0.0);
  358. glRotatef(viewRotz, 0.0, 0.0, 1.0);
  359. glTranslatef(-center[0], -center[1], 0);
  360. glTranslatef(anchor[0], anchor[1], 0);
  361. glBegin(GL_TRIANGLE_STRIP);
  362. {
  363. glColor3f(1., 0., 0.);
  364. glTexCoord2f(0, 0);
  365. glVertex3f(0, 0, 0);
  366. glColor3f(0., 1., 0.);
  367. glTexCoord2f(0, 1.0);
  368. glVertex3f(0, texture.height, 0);
  369. glColor3f(1., 0., 0.);
  370. glTexCoord2f(1.0, 0);
  371. glVertex3f(texture.width, 0, 0);
  372. glColor3f(0., 1., 0.);
  373. glTexCoord2f(1, 1);
  374. glVertex3f(texture.width, texture.height, 0);
  375. }
  376. glEnd();
  377. glPopMatrix();
  378. glutSwapBuffers();
  379. }
  380. int main(int argc, char **argv)
  381. {
  382. glutInit(&argc, argv);
  383. glutInitWindowSize(400, 400);
  384. glutInitDisplayMode(GLUT_RGB | GLUT_ALPHA | GLUT_DOUBLE);
  385. if (!glutCreateWindow("Image Convolutions")) {
  386. fprintf(stderr, "Couldn't create window!\n");
  387. exit(1);
  388. }
  389. glewInit();
  390. init();
  391. glutReshapeFunc(reshape);
  392. glutKeyboardFunc(keyPress);
  393. glutSpecialFunc(special);
  394. glutDisplayFunc(draw);
  395. glutMainLoop();
  396. return 0;
  397. }