OPENGL-ES의 교점 인덱스 드로잉

2289 단어 OPENGL-ES
OpenGLES를 배울 때 색인 캐시라는 개념을 만났습니다.인터넷에서 자료를 찾아보면 대부분의 코드가 안드로이드나 데스크톱 플랫폼을 겨냥한 것이고 대부분의 코드를 복사하면 효과가 없다.며칠 동안의 노력을 통해 색인 그림의 개념을 알게 되었다. 이른바 색인 그림은 큰 격자(mesh)를 그릴 때 효율적으로 그림을 그릴 수 있는 방식이다. 일반적인 삼각형을 그릴 때 각 삼각형이 Array 에 있어야 한다.버퍼 안에 세 개의 정점의 위치를 분배하고 각 정점마다 최소한sizeof(glfloat)*3의 메모리가 필요합니다. 사실 격자 안의 많은 정점은 공유됩니다. 즉, 중복된 정점에 메모리를 분배할 필요가 없고 gpu에 그림을 그릴 때 어떤 점을 그릴 것인지 지정하는 방법을 찾아야 합니다. 이것이 바로 색인 그림의 용도입니다.색인 드로잉 사용, array버퍼 안의 정점은 기본적으로 정점이 정점 수조 안의 위치에 따라 색인을 설정합니다. 우리는 그림을 그리기 전에 gpu 그림의 색인 순서를 지정하고, 그림을 그릴 때gldrawelement () 를 호출하면 gpu는 우리가 지정한 정점 순서에 따라 그림을 그릴 수 있습니다.다음은 코드 세션입니다. 간단하게 ViewController를 GLKViewController로 설정하고 self를 설정합니다.뷰가 GLKView인 경우 실행 가능
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, // Red
      1.0f, // Green
      1.0f, // Blue
      1.0f);// Alpha
   
   // 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 to be 
   // stored in GPU memory
   glGenBuffers(1,                // STEP 1
      &vertexBufferID);
   glBindBuffer(GL_ARRAY_BUFFER,  // STEP 2
      vertexBufferID); 
   glBufferData(                  // STEP 3
      GL_ARRAY_BUFFER,  // Initialize buffer contents
      sizeof(vertices), // Number of bytes to copy
      vertices,         // Address of bytes to copy
      GL_STATIC_DRAW);  // Hint: cache in GPU memory
    
    GLuint index;
    glGenBuffers(1, &index);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexs), indexs, GL_STATIC_DRAW);

좋은 웹페이지 즐겨찾기