Annotation of OpenXM/src/cfep/MyOpenGLCommand.m, Revision 1.7
1.1 takayama 1: //
2: // MyOpenGLCommand.m
3: // cfep
4: //
5: // Created by Nobuki Takayama on 06/02/18.
6: // Copyright 2006 OpenXM.org. All rights reserved.
7: //
8:
9: #import "MyOpenGLCommand.h"
10: #define min(a,b) (a>b?b:a)
11:
12:
13: @implementation MyOpenGLCommand
14: -(id)init {
15: int i;
16: command = nil;
17: opCode = -1;
18: endGroup = NO;
19: for (i=0; i<4; i++) f4[i] = 0.0;
20: for (i=0; i<4; i++) i4[i] = 0;
21: return self;
22: }
23: -(void) dealloc {
24: if (argv) [argv autorelease];
25: }
26: -(void)print {
27: int i;
1.3 takayama 28: NSLog(@"command=%@s, opCode=%d, [",command,opCode);
1.1 takayama 29: for (i=0; i<4; i++) NSLog(@"%f,",f4[i]);
30: NSLog(@"; ");
31: for (i=0; i<4; i++) NSLog(@"%d,",i4[i]);
32: NSLog(@"]\n");
33: }
1.3 takayama 34: -(NSString *)toString {
35: return [NSString stringWithFormat: @"%@[%d], %f, %f, %f, %f; %d, %d, %d, %d",command,opCode,f4[0],f4[1],f4[2],f4[3],i4[0],i4[1],i4[2],i4[3]];
36: }
1.1 takayama 37: +(MyOpenGLCommand *)allocAndCompile: (NSString *)cmd by: (id) sender {
38: MyOpenGLCommand *c;
39: c=[[MyOpenGLCommand alloc] init];
40: [c autorelease];
41: return [c compile: cmd by: sender];
42: }
43: -(BOOL) isEndGroup {
44: return endGroup;
45: }
46: -(MyOpenGLCommand *) compile: (NSString *)cmd by: (id) sender {
47: // parse cmd and set opCode and f4[]. opCode starts with CFEPgl.
48: // Interpreter code is in MyOpenGLView.m with the name execute:
49: NSArray *a;
50: NSString *s;
51: NSString *errmsg;
52: id x;
53: int fargc, iargc;
54: int i,n,ii;
55: double f;
56:
57: a = [MyUtil arrayOfStringFrom: cmd]; //"[glColor4f,1.0,1.0,0.0,1.0]" -> array of [NSString, MyFloat, MyFloat,..]
58: [a retain]; argv = a;
59: n = [a count];
60: fargc = 0; iargc = 0;
61: // for (i=0; i<n; i++) { NSLog(@"%@,",[a objectAtIndex: i]); }
62: if (n == 0) return nil;
63: x = [a objectAtIndex: 0];
64: if ([x isKindOfClass: [NSString class]] == YES) s = x; // isMemberOfClass does not work. ??
65: else {
66: NSLog(@"NSString is exected.\n");
67: s = @"";
68: }
69: if ([s compare: @"glBegin"] == NSOrderedSame) {
70: opCode = CFEPglBegin; fargc = 0; iargc = 1; endGroup = NO;
71:
72: }else if ([s compare: @"glColor4f"] == NSOrderedSame) {
73: opCode = CFEPglColor4f; fargc = 4; iargc = 0; endGroup = NO;
74:
1.3 takayama 75: }else if ([s compare: @"glClear"] == NSOrderedSame) {
76: opCode = CFEPglClear; fargc = 0; iargc = 1; endGroup = YES;
77:
78: }else if ([s compare: @"glClearColor"] == NSOrderedSame) {
79: opCode = CFEPglClearColor; fargc = 4; iargc = 0; endGroup = YES;
80:
81: }else if ([s compare: @"glClearDepth"] == NSOrderedSame) {
82: opCode = CFEPglClearDepth; fargc = 1; iargc = 0; endGroup = YES;
83:
1.5 takayama 84: }else if ([s compare: @"glDisable"] == NSOrderedSame) {
85: opCode = CFEPglDisable; fargc = 0; iargc = 1; endGroup = YES;
86:
87: }else if ([s compare: @"glEnable"] == NSOrderedSame) {
88: opCode = CFEPglEnable; fargc = 0; iargc = 1; endGroup = YES;
89:
1.1 takayama 90: }else if ([s compare: @"glEnd"] == NSOrderedSame) {
91: opCode = CFEPglEnd; fargc = 0; iargc = 0; endGroup = YES;
92:
1.3 takayama 93: }else if ([s compare: @"glFlush"] == NSOrderedSame) {
94: opCode = CFEPglFlush; fargc = 0; iargc = 0; endGroup = YES;
1.5 takayama 95:
96: }else if ([s compare: @"glLineStipple"] == NSOrderedSame) {
97: opCode = CFEPglLineStipple; fargc = 0; iargc = 2; endGroup = YES;
98:
99: }else if ([s compare: @"glLineWidth"] == NSOrderedSame) {
100: opCode = CFEPglLineWidth; fargc = 1; iargc = 0; endGroup = YES;
101:
102: }else if ([s compare: @"glNormal3f"] == NSOrderedSame) {
103: opCode = CFEPglNormal3f; fargc = 3; iargc = 0; endGroup = YES;
1.3 takayama 104:
105: }else if ([s compare: @"glPointSize"] == NSOrderedSame) {
106: opCode = CFEPglPointSize; fargc = 1; iargc = 0; endGroup = NO;
107:
1.1 takayama 108: }else if ([s compare: @"glRectf"] == NSOrderedSame) {
109: opCode = CFEPglRectf; fargc = 4; iargc = 0; endGroup = YES;
110:
111: }else if ([s compare: @"glVertex3f"] == NSOrderedSame) {
112: opCode = CFEPglVertex3f; fargc = 3; iargc = 0; endGroup = NO;
113:
1.6 takayama 114: }else if ([s compare: @"glVertex2f"] == NSOrderedSame) {
115: opCode = CFEPglVertex2f; fargc = 2; iargc = 0; endGroup = NO;
116:
1.1 takayama 117: }else if ([s compare: @"glib_line"] == NSOrderedSame) {
118: opCode = CFEPglib_line; fargc = 4; iargc = 1; endGroup = YES;
119:
120: }else if ([s compare: @"glib_putpixel"] == NSOrderedSame) {
121: opCode = CFEPglib_putpixel; fargc = 2; iargc = 1; endGroup = YES;
122:
123: }else if ([s compare: @"glib_flush"] == NSOrderedSame) {
124: opCode = CFEPglib_flush; fargc = 0; iargc = 0; endGroup = YES;
125:
126: }else if ([s compare: @"glib3_bounding_box"] == NSOrderedSame) {
127: opCode = CFEPglib3_bounding_box; fargc = 1; iargc = 0; endGroup = YES;
128:
129: }else if ([s compare: @"glib3_ray_init"] == NSOrderedSame) {
130: opCode = CFEPglib3_ray_init; fargc = 0; iargc = 0; endGroup = YES;
131:
132: }else if ([s compare: @"glib3_ray_reshape"] == NSOrderedSame) {
133: opCode = CFEPglib3_ray_reshape; fargc = 0; iargc = 0; endGroup = YES;
134:
135: }else if ([s compare: @"glib3_ray"] == NSOrderedSame) {
136: opCode = CFEPglib3_ray; fargc = 0; iargc = 0; endGroup = YES;
137: [sender output: @"Ray by David Bucciarelli. (MesaDemo, GPL)"];
138:
139: }else if ([s compare: @"glib3_std_scene0"] == NSOrderedSame) {
140: opCode = CFEPglib3_std_scene0; fargc = 0; iargc = 0; endGroup = YES;
141: [sender output: @"glib3_std_scene0(0,0,2) using glFrustum"];
142:
143: }else if ([s compare: @"glib3_icosahedron"] == NSOrderedSame) {
144: opCode = CFEPglib3_icosahedron; fargc = 0; iargc = 0; endGroup = YES;
145: [sender output: @"Icosahedron"];
146:
147: }else {
148: NSLog(@"Undefined command=<%@>\n",s);
149: errmsg = [NSString stringWithFormat: @"Undefined command=<%@>",s];
150: [sender output: errmsg];
151: return nil;
152: }
153: // Format glxxxpfqi fargc=p, iargc=q. Example: glxxx4f1i, 0.1,0.2,0.3,0.4,34
1.3 takayama 154: command = s;
155: [command retain]; // bug. How to release?
1.1 takayama 156: if (fargc > 0) {
157: for (i=1; i< min(n,fargc+1); i++) {
158: x = [a objectAtIndex: i];
159: if ([x isMemberOfClass: [MyFloat class]] == YES) f = [x getFValue];
160: else {
161: NSLog(@"float is exected.\n");
162: f = 0.0;
163: }
164: if (i-1 < 4) f4[i-1] = f;
165: }
166: }
167: if (iargc > 0) {
168: for (i=1+fargc; i< min(n,fargc+iargc+1); i++) {
169: x = [a objectAtIndex: i];
170: if ([x isMemberOfClass: [MyInt class]] == YES) ii = [x getIValue];
171: else {
172: ii = 0;
173: NSLog(@"int is exected.\n");
174: }
175: if (i-1-fargc < 4) i4[i-1-fargc] = ii;
176: }
177: }
178: return self;
179: }
180:
181: -(int) getOpCode { return opCode; }
182: -(double *)getF4 { return f4; }
183: -(int *) getI4 { return i4; }
184: -(NSArray *)getArgv { return argv; }
185:
186: @end
187:
188: /* Code to test MyFloat put in compile:
189: MyFloat *x;
190: NSLog(@"member test.\n"
191: x = [MyFloat allocWith: 1.0];
192: if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n");
193: if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n");
194: */
195: @implementation MyFloat
196: +(MyFloat *)allocWith: (float) f {
197: MyFloat *ff;
198: ff=[MyFloat alloc];
199: [ff setFValue: f];
200: return ff;
201: }
202: -(void) setFValue: (float) f { fValue = f; }
203: -(float) getFValue { return fValue; }
204: -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; }
205: @end
206:
207: @implementation MyInt
208: +(MyInt *)allocWith: (int) i {
209: MyInt *ii;
210: ii=[MyInt alloc];
211: [ii setIValue: i];
212: return ii;
213: }
214: -(void) setIValue: (int) i { iValue = i; }
215: -(int) getIValue { return iValue; }
216: -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; }
217: @end
218:
219: #define SMAX 0x1000 //4096*16
220: @implementation MyUtil
221: static int debugMyUtil = 0;
222: +(void) setDebugMyUtil {
223: if (debugMyUtil) debugMyUtil = 0;
224: else debugMyUtil = 1;
225: }
226: +(NSMutableArray *) arrayOfStringFrom: (NSString *) args {
227: int n,i,k,type;
228: unichar c;
229: unichar s[SMAX];
230: NSMutableArray *a; // for debug
231: NSMutableArray *aa;
232: NSString *ss;
233: int status,level;
234: status = -1; level = 0; type = 0;
235: n = [args length];
236: if (n >= SMAX-1) {
237: NSLog(@"Too big string for stringToArrayOfString.\n");
238: return nil;
239: }
240: a = [NSMutableArray arrayWithCapacity: 1];
241: aa = [NSMutableArray arrayWithCapacity: 1];
242: for (i=0, k=0; i<n; i++) {
243: c = [args characterAtIndex: i];
244: if (level == 0) {
245: switch (status) { // See my note on 2006-02-20.
246: case -1:
247: if ( c == '[') { k=0; status=0;}
248: break;
249: case 0:
250: if ( c == '[') {
251: k = 0; s[k] = c; k++; status = 1; type = 1; // type 1 : list
252: } else if (c == ',') {
253: } else if (c == '(') {
254: k=0; status = 21; type=0; //ex. (int)
255: } else if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
256: k=0; status = 2; type=3; s[k] = c; k++; // type -1 : string.
257: } else if (c > ' ') {
258: k=0; status = 2; type=0; s[k] = c; k++; // type 0 : float
259: } else if (c == ']') status = -1;
260: break;
261: case 2:
262: if ((c == ',') || (c == ']')) {
263: if (c == ',') status = 0; else status = -1;
264: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
265: else ss = @"";
266: [a addObject: ss];
267: if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]];
268: else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]];
269: else [aa addObject: ss];
270: }else{
271: s[k] = c; k++;
272: }
273: break;
274: case 1:
275: if (c == '[') {
276: s[k] = c; k++;
277: level++;
278: }else if (c == ']') {
279: s[k] = ']'; k++;
280: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
281: else ss = @"";
282: [a addObject: ss];
283: [aa addObject: [MyUtil arrayOfStringFrom: ss]];
284: status = 0;
285: }else {
286: s[k] = c; k++;
287: }
288: break;
289: case 21:
290: if (c == ')') {
291: if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2;
292: else type = 0;
293: status = 2; k = 0; // starting to read data.
294: }else if (c > ' ') {
295: s[k] = c; k++;
296: }
297: break;
298: }
299: }else if (level > 0) {
300: s[k] = c; k++;
301: if (c == '[') level++;
302: else if (c == ']') level--;
303: }
304: }
305: if (debugMyUtil) {
306: NSLog(@"a=%@\n",a);
307: NSLog(@"aa=%@\n",aa);
308: }
309: return aa;
310: }
311:
312: /*
313: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]);
314: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]);
315: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]);
316: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]);
317: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,...
318: */
319:
1.2 takayama 320: +(NSString *)pruneThings: (NSString *)ss {
321: int n,i, start,end;
322: unichar c;
323: unichar s[SMAX];
324: NSString *ans;
325: n = [ss length];
326: if (n >= SMAX-1) {
327: NSLog(@"Too big string for pruneThings.\n");
328: return nil;
329: }
330: start = 0; end = n-1;
331: for (i=0; i<n; i++) {
332: c = [ss characterAtIndex: i];
333: start = i;
334: if (c > 0x20) break;
335: }
336: for (i=n-1; i>=0; i--) {
337: c = [ss characterAtIndex: i];
338: end = i;
339: if (c > 0x20) break;
340: }
341: if (start > end) return nil;
342: for (i=0; i<= end-start ; i++) {
343: s[i] = [ss characterAtIndex: (start+i)];
344: s[i+1] =0;
345: }
346: ans = [NSString stringWithCharacters: s length: (end-start+1)];
347: return ans;
348: }
349:
350: +(id)attributedStringWithPath: (NSString *)path {
351: NSFileWrapper *theWrapper;
352: NSTextAttachment *theAttachment;
353: NSAttributedString *aStr;
354: theWrapper = [[NSFileWrapper alloc] initWithPath: path];
355: if (!theWrapper) NSLog(@"theWrapper is nil. Path=[%@]\n",path);
356: if (!theWrapper) return nil;
357: theAttachment = [[NSTextAttachment alloc] initWithFileWrapper:theWrapper];
358: aStr = [NSAttributedString attributedStringWithAttachment:theAttachment];
359: return aStr; // How should I do autorelease?
360: }
361:
362:
1.7 ! takayama 363: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>