Annotation of OpenXM/src/cfep/MyOpenGLCommand.m, Revision 1.1.1.1
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;
28: NSLog(@"command=%s, opCode=%d, [",command,opCode);
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: }
34: +(MyOpenGLCommand *)allocAndCompile: (NSString *)cmd by: (id) sender {
35: MyOpenGLCommand *c;
36: c=[[MyOpenGLCommand alloc] init];
37: [c autorelease];
38: return [c compile: cmd by: sender];
39: }
40: -(BOOL) isEndGroup {
41: return endGroup;
42: }
43: -(MyOpenGLCommand *) compile: (NSString *)cmd by: (id) sender {
44: // parse cmd and set opCode and f4[]. opCode starts with CFEPgl.
45: // Interpreter code is in MyOpenGLView.m with the name execute:
46: NSArray *a;
47: NSString *s;
48: NSString *errmsg;
49: id x;
50: int fargc, iargc;
51: int i,n,ii;
52: double f;
53:
54: a = [MyUtil arrayOfStringFrom: cmd]; //"[glColor4f,1.0,1.0,0.0,1.0]" -> array of [NSString, MyFloat, MyFloat,..]
55: [a retain]; argv = a;
56: n = [a count];
57: fargc = 0; iargc = 0;
58: // for (i=0; i<n; i++) { NSLog(@"%@,",[a objectAtIndex: i]); }
59: if (n == 0) return nil;
60: x = [a objectAtIndex: 0];
61: if ([x isKindOfClass: [NSString class]] == YES) s = x; // isMemberOfClass does not work. ??
62: else {
63: NSLog(@"NSString is exected.\n");
64: s = @"";
65: }
66: if ([s compare: @"glBegin"] == NSOrderedSame) {
67: opCode = CFEPglBegin; fargc = 0; iargc = 1; endGroup = NO;
68:
69: }else if ([s compare: @"glColor4f"] == NSOrderedSame) {
70: opCode = CFEPglColor4f; fargc = 4; iargc = 0; endGroup = NO;
71:
72: }else if ([s compare: @"glEnd"] == NSOrderedSame) {
73: opCode = CFEPglEnd; fargc = 0; iargc = 0; endGroup = YES;
74:
75: }else if ([s compare: @"glRectf"] == NSOrderedSame) {
76: opCode = CFEPglRectf; fargc = 4; iargc = 0; endGroup = YES;
77:
78: }else if ([s compare: @"glVertex3f"] == NSOrderedSame) {
79: opCode = CFEPglVertex3f; fargc = 3; iargc = 0; endGroup = NO;
80:
81: }else if ([s compare: @"glib_line"] == NSOrderedSame) {
82: opCode = CFEPglib_line; fargc = 4; iargc = 1; endGroup = YES;
83:
84: }else if ([s compare: @"glib_putpixel"] == NSOrderedSame) {
85: opCode = CFEPglib_putpixel; fargc = 2; iargc = 1; endGroup = YES;
86:
87: }else if ([s compare: @"glib_flush"] == NSOrderedSame) {
88: opCode = CFEPglib_flush; fargc = 0; iargc = 0; endGroup = YES;
89:
90: }else if ([s compare: @"glib3_bounding_box"] == NSOrderedSame) {
91: opCode = CFEPglib3_bounding_box; fargc = 1; iargc = 0; endGroup = YES;
92:
93: }else if ([s compare: @"glib3_ray_init"] == NSOrderedSame) {
94: opCode = CFEPglib3_ray_init; fargc = 0; iargc = 0; endGroup = YES;
95:
96: }else if ([s compare: @"glib3_ray_reshape"] == NSOrderedSame) {
97: opCode = CFEPglib3_ray_reshape; fargc = 0; iargc = 0; endGroup = YES;
98:
99: }else if ([s compare: @"glib3_ray"] == NSOrderedSame) {
100: opCode = CFEPglib3_ray; fargc = 0; iargc = 0; endGroup = YES;
101: [sender output: @"Ray by David Bucciarelli. (MesaDemo, GPL)"];
102:
103: }else if ([s compare: @"glib3_std_scene0"] == NSOrderedSame) {
104: opCode = CFEPglib3_std_scene0; fargc = 0; iargc = 0; endGroup = YES;
105: [sender output: @"glib3_std_scene0(0,0,2) using glFrustum"];
106:
107: }else if ([s compare: @"glib3_icosahedron"] == NSOrderedSame) {
108: opCode = CFEPglib3_icosahedron; fargc = 0; iargc = 0; endGroup = YES;
109: [sender output: @"Icosahedron"];
110:
111: }else {
112: NSLog(@"Undefined command=<%@>\n",s);
113: errmsg = [NSString stringWithFormat: @"Undefined command=<%@>",s];
114: [sender output: errmsg];
115: return nil;
116: }
117: // Format glxxxpfqi fargc=p, iargc=q. Example: glxxx4f1i, 0.1,0.2,0.3,0.4,34
118: if (fargc > 0) {
119: for (i=1; i< min(n,fargc+1); i++) {
120: x = [a objectAtIndex: i];
121: if ([x isMemberOfClass: [MyFloat class]] == YES) f = [x getFValue];
122: else {
123: NSLog(@"float is exected.\n");
124: f = 0.0;
125: }
126: if (i-1 < 4) f4[i-1] = f;
127: }
128: }
129: if (iargc > 0) {
130: for (i=1+fargc; i< min(n,fargc+iargc+1); i++) {
131: x = [a objectAtIndex: i];
132: if ([x isMemberOfClass: [MyInt class]] == YES) ii = [x getIValue];
133: else {
134: ii = 0;
135: NSLog(@"int is exected.\n");
136: }
137: if (i-1-fargc < 4) i4[i-1-fargc] = ii;
138: }
139: }
140: return self;
141: }
142:
143: -(int) getOpCode { return opCode; }
144: -(double *)getF4 { return f4; }
145: -(int *) getI4 { return i4; }
146: -(NSArray *)getArgv { return argv; }
147:
148:
149: @end
150:
151: /* Code to test MyFloat put in compile:
152: MyFloat *x;
153: NSLog(@"member test.\n"
154: x = [MyFloat allocWith: 1.0];
155: if ([x isMemberOfClass: [MyFloat class]] == YES) NSLog(@"[x class] is MyFloat\n");
156: if ([x isMemberOfClass: [MyInt class]] == YES) NSLog(@"[x class] is MyInt\n");
157: */
158: @implementation MyFloat
159: +(MyFloat *)allocWith: (float) f {
160: MyFloat *ff;
161: ff=[MyFloat alloc];
162: [ff setFValue: f];
163: return ff;
164: }
165: -(void) setFValue: (float) f { fValue = f; }
166: -(float) getFValue { return fValue; }
167: -(NSString *)description { return [NSString stringWithFormat: @"(float)%f",fValue]; }
168: @end
169:
170: @implementation MyInt
171: +(MyInt *)allocWith: (int) i {
172: MyInt *ii;
173: ii=[MyInt alloc];
174: [ii setIValue: i];
175: return ii;
176: }
177: -(void) setIValue: (int) i { iValue = i; }
178: -(int) getIValue { return iValue; }
179: -(NSString *)description { return [NSString stringWithFormat: @"(int)%d",iValue]; }
180: @end
181:
182: #define SMAX 0x1000 //4096*16
183: @implementation MyUtil
184: static int debugMyUtil = 0;
185: +(void) setDebugMyUtil {
186: if (debugMyUtil) debugMyUtil = 0;
187: else debugMyUtil = 1;
188: }
189: +(NSMutableArray *) arrayOfStringFrom: (NSString *) args {
190: int n,i,k,type;
191: unichar c;
192: unichar s[SMAX];
193: NSMutableArray *a; // for debug
194: NSMutableArray *aa;
195: NSString *ss;
196: int status,level;
197: status = -1; level = 0; type = 0;
198: n = [args length];
199: if (n >= SMAX-1) {
200: NSLog(@"Too big string for stringToArrayOfString.\n");
201: return nil;
202: }
203: a = [NSMutableArray arrayWithCapacity: 1];
204: aa = [NSMutableArray arrayWithCapacity: 1];
205: for (i=0, k=0; i<n; i++) {
206: c = [args characterAtIndex: i];
207: if (level == 0) {
208: switch (status) { // See my note on 2006-02-20.
209: case -1:
210: if ( c == '[') { k=0; status=0;}
211: break;
212: case 0:
213: if ( c == '[') {
214: k = 0; s[k] = c; k++; status = 1; type = 1; // type 1 : list
215: } else if (c == ',') {
216: } else if (c == '(') {
217: k=0; status = 21; type=0; //ex. (int)
218: } else if ((('A' <= c) && (c <= 'Z')) || (('a' <= c) && (c <= 'z'))) {
219: k=0; status = 2; type=3; s[k] = c; k++; // type -1 : string.
220: } else if (c > ' ') {
221: k=0; status = 2; type=0; s[k] = c; k++; // type 0 : float
222: } else if (c == ']') status = -1;
223: break;
224: case 2:
225: if ((c == ',') || (c == ']')) {
226: if (c == ',') status = 0; else status = -1;
227: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
228: else ss = @"";
229: [a addObject: ss];
230: if (type == 2) [aa addObject: [MyInt allocWith: [ss intValue]]];
231: else if (type == 0) [aa addObject: [MyFloat allocWith: [ss floatValue]]];
232: else [aa addObject: ss];
233: }else{
234: s[k] = c; k++;
235: }
236: break;
237: case 1:
238: if (c == '[') {
239: s[k] = c; k++;
240: level++;
241: }else if (c == ']') {
242: s[k] = ']'; k++;
243: if (k > 0) ss = [NSString stringWithCharacters: s length: k];
244: else ss = @"";
245: [a addObject: ss];
246: [aa addObject: [MyUtil arrayOfStringFrom: ss]];
247: status = 0;
248: }else {
249: s[k] = c; k++;
250: }
251: break;
252: case 21:
253: if (c == ')') {
254: if ((s[0] == 'i') && (s[1] == 'n') && (s[2] == 't')) type=2;
255: else type = 0;
256: status = 2; k = 0; // starting to read data.
257: }else if (c > ' ') {
258: s[k] = c; k++;
259: }
260: break;
261: }
262: }else if (level > 0) {
263: s[k] = c; k++;
264: if (c == '[') level++;
265: else if (c == ']') level--;
266: }
267: }
268: if (debugMyUtil) {
269: NSLog(@"a=%@\n",a);
270: NSLog(@"aa=%@\n",aa);
271: }
272: return aa;
273: }
274:
275: /*
276: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[]"]);
277: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,[]]"]);
278: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[1,2,[3,4],5]"]);
279: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @" [1, 2,3, 4,5]"]);
280: NSLog(@"mytest=%@\n",[MyUtil arrayOfStringFrom: @"[[1],2,[[3,[4]]],5]"]); // There is a bug. Output is 1,2,...
281: */
282:
283: @end
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>