據我所知,API 9引入了openGL ES 2.0。對在onCreate方法啓動只需添加:
// Check if the system supports OpenGL ES 2.0.
final ActivityManager activityManager =
(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final ConfigurationInfo configurationInfo =
activityManager.getDeviceConfigurationInfo();
final boolean supportsEs2 = configurationInfo.reqGlEsVersion >= 0x20000;
if (!supportsEs2)
{
Log.i("WLGFX","Device does not support OpenGL ES 2.0");
System.exit(-1);
return;
}
它也一直是我上次看了VAO的很長一段時間,我真的以爲他們得到了完全擺脫他們。爲了表現,請使用VBO。它們同樣易於創建和使用。以下是一些示例代碼片段:
void FFDisplay::init_texture_buffers() { // for overlay screen
GLfloat vertices[] = {
-1.0f, -1.0f, 0.0f, 1.0f, // X, Y, U, V
-1.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 1.0f, 1.0f
};
GLushort indices[] = { 2, 1, 0, 2, 0, 3 };
glGenBuffers(2, texture_buffers);
glBindBuffer(GL_ARRAY_BUFFER, texture_buffers[0]);
glBufferData(GL_ARRAY_BUFFER, 64, vertices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texture_buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 12, indices, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
texture_texture = 0;
}
void FFDisplay::init_overlay_texture() {
glGenTextures(1, &overlay_texture);
glBindTexture(GL_TEXTURE_2D, overlay_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_HINT, GL_FALSE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FFD_OVERLAY_WID, FFD_OVERLAY_HGT,
0, GL_RGBA, GL_UNSIGNED_BYTE, overlay_image);
}
void FFDisplay::draw_overlay() {
glUseProgram(prog_texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, overlay_texture);
glUniform1i(texture_texture, 0);
glEnableVertexAttribArray(texture_vertex);
glEnableVertexAttribArray(texture_uv);
glBindBuffer(GL_ARRAY_BUFFER, texture_buffers[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, texture_buffers[1]);
glVertexAttribPointer(texture_vertex, 2, GL_FLOAT, GL_FALSE, 16, NULL);
glVertexAttribPointer(texture_uv, 2, GL_FLOAT, GL_FALSE, 16, (void*)8);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
glDisableVertexAttribArray(texture_uv);
glDisableVertexAttribArray(texture_vertex);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
有大量關於GLES 2.0的信息可以幫助您入門。
無論如何,您需要在運行時檢查是否存在GL_OES_vertex_array_object,解析相應的入口點並使用它們。這應該是相當獨立的Android版本。 – peppe
是的,我在運行時檢查它是否可用(Cocos2d-x自動執行並保存它)。我只是想知道編譯時是否還有其他要求。 – keyboard
這是一個可選的擴展,你需要檢查它是一個GPU驅動程序的東西。在需要OpenGL ES 2.0的情況下,與Android無關。 –