Annotation of OpenXM/src/cfep/MyOpenGLView.m, Revision 1.2
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: }
84: // It was for a test.
85: -(void) drawRectSimple: (NSRect) rect withColor: (double) c {
86: glClearColor(1.0,1.0,1.0,1.0);
87: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
88: glColor4f(1.0,1.0,c,1.0);
89: glRectf(-0.6,-0.6,0.6,0.6);
90:
91: glFlush();
92: [[self openGLContext] flushBuffer];
93: }
94:
95: -(void) drawOglComm {
96: int i,n;
97: MyOpenGLCommand *gc;
98: // Execute oglComm.
99: n = [oglComm count];
100: NSLog(@"drawRect is called with n=%d, oglComSize=%d\n",n,oglCommSize);
101: for (i=0; i<oglCommSize; i++) {
102: gc = [oglComm objectAtIndex: i];
103: [self execute: gc];
104: }
105: glFlush();
106: [[self openGLContext] flushBuffer];
107: }
108: -(void) addOglComm: (NSString *) comm by: (id) sender {
109: MyOpenGLCommand *oc;
110: oc = [MyOpenGLCommand allocAndCompile: comm by: sender];
111: if (oc != nil) {
112: [oglComm addObject: oc]; // retain is automatically done. // dealloc should be implemented.
113: @synchronized(self) {
114: if ([oc isEndGroup] == YES) oglCommSize=[oglComm count];
115: }
116: if ([oc getOpCode] == CFEPglib_flush) [self setNeedsDisplay: YES];
1.2 ! takayama 117: else if ([oc getOpCode] == CFEPglFlush) [self setNeedsDisplay: YES];
1.1 takayama 118: // If oc is glib_flush, then call drawRect. (Generate an event.)
119: // Calling [self drawOglComm] directly is not safe, because the window might not be ready.
120: }
121: }
122:
1.2 ! takayama 123: -(NSMutableArray *)getListOfOglComm { return oglComm; }
! 124: -(NSMutableArray *)getListOfOglInitComm { return oglInitComm; }
! 125: -(int) countOfOglComm { return [oglComm count]; }
! 126: -(int) countOfOglInitComm { return [oglInitComm count];}
! 127: -(int) removeLastOfOglComm { if ([oglComm count]>0) [oglComm removeLastObject]; return [self countOfOglComm];}
! 128: -(int) removeLastOfOglInitComm {if ([oglInitComm count]>0) [oglInitComm removeLastObject]; return [self countOfOglInitComm];}
! 129: -(int) removeAllOfOglComm { if ([oglComm count]>0) [oglComm removeAllObjects]; return 0; }
! 130: -(int) removeAllOfOglInitComm {if ([oglInitComm count]>0) [oglInitComm removeAllObjects]; return 0; }
! 131:
1.1 takayama 132: -(void) drawOglInitComm {
133: int i,n;
134: MyOpenGLCommand *gc;
135: // Execute oglComm.
136: n = [oglInitComm count];
137: NSLog(@"drawOglInitComm is called with n=%d, oglInitComSize=%d\n",n,oglInitCommSize);
138: for (i=0; i<oglInitCommSize; i++) {
139: gc = [oglInitComm objectAtIndex: i];
140: [self execute: gc];
141: }
142: }
143: -(void) addOglInitComm: (NSString *) comm by: (id) sender {
144: MyOpenGLCommand *oc;
145: oc = [MyOpenGLCommand allocAndCompile: comm by: sender];
146: if (oc != nil) {
147: [oglInitComm addObject: oc]; // retain is automatically done. // dealloc should be implemented.
148: @synchronized(self) {
149: if ([oc isEndGroup] == YES) oglInitCommSize=[oglInitComm count];
150: }
1.2 ! takayama 151: initGl = 1;
! 152: if ([oc getOpCode] == CFEPglib_flush) { [self setNeedsDisplay: YES]; }
! 153: else if ([oc getOpCode] == CFEPglFlush) [self setNeedsDisplay: YES];
1.1 takayama 154: }
155: }
156:
157: // Byte compile is done by comple: in MyOpenGLCommand.m
158: -(void) execute: (MyOpenGLCommand *)gc {
159: int op;
160: float x,y,z,c;
161: int p,q,r,s;
162: double *v;
163: int *ii;
164: op = [gc getOpCode];
165: v = [gc getF4];
166: ii = [gc getI4];
167: x = v[0]; y = v[1]; z = v[2]; c = v[3];
168: p = ii[0]; q=ii[1]; r=ii[2]; s=ii[3];
169: // 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);
170: switch(op) {
171: case CFEPglBegin:
172: glBegin(p);
173: break;
1.2 ! takayama 174: case CFEPglClear:
! 175: glClear(p); break;
! 176: case CFEPglClearColor:
! 177: glClearColor(x,y,z,c); break;
! 178: case CFEPglClearDepth:
! 179: glClearDepth(x); break;
1.1 takayama 180: case CFEPglColor4f:
181: glColor4f(x,y,z,c); break;
182: case CFEPglEnd:
183: glEnd(); break;
1.2 ! takayama 184: case CFEPglFlush:
! 185: glFlush(); [self setNeedsDisplay: YES]; break;
! 186: case CFEPglPointSize:
! 187: glPointSize(x); break;
1.1 takayama 188: case CFEPglRectf:
189: glRectf(x,y,z,c); break;
190: case CFEPglVertex3f:
191: glVertex3f(x,y,z); break;
192:
193: case CFEPglib_line:
194: glib_line(x,y,z,c,p); break;
195: case CFEPglib_putpixel:
196: glib_putpixel(x,y,p); break;
197: case CFEPglib_flush:
1.2 ! takayama 198: [self setNeedsDisplay: YES];
! 199: // [[MyOpenGLController getOglWindow: gid] showCount];
! 200: break;
1.1 takayama 201:
202: case CFEPglib3_bounding_box:
203: glib3_bounding_box(x); break;
204: case CFEPglib3_icosahedron:
205: glib3_icosahedron(0.0f, 0.0f, -1.0f, 1.0f); break;
206: case CFEPglib3_ray:
207: [self glib3_ray]; break;
208: case CFEPglib3_ray_init:
209: glib3_ray_init(); break;
210: case CFEPglib3_ray_reshape:
211: glib3_ray_reshape(x,y); break;
212: case CFEPglib3_std_scene0:
213: [self glib3_std_scene0]; break;
214: default:
215: NSLog(@"Unknown opCode %d\n",op);
216: break;
217: }
218: }
219:
220: -(void) glib3_ray {
221: glib3_ray_change_alpha(-90.0+xeye*5);
222: glib3_ray_change_beta(90.0+yeye*3);
223: glib3_ray();
224: }
225: -(void) glib3_std_scene0 {
226: glib3_std_scene0(xeye,yeye,zeye);
227: }
228:
229: @end
230:
231: // Original glib functions
232: void glib_line(float x,float y, float x2,float y2, int color) {
233: glColor4f((color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0,0.0);
234: // NSLog(@"color=%f,%f,%f\n",(color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0);
235: glBegin(GL_LINES);
236: glVertex2f(x,y);
237: glVertex2f(x2,y2);
238: glEnd();
239: }
240: void glib_putpixel(float x,float y,int color) {
241: // NSLog(@"color=%f,%f,%f\n",(color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0);
242: glBegin(GL_POINTS);
243: glColor4f((color/0x10000)/256.0,((color/0x100)& 0xff)/256.0,(color % 0x100)/256.0,0.0);
244: glVertex2f(x,y);
245: glEnd();
246: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>