Search code examples
iosopengl-es-2.0glkit

Changing the camera position with openGL ES


I have an iOS simple project where I display a shape. So far so good. Now I am trying to change the camera position so that my object is viewed in isometric perspective. I cannot figure out how to move/rotate the camera with OpenGL-ES 2.x.

Of course, I could rotate the object itself to make it seen from an isometric perspective, but that hardly seems like the proper design.

Does anyone have a snippet of Objective-C code to share to help me position this camera correctly?

UPDATE USING THE HINTS IN THE RESPONSES:

I used Cocos3D to help with the matrix calculations

CC3GLMatrix *camera = [CC3GLMatrix matrix];
[camera populateFromTranslation:CC3VectorMake(0, 0, 0)];
[camera rotateBy:CC3VectorMake(30, 0, 0)];
[camera rotateBy:CC3VectorMake(0, 45, 0)];  
glUniformMatrix4fv(_cameraUniform, 1, 0, camera.glMatrix);

Then added the camera in the vertex

gl_Position = Projection * Modelview * Camera * Position;

Solution

  • OpenGL ES, even more so as of 2.x, doesn't have any concept of objects versus worlds versus cameras versus anything. In broad terms, it has a stream of input vertices, arranged in some way to make geometry, which are processed individually according to some uniform state, then it works out which fragments will need painting and individually performs a calculation to determine the output colour per fragment.

    You supply:

    • vertices
    • uniforms
    • the vertex shader
    • the fragment shader

    Usually people have the vertices describe locations relative to the centre of a model.

    They have some state that describes where that model is in the world, some other state that describes where the camera is in that world. They supply that information as uniforms, usually combined into a single matrix.

    Then they'll have a vertex shader that, more or less, just applies the uniform to the vertex.

    Finally the fragment shader will sample some textures or whatever is necessary to paint that face.

    So in your case you would specify a different transform for the object that made it look isometric. The reason that isn't bad design as your second paragraph queries is that you'd compute that transform as a final step in sending state to OpenGL, taking into account the two separate inputs. So you're keeping the two things separately in your program, then combining them into one just for OpenGL.