簡體   English   中英

在GLKit中用顏色繪制紋理和線條之間的沖突

[英]Conflict between drawing both textures and lines with colors in GLKit

我的目的是在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似乎會出現問題。 一旦將color屬性應用於線條,所有的紋理也將以與線條相同的顏色上色。

編輯:部分問題似乎可以通過使用解決

glDisableVertexAttribArray(GLKVertexAttribColor);

在上面最后一行的glDrawArrays之后。

我的紋理不再使用與線條相同的顏色,但是現在將它們顯示為白色矩形,這實際上並沒有改善。

另外,如果我注釋掉“ self.effect.texture2d0.enabled = YES”和“ self.effect.texture2d0.enabled = NO”,則可以正確顯示我的紋理,但是我的線條變為黑色(當它應該為綠色時)。

編輯II:我想我真正想要的是一種關閉一個塊的紋理(繪制線),然后再次將其重新打開以繪制三角形的方法。 而且我認為出了什么問題是self.effect.texture2d0.enabled為繪制的所有內容設置了狀態。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM