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

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

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

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