Annotation of OpenXM/src/cfep/MyOpenGLView.m, Revision 1.3
1.1 takayama 1: //
2: // MyOpenGLView.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 "MyOpenGLView.h"
1.2 takayama 10: #import "MyOpenGLController.h"
1.1 takayama 11: #include "mygl.h"
12:
13: @implementation MyOpenGLView
14: -(id) initWithFrame: (NSRect) frame {
15: NSLog(@"initWithFrame\n");
16: oglComm = [NSMutableArray arrayWithCapacity: 100];
17: [oglComm retain];
18: oglInitComm = [NSMutableArray arrayWithCapacity: 1];
19: [oglInitComm retain];
20: xeye = 0.0;
21: yeye = 0.0;
22: zeye = 2.0;
23: initGl = 1;
24:
25: [super initWithFrame: frame];
26: if (self) {
27: NSOpenGLPixelFormatAttribute attributes[]={
28: NSOpenGLPFAAccelerated,
29: NSOpenGLPFADepthSize,16, // We need this, otherwise depth buffer will no be enabled.
30: NSOpenGLPFAMinimumPolicy,
31: NSOpenGLPFAClosestPolicy,
32: 0};
33: NSOpenGLPixelFormat *format;
34: format = [[[NSOpenGLPixelFormat alloc] initWithAttributes: attributes] autorelease];
35: [self initWithFrame: frame pixelFormat: format];
36: }
37: return self;
38: }
39:
1.2 takayama 40: -(void) showEyePos {[[MyOpenGLController getOglWindow: gid] showEyeX: xeye Y: yeye Z: zeye];}
1.1 takayama 41: -(IBAction) setXeye: (id) sender {
42: xeye=([sender floatValue]-50)*0.1; initGl = 1;
43: NSLog(@"xeye=%f\n",xeye);
1.2 takayama 44: [self showEyePos];
1.1 takayama 45: [self setNeedsDisplay: YES];
46: }
47: -(IBAction) setYeye: (id) sender {
48: float y;
49: y=([sender floatValue]-50)*0.1; initGl = 1;
50: yeye=y;
1.2 takayama 51: [self showEyePos];
1.1 takayama 52: //NSLog(@"yeye=%f\n",yeye);
53: [self setNeedsDisplay: YES];
54: }
55: -(IBAction) setZeye: (id) sender {
56: float z;
57: z=([sender floatValue]-50)*0.1+2.0; initGl = 1;
58: zeye=z;
1.2 takayama 59: [self showEyePos];
1.1 takayama 60: //NSLog(@"zeye=%f\n",zeye);
61: [self setNeedsDisplay: YES];
62: }
63: -(void) drawRect: (NSRect) rect {
64: if (initGl) { [self initGL]; initGl = 0;}
65: [self drawOglComm];
66: //For testing. [self drawRectSimple: rect withColor: 0.0];
67: }
68: -(void) setFrameSize: (NSSize) newSize {
69: [super setFrameSize: newSize];
70: //[[self openGLContext] makeCurrentContext];
71: glViewport(0,0,(GLsizei)newSize.width,(GLsizei) newSize.height);
72: initGl=1;
73: [self setNeedsDisplay: YES];
74: }
1.2 takayama 75: -(void) setGid: (int) p{
76: gid = p;
77: }
1.1 takayama 78: -(void) initGL {
79: // Initialization codes are here.
80: glClearColor(1.0,1.0,1.0,1.0);
81: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
82: [self drawOglInitComm];
83: }
1.3 ! takayama 84: -(void) setInitGL: (id) sender { initGl = 1; }
1.1 takayama 85: // It was for a test.
86: -(void) drawRectSimple: (NSRect) rect withColor: (double) c {
87: glClearColor(1.0,1.0,1.0,1.0);
88: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
89: glColor4f(1.0,1.0,c,1.0);
90: glRectf(-0.6,-0.6,0.6,0.6);
91:
92: glFlush();
93: [[self openGLContext] flushBuffer];
94: }
95:
96: -(void) drawOglComm {
97: int i,n;
98: MyOpenGLCommand *gc;
99: // Execute oglComm.
100: n = [oglComm count];
101: NSLog(@"drawRect is called with n=%d, oglComSize=%d\n",n,oglCommSize);
102: for (i=0; i<oglCommSize; i++) {
103: gc = [oglComm objectAtIndex: i];
104: [self execute: gc];
105: }
106: glFlush();
107: [[self openGLContext] flushBuffer];
108: }
109: -(void) addOglComm: (NSString *) comm by: (id) sender {
110: MyOpenGLCommand *oc;
111: oc = [MyOpenGLCommand allocAndCompile: comm by: sender];
112: if (oc != nil) {
113: [oglComm addObject: oc]; // retain is automatically done. // dealloc should be implemented.
114: @synchronized(self) {
115: if ([oc isEndGroup] == YES) oglCommSize=[oglComm count];
116: }
117: if ([oc getOpCode] == CFEPglib_flush) [self setNeedsDisplay: YES];
1.2 takayama 118: else if ([oc getOpCode] == CFEPglFlush) [self setNeedsDisplay: YES];
1.1 takayama 119: // If oc is glib_flush, then call drawRect. (Generate an event.)
120: // Calling [self drawOglComm] directly is not safe, because the window might not be ready.
121: }
122: }
123:
1.2 takayama 124: -(NSMutableArray *)getListOfOglComm { return oglComm; }
125: -(NSMutableArray *)getListOfOglInitComm { return oglInitComm; }
1.3 ! takayama 126: -(int) getOglCommSize { return oglCommSize; }
! 127: -(int) getOglInitCommSize { return oglInitCommSize; }
1.2 takayama 128: -(int) countOfOglComm { return [oglComm count]; }
129: -(int) countOfOglInitComm { return [oglInitComm count];}
1.3 ! takayama 130: -(int) removeLastOfOglComm { if ([oglComm count]>0) [oglComm removeLastObject]; [self updateOglCommSize]; return [self countOfOglComm];}
! 131: -(int) removeLastOfOglInitComm {if ([oglInitComm count]>0) [oglInitComm removeLastObject]; [self updateOglInitCommSize]; return [self countOfOglInitComm];}
! 132: -(int) removeAllOfOglComm { if ([oglComm count]>0) [oglComm removeAllObjects]; oglCommSize = 0; return 0; }
! 133: -(int) removeAllOfOglInitComm {if ([oglInitComm count]>0) [oglInitComm removeAllObjects]; oglInitCommSize = 0; return 0; }
! 134: -(void) updateOglCommSize {
! 135: int n,i;
! 136: MyOpenGLCommand *cc;
! 137: n = [oglComm count];
! 138: for (i=n-1; i>=0; i--) {
! 139: cc = [oglComm objectAtIndex: i];
! 140: if ([cc isEndGroup]) { oglCommSize = i+1; return ; }
! 141: }
! 142: oglCommSize = 0;
! 143: }
! 144: -(void) updateOglInitCommSize {
! 145: int n,i;
! 146: MyOpenGLCommand *cc;
! 147: n = [oglInitComm count];
! 148: for (i=n-1; i>=0; i--) {
! 149: cc = [oglInitComm objectAtIndex: i];
! 150: if ([cc isEndGroup]) { oglInitCommSize = i+1; return ; }
! 151: }
! 152: oglInitCommSize = 0;
! 153: }
1.2 takayama 154:
1.1 takayama 155: -(void) drawOglInitComm {
156: int i,n;
157: MyOpenGLCommand *gc;
158: // Execute oglComm.
159: n = [oglInitComm count];
160: NSLog(@"drawOglInitComm is called with n=%d, oglInitComSize=%d\n",n,oglInitCommSize);
161: for (i=0; i<oglInitCommSize; i++) {
162: gc = [oglInitComm objectAtIndex: i];
163: [self execute: gc];
164: }
165: }
166: -(void) addOglInitComm: (NSString *) comm by: (id) sender {
167: MyOpenGLCommand *oc;
168: oc = [MyOpenGLCommand allocAndCompile: comm by: sender];
169: if (oc != nil) {
170: [oglInitComm addObject: oc]; // retain is automatically done. // dealloc should be implemented.
171: @synchronized(self) {
172: if ([oc isEndGroup] == YES) oglInitCommSize=[oglInitComm count];
173: }
1.2 takayama 174: initGl = 1;
175: if ([oc getOpCode] == CFEPglib_flush) { [self setNeedsDisplay: YES]; }
176: else if ([oc getOpCode] == CFEPglFlush) [self setNeedsDisplay: YES];
1.1 takayama 177: }
178: }
179:
180: // Byte compile is done by comple: in MyOpenGLCommand.m
181: -(void) execute: (MyOpenGLCommand *)gc {
182: int op;
183: float x,y,z,c;
184: int p,q,r,s;
185: double *v;
186: int *ii;
187: op = [gc getOpCode];
188: v = [gc getF4];
189: ii = [gc getI4];
190: x = v[0]; y = v[1]; z = v[2]; c = v[3];
191: p = ii[0]; q=ii[1]; r=ii[2]; s=ii[3];
192: // NSLog(@"opCode=%d, (x,y,z,c)=(%f,%f,%f,%f), (p,q,r,s)=(%d,%d,%d,%d)\n",op,x,y,z,c,p,q,r,s);
193: switch(op) {
194: case CFEPglBegin:
195: glBegin(p);
196: break;
1.2 takayama 197: case CFEPglClear:
198: glClear(p); break;
199: case CFEPglClearColor:
200: glClearColor(x,y,z,c); break;
201: case CFEPglClearDepth:
202: glClearDepth(x); break;
1.1 takayama 203: case CFEPglColor4f:
204: glColor4f(x,y,z,c); break;
205: case CFEPglEnd:
206: glEnd(); break;
1.2 takayama 207: case CFEPglFlush:
1.3 ! takayama 208: glFlush(); [self setInitGL: nil]; [self setNeedsDisplay: YES]; break;
1.2 takayama 209: case CFEPglPointSize:
210: glPointSize(x); break;
1.1 takayama 211: case CFEPglRectf:
212: glRectf(x,y,z,c); break;
213: case CFEPglVertex3f:
214: glVertex3f(x,y,z); break;
215:
216: case CFEPglib_line:
217: glib_line(x,y,z,c,p); break;
218: case CFEPglib_putpixel:
219: glib_putpixel(x,y,p); break;
220: case CFEPglib_flush:
1.3 ! takayama 221: [self setInitGL: nil]; [self setNeedsDisplay: YES];
1.2 takayama 222: // [[MyOpenGLController getOglWindow: gid] showCount];
223: break;
1.1 takayama 224:
225: case CFEPglib3_bounding_box:
226: glib3_bounding_box(x); break;
227: case CFEPglib3_icosahedron:
228: glib3_icosahedron(0.0f, 0.0f, -1.0f, 1.0f); break;
229: case CFEPglib3_ray:
230: [self glib3_ray]; break;
231: case CFEPglib3_ray_init:
232: glib3_ray_init(); break;
233: case CFEPglib3_ray_reshape:
234: glib3_ray_reshape(x,y); break;
235: case CFEPglib3_std_scene0:
236: [self glib3_std_scene0]; break;
237: default:
238: NSLog(@"Unknown opCode %d\n",op);
239: break;
240: }
241: }
242:
243: -(void) glib3_ray {
244: glib3_ray_change_alpha(-90.0+xeye*5);
245: glib3_ray_change_beta(90.0+yeye*3);
246: glib3_ray();
247: }
248: -(void) glib3_std_scene0 {
249: glib3_std_scene0(xeye,yeye,zeye);
250: }
251:
252: @end
253:
254: // Original glib functions
255: void glib_line(float x,float y, float x2,float y2, int color) {
256: glColor4f((color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0,0.0);
257: // NSLog(@"color=%f,%f,%f\n",(color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0);
258: glBegin(GL_LINES);
259: glVertex2f(x,y);
260: glVertex2f(x2,y2);
261: glEnd();
262: }
263: void glib_putpixel(float x,float y,int color) {
264: // NSLog(@"color=%f,%f,%f\n",(color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0);
265: glBegin(GL_POINTS);
266: glColor4f((color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0,0.0);
267: glVertex2f(x,y);
268: glEnd();
269: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>