Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 12th, 2012  |  syntax: None  |  size: 2.42 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I draw thousands of squares with glkit, opengl es2?
  2. - (void)glkViewControllerUpdate:(GLKViewController *)controller
  3. {
  4.  
  5. //static float transY = 0.0f;
  6. //float y = sinf(transY)/2.0f;
  7. //transY += 0.175f;
  8.  
  9. GLKMatrix4 modelview = GLKMatrix4MakeTranslation(0, 0, -5.f);
  10. effect.transform.modelviewMatrix = modelview;
  11.  
  12. //GLfloat ratio = self.view.bounds.size.width/self.view.bounds.size.height;
  13. GLKMatrix4 projection = GLKMatrix4MakeOrtho(0, 768, 1024, 0, 0.1f, 20.0f);    
  14. effect.transform.projectionMatrix = projection;
  15. _isOpenGLViewReady = YES;
  16. }
  17.  
  18. - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
  19. {
  20. if(_model.updateView && _isOpenGLViewReady)
  21. {
  22.  
  23.     glClear(GL_COLOR_BUFFER_BIT);
  24.     [effect prepareToDraw];
  25.     int pixelSize = _model.pixelSize;
  26.     if(!_model.isReady)
  27.         return;
  28.     //NSLog(@"UPDATING: %d, %d", _model.rows, _model.columns);
  29.     for(int i = 0; i < _model.rows; i++)
  30.     {
  31.         for(int ii = 0; ii < _model.columns; ii++)
  32.         {
  33.             ColorModel *color = [_model getColorAtRow:i andColumn:ii];
  34.             CGRect rect = CGRectMake(ii * pixelSize, i*pixelSize, pixelSize, pixelSize);
  35.             //[self drawRectWithRect:rect withColor:c];
  36.             GLubyte squareColors[] = {
  37.                 color.red, color.green, color.blue, 255,
  38.                 color.red, color.green, color.blue, 255,
  39.                 color.red, color.green, color.blue, 255,
  40.                 color.red, color.green, color.blue, 255
  41.             };
  42.  
  43.             // NSLog(@"Drawing color with red: %d", color.red);
  44.  
  45.  
  46.             int xVal = rect.origin.x;
  47.             int yVal = rect.origin.y;
  48.             int width = rect.size.width;
  49.             int height = rect.size.height;
  50.             GLfloat squareVertices[] = {
  51.                 xVal, yVal, 1,
  52.                 xVal + width, yVal, 1,
  53.                 xVal,  yVal + height, 1,
  54.                 xVal + width,  yVal + height, 1
  55.             };    
  56.  
  57.             glEnableVertexAttribArray(GLKVertexAttribPosition);
  58.             glEnableVertexAttribArray(GLKVertexAttribColor);
  59.  
  60.             glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, squareVertices);
  61.             glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, squareColors);
  62.  
  63.             glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
  64.  
  65.             glDisableVertexAttribArray(GLKVertexAttribPosition);
  66.             glDisableVertexAttribArray(GLKVertexAttribColor);
  67.  
  68.  
  69.         }
  70.     }  
  71.     _model.updateView = YES;
  72. }