OGeek|极客世界-中国程序员成长平台

标题: ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单的线条 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:35
标题: ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单的线条

我很难通过 OpenGL ES 2.0 着色器绘制简单的线条。 我正在尝试将 glDrawArraysGL_LINES 一起使用,但到目前为止我一直没有成功。

我的顶点着色器是你能得到的最基本的:

attribute vec4 Position; 
attribute vec4 SourceColor; 

uniform mat4 Projection;
uniform mat4 ModelView;

varying vec4 DestinationColor; 

void main(void) { 
    DestinationColor = SourceColor; 
    gl_Position = Projection * ModelView * Position;
}

片段着色器:

varying lowp vec4 DestinationColor;

void main(void) {
    gl_FragColor = DestinationColor;
}

我有一些疑问:

  1. 如何设置起点和终点坐标?
  2. 在这种情况下如何设置 Projection 和 ModelView 矩阵?对于正方形,ModelView 矩阵将保持其中心坐标,但在 Line 的情况下会发生什么?

有人有例子吗?

编辑:

好的,我有以下代码:

    glLineVertices[0].Position[0] = origin.x;
    glLineVertices[0].Position[1] = origin.y;
    glLineVertices[0].Position[2] = 0;
    glLineVertices[0].Color[0] = 1.0f;
    glLineVertices[0].Color[1] = 0.0f;
    glLineVertices[0].Color[2] = 0.0f;
    glLineVertices[0].Color[3] = 0.0f;
    glLineVertices[0].Normal[0] = 0.0f;
    glLineVertices[0].Normal[1] = 0.0f;
    glLineVertices[0].Normal[2] = 1.0f;

    glLineVertices[1].Position[0] = end.x;
    glLineVertices[1].Position[1] = end.y;
    glLineVertices[1].Position[2] = 0;
    glLineVertices[1].Color[0] = 1.0f;
    glLineVertices[1].Color[1] = 0.0f;
    glLineVertices[1].Color[2] = 0.0f;
    glLineVertices[1].Color[3] = 0.0f;
    glLineVertices[1].Normal[0] = 0.0f;
    glLineVertices[1].Normal[1] = 0.0f;
    glLineVertices[1].Normal[2] = 1.0f;

    glGenVertexArraysOES(1, &vertexArray);
    glBindVertexArrayOES(vertexArray);

    glGenBuffers(1, &vertexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, vertexStructureSize, glLineVertices, GL_STATIC_DRAW);

    glEnableVertexAttribArray(shaderManager.currentAttributeVertexPosition);
    glVertexAttribPointer(shaderManager.currentAttributeVertexPosition, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), 0);
    glEnableVertexAttribArray(shaderManager.currentAttributeSourceColor);
    glVertexAttribPointer(shaderManager.currentAttributeSourceColor, 4, GL_FLOAT, GL_FALSE,  sizeof(glLineVertex), (char *)12);
    glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
    glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);

使用渲染方法:

- (void)render
{
    //It might as well be the Identity Matrix, nothing happens either way.
    //modelViewMatrix = GLKMatrix4Identity;   
    modelViewMatrix = GLKMatrix4MakeTranslation(0, 0, 0.0f);    
    glUniformMatrix4fv(shaderManager.currentUniformModelViewMatrix, 1, 0, modelViewMatrix.m);

    glBindVertexArrayOES(vertexArray);
    glDrawArrays(GL_LINES, 0, size);  

}

还有这个投影矩阵:

_projectionMatrix = GLKMatrix4MakeOrtho(0, self.view.bounds.size.width, 0, self.view.bounds.size.height, -1.0f, 1.0f);

[shaderManager useShaderProgram"LineShader"];

glUniformMatrix4fv([shaderManager getUniform"rojection" ofShader"LineShader"], 1, 0, _projectionMatrix.m);

一旦我使用:

[lineDraw render];

什么都没有发生。 有谁知道为什么?



Best Answer-推荐答案


我终于明白了。

这些简单的线条:

glEnableVertexAttribArray(shaderManager.currentAttributeVertexNormal);
glVertexAttribPointer(shaderManager.currentAttributeVertexNormal, 3, GL_FLOAT, GL_FALSE, sizeof(glLineVertex), (char *)28);

导致了这个问题,因为我暂时禁用了着色器中法线的使用,注释掉了对应的属性。不知道这样就不会产生draw,getError()也不会抛出任何错误。

不过,我会认为上述答案是正确的,因为它帮助我澄清了坐标和对应矩阵的计算。在这个例子中,普通的 2D 线,不需要任何特殊的 modelViewMatrix,它可以保持为“GLKMatrix4Identity”。

再次感谢

关于ios - 在 IOS 上的 OpenGL ES 2.0 着色器中绘制简单的线条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8289727/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4