GLES2中的奇怪着色器行为

时间:2013-10-03 10:43:36

标签: c++ opengl-es opengl-es-2.0 shader

嘿所有你能注意到这段代码有什么问题吗?它直接来自书籍#34; Open GLES 2.0编程指南"我一直在寻找结果和处理代码改变等事情,它仍然无法正常工作!此代码生成的结果通过图像Rectangle is shown when it should be a triangle

附加

顶点着色器代码:

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

1 个答案:

答案 0 :(得分:0)

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

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