我已经设法使用iOS的GLKit在Open GL ES 2.0中创建单个网格。我无法弄清楚的是如何创建第二个网格,看起来与第一个网格相同,但位置不同。
我认为最有帮助的是,如果有人可以简单地提供一些绘制多个网格的示例代码,我认为我认为这完全错了。尽管如此,这里列出了我为完成这项工作所做的工作。
但这不起作用。屏幕上只绘制了一个对象。
通过查看其他人的问题和答案,听起来好像我应该有多个顶点缓冲区?我不知道;我非常困惑。
答案 0 :(得分:5)
借助GLKBaseEffect类,使用GLKit渲染具有相同几何但不同位置或方向的多个网格非常容易。看看Xcode提供的“OpenGL Game”模板,它使用GLKit渲染一个立方体,使用OpenGL ES 2.0渲染另一个立方体。
这是对OpenGL Game示例的修改,它在两个旋转的上面添加了一个新的立方体:
- (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);
}
“渲染另一个版本”和“再次渲染对象”注释之间的代码是新的。它需要做的第一件事是为额外的立方体提出一个新的模型视图矩阵;这是基于update:
中的代码(未显示)。通常我会把代码放在那里,但我想保持样本简短。
接下来它渲染了立方体,这对于GLKit非常简单。耶GLKit!
我希望这会有所帮助。听起来你正在用额外的顶点和索引数组咆哮错误的树;如果你想渲染不同的几何形状,但是你在不同的地方说同一个模型,这是必要的。这是GLKit非常适合的。
答案 1 :(得分:0)
由于只有一个基本效果,我认为你会在更新中进行矩阵计算,并在再次绘制之前将其分配给基本效果modelviewmatrix?
答案 2 :(得分:0)
John Riselvato在上面评论说他真的希望他能看到更新:方法内容。我真的希望自己看到这一点,因为我在使用相同的GLKBaseEffect对象时将多个对象放在屏幕上很困难。这是我最终如何做到的:
// 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));
}
希望这有助于某人。