1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
/**
* Example program for libsixel-OpenGL integration
* examples/opengl/main.c
*
* GLX pbuffer initialization part is originally written by
* Brian Paul for the "OpenGL and Window System Integration"
* course presented at SIGGRAPH '97. Updated on 5 October 2002.
*
* Updated on 31 January 2004 to use native GLX by
* Andrew P. Lentvorski, Jr. <bsder@allcaps.org>
*
* Hayaki Saito <saitoha@me.com> added OSMesa and OSX pbuffer
* initialization code.
*
* original source:
* https://cgit.freedesktop.org/mesa/demos/tree/src/xdemos/glxpbdemo.c
*
* original license:
*
* Copyright (C) 1999-2007 Brian Paul All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include "config.h"
#if HAVE_OSMESA
# define USE_OSMESA 1
#elif HAVE_CGL
# define USE_CGL 1
#elif HAVE_X11
# define USE_GLX 1
#else
#endif
#if USE_OSMESA
# include <GL/osmesa.h>
#elif USE_CGL
# include <OpenGL/gl.h>
# include <OpenGL/OpenGL.h>
#elif USE_GLX
# include <X11/Xlib.h>
# include <GL/glx.h>
# include <GL/gl.h>
#endif
#include <sys/signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <memory.h>
#include <math.h>
#include <errno.h>
# include <termios.h>
# include <sys/ioctl.h>
#include <sys/select.h>
#ifndef M_PI
# define M_PI 3.1415926535897932386
#endif
#include <sixel.h> /* libsixel */
#if USE_OSMESA
static unsigned char *pbuffer;
static OSMesaContext context;
#elif USE_CGL
static CGLPBufferObj pbuffer;
static CGLContextObj context;
#elif USE_GLX && (defined(GLX_VERSION_1_3) || defined(GLX_VERSION_1_4))
static Display *display = NULL;
static GLXPbuffer pbuffer = 0;
static GLXContext context;
#endif
static volatile int signaled = 0;
static void
sighandler(int sig)
{
signaled = sig;
}
static int
setup(int width, int height)
{
#if USE_OSMESA
const size_t size = width * height * 4;
pbuffer = malloc(size);
memset(pbuffer, 0x21, size);
context = OSMesaCreateContextExt(GL_RGBA, 24, 0, 0, 0);
OSMesaMakeCurrent(context, (void *)pbuffer, GL_UNSIGNED_BYTE, width, height);
return 0;
#elif USE_CGL
/* OpenGL PBuffer initialization: OSX specific */
CGLPixelFormatAttribute pfattr[] = {
kCGLPFAPBuffer,
(CGLPixelFormatAttribute)0
};
CGLPixelFormatObj pixformat;
GLint npixels;
int e;
e = CGLChoosePixelFormat(pfattr, &pixformat, &npixels);
if (e != kCGLNoError) {
fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e);
return e;
}
e = CGLCreateContext(pixformat, 0, &context);
if (e != kCGLNoError) {
fprintf(stderr, "CGLChoosePixelFormat failed, err %d\n", e);
return e;
}
e = CGLDestroyPixelFormat(pixformat);
if (e != kCGLNoError) {
fprintf(stderr, "CGLDestroyPixelFormat failed, err %d\n", e);
return e;
}
e = CGLSetCurrentContext(context);
if (e != kCGLNoError) {
fprintf(stderr, "CGLSetCurrentContext failed, err %d\n", e);
return e;
}
e = CGLCreatePBuffer(width, height, GL_TEXTURE_2D, GL_RGB, 0, &pbuffer);
if (e != kCGLNoError) {
fprintf(stderr, "CGLCreatePBuffer failed, err %d\n", e);
return e;
}
e = CGLSetPBuffer(context, pbuffer, 0, 0, 0);
if (e != kCGLNoError) {
fprintf(stderr, "CGLSetPBuffer failed, err %d\n", e);
return e;
}
return (int)kCGLNoError;
#elif USE_GLX
int result = (-1);
char *glxversion;
int screen;
GLXFBConfig *fbConfigs = NULL;
GLXFBConfig chosenFBConfig;
GLXFBConfig fbconfig = 0;
GLXPbuffer pbuffer = None;
int nConfigs;
int fbconfigid;
int fbAttribs[] = {
GLX_RENDER_TYPE, GLX_RGBA_BIT,
GLX_DEPTH_SIZE, 1,
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_PBUFFER_BIT,
None
};
int pbAttribs[] = {
GLX_PBUFFER_WIDTH, 0,
GLX_PBUFFER_HEIGHT, 0,
GLX_LARGEST_PBUFFER, False,
GLX_PRESERVED_CONTENTS, False,
None
};
/* Open the X display */
display = XOpenDisplay(NULL);
if (!display) {
fprintf(stderr, "Error: couldn't open default X display.\n");
goto end;
}
/* Get default screen */
screen = DefaultScreen(display);
glxversion = (char *) glXGetClientString(display, GLX_VERSION);
if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) {
goto end;
}
glxversion = (char *) glXQueryServerString(display, screen, GLX_VERSION);
if (!(strstr(glxversion, "1.3") || strstr(glxversion, "1.4"))) {
goto end;
}
/* Create Pbuffer */
pbAttribs[1] = width;
pbAttribs[3] = height;
fbConfigs = glXChooseFBConfig(display, screen, fbAttribs, &nConfigs);
if (0 == nConfigs || !fbConfigs) {
fprintf(stderr, "Error: glxChooseFBConfig failed\n");
XFree(fbConfigs);
goto end;
}
chosenFBConfig = fbConfigs[0];
glXGetFBConfigAttrib(display, chosenFBConfig, GLX_FBCONFIG_ID, &fbconfigid);
/* Create the pbuffer using first fbConfig in the list that works. */
pbuffer = glXCreatePbuffer(display, chosenFBConfig, pbAttribs);
if (pbuffer) {
fbconfig = chosenFBConfig;
}
XFree(fbConfigs);
if (pbuffer == None) {
fprintf(stderr, "Error: couldn't create pbuffer\n");
goto end;
}
/* Create GLX context */
context = glXCreateNewContext(display, fbconfig, GLX_RGBA_TYPE, NULL, True);
if (!context) {
fprintf(stderr, "Error: Couldn't create GLXContext\n");
goto end;
}
/* Bind context to pbuffer */
if (!glXMakeCurrent(display, pbuffer, context)) {
fprintf(stderr, "Error: glXMakeCurrent failed\n");
goto end;
}
result = 0;
end:
if (fbConfigs)
XFree(fbConfigs);
if (display)
XCloseDisplay(display);
return result;
#else
/* TODO: pbuffer initialization */
return 0;
#endif
}
static int
cleanup(void)
{
#if USE_OSMESA
OSMesaDestroyContext(context);
free(pbuffer);
#elif USE_CGL
(void)CGLDestroyContext(context);
(void)CGLDestroyPBuffer(pbuffer);
#elif USE_GLX
display = XOpenDisplay(NULL);
glXDestroyPbuffer(display, pbuffer);
XCloseDisplay(display);
#else
/* TODO: cleanup pbuffer and OpenGL context */
#endif
return 0;
}
static int
draw_scene(void)
{
static GLfloat rot1, rot2;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-1.5, 0, -6);
glRotatef(rot1 += 1.5, 0, 1, 0);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0); glVertex3f(0, 1, 0);
glColor3f(0, 1, 0); glVertex3f(-1, -1, 1);
glColor3f(0, 0, 1); glVertex3f(1, -1, 1);
glColor3f(1, 0, 0); glVertex3f(0, 1, 0);
glColor3f(0, 0, 1); glVertex3f(1, -1, 1);
glColor3f(0, 1, 0); glVertex3f(1, -1, -1);
glColor3f(1, 0, 0); glVertex3f(0, 1, 0);
glColor3f(0, 1, 0); glVertex3f(1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, -1);
glColor3f(1, 0, 0); glVertex3f(0, 1, 0);
glColor3f(0, 0, 1); glVertex3f(-1, -1, -1);
glColor3f(0, 1, 0); glVertex3f(-1, -1, 1);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.5, 0, -6);
glRotatef(rot2 += 1.8, 1, 0, 0);
glBegin(GL_QUADS);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glColor3f(0, 0, 0); glVertex3f(-1, -1, -1);
glColor3f(0, 1, 0); glVertex3f(-1, 1, -1);
glColor3f(1, 1, 0); glVertex3f( 1, 1, -1);
glColor3f(1, 0, 0); glVertex3f( 1, -1, -1);
glColor3f(0, 0, 1); glVertex3f(-1, -1, 1);
glColor3f(0, 1, 1); glVertex3f(-1, 1, 1);
glColor3f(1, 1, 1); glVertex3f( 1, 1, 1);
glColor3f(1, 0, 1); glVertex3f( 1, -1, 1);
glEnd();
glPopMatrix();
return 0;
}
static SIXELSTATUS
output_sixel(unsigned char *pixbuf, int width, int height,
int pixelformat, int ncolors)
{
sixel_encoder_t *encoder;
SIXELSTATUS status;
char opt[256];
status = sixel_encoder_new(&encoder, NULL);
if (SIXEL_FAILED(status))
return status;
if (sprintf(opt, "%d", ncolors) <= 0)
return SIXEL_LIBC_ERROR | (errno | 0xff);
status = sixel_encoder_setopt(encoder, SIXEL_OPTFLAG_COLORS, opt);
if (SIXEL_FAILED(status))
return status;
status = sixel_encoder_encode_bytes(
encoder, pixbuf, width, height, pixelformat, NULL, (-1));
if (SIXEL_FAILED(status))
return status;
sixel_encoder_unref(encoder);
return status;
}
static int
wait_stdin(int usec)
{
fd_set rfds;
struct timeval tv;
int ret = 0;
tv.tv_sec = usec / 1000000;
tv.tv_usec = usec % 1000000;
FD_ZERO(&rfds);
FD_SET(STDIN_FILENO, &rfds);
ret = select(STDIN_FILENO + 1, &rfds, NULL, NULL, &tv);
return ret;
}
static void
scroll_on_demand(int pixelheight)
{
struct winsize size = {0, 0, 0, 0};
struct termios old_termios;
struct termios new_termios;
int row = 0;
int col = 0;
int cellheight;
int scroll;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &size);
if (size.ws_ypixel <= 0) {
printf("\033[H\0337");
return;
}
/* set the terminal to cbreak mode */
tcgetattr(STDIN_FILENO, &old_termios);
memcpy(&new_termios, &old_termios, sizeof(old_termios));
new_termios.c_lflag &= ~(ECHO | ICANON);
new_termios.c_cc[VMIN] = 1;
new_termios.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSAFLUSH, &new_termios);
/* request cursor position report */
printf("\033[6n");
if (wait_stdin(1000 * 1000) != (-1)) { /* wait 1 sec */
if (scanf("\033[%d;%dR", &row, &col) == 2) {
cellheight = pixelheight * size.ws_row / size.ws_ypixel + 1;
scroll = cellheight + row - size.ws_row + 1;
printf("\033[%dS\033[%dA", scroll, scroll);
printf("\0337");
} else {
printf("\033[H\0337");
}
}
tcsetattr(STDIN_FILENO, TCSAFLUSH, &old_termios);
}
int
main(int argc, char** argv)
{
SIXELSTATUS status;
unsigned char *pixbuf;
int width = 400;
int height = 300;
int ncolors = 16;
(void) argc;
(void) argv;
if (signal(SIGINT, sighandler) == SIG_ERR)
return (-1);
if (setup(width, height) != 0)
return (-1);
scroll_on_demand(height);
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, (GLsizei)width, (GLsizei)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLfloat fovy = 45;
GLfloat aspect = (GLfloat)width / (GLfloat)height;
GLfloat znear = 0.1;
GLfloat zfar = 100;
GLfloat radian= 2 * M_PI * fovy / 360.0;
GLfloat t = (GLfloat)(1.0 / tan(radian / 2));
GLfloat matrix[]={
t / aspect, 0, 0, 0,
0, t, 0, 0,
0, 0, (zfar + znear) / (znear - zfar), -1,
0, 0, (2 * zfar * znear) / (znear - zfar), 0
};
glLoadMatrixf(matrix);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#if !defined(USE_OSMESA)
pixbuf = malloc(width * height * 3);
#endif
while (!signaled) {
glPushMatrix();
glScalef(1, -1, 1);
draw_scene();
glPopMatrix();
if (signaled)
break;
printf("\0338");
fflush(stdout);
#if USE_OSMESA
pixbuf = pbuffer;
#else
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, pixbuf);
#endif
status = output_sixel(
pixbuf, width, height,
#if USE_OSMESA
SIXEL_PIXELFORMAT_RGBA8888,
#else
SIXEL_PIXELFORMAT_RGB888,
#endif
ncolors);
if (SIXEL_FAILED(status)) {
fprintf(stderr, "%s\n%s\n",
sixel_helper_format_error(status),
sixel_helper_get_additional_message());
break;
}
}
#if !defined(USE_OSMESA)
free(pixbuf);
#endif
printf("\033\\");
if (cleanup() != 0)
return (-1);
return 0;
}
/* EOF */
|