Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Object Getting Stretched with glViewport / glScissor

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    8

    Object Getting Stretched with glViewport / glScissor

    I'm writing an iPad app where I have the screen split into four quadrants (just working on the upper left quadrant at the moment). For now, I'm drawing a simple cube. When I drew this prior to using glViewport (i.e. in the center of the screen), it had perfect proportions, albeit in the wrong place on the screen:

    Click image for larger version. 

Name:	Screen Shot 2013-11-13 at 1.03.41 PM.jpg 
Views:	62 
Size:	8.4 KB 
ID:	1183

    But then I moved on to trying to project his image in the upper left quadrant. As soon as I did that, the cube gets stretched vertically. It's in basically the right place, but wrong proportions:

    Click image for larger version. 

Name:	Screen Shot 2013-11-13 at 1.02.50 PM.jpg 
Views:	69 
Size:	8.3 KB 
ID:	1182

    Here is the code. Can anyone help me to get this back to square? I tried scaling it by the aspect ratio. That helped, but wasn't correct. I don't want to guess by trial-and-error, so I'm looking for a correct approach. Thank you!

    Code :
    - (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
    {  
        glClearColor(0.65f, 0.65f, 0.65f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
        aspect = fabsf(self.view.bounds.size.width / self.view.bounds.size.height); //1024 x 768
     
        // localize the output to the upper left quadrant of the screen
        // IF I COMMENT OUT THESE NEXT THREE LINES, THE CUBE DRAWS WITH CORRECT PORPORTIONS
        glViewport(0, FRAME_HEIGHT/2, FRAME_WIDTH/2, FRAME_HEIGHT);
        glScissor(0, FRAME_HEIGHT/2, FRAME_WIDTH/2, FRAME_HEIGHT);
        glEnable(GL_SCISSOR_TEST);
     
        glClearColor(0.3, 0.3, 0.3, 1.0);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);    
     
        projectionMatrix = GLKMatrix4MakePerspective(GLKMathDegreesToRadians(65.0f), aspect, 0.1f, 10.0f);
        self.effect.transform.projectionMatrix = projectionMatrix;
     
        self.effect.transform.modelviewMatrix = modelViewMatrix;
     
        glUseProgram(_program1);
     
        modelViewMatrix =  GLKMatrix4MakeTranslation(_model_translate_x, _model_translate_y, _model_translate_z); // this basically initializes MMV to identity
        _modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
     
        modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_model_rot_x), 1.0f, 0.0f, 0.0f);
        modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_model_rot_y), 0.0f, 1.0f, 0.0f);
        modelViewMatrix = GLKMatrix4Rotate(modelViewMatrix, GLKMathDegreesToRadians(_model_rot_z), 0.0f, 0.0f, 1.0f);
     
        _normalMatrix = GLKMatrix3InvertAndTranspose(GLKMatrix4GetMatrix3(modelViewMatrix), NULL); 
        _modelViewProjectionMatrix = GLKMatrix4Multiply(projectionMatrix, modelViewMatrix);
     
        glBindVertexArrayOES( [my_cube getVertexArray] ); // make the cube the current object
     
        glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX1], 1, 0, _modelViewProjectionMatrix.m);
        glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX1], 1, 0, _normalMatrix.m);
     
        // Draw the 3D cube
        glDrawArrays(GL_TRIANGLES, 0, 36);
     
    }

  2. #2
    Member Regular Contributor
    Join Date
    Jun 2013
    Posts
    490
    Quote Originally Posted by joatmon View Post
    Code :
        glViewport(0, FRAME_HEIGHT/2, FRAME_WIDTH/2, FRAME_HEIGHT);
        glScissor(0, FRAME_HEIGHT/2, FRAME_WIDTH/2, FRAME_HEIGHT);
    In both cases, the last parameter should be FRAME_HEIGHT/2. The parameters are x,y,width,height, not left,bottom,right,top.

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2013
    Posts
    8
    Thank you so much! I didn't realize that I misunderstood that parameter.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •