【问题标题】:Conflict between drawing both textures and lines with colors in GLKit在GLKit中用颜色绘制纹理和线条之间的冲突
【发布时间】:2014-05-08 23:52:43
【问题描述】:

我的目标是在 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”,那么我的纹理会正确显示,但我的线条会变黑(应该是绿色)。

编辑二:我认为我真正想要的是一种关闭纹理的方法(绘制线条),然后再次打开它们以绘制三角形。而且我认为问题在于 self.effect.texture2d0.enabled 设置了所有绘制的状态。

【问题讨论】:

    标签: ios opengl-es-2.0 glkit


    【解决方案1】:

    self.effect.texture2d0.enabled = TRUE/FALSE 确实为 preparetodraw 块中的所有内容设置了状态,因此解决方案是使用两个 preparetodraw。

    第一块:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-09
      • 2014-11-28
      • 2011-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多