0

大家好 你能注意到这段代码有什么问题吗?它直接来自“Open GLES 2.0 Programming Guide”一书,我一直在寻找结果并解决代码更改等问题,但它仍然无法正常工作!此代码产生的结果通过图像附加当它应该是三角形时显示矩形

顶点着色器代码:

attribute vec4 v_Position;

void main()
{
    gl_Position = v_Position;
}

片段着色器代码:

precision mediump float;

void main()
{
    gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}

最后我的代码:(注意着色器编译成功,两个着色器程序链接到名为“shaderProgram”的程序中)

GLfloat verts[] = {0.0f, 0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
    0.5f, -0.5f, 0.0f};
GLushort inds[] = {0,1,2};
GLuint ibo = 0;
glUseProgram(shaderProgram);
////get the buffer object
glGenBuffers(1,&vbo);
glGenBuffers(1,&ibo);

glBindBuffer(GL_ARRAY_BUFFER,vbo);
glBufferData(GL_ARRAY_BUFFER,3*3,verts,GL_STATIC_DRAW);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort)*3,inds,GL_STATIC_DRAW);

GLint vPos = glGetAttribLocation(shaderProgram, "v_Position");
glBindBuffer(GL_ARRAY_BUFFER,vbo);
glEnableVertexAttribArray(vPos);
glVertexAttribPointer(vPos,3,GL_FLOAT,GL_FALSE,3,0);
glBindAttribLocation(shaderProgram, vPos, "v_Position");


glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);

glDeleteBuffers(1,&vbo);
glDeleteBuffers(1,&ibo);
4

1 回答 1

0

问题似乎在函数中 glVertexAttribPointer(vPos,3,GL_FLOAT,GL_FALSE,3,0);

第 5 个参数是步幅,最后一个参数是大小。在这种情况下,您不需要它:它必须是: glVertexAttribPointer(vPos,3,GL_FLOAT,GL_FALSE,0, nullptr);

于 2013-10-03T10:54:16.313 回答