OpenGL ES简单示例
以下是一个使用OpenGL ES 2.0的简单示例,它创建一个三角形:
#include <jni.h>
#include <errno.h>
#include <android/log.h>
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, "native-activity", fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, "native-activity", fmt, ##args)
#include <GLES2/gl2.h>
GLuint gProgram;
static void checkGlError(const char* op) {
for (GLint error = glGetError(); error; error = glGetError()) {
LOGI("after %s() glError (0x%x)\n", op, error);
}
}
extern "C"
JNIEXPORT void JNICALL
Java_com_example_myapp_MainActivity_init(JNIEnv* env, jobject obj, jint width, jint height) {
const char vShaderStr[] =
"attribute vec4 a_position;\n"
"void main() {\n"
" gl_Position = a_position;\n"
"}\n";
const char fShaderStr[] =
"precision mediump float;\n"
"void main() {\n"
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
"}\n";
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexShader, 1, &vShaderStr, NULL);
glShaderSource(fragmentShader, 1, &fShaderStr, NULL);
GLint compiled = 0;
glCompileShader(vertexShader);
glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(vertexShader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(vertexShader, infoLen, NULL, infoLog);
LOGE("Error compiling vertex shader:%s\n", infoLog);
free(infoLog);
}
glDeleteShader(vertexShader);
return;
}
glCompileShader(fragmentShader);
glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLint infoLen = 0;
glGetShaderiv(fragmentShader, GL_INFO_LOG_LENGTH, &infoLen);
if (infoLen > 1) {
char* infoLog = (char*)malloc(sizeof(char) * infoLen);
glGetShaderInfoLog(fragmentShader, infoLen, NULL, infoLog);
LOGE("Error compiling fragment shader:%s\n", infoLog);
free(infoLog);
}
glDeleteShader(fragmentShader);
评论已关闭