PDA

View Full Version : Object Getting Stretched with glViewport / glScissor



joatmon
11-13-2013, 12:07 PM
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:

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:

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!


- (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);

}

GClements
11-14-2013, 12:05 AM
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.

joatmon
11-14-2013, 07:40 AM
Thank you so much! I didn't realize that I misunderstood that parameter.