- How do I draw thousands of squares with glkit, opengl es2?
- - (void)glkViewControllerUpdate:(GLKViewController *)controller
- {
- //static float transY = 0.0f;
- //float y = sinf(transY)/2.0f;
- //transY += 0.175f;
- GLKMatrix4 modelview = GLKMatrix4MakeTranslation(0, 0, -5.f);
- effect.transform.modelviewMatrix = modelview;
- //GLfloat ratio = self.view.bounds.size.width/self.view.bounds.size.height;
- GLKMatrix4 projection = GLKMatrix4MakeOrtho(0, 768, 1024, 0, 0.1f, 20.0f);
- effect.transform.projectionMatrix = projection;
- _isOpenGLViewReady = YES;
- }
- - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
- {
- if(_model.updateView && _isOpenGLViewReady)
- {
- glClear(GL_COLOR_BUFFER_BIT);
- [effect prepareToDraw];
- int pixelSize = _model.pixelSize;
- if(!_model.isReady)
- return;
- //NSLog(@"UPDATING: %d, %d", _model.rows, _model.columns);
- for(int i = 0; i < _model.rows; i++)
- {
- for(int ii = 0; ii < _model.columns; ii++)
- {
- ColorModel *color = [_model getColorAtRow:i andColumn:ii];
- CGRect rect = CGRectMake(ii * pixelSize, i*pixelSize, pixelSize, pixelSize);
- //[self drawRectWithRect:rect withColor:c];
- GLubyte squareColors[] = {
- color.red, color.green, color.blue, 255,
- color.red, color.green, color.blue, 255,
- color.red, color.green, color.blue, 255,
- color.red, color.green, color.blue, 255
- };
- // NSLog(@"Drawing color with red: %d", color.red);
- int xVal = rect.origin.x;
- int yVal = rect.origin.y;
- int width = rect.size.width;
- int height = rect.size.height;
- GLfloat squareVertices[] = {
- xVal, yVal, 1,
- xVal + width, yVal, 1,
- xVal, yVal + height, 1,
- xVal + width, yVal + height, 1
- };
- glEnableVertexAttribArray(GLKVertexAttribPosition);
- glEnableVertexAttribArray(GLKVertexAttribColor);
- glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 0, squareVertices);
- glVertexAttribPointer(GLKVertexAttribColor, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, squareColors);
- glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
- glDisableVertexAttribArray(GLKVertexAttribPosition);
- glDisableVertexAttribArray(GLKVertexAttribColor);
- }
- }
- _model.updateView = YES;
- }