File: glm.c

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (356 lines) | stat: -rw-r--r-- 10,632 bytes parent folder | download | duplicates (7)
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
/*
 *
 * glm.c -- implementation of glm functions
 *
 * 08-Dec-2005 -- created (RFM)
 * 11-Nov-2006 -- Minimal fixes for IntelMac Matlab beta: Replace mxGetPr by mxGetData where appropriate.
 */

#include "mogltypes.h"

// device information
AGLDevice device;
AGLContext context;
CGDirectDisplayID displayID;
int screenw, screenh, bitsperpixel;
double fps;
GLuint fontList;
int fontID,fontStyle,fontSize;
CFDictionaryRef enterMode;

// general-purpose text buffer
#define TEXT_SIZE 4096
char text[TEXT_SIZE];

void glm_close(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // delete font
    if( fontList>0 )
        glDeleteLists(fontList,256);

    // tear down context
    aglSetCurrentContext( NULL );
    aglSetDrawable( context, NULL );
    aglDestroyContext( context );

    // restore clut
    CGDisplayRestoreColorSyncSettings();
    
    // release displays
    CGDisplaySwitchToMode(displayID,enterMode);
    CGDisplayRelease(displayID);

	// clear display information
	device=NULL;
    context=NULL;
	displayID=NULL;
	screenw=screenh=0;
	fps=0;

}

void glm_getkeys(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    plhs[0]=mxCreateNumericMatrix(4,1,mxUINT32_CLASS,mxREAL);
    GetKeys((void *)mxGetData(plhs[0]));
    
}

void glm_getmouse(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // get position
    Point pt;
    GetMouse(&pt);
    plhs[0]=mxCreateDoubleMatrix(1,2,mxREAL);
    double *dmat=mxGetPr(plhs[0]);
    dmat[0]=pt.h;
    dmat[1]=pt.v;
    
    // get button state
    plhs[1]=mxCreateDoubleMatrix(1,1,mxREAL);
    *mxGetPr(plhs[1])=Button();
    
}

void glm_getscreeninfo(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

	// return screen size in pixels
    plhs[0]=mxCreateDoubleMatrix(1,2,mxREAL);
    double *p=mxGetPr(plhs[0]);
    p[0]=(double)screenw;
    p[1]=(double)screenh;
	
	// return frames per secone
    plhs[1]=mxCreateDoubleMatrix(1,1,mxREAL);
	*mxGetPr(plhs[1])=(double)fps;
    
	// return bits per pixel
    plhs[2]=mxCreateDoubleMatrix(1,1,mxREAL);
	*mxGetPr(plhs[2])=(double)bitsperpixel;
    
}

void glm_open(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

    // get requested device
    int i=1,nRequestedDevice=(int)mxGetScalar(prhs[0]);
    device=GetDeviceList();
    while( i++<nRequestedDevice & device!=NULL )
        device=GetNextDevice(device);
    if( device==NULL )
        mxErrMsgTxt("invalid device number");
    
    // get CGDirectDisplayID for this device
    displayID = QDGetCGDirectDisplayID(device);

    // switch display mode
    enterMode=CGDisplayCurrentMode(displayID);
    if( mxGetScalar(prhs[1])>0 ) {
        boolean_t exact;
        CFDictionaryRef mode=CGDisplayBestModeForParameters(displayID,
            (size_t)mxGetScalar(prhs[3]),(size_t)mxGetScalar(prhs[1]),(size_t)mxGetScalar(prhs[2]),
            &exact);
        if(!exact)
            mxErrMsgTxt("invalid display mode");
        CGDisplayCapture(displayID);
        CGDisplaySwitchToMode(displayID,mode);
    }
    else
        CGDisplayCapture(displayID);

    // find a pixel format
    AGLPixelFormat fmt = aglChoosePixelFormat(&device,1,(const GLint *)mxGetData(prhs[4]));
    if( fmt==NULL )
        mxErrMsgTxt("no matching pixel format");
    
    // create context and enter full-screen mode
    context = aglCreateContext(fmt,NULL);
    aglEnable(context,AGL_FS_CAPTURE_SINGLE);
    aglSetFullScreen(context,0,0,0,0);
    aglSetCurrentContext(context);
    aglDestroyPixelFormat(fmt);

    // record screen information
	CFDictionaryRef dispInfo=CGDisplayCurrentMode(displayID);
	CFNumberRef dispNum;
	// width
	dispNum=CFDictionaryGetValue(dispInfo,kCGDisplayWidth);
	CFNumberGetValue(dispNum,kCFNumberIntType,&screenw);
	// height
	dispNum=CFDictionaryGetValue(dispInfo,kCGDisplayHeight);
	CFNumberGetValue(dispNum,kCFNumberIntType,&screenh);
	// refresh rate
	dispNum=CFDictionaryGetValue(dispInfo,kCGDisplayRefreshRate);
	CFNumberGetValue(dispNum,kCFNumberDoubleType,&fps);
	// bits per pixel
	dispNum=CFDictionaryGetValue(dispInfo,kCGDisplayBitsPerPixel);
	CFNumberGetValue(dispNum,kCFNumberIntType,&bitsperpixel);
    
    int argp=0;
    char *str="cmd";
    glutInit(&argp,&str);
    
}

void glm_packpixels(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // get image
    double *dmat=mxGetPr(prhs[0]);
    int m=mxGetM(prhs[0]),n=mxGetN(prhs[0])/3;
    int padn=( (4-((3*n)%4)) % 4 );
    
    // create return argument
    int n2=3*n+padn;
    plhs[0]=mxCreateNumericMatrix(n2*m+8,1,mxUINT8_CLASS,mxREAL);
    unsigned char *umat=(unsigned char*)mxGetData(plhs[0]);
    
    // store image format and size
    umat[n2*m  ]=GL_RGB/256;
    umat[n2*m+1]=GL_RGB%256;
    umat[n2*m+2]=GL_UNSIGNED_BYTE/256;
    umat[n2*m+3]=GL_UNSIGNED_BYTE%256;
    umat[n2*m+4]=n/256;
    umat[n2*m+5]=n%256;
    umat[n2*m+6]=m/256;
    umat[n2*m+7]=m%256;
    
    // reformat and reorder (much slower if inner and outer loops exchanged)
    int i,j;
    for(j=0;j<n;j++) {
        for(i=0;i<m;i++) {
            umat[3*j  +n2*((m-1)-i)]=(unsigned char)dmat[i+m*j];
            umat[3*j+1+n2*((m-1)-i)]=(unsigned char)dmat[i+m*(j+n)];
            umat[3*j+2+n2*((m-1)-i)]=(unsigned char)dmat[i+m*(j+2*n)];
        }
    }
    
}

// do this type casting in the m-file wrapper
void glm_setclut(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    int i;
    double *clut=mxGetPr(prhs[0]);
    CGByteValue pnRed[256],pnGreen[256],pnBlue[256];
    for(i=0;i<256;i++) {
        pnRed[i]=  (CGByteValue)clut[i];
        pnGreen[i]=(CGByteValue)clut[i+256];
        pnBlue[i]= (CGByteValue)clut[i+512];
    }
    CGSetDisplayTransferByByteTable(displayID,256,pnRed,pnGreen,pnBlue);
    
}

void glm_setfont(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // delete previous font
    if( fontList>0 )
        glDeleteLists(fontList,256);
    
    // get font ID, style, size
    fontID=   (int)mxGetScalar(prhs[0]);
    fontStyle=(int)mxGetScalar(prhs[1]);
    fontSize= (int)mxGetScalar(prhs[2]);
    
    // create font
    fontList=glGenLists(256);
    aglUseFont(context,fontID,fontStyle,fontSize,0,256,fontList);

}

void glm_setmouse(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // set position
    if( mxGetM(prhs[0])>0 ) {
        double *p=mxGetPr(prhs[0]);
        CGPoint pos;
        pos.x=(float)p[0];
        pos.y=(float)p[1];
        CGDisplayMoveCursorToPoint(displayID,pos);
    }
    
    // set visibility
    if( mxGetM(prhs[1])>0 ) {
        if( mxGetScalar(prhs[1])!=0 )
            CGDisplayShowCursor(displayID);
        else
            CGDisplayHideCursor(displayID);
    }

}

void glm_setswapinterval(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

    GLint swapinterval=(GLint)mxGetScalar(prhs[0]);
    aglSetInteger( context, AGL_SWAP_INTERVAL, &swapinterval );
    
}

AbsoluteTime entrytime;
unsigned char pausepoint[4];
GLint nRead,nDraw;

void glm_swapbuffers(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
  // create return argument and record entry time
  if( nlhs>0 ) {
    plhs[0]=mxCreateNumericMatrix(1,1,mxUINT64_CLASS,mxREAL);
    if( nlhs>1 ) {
      plhs[1]=mxCreateNumericMatrix(1,1,mxUINT64_CLASS,mxREAL);
      entrytime=UpTime();
    }
  }

  // swap buffers
  aglSwapBuffers(context);

  // do a trivial graphics operation so that execution halts here
  // until buffers are swapped
  if( mxGetScalar(prhs[0])>0 ) {
    glGetIntegerv(GL_READ_BUFFER,&nRead);
    glGetIntegerv(GL_DRAW_BUFFER,&nDraw);
    glReadBuffer(GL_BACK);
    glDrawBuffer(GL_BACK);
    glReadPixels(0,0,1,1,GL_RGBA,GL_UNSIGNED_BYTE,pausepoint);
    glWindowPos2d(0,0);
    glDrawPixels(1,1,GL_RGBA,GL_UNSIGNED_BYTE,pausepoint);
    glReadBuffer(nRead);
    glDrawBuffer(nDraw);
    glFinish();
  }

  // record the time
  if( nlhs>0 ) {
    // record the time
    *(AbsoluteTime *)mxGetData(plhs[0])=UpTime();
    if( nlhs>1 )
      *(long long *)mxGetData(plhs[1])=*(long long *)mxGetData(plhs[0])-*(long long *)&entrytime;
  }
}

void glm_text(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {

    // check that a font has been created
    if( fontList==0 )
        mexErrMsgTxt("no font has been chosen");
    
    // get string
    mxGetString(prhs[0],text,TEXT_SIZE);
    
    // draw string
    glListBase(fontList);
    glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
    
}

void glm_textsize(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    
    // check that a font has been created
    if( fontList==0 )
        mexErrMsgTxt("no font has been chosen");

    // get string
    mxGetString(prhs[0],text,TEXT_SIZE);
    
    // save state and use logic operations to prevent bitmap rendering
    glPushAttrib(GL_COLOR_BUFFER_BIT);
    glEnable(GL_COLOR_LOGIC_OP);
    glLogicOp(GL_NOOP);
    
    // see how much raster position changes when text is drawn
    double rpos[]={ 0, 0 };
    glWindowPos2dv(rpos);
    glListBase(fontList);
    glCallLists(strlen(text),GL_UNSIGNED_BYTE,text);
    glGetDoublev(GL_CURRENT_RASTER_POSITION,rpos);
    
    // restore state
    glPopAttrib();
    
    // return text size in pixels
    plhs[0]=mxCreateDoubleMatrix(1,2,mxREAL);
    double *p=mxGetPr(plhs[0]);
    p[0]=(double)rpos[0];
    p[1]=(double)fontSize;
    
}

// command map:  moglcore string commands and functions that handle them
// *** it's important that this list be kept in alphabetical order, 
//     and that glm_map_count be updated for 
//     each new entry ***
int glm_map_count=13;
cmdhandler glm_map[] = {
{ "glmClose",                       glm_close                           },
{ "glmGetKeys",                     glm_getkeys                         },
{ "glmGetMouse",                    glm_getmouse                        },
{ "glmGetScreenInfo",               glm_getscreeninfo                   },
{ "glmOpen",                        glm_open                            },
{ "glmPackPixels",                  glm_packpixels                      },
{ "glmSetClut",                     glm_setclut                         },
{ "glmSetFont",                     glm_setfont                         },
{ "glmSetMouse",                    glm_setmouse                        },
{ "glmSetSwapInterval",             glm_setswapinterval                 },
{ "glmSwapBuffers",                 glm_swapbuffers                     },
{ "glmText",                        glm_text                            },
{ "glmTextSize",                    glm_textsize                        }};