Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

driver_gl.c 13 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file driver_gl.c
  12. * \version \$Id$
  13. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  14. * \brief OpenGL driver
  15. *
  16. * This file contains the libcaca OpenGL input and output driver
  17. */
  18. #include "config.h"
  19. #if defined(USE_GL)
  20. #ifdef HAVE_OPENGL_GL_H
  21. # include <OpenGL/gl.h>
  22. # include <GLUT/glut.h>
  23. #else
  24. # include <GL/gl.h>
  25. # include <GL/glut.h>
  26. # include <GL/freeglut_ext.h>
  27. #endif
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include "caca.h"
  32. #include "caca_internals.h"
  33. #include "cucul.h"
  34. #include "cucul_internals.h"
  35. /*
  36. * Global variables
  37. */
  38. /* Ok, I just suck. */
  39. static GLbyte const gl_bgpal[][4] =
  40. {
  41. { 0x00, 0x00, 0x00, 0x7f },
  42. { 0x00, 0x00, 0x3f, 0x7f },
  43. { 0x00, 0x3f, 0x00, 0x7f },
  44. { 0x00, 0x3f, 0x3f, 0x7f },
  45. { 0x3f, 0x00, 0x00, 0x7f },
  46. { 0x3f, 0x00, 0x3f, 0x7f },
  47. { 0x3f, 0x3f, 0x00, 0x7f },
  48. { 0x3f, 0x3f, 0x3f, 0x7f },
  49. // + intensity
  50. // >.
  51. // ()
  52. // ^^
  53. { 0x1f, 0x1f, 0x1f, 0x7f },
  54. { 0x1f, 0x1f, 0x7f, 0x7f },
  55. { 0x1f, 0x7f, 0x1f, 0x7f },
  56. { 0x1f, 0x7f, 0x7f, 0x7f },
  57. { 0x7f, 0x1f, 0x1f, 0x7f },
  58. { 0x7f, 0x1f, 0x7f, 0x7f },
  59. { 0x7f, 0x7f, 0x1f, 0x7f },
  60. { 0x7f, 0x7f, 0x7f, 0x7f }
  61. };
  62. static caca_t *gl_kk; /* FIXME: we ought to get rid of this */
  63. /*
  64. * Local functions
  65. */
  66. static void gl_handle_keyboard(unsigned char, int, int);
  67. static void gl_handle_special_key(int, int, int);
  68. static void gl_handle_reshape(int, int);
  69. static void gl_handle_mouse(int, int, int, int);
  70. static void gl_handle_mouse_motion(int, int);
  71. static void _display(void);
  72. struct driver_private
  73. {
  74. int window;
  75. unsigned int width, height;
  76. unsigned int new_width, new_height;
  77. float font_width, font_height;
  78. float incx, incy;
  79. int id[128 - 32];
  80. unsigned char bit;
  81. unsigned char mouse_changed, mouse_clicked;
  82. unsigned int mouse_x, mouse_y;
  83. unsigned int mouse_button, mouse_state;
  84. unsigned char key;
  85. int special_key;
  86. float sw, sh;
  87. };
  88. static int gl_init_graphics(caca_t *kk)
  89. {
  90. char *empty_texture;
  91. char const *geometry;
  92. char *argv[2] = { "", NULL };
  93. unsigned int width = 0, height = 0;
  94. int argc = 1;
  95. int i;
  96. kk->drv.p = malloc(sizeof(struct driver_private));
  97. gl_kk = kk;
  98. #if defined(HAVE_GETENV)
  99. geometry = getenv("CACA_GEOMETRY");
  100. if(geometry && *geometry)
  101. sscanf(geometry, "%ux%u", &width, &height);
  102. #endif
  103. if(width && height)
  104. _cucul_set_size(kk->qq, width, height);
  105. kk->drv.p->font_width = 9;
  106. kk->drv.p->font_height = 15;
  107. kk->drv.p->width = kk->qq->width * kk->drv.p->font_width;
  108. kk->drv.p->height = kk->qq->height * kk->drv.p->font_height;
  109. kk->drv.p->bit = 0;
  110. kk->drv.p->mouse_changed = kk->drv.p->mouse_clicked = 0;
  111. kk->drv.p->mouse_button = kk->drv.p->mouse_state = 0;
  112. kk->drv.p->key = 0;
  113. kk->drv.p->special_key = 0;
  114. kk->drv.p->sw = 9.0f / 16.0f;
  115. kk->drv.p->sh = 15.0f / 16.0f;
  116. glutInit(&argc, argv);
  117. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  118. glutInitWindowSize(kk->drv.p->width, kk->drv.p->height);
  119. kk->drv.p->window = glutCreateWindow("caca for GL");
  120. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  121. glDisable(GL_CULL_FACE);
  122. glDisable(GL_DEPTH_TEST);
  123. glutKeyboardFunc(gl_handle_keyboard);
  124. glutSpecialFunc(gl_handle_special_key);
  125. glutReshapeFunc(gl_handle_reshape);
  126. glutDisplayFunc(_display);
  127. glutMouseFunc(gl_handle_mouse);
  128. glutMotionFunc(gl_handle_mouse_motion);
  129. glutPassiveMotionFunc(gl_handle_mouse_motion);
  130. glLoadIdentity();
  131. glMatrixMode(GL_PROJECTION);
  132. glPushMatrix();
  133. glLoadIdentity();
  134. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  135. glMatrixMode(GL_MODELVIEW);
  136. glClear(GL_COLOR_BUFFER_BIT);
  137. empty_texture = malloc(16 * 16 * 4);
  138. if(empty_texture == NULL)
  139. return -1;
  140. memset(empty_texture, 0xff, 16 * 16 * 4);
  141. glEnable(GL_TEXTURE_2D);
  142. for(i = 32; i < 128; i++)
  143. {
  144. glGenTextures(1, (GLuint*)&kk->drv.p->id[i - 32]);
  145. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  146. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  147. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  148. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
  149. 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
  150. }
  151. for(i = 32; i < 128; i++)
  152. {
  153. glDisable(GL_TEXTURE_2D);
  154. glClear(GL_COLOR_BUFFER_BIT);
  155. glColor3f(1, 1, 1);
  156. glRasterPos2f(0, 15);
  157. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i);
  158. glEnable(GL_TEXTURE_2D);
  159. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[i - 32]);
  160. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  161. 0, kk->drv.p->height - 16, 16, 16, 0);
  162. #ifdef HAVE_GLUTCHECKLOOP
  163. glutCheckLoop();
  164. #else
  165. glutMainLoopEvent();
  166. #endif
  167. glutPostRedisplay();
  168. }
  169. return 0;
  170. }
  171. static int gl_end_graphics(caca_t *kk)
  172. {
  173. glutDestroyWindow(kk->drv.p->window);
  174. free(kk->drv.p);
  175. return 0;
  176. }
  177. static int gl_set_window_title(caca_t *kk, char const *title)
  178. {
  179. glutSetWindowTitle(title);
  180. return 0;
  181. }
  182. static unsigned int gl_get_window_width(caca_t *kk)
  183. {
  184. return kk->drv.p->width;
  185. }
  186. static unsigned int gl_get_window_height(caca_t *kk)
  187. {
  188. return kk->drv.p->height;
  189. }
  190. static void gl_display(caca_t *kk)
  191. {
  192. unsigned int x, y, line;
  193. glClear(GL_COLOR_BUFFER_BIT);
  194. line = 0;
  195. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  196. {
  197. uint32_t *attr = kk->qq->attr + line * kk->qq->width;
  198. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  199. {
  200. glDisable(GL_TEXTURE_2D);
  201. glColor4bv(gl_bgpal[_cucul_rgba32_to_ansi4bg(*attr++)]);
  202. glBegin(GL_QUADS);
  203. glVertex2f(x, y);
  204. glVertex2f(x + kk->drv.p->font_width, y);
  205. glVertex2f(x + kk->drv.p->font_width,
  206. y + kk->drv.p->font_height);
  207. glVertex2f(x, y + kk->drv.p->font_height);
  208. glEnd();
  209. }
  210. line++;
  211. }
  212. /* 2nd pass, avoids changing render state too much */
  213. glEnable(GL_BLEND);
  214. glEnable(GL_TEXTURE_2D);
  215. glBlendFunc(GL_ONE, GL_ONE);
  216. line = 0;
  217. for(y = 0; y < kk->drv.p->height; y += kk->drv.p->font_height)
  218. {
  219. uint32_t *attr = kk->qq->attr + line * kk->qq->width;
  220. uint32_t *chars = kk->qq->chars + line * kk->qq->width;
  221. for(x = 0; x < kk->drv.p->width; x += kk->drv.p->font_width)
  222. {
  223. uint32_t c = *chars++;
  224. if(c > 0x00000020 && c < 0x00000080)
  225. {
  226. glBindTexture(GL_TEXTURE_2D, kk->drv.p->id[c - 32]);
  227. glColor4bv(gl_bgpal[_cucul_rgba32_to_ansi4fg(*attr)]);
  228. glBegin(GL_QUADS);
  229. glTexCoord2f(0, kk->drv.p->sh);
  230. glVertex2f(x, y);
  231. glTexCoord2f(kk->drv.p->sw, kk->drv.p->sh);
  232. glVertex2f(x + kk->drv.p->font_width, y);
  233. glTexCoord2f(kk->drv.p->sw, 0);
  234. glVertex2f(x + kk->drv.p->font_width,
  235. y + kk->drv.p->font_height);
  236. glTexCoord2f(0, 0);
  237. glVertex2f(x, y + kk->drv.p->font_height);
  238. glEnd();
  239. }
  240. attr++;
  241. }
  242. line++;
  243. }
  244. glDisable(GL_BLEND);
  245. glDisable(GL_TEXTURE_2D);
  246. #ifdef HAVE_GLUTCHECKLOOP
  247. glutCheckLoop();
  248. #else
  249. glutMainLoopEvent();
  250. #endif
  251. glutSwapBuffers();
  252. glutPostRedisplay();
  253. }
  254. static void gl_handle_resize(caca_t *kk)
  255. {
  256. kk->drv.p->width = kk->drv.p->new_width;
  257. kk->drv.p->height = kk->drv.p->new_height;
  258. glMatrixMode(GL_PROJECTION);
  259. glPushMatrix();
  260. glLoadIdentity();
  261. glViewport(0, 0, kk->drv.p->width, kk->drv.p->height);
  262. gluOrtho2D(0, kk->drv.p->width, kk->drv.p->height, 0);
  263. glMatrixMode(GL_MODELVIEW);
  264. }
  265. static int gl_get_event(caca_t *kk, struct caca_event *ev)
  266. {
  267. #ifdef HAVE_GLUTCHECKLOOP
  268. glutCheckLoop();
  269. #else
  270. glutMainLoopEvent();
  271. #endif
  272. if(kk->resize.resized)
  273. {
  274. ev->type = CACA_EVENT_RESIZE;
  275. ev->data.resize.w = kk->qq->width;
  276. ev->data.resize.h = kk->qq->height;
  277. return 1;
  278. }
  279. if(kk->drv.p->mouse_changed)
  280. {
  281. ev->type = CACA_EVENT_MOUSE_MOTION;
  282. ev->data.mouse.x = kk->mouse.x;
  283. ev->data.mouse.y = kk->mouse.y;
  284. kk->drv.p->mouse_changed = 0;
  285. if(kk->drv.p->mouse_clicked)
  286. {
  287. _push_event(kk, ev);
  288. ev->type = CACA_EVENT_MOUSE_PRESS;
  289. ev->data.mouse.button = kk->drv.p->mouse_button;
  290. kk->drv.p->mouse_clicked = 0;
  291. }
  292. return 1;
  293. }
  294. if(kk->drv.p->key != 0)
  295. {
  296. ev->type = CACA_EVENT_KEY_PRESS;
  297. ev->data.key.c = kk->drv.p->key;
  298. ev->data.key.ucs4 = (uint32_t)kk->drv.p->key;
  299. ev->data.key.utf8[0] = kk->drv.p->key;
  300. ev->data.key.utf8[1] = '\0';
  301. kk->drv.p->key = 0;
  302. return 1;
  303. }
  304. if(kk->drv.p->special_key != 0)
  305. {
  306. switch(kk->drv.p->special_key)
  307. {
  308. case GLUT_KEY_F1 : ev->data.key.c = CACA_KEY_F1; break;
  309. case GLUT_KEY_F2 : ev->data.key.c = CACA_KEY_F2; break;
  310. case GLUT_KEY_F3 : ev->data.key.c = CACA_KEY_F3; break;
  311. case GLUT_KEY_F4 : ev->data.key.c = CACA_KEY_F4; break;
  312. case GLUT_KEY_F5 : ev->data.key.c = CACA_KEY_F5; break;
  313. case GLUT_KEY_F6 : ev->data.key.c = CACA_KEY_F6; break;
  314. case GLUT_KEY_F7 : ev->data.key.c = CACA_KEY_F7; break;
  315. case GLUT_KEY_F8 : ev->data.key.c = CACA_KEY_F8; break;
  316. case GLUT_KEY_F9 : ev->data.key.c = CACA_KEY_F9; break;
  317. case GLUT_KEY_F10: ev->data.key.c = CACA_KEY_F10; break;
  318. case GLUT_KEY_F11: ev->data.key.c = CACA_KEY_F11; break;
  319. case GLUT_KEY_F12: ev->data.key.c = CACA_KEY_F12; break;
  320. case GLUT_KEY_LEFT : ev->data.key.c = CACA_KEY_LEFT; break;
  321. case GLUT_KEY_RIGHT: ev->data.key.c = CACA_KEY_RIGHT; break;
  322. case GLUT_KEY_UP : ev->data.key.c = CACA_KEY_UP; break;
  323. case GLUT_KEY_DOWN : ev->data.key.c = CACA_KEY_DOWN; break;
  324. default: ev->type = CACA_EVENT_NONE; return 0;
  325. }
  326. ev->type = CACA_EVENT_KEY_PRESS;
  327. ev->data.key.ucs4 = 0;
  328. ev->data.key.utf8[0] = '\0';
  329. kk->drv.p->special_key = 0;
  330. return 1;
  331. }
  332. ev->type = CACA_EVENT_NONE;
  333. return 0;
  334. }
  335. static void gl_set_mouse(caca_t *kk, int flag)
  336. {
  337. if(flag)
  338. glutSetCursor(GLUT_CURSOR_RIGHT_ARROW);
  339. else
  340. glutSetCursor(GLUT_CURSOR_NONE);
  341. }
  342. /*
  343. * XXX: following functions are local
  344. */
  345. static void gl_handle_keyboard(unsigned char key, int x, int y)
  346. {
  347. caca_t *kk = gl_kk;
  348. kk->drv.p->key = key;
  349. }
  350. static void gl_handle_special_key(int key, int x, int y)
  351. {
  352. caca_t *kk = gl_kk;
  353. kk->drv.p->special_key = key;
  354. }
  355. static void gl_handle_reshape(int w, int h)
  356. {
  357. caca_t *kk = gl_kk;
  358. if(kk->drv.p->bit) /* Do not handle reshaping at the first time */
  359. {
  360. kk->drv.p->new_width = w;
  361. kk->drv.p->new_height = h;
  362. kk->resize.w = w / kk->drv.p->font_width;
  363. kk->resize.h = (h / kk->drv.p->font_height) + 1;
  364. kk->resize.resized = 1;
  365. }
  366. else
  367. kk->drv.p->bit = 1;
  368. }
  369. static void gl_handle_mouse(int button, int state, int x, int y)
  370. {
  371. caca_t *kk = gl_kk;
  372. kk->drv.p->mouse_clicked = 1;
  373. kk->drv.p->mouse_button = button;
  374. kk->drv.p->mouse_state = state;
  375. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  376. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  377. kk->mouse.x = kk->drv.p->mouse_x;
  378. kk->mouse.y = kk->drv.p->mouse_y;
  379. kk->drv.p->mouse_changed = 1;
  380. }
  381. static void gl_handle_mouse_motion(int x, int y)
  382. {
  383. caca_t *kk = gl_kk;
  384. kk->drv.p->mouse_x = x / kk->drv.p->font_width;
  385. kk->drv.p->mouse_y = y / kk->drv.p->font_height;
  386. kk->mouse.x = kk->drv.p->mouse_x;
  387. kk->mouse.y = kk->drv.p->mouse_y;
  388. kk->drv.p->mouse_changed = 1;
  389. }
  390. static void _display(void)
  391. {
  392. caca_t *kk = gl_kk;
  393. gl_display(kk);
  394. }
  395. /*
  396. * Driver initialisation
  397. */
  398. int gl_install(caca_t *kk)
  399. {
  400. #if defined(HAVE_GETENV) && defined(GLUT_XLIB_IMPLEMENTATION)
  401. if(!getenv("DISPLAY") || !*(getenv("DISPLAY")))
  402. return -1;
  403. #endif
  404. kk->drv.driver = CACA_DRIVER_GL;
  405. kk->drv.init_graphics = gl_init_graphics;
  406. kk->drv.end_graphics = gl_end_graphics;
  407. kk->drv.set_window_title = gl_set_window_title;
  408. kk->drv.get_window_width = gl_get_window_width;
  409. kk->drv.get_window_height = gl_get_window_height;
  410. kk->drv.display = gl_display;
  411. kk->drv.handle_resize = gl_handle_resize;
  412. kk->drv.get_event = gl_get_event;
  413. kk->drv.set_mouse = gl_set_mouse;
  414. return 0;
  415. }
  416. #endif /* USE_GL */