我的目标是在GlKViewController中绘制纹理并在顶部绘制彩色线条。这两个部分都是单独工作的,但是当我将两者合并到同一个绘图功能中时,我会丢失纹理 - 它们的颜色与线条颜色相同。
在我的绘画方法中 -
首先,我的纹理三角形的代码:
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect {
[EAGLContext setCurrentContext:self.context];
[self.effect prepareToDraw];
self.effect.texture2d0.enabled = YES;
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
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, TexCoord));
glDrawElements(GL_TRIANGLES, nLiveSquares*6, GL_UNSIGNED_SHORT, 0);
其次是我的代码:
self.effect.texture2d0.enabled = NO;
glDisable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, _lineBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,2,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor,4,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Color));
// Set the line width
glLineWidth(50.0);
// Render the line
glDrawArrays(GL_LINE_STRIP, 0, 2);
使用GLKVertexAttribColor似乎会出现问题。一旦我将颜色属性应用于线条,纹理也会以与线条相同的颜色着色。
编辑:部分问题似乎是使用
解决的glDisableVertexAttribArray(GLKVertexAttribColor);
在上面最后一行的glDrawArrays之后。
我的纹理不再与线条颜色相同,但现在它们显示为白色矩形,这实际上并不是一种改进。
另外,如果我注释掉'self.effect.texture2d0.enabled = YES'和'self.effect.texture2d0.enabled = NO'那么我的纹理会正确显示,但我的线条会变黑(当它应该是绿色)。
编辑II:我认为我真正想要的是一种方法来关闭一个块的纹理(绘制线条),然后再次将它们重新打开以绘制三角形。而且我认为出现问题的是self.effect.texture2d0.enabled为所绘制的所有内容设置状态。
答案 0 :(得分:0)
self.effect.texture2d0.enabled = TRUE / FALSE确实为preparetodraw块中的所有内容设置了状态,因此解决方案是使用两个preparetodraws。
第一块:
[EAGLContext setCurrentContext:self.context];
self.effect.texture2d0.enabled = YES;
[self.effect prepareToDraw];
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
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, TexCoord));
glDrawElements(GL_TRIANGLES, nLiveSquares*6, GL_UNSIGNED_SHORT, 0);
接下来是第二块:
self.effect.texture2d0.enabled = NO;
[self.effect prepareToDraw];
glDisable(GL_BLEND);
glBindBuffer(GL_ARRAY_BUFFER, _lineBuffer);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition,2,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Position));
glEnableVertexAttribArray(GLKVertexAttribColor);
glVertexAttribPointer(GLKVertexAttribColor,4,GL_FLOAT,GL_FALSE,sizeof(LinePoint),(const GLvoid *) offsetof(LinePoint, Color));
// Set the line width
glLineWidth(50.0);
// Render the line
glDrawArrays(GL_LINE_STRIP, 0, 2);