25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

driver_gl.c 8.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. /*
  237. * XXX: following functions are local
  238. */
  239. static void gl_handle_keyboard(unsigned char key, int x, int y)
  240. {
  241. caca_t *kk = gl_kk;
  242. kk->gl.key = key;
  243. }
  244. static void gl_handle_special_key(int key, int x, int y)
  245. {
  246. caca_t *kk = gl_kk;
  247. kk->gl.special_key = key;
  248. }
  249. static void gl_handle_reshape(int w, int h)
  250. {
  251. caca_t *kk = gl_kk;
  252. if(kk->gl.bit) /* Do not handle reshaping at the first time */
  253. {
  254. kk->gl.new_width = w;
  255. kk->gl.new_height = h;
  256. kk->gl.resized = 1;
  257. }
  258. else
  259. kk->gl.bit = 1;
  260. }
  261. static void gl_handle_mouse(int button, int state, int x, int y)
  262. {
  263. caca_t *kk = gl_kk;
  264. kk->gl.mouse_clicked = 1;
  265. kk->gl.mouse_button = button;
  266. kk->gl.mouse_state = state;
  267. kk->gl.mouse_x = x / kk->gl.font_width;
  268. kk->gl.mouse_y = y / kk->gl.font_height;
  269. kk->gl.mouse_changed = 1;
  270. }
  271. static void gl_handle_mouse_motion(int x, int y)
  272. {
  273. caca_t *kk = gl_kk;
  274. kk->gl.mouse_x = x / kk->gl.font_width;
  275. kk->gl.mouse_y = y / kk->gl.font_height;
  276. kk->gl.mouse_changed = 1;
  277. }
  278. /*
  279. * Driver initialisation
  280. */
  281. void gl_init_driver(caca_t *kk)
  282. {
  283. kk->driver.driver = CACA_DRIVER_GL;
  284. kk->driver.init_graphics = gl_init_graphics;
  285. kk->driver.end_graphics = gl_end_graphics;
  286. kk->driver.set_window_title = gl_set_window_title;
  287. kk->driver.get_window_width = gl_get_window_width;
  288. kk->driver.get_window_height = gl_get_window_height;
  289. kk->driver.display = gl_display;
  290. kk->driver.handle_resize = gl_handle_resize;
  291. }
  292. #endif /* USE_GL */