[BACK]Return to MyOpenGLView.m CVS log [TXT][DIR] Up to [local] / OpenXM / src / cfep

Annotation of OpenXM/src/cfep/MyOpenGLView.m, Revision 1.7

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>