2009-09-23 21:48:07 +00:00
|
|
|
// OpenGL ES 2.0 code
|
|
|
|
|
|
|
|
#include <nativehelper/jni.h>
|
|
|
|
#define LOG_TAG "GL2JNI gl_code.cpp"
|
|
|
|
#include <utils/Log.h>
|
2009-09-26 02:07:31 +00:00
|
|
|
|
2009-09-23 21:48:07 +00:00
|
|
|
#include <EGL/egl.h>
|
|
|
|
#include <GLES2/gl2.h>
|
|
|
|
#include <GLES2/gl2ext.h>
|
|
|
|
|
2009-09-26 02:07:31 +00:00
|
|
|
#include <stdio.h>
|
2009-09-23 21:48:07 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
|
|
|
static void printGLString(const char *name, GLenum s) {
|
|
|
|
const char *v = (const char *) glGetString(s);
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("GL %s = %s\n", name, v);
|
2009-09-23 21:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void checkGlError(const char* op) {
|
|
|
|
for (GLint error = glGetError(); error; error
|
|
|
|
= glGetError()) {
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("after %s() glError (0x%x)\n", op, error);
|
2009-09-23 21:48:07 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-26 02:07:31 +00:00
|
|
|
|
2009-09-23 21:48:07 +00:00
|
|
|
static const char gVertexShader[] = "attribute vec4 vPosition;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_Position = vPosition;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
static const char gFragmentShader[] = "precision mediump float;\n"
|
|
|
|
"void main() {\n"
|
|
|
|
" gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
GLuint loadShader(GLenum shaderType, const char* pSource) {
|
|
|
|
GLuint shader = glCreateShader(shaderType);
|
|
|
|
if (shader) {
|
|
|
|
glShaderSource(shader, 1, &pSource, NULL);
|
|
|
|
glCompileShader(shader);
|
|
|
|
GLint compiled = 0;
|
|
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
|
|
|
|
if (!compiled) {
|
|
|
|
GLint infoLen = 0;
|
|
|
|
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
|
|
|
|
if (infoLen) {
|
|
|
|
char* buf = (char*) malloc(infoLen);
|
|
|
|
if (buf) {
|
|
|
|
glGetShaderInfoLog(shader, infoLen, NULL, buf);
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Could not compile shader %d:\n%s\n",
|
2009-09-23 21:48:07 +00:00
|
|
|
shaderType, buf);
|
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
glDeleteShader(shader);
|
|
|
|
shader = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shader;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) {
|
|
|
|
GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource);
|
|
|
|
if (!vertexShader) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource);
|
|
|
|
if (!pixelShader) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint program = glCreateProgram();
|
|
|
|
if (program) {
|
|
|
|
glAttachShader(program, vertexShader);
|
|
|
|
checkGlError("glAttachShader");
|
|
|
|
glAttachShader(program, pixelShader);
|
|
|
|
checkGlError("glAttachShader");
|
|
|
|
glLinkProgram(program);
|
|
|
|
GLint linkStatus = GL_FALSE;
|
|
|
|
glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
|
|
|
|
if (linkStatus != GL_TRUE) {
|
|
|
|
GLint bufLength = 0;
|
|
|
|
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
|
|
|
|
if (bufLength) {
|
|
|
|
char* buf = (char*) malloc(bufLength);
|
|
|
|
if (buf) {
|
|
|
|
glGetProgramInfoLog(program, bufLength, NULL, buf);
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Could not link program:\n%s\n", buf);
|
2009-09-23 21:48:07 +00:00
|
|
|
free(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
glDeleteProgram(program);
|
|
|
|
program = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return program;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint gProgram;
|
|
|
|
GLuint gvPositionHandle;
|
|
|
|
|
|
|
|
bool setupGraphics(int w, int h) {
|
|
|
|
printGLString("Version", GL_VERSION);
|
|
|
|
printGLString("Vendor", GL_VENDOR);
|
|
|
|
printGLString("Renderer", GL_RENDERER);
|
|
|
|
printGLString("Extensions", GL_EXTENSIONS);
|
|
|
|
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("setupGraphics(%d, %d)", w, h);
|
2009-09-23 21:48:07 +00:00
|
|
|
gProgram = createProgram(gVertexShader, gFragmentShader);
|
|
|
|
if (!gProgram) {
|
2012-01-06 19:20:56 +00:00
|
|
|
ALOGE("Could not create program.");
|
2009-09-23 21:48:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
|
|
|
|
checkGlError("glGetAttribLocation");
|
2012-01-04 20:05:49 +00:00
|
|
|
ALOGI("glGetAttribLocation(\"vPosition\") = %d\n",
|
2009-09-23 21:48:07 +00:00
|
|
|
gvPositionHandle);
|
|
|
|
|
|
|
|
glViewport(0, 0, w, h);
|
|
|
|
checkGlError("glViewport");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GLfloat gTriangleVertices[] = { 0.0f, 0.5f, -0.5f, -0.5f,
|
|
|
|
0.5f, -0.5f };
|
|
|
|
|
|
|
|
void renderFrame() {
|
|
|
|
static float grey;
|
|
|
|
grey += 0.01f;
|
|
|
|
if (grey > 1.0f) {
|
|
|
|
grey = 0.0f;
|
|
|
|
}
|
|
|
|
glClearColor(grey, grey, grey, 1.0f);
|
|
|
|
checkGlError("glClearColor");
|
|
|
|
glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
checkGlError("glClear");
|
|
|
|
|
|
|
|
glUseProgram(gProgram);
|
|
|
|
checkGlError("glUseProgram");
|
|
|
|
|
|
|
|
glVertexAttribPointer(gvPositionHandle, 2, GL_FLOAT, GL_FALSE, 0, gTriangleVertices);
|
|
|
|
checkGlError("glVertexAttribPointer");
|
|
|
|
glEnableVertexAttribArray(gvPositionHandle);
|
|
|
|
checkGlError("glEnableVertexAttribArray");
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 3);
|
|
|
|
checkGlError("glDrawArrays");
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height);
|
|
|
|
JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj);
|
2009-09-26 02:07:31 +00:00
|
|
|
};
|
|
|
|
|
2013-03-27 13:05:29 +00:00
|
|
|
JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_init(JNIEnv * env, jobject obj, jint width, jint height)
|
2009-09-26 02:07:31 +00:00
|
|
|
{
|
|
|
|
setupGraphics(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_com_android_gl2jni_GL2JNILib_step(JNIEnv * env, jobject obj)
|
|
|
|
{
|
|
|
|
renderFrame();
|
|
|
|
}
|
2009-09-23 21:48:07 +00:00
|
|
|
|