0

I'm trying to make a OpenGL project to iOS but glViewport can`t work on it.

Look the simple code example:

- (void)viewDidLoad
{

    [super viewDidLoad];

    EAGLContext *context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    [EAGLContext setCurrentContext:context];

    const CGRect frame = [[UIScreen mainScreen] bounds];

    //GLKView *glView = [[GLKView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
    GLKView* glView = [[GLKView alloc] initWithFrame:frame context:context];
    [self.view addSubview:glView];

    GLuint renderbuffer;
    glGenRenderbuffers(1, &renderbuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, renderbuffer);
    [context renderbufferStorage:GL_RENDERBUFFER fromDrawable:(CAEAGLLayer*)glView.layer];

    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderbuffer);

    // Set the viewport 
    glViewport(100, 100, 300, 300);

    // Clear
    glClearColor(1, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);

    // Present renderbuffer
    [context presentRenderbuffer:GL_RENDERBUFFER];
}

Any changes I do in glViewport don`t make any effects.

But if I add "initWithFrame:CGRectMake(100, 100, 300, 300)" it will work, but I really need glViewport.

What am I doing wrong?

rmaddy
  • 314,917
  • 42
  • 532
  • 579

2 Answers2

1

I made more researches and tested that glViewport should be called on every frame when use GLKView to create OpenGL environment.

To see glViewport working as usual is needed to use pure EAGLView.

I found the answered in this thread: glViewport different result in Android and iOS

Community
  • 1
  • 1
0

glClear does not respect the current viewport. If you want to select a sub-region to clear, you need to set the scissor box with glScissor, then enable scissoring with glEnable(GL_SCISSOR_TEST).

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
  • Thank you, it worked to what I need. But what is the real propose of glViewport? If I comment it nothing will change in screen. – Eduardo Dória Mar 20 '16 at 17:52
  • @EduardoDória: `glViewport` affects primitives you render: triangles, lines, etc. Clearing operations don't render primitives. – Nicol Bolas Mar 20 '16 at 18:48