4

I've managed to create a single mesh in Open GL ES 2.0 using iOS's GLKit. What I can't figure out is how to create a second mesh that looks identical to the first except with a different position.

I think what would be most helpful would be if someone could simply provide some sample code that draws multiple meshes, as I think I'm going about this entirely wrong. Nonetheless, here's a list of what I've done to try make this work.

  • I created two C-arrays of vertex information. The first is my template (hardcoded coordinates centered at 0,0,0) and the second is what I'd like displayed (initially empty).
  • I created two C-arrays of indice information. Same deal as with the vertex information.
  • I have two global variable ints, vertexCount and indiceCount, both initially 0.
  • I have a c function, createAt(float x, float y, float z). This function copies my vertex template into my vertex display array and then applies the passed in offsets to each vertex. It also copies indices in and applies an offset so that they point at the new display vertexes.

This doesn't work though. Only one object gets drawn on screen.

Looking through other people's questions and answers, it sounds like maybe I should have multiple vertex buffers? I don't know; I'm horribly confused.

pasawaya
  • 11,515
  • 7
  • 53
  • 92
ArtOfWarfare
  • 20,617
  • 19
  • 137
  • 193
  • As a follow up on this, I ultimately gave up on figuring out the mess that is OpenGL/OpenGL ES and instead used Ogre 3D + Blender to make my game. Much faster and easier... Ogre 3D is a general 3D engine, it's not a game engine meaning it's more than flexible enough for anyone's 3D needs. I strongly recommend it for anyone currently (mistakenly) looking at OpenGL. Yes, game engines are made from it, no, you shouldn't spend the next few years figuring out how if you want to make your game in this lifetime. – ArtOfWarfare Aug 17 '12 at 14:01
  • As a further update, I'm actually using Unity3D now, even for things which wouldn't be called games typically (IE, I have a specialized 3D modeling program I made in Unity3D). – ArtOfWarfare Oct 15 '14 at 11:50

3 Answers3

5

Rendering multiple meshes with the same geometry but different positions or orientations is quite easy with GLKit, thanks to the GLKBaseEffect class. Take a look at the "OpenGL Game" template provided by Xcode, which renders a cube with GLKit and another cube with OpenGL ES 2.0.

Here's a modification of the OpenGL Game sample which adds a new cube above the two spinning ones:

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

    glBindVertexArrayOES(_vertexArray);

    // Render the object with GLKit
    [self.effect prepareToDraw];

    glDrawArrays(GL_TRIANGLES, 0, 36);

    // Render another version of the object with GLKit

    // First put it in a different place (you would normally
    // do this in the update loop):
    GLKMatrix4 baseModelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 0.0f, -4.0f);
    baseModelViewMatrix = GLKMatrix4Rotate(baseModelViewMatrix, _rotation, 0.0f, 1.0f, 0.0f);    
    GLKMatrix4 modelViewMatrix = GLKMatrix4MakeTranslation(0.0f, 1.5, -0.0f);
    modelViewMatrix = GLKMatrix4Multiply(baseModelViewMatrix, modelViewMatrix);
    self.effect.transform.modelviewMatrix = modelViewMatrix;

    // Now render it:
    [self.effect prepareToDraw];
    glDrawArrays(GL_TRIANGLES, 0, 36);

    // Render the object again with ES2
    glUseProgram(_program);

    glUniformMatrix4fv(uniforms[UNIFORM_MODELVIEWPROJECTION_MATRIX], 1, 0, _modelViewProjectionMatrix.m);
    glUniformMatrix3fv(uniforms[UNIFORM_NORMAL_MATRIX], 1, 0, _normalMatrix.m);

    glDrawArrays(GL_TRIANGLES, 0, 36);
}

The code in-between the "Render another version" and "Render the object again" comments is new. The first thing it needs to do is come up with a new modelview matrix for the additional cube; this is based on the code in update: (not shown). Normally I'd put the code up there but I wanted to keep the sample short.

Next it renders the cube, which is pretty simple with GLKit. Yay GLKit!

Screenshot of three cubes in Apple's GLKit / OpenGL Game sample

I hope this helps. It sounds like you're barking up the wrong tree with additional vertex and index arrays; this is necessary if you want to render different geometries, but you said the same model in different places. That's something GLKit is ideal for.

jblocksom
  • 14,127
  • 5
  • 34
  • 30
  • Wow. I can't believe Apple had sample code for exactly what I needed hidden as a template. Thank you for your additions, they should help with figuring out what everything does. – ArtOfWarfare Feb 07 '12 at 03:12
  • 1
    Should you not be running those additions in your update method rather than the draw method? – ingh.am Mar 17 '12 at 21:12
  • How will the update: look like if you move these additions there? how to handle different updates to different draw calls? – Bach Apr 28 '13 at 12:28
  • By the looks of it all the code which sets up the modelView matrix and applies it to the GLKBaseEffect will be in update:. – JonEasy Jan 13 '14 at 16:31
0

Since there is only 1 base effect, I take it you would do the matrix calculations in the update, and just assign it to the base effect modelviewmatrix before drawing again?

Baxpin
  • 65
  • 7
0

John Riselvato comments above that he really wishes he could see the update: method contents. I really wished to see this myself as I was having difficulty wrapping my head around putting multiple objects on screen, while using the same GLKBaseEffect object. Here's how I finally did it:

// update method where I create 3 different modelViewMatrices to use with my GLKBaseEffect object later in the draw method
- (void)updateWithDeltaTime:(NSTimeInterval)deltaTime
{
    _rotation1 += 120.0 * deltaTime;
    _rotation2 += 150.0 * deltaTime;
    _rotation3 += 180.0 * deltaTime;

    GLKMatrix4 modelViewMatrix = GLKMatrix4Identity;
    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, -1.0f, 0.0f, -4.0f);
    modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
    modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation1));
    _modelViewMatrix1 = modelViewMatrix;

    modelViewMatrix = GLKMatrix4Identity;
    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 0.0f, 0.0f, -4.0f);
    modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
    modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation2));
    _modelViewMatrix2 = modelViewMatrix;

    modelViewMatrix = GLKMatrix4Identity;
    modelViewMatrix = GLKMatrix4Translate(modelViewMatrix, 1.0f, 0.0f, -4.0f);
    modelViewMatrix = GLKMatrix4RotateY(modelViewMatrix, GLKMathDegreesToRadians(90.0f));
    modelViewMatrix = GLKMatrix4RotateZ(modelViewMatrix, GLKMathDegreesToRadians(_rotation3));
    _modelViewMatrix3 = modelViewMatrix;
}

// draw method where I re-use my GLKBaseEffect object, applying the 3 modelViewMatrices I set in the update: method above
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    _effect.transform.modelviewMatrix = _modelViewMatrix1;
    [_effect prepareToDraw];
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer1);
    glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);

    _effect.transform.modelviewMatrix = _modelViewMatrix2;
    [_effect prepareToDraw];
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer2);
    glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);

    _effect.transform.modelviewMatrix = _modelViewMatrix3;
    [_effect prepareToDraw];
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer3);
    glDrawArrays(GL_TRIANGLES, 0, cylinderVertices);
}

// how i initially setup my GLKBaseEffect object and my opengl buffers
- (void)_setupGL
{
    [EAGLContext setCurrentContext:_context];
    //glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);

    _effect = [[GLKBaseEffect alloc] init];
    [self _createEffect];
    [_effect prepareToDraw];

    glGenBuffers(1, &_vertexBuffer1);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer1);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &_vertexBuffer2);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer2);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &_vertexBuffer3);
    glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer3);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(GLKVertexAttribPosition);
    glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)offsetof(Vertex, Position));
    glEnableVertexAttribArray(GLKVertexAttribTexCoord0);
    glVertexAttribPointer(GLKVertexAttribTexCoord0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)offsetof(Vertex, Texel));
    glEnableVertexAttribArray(GLKVertexAttribNormal);
    glVertexAttribPointer(GLKVertexAttribNormal, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (const GLvoid *)offsetof(Vertex, Normal));
}

Hope this helps someone.

markckim
  • 499
  • 5
  • 15