glDrawElements 不绘制任何东西
glDrawElements not drawing anything
我正在使用 c/c++ (NDK) for android 在 opengl es 2.0 中编程。我正在使用 GLM 库!
到目前为止,我可以使用 glDrawArrays 在黑色背景上绘制一个白色三角形,但是当我尝试使用 glDrawElements 绘制相同的三角形时,它没有绘制任何东西。
相关代码:
float triangleCoords[] = { // in counterclockwise order:
0, 5, 5, // top
-5, 0, 5, // bottom left
5, 0, 5 // bottom right
};
float color[] = { 1,1,1, 1.0f };
GLuint drawOrder[]={0,1,2};
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
int positionHandle=glGetAttribLocation(program,"vPosition");
int matrixHandle = glGetUniformLocation(program, "uMVPMatrix");
glEnableVertexAttribArray(positionHandle);
glUniformMatrix4fv(matrixHandle,1,GL_FALSE,glm::value_ptr(mvpMatrix));
glVertexAttribPointer(positionHandle,3,GL_FLOAT,GL_FALSE,0,triangleCoords);
int colorHandle =glGetUniformLocation(program, "vColor");
glUniform4fv(colorHandle, 1, color);
//glDrawArrays(GL_TRIANGLES, 0, 3); <== This works
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,drawOrder); <== This does not works
glDisableVertexAttribArray(positionHandle);
//swapbuffers and stuff
着色器非常基础
std::string vertexShaderCode =
"attribute vec4 vPosition; "
"uniform mat4 uMVPMatrix; "
"void main() {"
" gl_Position = uMVPMatrix*vPosition;"
"}";
std::string fragmentShaderCode =
"precision mediump float;"
"uniform vec4 vColor;"
"void main() {"
" gl_FragColor = vColor;"
"}";
OpenGLES 不保证 GL_UNSIGNED_INT 支持索引。使用 GL_UNSIGNED_SHORT:
GLushort drawOrder[]={0,1,2};
...
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,drawOrder);
我正在使用 c/c++ (NDK) for android 在 opengl es 2.0 中编程。我正在使用 GLM 库!
到目前为止,我可以使用 glDrawArrays 在黑色背景上绘制一个白色三角形,但是当我尝试使用 glDrawElements 绘制相同的三角形时,它没有绘制任何东西。
相关代码:
float triangleCoords[] = { // in counterclockwise order:
0, 5, 5, // top
-5, 0, 5, // bottom left
5, 0, 5 // bottom right
};
float color[] = { 1,1,1, 1.0f };
GLuint drawOrder[]={0,1,2};
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
int positionHandle=glGetAttribLocation(program,"vPosition");
int matrixHandle = glGetUniformLocation(program, "uMVPMatrix");
glEnableVertexAttribArray(positionHandle);
glUniformMatrix4fv(matrixHandle,1,GL_FALSE,glm::value_ptr(mvpMatrix));
glVertexAttribPointer(positionHandle,3,GL_FLOAT,GL_FALSE,0,triangleCoords);
int colorHandle =glGetUniformLocation(program, "vColor");
glUniform4fv(colorHandle, 1, color);
//glDrawArrays(GL_TRIANGLES, 0, 3); <== This works
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_INT,drawOrder); <== This does not works
glDisableVertexAttribArray(positionHandle);
//swapbuffers and stuff
着色器非常基础
std::string vertexShaderCode =
"attribute vec4 vPosition; "
"uniform mat4 uMVPMatrix; "
"void main() {"
" gl_Position = uMVPMatrix*vPosition;"
"}";
std::string fragmentShaderCode =
"precision mediump float;"
"uniform vec4 vColor;"
"void main() {"
" gl_FragColor = vColor;"
"}";
OpenGLES 不保证 GL_UNSIGNED_INT 支持索引。使用 GL_UNSIGNED_SHORT:
GLushort drawOrder[]={0,1,2};
...
glDrawElements(GL_TRIANGLES,3,GL_UNSIGNED_SHORT,drawOrder);