25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

431 lines
11 KiB

  1. /*
  2. * libcaca 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. #include <GL/gl.h>
  21. #include <GL/glut.h>
  22. #include <GL/freeglut_ext.h>
  23. #include <stdio.h> /* BUFSIZ */
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #if defined(HAVE_UNISTD_H)
  27. # include <unistd.h>
  28. #endif
  29. #include <stdarg.h>
  30. #include "caca.h"
  31. #include "caca_internals.h"
  32. #include "cucul.h"
  33. #include "cucul_internals.h"
  34. /*
  35. * Global variables
  36. */
  37. /* Ok, I just suck. */
  38. static GLbyte const gl_bgpal[][4] =
  39. {
  40. { 0x00, 0x00, 0x00, 0x7f },
  41. { 0x00, 0x00, 0x3f, 0x7f },
  42. { 0x00, 0x3f, 0x00, 0x7f },
  43. { 0x00, 0x3f, 0x3f, 0x7f },
  44. { 0x3f, 0x00, 0x00, 0x7f },
  45. { 0x3f, 0x00, 0x3f, 0x7f },
  46. { 0x3f, 0x3f, 0x00, 0x7f },
  47. { 0x3f, 0x3f, 0x3f, 0x7f },
  48. // + intensity
  49. { 0x00, 0x00, 0x00, 0x7f },
  50. { 0x00, 0x00, 0x7f, 0x7f },
  51. { 0x00, 0x7f, 0x00, 0x7f },
  52. { 0x00, 0x7f, 0x7f, 0x7f },
  53. { 0x7f, 0x00, 0x00, 0x7f },
  54. { 0x7f, 0x00, 0x7f, 0x7f },
  55. { 0x7f, 0x7f, 0x00, 0x7f },
  56. { 0x7f, 0x7f, 0x7f, 0x7f }
  57. };
  58. static caca_t *gl_kk; /* FIXME: we ought to get rid of this */
  59. /*
  60. * Local functions
  61. */
  62. static void gl_handle_keyboard(unsigned char, int, int);
  63. static void gl_handle_special_key(int, int, int);
  64. static void gl_handle_reshape(int, int);
  65. static void gl_handle_mouse(int, int, int, int);
  66. static void gl_handle_mouse_motion(int, int);
  67. static int gl_init_graphics(caca_t *kk)
  68. {
  69. char *empty_texture;
  70. char const *geometry;
  71. char *argv[2] = { "", NULL };
  72. unsigned int width = 0, height = 0;
  73. int argc = 1;
  74. int i;
  75. gl_kk = kk;
  76. geometry = getenv("CACA_GEOMETRY");
  77. if(geometry && *(geometry))
  78. sscanf(geometry, "%ux%u", &width, &height);
  79. if(width && height)
  80. cucul_set_size(kk->qq, width, height);
  81. kk->gl.font_width = 9;
  82. kk->gl.font_height = 15;
  83. kk->gl.width = kk->qq->width * kk->gl.font_width;
  84. kk->gl.height = kk->qq->height * kk->gl.font_height;
  85. kk->gl.resized = 0;
  86. kk->gl.bit = 0;
  87. kk->gl.mouse_changed = kk->gl.mouse_clicked = 0;
  88. kk->gl.mouse_button = kk->gl.mouse_state = 0;
  89. kk->gl.key = 0;
  90. kk->gl.special_key = 0;
  91. kk->gl.sw = 9.0f / 16.0f;
  92. kk->gl.sh = 15.0f / 16.0f;
  93. glutInit(&argc, argv);
  94. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  95. glutInitWindowSize(kk->gl.width, kk->gl.height);
  96. kk->gl.window = glutCreateWindow("caca for GL");
  97. gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
  98. glDisable(GL_CULL_FACE);
  99. glDisable(GL_DEPTH_TEST);
  100. glutKeyboardFunc(gl_handle_keyboard);
  101. glutSpecialFunc(gl_handle_special_key);
  102. glutReshapeFunc(gl_handle_reshape);
  103. glutMouseFunc(gl_handle_mouse);
  104. glutMotionFunc(gl_handle_mouse_motion);
  105. glutPassiveMotionFunc(gl_handle_mouse_motion);
  106. glLoadIdentity();
  107. glMatrixMode(GL_PROJECTION);
  108. glPushMatrix();
  109. glLoadIdentity();
  110. gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
  111. glMatrixMode(GL_MODELVIEW);
  112. glClear(GL_COLOR_BUFFER_BIT);
  113. empty_texture = malloc(16 * 16 * 4);
  114. if(empty_texture == NULL)
  115. return -1;
  116. memset(empty_texture, 0xff, 16 * 16 * 4);
  117. glEnable(GL_TEXTURE_2D);
  118. for(i = 0; i < 94; i++)
  119. {
  120. glGenTextures(1, (GLuint*)&kk->gl.id[i]);
  121. glBindTexture(GL_TEXTURE_2D, kk->gl.id[i]);
  122. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  123. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  124. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8,
  125. 16, 16, 0, GL_RGB, GL_UNSIGNED_BYTE, empty_texture);
  126. }
  127. for(i = 0; i < 94; i++)
  128. {
  129. glDisable(GL_TEXTURE_2D);
  130. glClear(GL_COLOR_BUFFER_BIT);
  131. glColor3f(1, 1, 1);
  132. glRasterPos2f(0, 15);
  133. glutBitmapCharacter(GLUT_BITMAP_9_BY_15, i + 32);
  134. glEnable(GL_TEXTURE_2D);
  135. glBindTexture(GL_TEXTURE_2D, kk->gl.id[i]);
  136. glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,
  137. 0, kk->gl.height - 16, 16, 16, 0);
  138. glutMainLoopEvent();
  139. glutPostRedisplay();
  140. }
  141. return 0;
  142. }
  143. static int gl_end_graphics(caca_t *kk)
  144. {
  145. glutDestroyWindow(kk->gl.window);
  146. return 0;
  147. }
  148. static int gl_set_window_title(caca_t *kk, char const *title)
  149. {
  150. glutSetWindowTitle(title);
  151. return 0;
  152. }
  153. static unsigned int gl_get_window_width(caca_t *kk)
  154. {
  155. return kk->gl.width;
  156. }
  157. static unsigned int gl_get_window_height(caca_t *kk)
  158. {
  159. return kk->gl.height;
  160. }
  161. static void gl_display(caca_t *kk)
  162. {
  163. unsigned int x, y, line;
  164. glClear(GL_COLOR_BUFFER_BIT);
  165. line = 0;
  166. for(y = 0; y < kk->gl.height; y += kk->gl.font_height)
  167. {
  168. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  169. for(x = 0; x < kk->gl.width; x += kk->gl.font_width)
  170. {
  171. glDisable(GL_TEXTURE_2D);
  172. glColor4bv(gl_bgpal[attr[0] >> 4]);
  173. glBegin(GL_QUADS);
  174. glVertex2f(x, y);
  175. glVertex2f(x + kk->gl.font_width, y);
  176. glVertex2f(x + kk->gl.font_width, y + kk->gl.font_height);
  177. glVertex2f(x, y + kk->gl.font_height);
  178. glEnd();
  179. attr++;
  180. }
  181. line++;
  182. }
  183. /* 2nd pass, avoids changing render state too much */
  184. glEnable(GL_BLEND);
  185. glEnable(GL_TEXTURE_2D);
  186. glBlendFunc(GL_ONE, GL_ONE);
  187. line = 0;
  188. for(y = 0; y < kk->gl.height; y += kk->gl.font_height)
  189. {
  190. uint8_t *attr = kk->qq->attr + line * kk->qq->width;
  191. uint32_t *chars = kk->qq->chars + line * kk->qq->width;
  192. for(x = 0; x < kk->gl.width; x += kk->gl.font_width)
  193. {
  194. if(*chars != (uint32_t)' ')
  195. {
  196. char ch = *chars & 0x7f;
  197. /* FIXME: check ch bounds */
  198. glBindTexture(GL_TEXTURE_2D, kk->gl.id[ch - 32]);
  199. glColor4bv(gl_bgpal[attr[0] & 0xf]);
  200. glBegin(GL_QUADS);
  201. glTexCoord2f(0, kk->gl.sh);
  202. glVertex2f(x, y);
  203. glTexCoord2f(kk->gl.sw, kk->gl.sh);
  204. glVertex2f(x + kk->gl.font_width, y);
  205. glTexCoord2f(kk->gl.sw, 0);
  206. glVertex2f(x + kk->gl.font_width, y + kk->gl.font_height);
  207. glTexCoord2f(0, 0);
  208. glVertex2f(x, y + kk->gl.font_height);
  209. glEnd();
  210. }
  211. attr++;
  212. chars++;
  213. }
  214. line++;
  215. }
  216. glDisable(GL_BLEND);
  217. glDisable(GL_TEXTURE_2D);
  218. glutMainLoopEvent();
  219. glutSwapBuffers();
  220. glutPostRedisplay();
  221. }
  222. static void gl_handle_resize(caca_t *kk, unsigned int *new_width,
  223. unsigned int *new_height)
  224. {
  225. kk->gl.width = kk->gl.new_width;
  226. kk->gl.height = kk->gl.new_height;
  227. *new_width = kk->gl.width / kk->gl.font_width;
  228. *new_height = (kk->gl.height / kk->gl.font_height) + 1;
  229. glMatrixMode(GL_PROJECTION);
  230. glPushMatrix();
  231. glLoadIdentity();
  232. glViewport(0, 0, kk->gl.width, kk->gl.height);
  233. gluOrtho2D(0, kk->gl.width, kk->gl.height, 0);
  234. glMatrixMode(GL_MODELVIEW);
  235. }
  236. static unsigned int gl_get_event(caca_t *kk)
  237. {
  238. unsigned int event = 0;
  239. glutMainLoopEvent();
  240. if(kk->gl.resized && !kk->resize)
  241. {
  242. kk->resize = 1;
  243. kk->gl.resized = 0;
  244. return CACA_EVENT_RESIZE;
  245. }
  246. if(kk->gl.mouse_changed)
  247. {
  248. if(kk->gl.mouse_clicked)
  249. {
  250. event |= CACA_EVENT_MOUSE_PRESS | kk->gl.mouse_button;
  251. kk->gl.mouse_clicked = 0;
  252. }
  253. kk->mouse_x = kk->gl.mouse_x;
  254. kk->mouse_y = kk->gl.mouse_y;
  255. event |= CACA_EVENT_MOUSE_MOTION | (kk->mouse_x << 12) | kk->mouse_y;
  256. kk->gl.mouse_changed = 0;
  257. }
  258. if(kk->gl.key != 0)
  259. {
  260. event |= CACA_EVENT_KEY_PRESS;
  261. event |= kk->gl.key;
  262. kk->gl.key = 0;
  263. return event;
  264. }
  265. if(kk->gl.special_key != 0)
  266. {
  267. event |= CACA_EVENT_KEY_PRESS;
  268. switch(kk->gl.special_key)
  269. {
  270. case GLUT_KEY_F1 : kk->gl.special_key = 0; return event | CACA_KEY_F1;
  271. case GLUT_KEY_F2 : kk->gl.special_key = 0; return event | CACA_KEY_F2;
  272. case GLUT_KEY_F3 : kk->gl.special_key = 0; return event | CACA_KEY_F3;
  273. case GLUT_KEY_F4 : kk->gl.special_key = 0; return event | CACA_KEY_F4;
  274. case GLUT_KEY_F5 : kk->gl.special_key = 0; return event | CACA_KEY_F5;
  275. case GLUT_KEY_F6 : kk->gl.special_key = 0; return event | CACA_KEY_F6;
  276. case GLUT_KEY_F7 : kk->gl.special_key = 0; return event | CACA_KEY_F7;
  277. case GLUT_KEY_F8 : kk->gl.special_key = 0; return event | CACA_KEY_F8;
  278. case GLUT_KEY_F9 : kk->gl.special_key = 0; return event | CACA_KEY_F9;
  279. case GLUT_KEY_F10: kk->gl.special_key = 0; return event | CACA_KEY_F10;
  280. case GLUT_KEY_F11: kk->gl.special_key = 0; return event | CACA_KEY_F11;
  281. case GLUT_KEY_F12: kk->gl.special_key = 0; return event | CACA_KEY_F12;
  282. case GLUT_KEY_LEFT : kk->gl.special_key = 0; return event | CACA_KEY_LEFT;
  283. case GLUT_KEY_RIGHT: kk->gl.special_key = 0; return event | CACA_KEY_RIGHT;
  284. case GLUT_KEY_UP : kk->gl.special_key = 0; return event | CACA_KEY_UP;
  285. case GLUT_KEY_DOWN : kk->gl.special_key = 0; return event | CACA_KEY_DOWN;
  286. default: return CACA_EVENT_NONE;
  287. }
  288. }
  289. return CACA_EVENT_NONE;
  290. }
  291. /*
  292. * XXX: following functions are local
  293. */
  294. static void gl_handle_keyboard(unsigned char key, int x, int y)
  295. {
  296. caca_t *kk = gl_kk;
  297. kk->gl.key = key;
  298. }
  299. static void gl_handle_special_key(int key, int x, int y)
  300. {
  301. caca_t *kk = gl_kk;
  302. kk->gl.special_key = key;
  303. }
  304. static void gl_handle_reshape(int w, int h)
  305. {
  306. caca_t *kk = gl_kk;
  307. if(kk->gl.bit) /* Do not handle reshaping at the first time */
  308. {
  309. kk->gl.new_width = w;
  310. kk->gl.new_height = h;
  311. kk->gl.resized = 1;
  312. }
  313. else
  314. kk->gl.bit = 1;
  315. }
  316. static void gl_handle_mouse(int button, int state, int x, int y)
  317. {
  318. caca_t *kk = gl_kk;
  319. kk->gl.mouse_clicked = 1;
  320. kk->gl.mouse_button = button;
  321. kk->gl.mouse_state = state;
  322. kk->gl.mouse_x = x / kk->gl.font_width;
  323. kk->gl.mouse_y = y / kk->gl.font_height;
  324. kk->gl.mouse_changed = 1;
  325. }
  326. static void gl_handle_mouse_motion(int x, int y)
  327. {
  328. caca_t *kk = gl_kk;
  329. kk->gl.mouse_x = x / kk->gl.font_width;
  330. kk->gl.mouse_y = y / kk->gl.font_height;
  331. kk->gl.mouse_changed = 1;
  332. }
  333. /*
  334. * Driver initialisation
  335. */
  336. void gl_init_driver(caca_t *kk)
  337. {
  338. kk->driver.driver = CACA_DRIVER_GL;
  339. kk->driver.init_graphics = gl_init_graphics;
  340. kk->driver.end_graphics = gl_end_graphics;
  341. kk->driver.set_window_title = gl_set_window_title;
  342. kk->driver.get_window_width = gl_get_window_width;
  343. kk->driver.get_window_height = gl_get_window_height;
  344. kk->driver.display = gl_display;
  345. kk->driver.handle_resize = gl_handle_resize;
  346. kk->driver.get_event = gl_get_event;
  347. }
  348. #endif /* USE_GL */