Question

I have recently started a new iOS project based off of the OpenGL sample. I have added my own camera movement code, and I have added an NSMutableArray that contains instances of Blocks (currently only contain 3D position). I have modified the drawing code do draw instances of the cube model included with the sample from this array.

When I navigate the camera all of the cubes behave as I expect (they all stay in their original places, but I move the camera around them), except one cube seems to slide a little apart from the others when the camera changes. Whenever I move the camera (position or rotation) this block does stay in the same general location, but it slides slightly in the opposite direction of the camera movement. When the camera is stationary it is perfectly where it should be.

This block always seems to be the last one drawn. If I add a conditional to skip that block it picks a different one.

I've gone through my code over and over again, and I can't see why one block should behave differently than the others.

Here is all of the relevant code:

    ViewMatrix = GLKMatrix4MakeRotation(CameraRotation.y, 1.0f, 0.0f, 0.0f);
ViewMatrix = GLKMatrix4Rotate(ViewMatrix, CameraRotation.x, 0.0f, 1.0f, 0.0f);
ViewMatrix = GLKMatrix4TranslateWithVector3(ViewMatrix, CameraPosition);
ViewMatrix = GLKMatrix4Translate(ViewMatrix, 0.0f, -1.5f, 0.0f);

glBindVertexArrayOES(_vertexArray);


for (int i = 0; i < blocks.count; i++){
    Block *b = [blocks objectAtIndex:i];

    if (true){

        [self.effect prepareToDraw];

        GLKMatrix4 ModelViewMatrix = GLKMatrix4MakeTranslation(b.position.x, b.position.y, b.position.z);

        ModelViewMatrix = GLKMatrix4Multiply(ViewMatrix, ModelViewMatrix);

        self.effect.transform.modelviewMatrix = ModelViewMatrix;


        glDrawArrays(GL_TRIANGLES, 0, 36);
    }
}

As you can see, all of the blocks are drawn with the exact same code. Why would one behave differently?

Was it helpful?

Solution

The answer to your problem is simple, you should call prepareToDraw after you set the effect. You should always first configure the effect and than call prepareToDraw. So just move the [self.effect prepareToDraw] right before glDrawArrays(..). Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top