0 Replies
      Latest reply on Jan 18, 2020 10:11 PM by lslboyhh
      lslboyhh Level 1 Level 1 (0 points)

        Hi:

           I am studying  OpenGL on macosx and studying OpenGLES on iOS, on the iOS, I can use the GLKBaseEffect to develop a OpenGLES app and It worked very well, here is my code which for iOS platform.

         

        #import "ViewController.h"
        
        @interface ViewController ()
        
        @end
        
        @implementation ViewController
        @synthesize baseEffect;
        
        //////////////////////////////////
        // This data type is used to store information for each vertex
        typedef struct
        {
          GLKVector3 positionCoords;
        }
        SceneVertex;
        
        static const SceneVertex vertices[] =
        {
          {{-0.5f, -0.5f, 0.0}},  // lower left corner
          {{0.5f, -0.5f, 0.0}},   // lower right corner
          {{-0.5f, 0.5f, 0.0}}    // upper left corner
        };
        
        //////////////////////////////////////////////////////
        // Called when the view controller's view is loaded
        // Perform initialization before the view is asked to draw
        - (void)viewDidLoad {
          [super viewDidLoad];
        
          // Verify the type  of view  created automatically by the
          // Interface Builder storyboard
          GLKView *view = (GLKView *)self.view;
          NSAssert([view isKindOfClass:[GLKView class]], @"View controller's view is not a GLKView");
        
          // Create an OpenGL ES 2.0 context and provide it to the
          // view
          view.context = [[EAGLContext alloc]initWithAPI:kEAGLRenderingAPIOpenGLES2];
        
          // Make the new context current
          [EAGLContext setCurrentContext:view.context];
        
          // Create a base effect that provides standard OpenGL ES 2.0
          // Shading Language programs and set constants to be used for
          // all subsequent rendering
          self.baseEffect = [[GLKBaseEffect alloc]init];
          self.baseEffect.useConstantColor = GL_TRUE;
          self.baseEffect.constantColor = GLKVector4Make(1.0f, 1.0f, 1.0f, 1.0f);
          // Set the background color stored in the current context;
          glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  // background color
        
          // Generate, bind , and initialize contents of a buffer
          // stored in GPU memory
          glGenBuffers(1,     // STEP 1
          &vertexBufferID);
          glBindBuffer(GL_ARRAY_BUFFER,   // STEP 2
          vertexBufferID);
          glBufferData(         // STEP 3
          GL_ARRAY_BUFFER,    // Initialize buffer contentw
          sizeof(vertices),   // Number of bytes to copy
          vertices,              // Address of bytes to copy
          GL_STATIC_DRAW);    // Hint: cache in GPU memory
        }
        ///////////////////////////////////////////////////////
        // GLKView delegate method: Called by the view controller's view
        // whenever Cocoa Touch asks the view controller;s view to
        // draw itself. (In this case, render into a frame buffer that
        // shares memory with a Core Animation Layer)
        -(void)glkView:(GLKView *)view drawInRect:(CGRect)rect{
          [self.baseEffect prepareToDraw];
          // Clear Frame  Buffer (erase previous drawing)
          glClear(GL_COLOR_BUFFER_BIT);
        
          // Enable use if position from bound vertex buffer
          glEnableVertexAttribArray(      // STEP 4
            GLKVertexAttribPosition);
        
          glVertexAttribPointer(    // STEP 5
            GLKVertexAttribPosition,
            3,                //   three components per vertex
            GL_FLOAT,     // data is floating point
            GL_FALSE,     // no fixex point scalling
            sizeof(SceneVertex),      // no gaps in data
            NULL);                    // NULL tells GPU to start at beginning of bound buffer
        
          // Draw triangles using the first three vertices in the
          // currently bound vertex buffer
          glDrawArrays(GL_TRIANGLES,   // STEP 6
          0,       // Start with first vertex in currently bound buffer
          3);     // Use three vertices from currently bound buffer
        }
        

         

        so I tried use the GLKBaseEffect to render the macosx OpenGL app,here is the code for macosx platform:

         

        //
        //  MyOpenGLView.h
        //  OpenGLES_Ch6_1_MacOSX
        //
        //  Created by LEE on 1/16/20.
        //  Copyright © 2020 LEE. All rights reserved.
        //
        
        #import <Cocoa/Cocoa.h>
        #import <GLKit/GLKit.h>
        
        @class AGLKVertexAttribArrayBuffer;
        NS_ASSUME_NONNULL_BEGIN
        
        @interface MyOpenGLView : NSOpenGLView
        
        @end
        
        NS_ASSUME_NONNULL_END
        
        

         

        //
        //  MyOpenGLView.m
        //  OpenGLES_Ch6_1_MacOSX
        //
        //  Created by LEE on 1/16/20.
        //  Copyright © 2020 LEE. All rights reserved.
        //
        
        #import "MyOpenGLView.h"
        #import <OpenGL/gl.h>
        #import "SceneCarModel.h"
        #import "SceneRinkModel.h"
        @interface MyOpenGLView ()
        
        {
        
          GLuint _program;
        
          GLuint vertexBufferID;
        
        }
        
        
        @property (strong,nonatomic) GLKBaseEffect *baseEffect;
        @end
        @implementation MyOpenGLView
        @synthesize baseEffect;
        
        //////////////////////////////////////////////////////////////////
        // GLSL program uniform indices.
        enum{
          UNIFORM_MODELVIEWPROJECTION_MATRIX,
          USECONSTANTCOLOR,
          NUM_UNIFORMS
        };
        
        ////////////////////////////////////////////////////////////////
        // GLSL program uniform IDs.
        GLint uniforms[NUM_UNIFORMS];
        
        typedef struct
        {
          GLKVector3 positionCoords;
        }
        SceneVertex;
        
        static const SceneVertex vertices[] =
        {
          {{-0.5f, -0.5f, 0.0}},  // lower left corner
          {{0.5f, -0.5f, 0.0}},   // lower right corner
          {{-0.5f, 0.5f, 0.0}}    // upper left corner
        };
        
        
        
        -(instancetype)initWithCoder:(NSCoder *)coder{
          if (self = [super initWithCoder:coder]) {
          [self.openGLContext makeCurrentContext];
          self.baseEffect = [[GLKBaseEffect alloc]init];
          self.baseEffect.useConstantColor = GL_TRUE;
          self.baseEffect.constantColor = GLKVector4Make(0.0f, 1.0f, 1.0f, 1.0f);
          // Create a base effect that provides standard OpenGL ES 2.0
          // Shading Language programs and set constants to be used for
          // all subsequent rendering
          // Set the background color stored in the current context;
          glClearColor(0.0f, 0.0f, 0.0f, 1.0f);  // background color
        
          // Generate, bind , and initialize contents of a buffer
          // stored in GPU memory
          glGenBuffers(1,     // STEP 1
          &vertexBufferID);
          glBindBuffer(GL_ARRAY_BUFFER,   // STEP 2
          vertexBufferID);
          glBufferData(         // STEP 3
          GL_ARRAY_BUFFER,    // Initialize buffer contentw
          sizeof(vertices),   // Number of bytes to copy
          vertices,              // Address of bytes to copy
          GL_STATIC_DRAW);    // Hint: cache in GPU memory
        
          }
          return self;
        }
        
        
        
        -(void)drawRect:(NSRect)dirtyRect{
          [super drawRect:dirtyRect];
        
          // glFinish();
        
        }
        
        
        -(void)update{
        // [super update];
          [self.baseEffect prepareToDraw];
          // Clear Frame  Buffer (erase previous drawing)
          glClear(GL_COLOR_BUFFER_BIT);
        
        
          // Enable use if position from bound vertex buffer
          glEnableVertexAttribArray(      // STEP 4
            GLKVertexAttribPosition);
        
          glVertexAttribPointer(    // STEP 5
            GLKVertexAttribPosition,
            3,                //   three components per vertex
            GL_FLOAT,     // data is floating point
            GL_FALSE,     // no fixex point scalling
            sizeof(SceneVertex),      // no gaps in data
            NULL);                    // NULL tells GPU to start at beginning of bound buffer
        
        
          // Draw triangles using the first three vertices in the
          // currently bound vertex buffer
          glDrawArrays(GL_TRIANGLES,   // STEP 6
          0,       // Start with first vertex in currently bound buffer
          3);     // Use three vertices from currently bound buffer
          glFinish();
        
        }
        
        @end
        

         

        it's seems the GLKBaseEffect not work on the macosx OpenGL app, I don't why! could you give me some suggestion, thanks a lot!