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
|
/*
$Id: opengl.c,v 1.1 2006-03-15 04:54:54 matju Exp $
GridFlow
Copyright (c) 2001,2002,2003,2004,2005 by Mathieu Bouchard
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
See file ../COPYING for further informations on licensing terms.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "../base/grid.h.fcs"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <signal.h>
#ifdef MACOSX
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#else
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <setjmp.h>
static bool in_use = false;
\class FormatOpenGL < Format
struct FormatOpenGL : Format {
int window;
GLuint gltex;
P<BitPacking> bit_packing;
P<Dim> dim;
Pt<uint8> buf;
\decl void call ();
\decl void initialize (Symbol mode);
\decl void close ();
\decl void resize_window (int sx, int sy);
\grin 0
};
static jmp_buf hack;
static void my_idle () { longjmp(hack,1); }
\def void call () {
int32 sy = dim->get(0);
int32 sx = dim->get(1);
glEnable(GL_TEXTURE_2D);
if (!gltex) {
}
glTexSubImage2D(GL_TEXTURE_2D,0,0,0,sx,sy,GL_RGBA,GL_UNSIGNED_BYTE,buf);
glBindTexture(GL_TEXTURE_2D, gltex);
glBegin(GL_QUADS);
glColor3f(1.f,1.f,1.f);
glTexCoord2f(0.f,0.f); glVertex2f(0.f,0.f);
glTexCoord2f(1.f,0.f); glVertex2f( sx,0.f);
glTexCoord2f(1.f,1.f); glVertex2f( sx, sy);
glTexCoord2f(0.f,1.f); glVertex2f(0.f, sy);
glEnd();
//Here comes some (un)fair amount of arm-twisting
//This is for processing queued events and then "returning".
glutIdleFunc(my_idle);
if(!setjmp(hack)) glutMainLoop();
//done
IEVAL(rself,"@clock.delay 100");
}
\def void resize_window (int sx, int sy) {L
dim = new Dim(sy,sx,3);
char foo[666];
sprintf(foo,"GridFlow/GL (%d,%d,3)",sy,sx);
if ((unsigned)window==0xDeadBeef) {
glutInitWindowSize(sx,sy);
window = glutCreateWindow(foo);
} else {
glutReshapeWindow(sx,sy);
}
if (buf) delete buf.p;
buf = ARRAY_NEW(uint8,sy*sx*4);
glViewport(0,0,sx,sy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,sx,sy,0,-99999,99999);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (gltex) glDeleteTextures(1, (GLuint*)&gltex);
glGenTextures(1, (GLuint*)&gltex);
glBindTexture(GL_TEXTURE_2D,gltex);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,sx,sy,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);
}
GRID_INLET(FormatOpenGL,0) {
if (in->dim->n != 3)
RAISE("expecting 3 dimensions: rows,columns,channels");
if (in->dim->get(2) != 3)
RAISE("expecting 3 channels: red,green,blue (got %d)",in->dim->get(2));
int sxc = in->dim->prod(1);
int sx = in->dim->get(1), osx = dim->get(1);
int sy = in->dim->get(0), osy = dim->get(0);
in->set_factor(sxc);
if (sx!=osx || sy!=osy) resize_window(0,0,sx,sy);
} GRID_FLOW {
int sxc = in->dim->prod(1);
int sx = in->dim->get(1);
int bypl = 4*sx;
int y = in->dex / sxc;
assert((in->dex % sxc) == 0);
assert((n % sxc) == 0);
for (; n>0; y++, data+=sxc, n-=sxc) bit_packing->pack(sx, data, buf+y*bypl);
} GRID_FINISH {
} GRID_END
\def void close () {
IEVAL(rself,"@clock.unset");
if (gltex) glDeleteTextures(1, (GLuint*)&gltex);
if (buf) delete buf.p;
in_use=false;
if ((unsigned)window!=0xDeadBeef) {
glutDestroyWindow(window);
window=0xDeadBeef;
}
}
\def void initialize (Symbol mode) {
rb_call_super(argc,argv);
if (in_use) RAISE("only one #io:opengl object at a time; sorry");
in_use=true;
if (mode!=SYM(out)) RAISE("write-only, sorry");
gltex = 0;
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDisable(GL_ALPHA_TEST);
glDisable(GL_CULL_FACE);
glDisable(GL_DITHER);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
window = 0xDeadBeef;
int dummy = 0;
glutInit(&dummy,0);
glutInitDisplayMode(GLUT_RGBA);
resize_window(0,0,320,240);
uint32 mask[3] = {0xff000000,0x00ff0000,0x0000ff00};
bit_packing = new BitPacking(4,4,3,mask);
IEVAL(rself,"@clock = Clock.new self; @clock.delay 0");
gfpost("@clock = Clock.new self");
}
\classinfo {
IEVAL(rself,"install '#io:opengl',1,1;@comment='Silicon Graphics OpenGL'");
}
\end class FormatOpenGL
void startup_opengl () {
\startall
}
|