7

I have a GLKView which pretty much displays the shape I want but there are a few things that still do not work as desired.

      3_______________________2
      |\                     /|
      | \_ _ _ _ _ _ _ _ _ _/ |
      | /4                 5\ |
      |/_____________________\|
      0                       1
  • texture mapping is working on the front-face, the other two faces do not really work, there are triangles lacking.

  • When I use colours, the faces seem to appear as desired, however the back faces do not join at the edge formed by the points 4 and 5 (look at the drawing I provided above).
    I want (if you look at it from the side) the three faces to shape an equilateral triangle.

I have temporarily commented out the texture mapping sections of the code so that you can test it If you like.

Here I set up my view (note my view's dimensions. The coordinate system works fine without fiddling around with low values. The faces look good, they just do not, as I said join at the back):

    EAGLContext * context = [[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES2];
    OpenGLShape *view = [[OpenGLShape alloc] initWithFrame:CGRectMake(0, 200, 320, 80) context:context];
    view.delegate = view;
    [view setupGL];
    [self.view addSubview:view];

- (void)setupGL {

    [EAGLContext setCurrentContext:self.myContext];

    //glEnable(GL_CULL_FACE);


    self.effect = [[GLKBaseEffect alloc] init];

    BOOL useTexture = NO;

    // Create default framebuffer object.
    glGenFramebuffers(1, &defaultFrameBuffer);
    glBindFramebuffer(GL_FRAMEBUFFER, defaultFrameBuffer);

    if(useTexture){
        NSDictionary * options = [NSDictionary dictionaryWithObjectsAndKeys:
                              [NSNumber numberWithBool:YES],
                              GLKTextureLoaderOriginBottomLeft,
                              nil];

        NSError * error;
        NSString *path = [[NSBundle mainBundle] pathForResource:@"blogcell@2x" ofType:@"png"];
        GLKTextureInfo * info = [GLKTextureLoader textureWithContentsOfFile:path options:options error:&error];
        if (info == nil) {
            NSLog(@"Error loading file: %@", [error localizedDescription]);
        }
        self.effect.texture2d0.name = info.name;
        self.effect.texture2d0.enabled = true;

        glGenBuffers(1, &texArray);
        glBindBuffer(GL_ARRAY_BUFFER, texArray);
        glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
        glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, 0,0);
        glBufferData(GL_ARRAY_BUFFER, sizeof(TexCoords), TexCoords, GL_STATIC_DRAW);


    }else{

        glGenBuffers(1, &colArray);
        glBindBuffer(GL_ARRAY_BUFFER, colArray);
        glEnableVertexAttribArray(GLKVertexAttribColor);
        glVertexAttribPointer(GLKVertexAttribColor, 4, GL_FLOAT, GL_FALSE, 0, 0);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Color), Color, GL_STATIC_DRAW);
    }

    glGenRenderbuffers(1, &depthBuffer);
    glBindRenderbuffer(GL_RENDERBUFFER, depthBuffer);
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, 320.0f, 80.0f);
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBuffer);

    glEnable(GL_DEPTH_TEST);

    glGenBuffers(1, &vertexArray);
    glBindBuffer(GL_ARRAY_BUFFER, vertexArray);
    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition,3,GL_FLOAT,GL_FALSE,0,0);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);




    self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(51.4f,4.0f, 0.1f, 10.75f);
   rotMatrix = GLKMatrix4Translate(self.effect.transform.modelviewMatrix,0, 0, -3);


}




- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {

    self.opaque = NO;
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    [self.effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, sizeof(Vertices));
}

red = front face (made up by points 0123)

blue = back face (top - made up by points 4523)

green = back face (bottom - made up by points 0154)

white = background

I have also made the faces semi-transparent (alpha = 0.5) for the sake of seeing whether they blend/cull etc.

Initial state (no rotation)

Slight rotation around x-axis

Almost full rotation around x-axis

the_critic
  • 12,720
  • 19
  • 67
  • 115
  • Have you considered Cocos2d? That can make working with OpenGL ES allot simpler and reduce problems like the one you are having. – Todd Moses Dec 31 '12 at 04:37
  • Yes I have been using cocos2d for the last two years, but I would really like to dive into OpenGL. The problem is, I predominantly learn by doing something myself and especially learn from the problems I make. So if anybody could state mistakes that I made I would be really happy. – the_critic Dec 31 '12 at 12:11
  • Could you elaborate on 'not working' ? Images ? – rotoglup Dec 31 '12 at 12:43
  • As I stated in my question the two problems are: 1) the textures do not map properly onto my faces and 2) the two back faces do not meet at the edge they are supposed to be meeting at a z-value of -1.73. I calculated it with the pythagorean theorem so that every face has the same edge length. – the_critic Dec 31 '12 at 13:19
  • I have little experience in OpenGL, so I do not even know if I set up the vertices correctly. If I only use colors (no texture mapping) then it seems fine (the back faces still do not meet in the back). But as soon as I use textures the shape seems to lack some triangles of the faces. I have implemented a bit of code that rotates the shape and whenever I rotate it I assume the culling effect screws something up. – the_critic Dec 31 '12 at 13:27
  • @rraallvv I have updated my question. Please take a look. Should you require any more resources/information please let me know. – the_critic Jan 02 '13 at 00:31
  • @MartinE. It could be that you have set the far clipping plane to 1.0, try modifying it to a value greater than 1.73 – rraallvv Jan 02 '13 at 00:53
  • thank you @rraallvv that seems totally logical. However, having messed around with the glFrustum and glViewport I am left with no viable solution. I have not got the necessary knowledge to create any useful code from here. I tried the following:`glViewport(0, 0, 320, 80);` and `glFrustumf(-1.0f, 1.0f, -1.0f, 1.0f, 2.0f, -2.0f);` to no avail... – the_critic Jan 02 '13 at 01:14
  • glOrtho(-1.0, 1.0, -1.0, 1.0, -2.0, 2.0); please see [this question](http://stackoverflow.com/questions/2571402/c-opengl-glortho-please-explain-the-usage-of-this-command) – rraallvv Jan 02 '13 at 01:35
  • @rraallvv Alright, it did not work. Where do I put this line anyway ? Does it matter where I put it ? I just get `GL ERROR: 0x0502` – the_critic Jan 02 '13 at 01:58
  • @MartinE. Please try `effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(left, right, bottom, top, -2.0, 2.0);` immediately after `self.effect = [[GLKBaseEffect alloc] init];` in your method setupGL. [This page](http://games.ianterrell.com/how-to-set-up-a-2d-scene-with-glkit/) has very compressible information, it is like the previous suggestion but using GLKit GLKBaseEffect – rraallvv Jan 02 '13 at 02:31
  • Brilliant it (almost) works now that I have included this line, thank you very much ! Textures also seem to work now. One thing I noticed, is that I don't see any difference in depth. So I want to do it with a frustum or perspective projection matrix. Again, I tried something like `self.effect.transform.projectionMatrix = GLKMatrix4MakeFrustum(-0.01,0.01,-0.01f,0.01f,0.05f,2.0f);` but this messes with my rotation point and generally does not seem like a nice solution. – the_critic Jan 02 '13 at 12:51
  • @rraallvv Well, I think `self.effect.transform.projectionMatrix = GLKMatrix4MakePerspective(45.0f, 4.0f, 0.01, 2.0f);` does what I am aiming for but I have no idea how to set it up correctly. The other faces now do not seem to be shown. – the_critic Jan 02 '13 at 14:19

2 Answers2

10

In OpenGL the camera is always at {0,0,0}, what changes position is the 3D world.

For an orthogonal projection the projection matrix is given by:

self.effect.transform.projectionMatrix = GLKMatrix4MakeOrtho(left, right, bottom, top, front, back);

enter image description here

Objects in an orthogonal projection look the same size when they move near or far the camera.

Objects in a perspective projection look smaller when they move far the camera.

enter image description here

For a perspective projection the projection matrix is given by:

self.effect.transform.projectionMatrix = GLKMatrix4 GLKMatrix4MakePerspective (fov, aspect, front, back);
);

For a perspective projection the field of view (fov) works like the one on a real camera, for small values the perspective is almost unnoticeable, for big values the distortion due to the perspective projection is big.

The fov angle is in radians, and can be tweaked until the scene looks right for your application

aspect is the screen aspect ratio between the horizontal and the vertical viewing area.

For practical reasons the fov is specified in degrees, so it can be used like follows

GLKMatrix4MakePerspective(fov * M_PI / 180.0f,
                                          screenWidth / screenHeight,
                                          front, back);

The near clipping plane needs to be greater than zero in a perspective projection, so you need to add a translation to the modelview matrix, that separates the world from the camara a little bit.

This translation is specified in the model view matrix

self.effect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0, 0, -frontClippingPlane);

Update

The clipping volume needs to include all vertices to avoid z-fighting with the front clipping plane, in which case the final translation in a perspective projection needs to be a little bit further

self.effect.transform.modelviewMatrix = GLKMatrix4MakeTranslation(0, 0, -frontClippingPlane - 0.1f);

For rotate the model and translate it to its final position you need to combine the two transformations as follows:

GLKMatrix4 rotation = GLKMatrix4MakeRotation(angle*M_PI/180, 1, 0, 0); // angle in degrees and x,y, and z coordinates for the rotation axis
GLKMatrix4 translation = GLKMatrix4MakeTranslation( 0, 0, -front - 0.1f);
self.effect.transform.modelviewMatrix = GLKMatrix4Multiply(rotation, translation); // the order matters

The following are the rotation axis and directions for OpenGL, which is right-handed

enter image description here

This is the final effect

enter image description here

rraallvv
  • 2,875
  • 6
  • 30
  • 67
  • Very good answer, thank you very much rraallvv. However now the front face is not there and if I turn off culling i only see the two back faces. I assume the translation of my model view matrix along the z-axis is not working as my perspective does not take the front face into account. Rotation is also awkward, as my shape is not revolving around the middle of my view. I really appreciate your answer and even though you helped me get nearer to my goal, it still looks really not at all how I intended it to be. – the_critic Jan 02 '13 at 17:12
  • Ok I messed with the model view matrix in my touchesMoved method, which basically did not then take into account the translation I had added before and thus the translation had not had any effect at all. So, now I see all the faces, however not in their entirety. So now it seems as if my front face is culled while the back faces are not really... Oh my god this thing is killing me ... – the_critic Jan 02 '13 at 17:39
  • Brilliant. Thank you so much. One more thing though: The faces that are at the back seem to be shown even though they are clearly behind other faces. How do I solve that ? – the_critic Jan 02 '13 at 19:19
  • When I enable culling - upon (almost) full rotation - all the faces vanish. Do I initially specify vertices with respect to their position , i.e. in a clockwise order if they are at the back, and thus invisible? – the_critic Jan 02 '13 at 19:26
  • @MartinE. glEnable(GL_DEPTH_TEST); prevents pixels with z value less than the current pixel in the screen to be rendered, you can enable depth testing in your setupGL method. Also [this](http://stackoverflow.com/questions/9831263/gl-cull-face-makes-all-objects-disappear) question can clarify what is happening by enabling GL_CULL_FACE – rraallvv Jan 02 '13 at 19:36
  • Thanks again. I do not know how to use this. First I did not have anything but `glEnable(GL_DEPTH_TEST);`. Then in my update method I clear both color and depth buffer `glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);`. And finally I added a frame and depth buffer, to no avail. The back faces still show through the front faces. – the_critic Jan 02 '13 at 20:03
  • @MartinE. I'm guessing `OpenGLShape` is subclassing `GLKView`, if so you can add `view.drawableDepthFormat = GLKViewDrawableDepthFormat16;` before `[self setupGL];` and remove the `depthBuffer` created "by hand" – rraallvv Jan 02 '13 at 21:14
  • As soon as I am able to award the bounty (in 2 hours' time) I will award it to you :). Thanks again – the_critic Jan 02 '13 at 21:25
  • @MartinE: Don't forget the bounty ;) – Goz Jan 08 '13 at 15:19
  • @MartinE: No you haven't. You've accepted his answer. To award the bounty click the "+50" underneath the acceptance tick. – Goz Jan 09 '13 at 22:38
  • Are you sure ? This is a screenshot from my reputation "timeline" http://i45.tinypic.com/kanh5.png . I have already once clicked the +50 button on his answer, I am 99% sure... – the_critic Jan 09 '13 at 23:35
0

Is your data correct? I don't see how the Vertices array is filled.

The winding on your vertices could be a problem here, if your data is as you list it eg red front face 0123 is in that order.

The default winding is normally CCW, and you have described your rects as counter-clockwise, which need to break down into GL_TRIANGLES correctly: eg 0123 makes 013, and 123.

But if your diagram is correct then whereas the others are facing out, the red rectangle will be facing into the triangular prism formed by the 3 rectangular sides.

To see what I mean fold a sheet of paper with 2 horizontal folds to form a triangular prism, tape it along the join, and number the vertices.

If you have only textures defined for the front face when you go into "texture mode" you might lose that face.

Sez
  • 1,275
  • 11
  • 26