22 #include <android/log.h>
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
33 #define LOG_TAG "libNative"
34 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
35 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
39 "attribute vec4 vertexPosition;\n"
40 "attribute vec3 vertexColour;\n"
41 "varying vec3 fragColour;\n"
42 "uniform mat4 projection;\n"
43 "uniform mat4 modelView;\n"
46 " gl_Position = projection * modelView * vertexPosition;\n"
47 " fragColour = vertexColour;\n"
53 "precision mediump float;\n"
54 "varying vec3 fragColour;\n"
57 " gl_FragColor = vec4(fragColour, 1.0);\n"
63 GLuint shader = glCreateShader(shaderType);
66 glShaderSource(shader, 1, &shaderSource, NULL);
67 glCompileShader(shader);
69 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
70 if (compiled != GL_TRUE)
73 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
77 char * logBuffer = (
char*) malloc(infoLen);
79 if (logBuffer != NULL)
81 glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
82 LOGE(
"Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
87 glDeleteShader(shader);
99 if (vertexShader == 0)
105 if (fragmentShader == 0)
114 glAttachShader(program, vertexShader);
115 glAttachShader(program, fragmentShader);
116 glLinkProgram(program);
117 GLint linkStatus = GL_FALSE;
118 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
119 if(linkStatus != GL_TRUE)
122 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
125 char* logBuffer = (
char*) malloc(bufLength);
127 if (logBuffer != NULL)
129 glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
130 LOGE(
"Could not link program:\n%s\n", logBuffer);
135 glDeleteProgram(program);
159 LOGE (
"Could not create program");
170 glEnable(GL_DEPTH_TEST);
172 glViewport(0, 0, width, height);
234 GLushort
indices[] = {0, 2, 3, 0, 1, 3, 4, 6, 7, 4, 5, 7, 8, 9, 10, 11, 8, 10, 12, 13, 14, 15, 12, 14, 16, 17, 18, 16, 19, 18, 20, 21, 22, 20, 23, 22};
240 glClearColor(0.0
f, 0.0
f, 0.0
f, 1.0
f);
241 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
259 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT,
indices);
271 JNIEnv * env, jobject obj, jint
width, jint
height);
273 JNIEnv * env, jobject obj);
277 JNIEnv * env, jobject obj, jint
width, jint
height)
283 JNIEnv * env, jobject obj)
float projectionMatrix[16]
float modelViewMatrix[16]
GLint GLsizei GLsizei height
static const char glFragmentShader[]
void matrixIdentityFunction(float *matrix)
Takes a 4 * 4 and sets the elements to the Identity function.
GLuint vertexColourLocation
void matrixPerspective(float *matrix, float fieldOfView, float aspectRatio, float zNear, float zFar)
Create a perspective projection matrix and store the results in the first parameter.
static const char glVertexShader[]
void matrixRotateX(float *matrix, float angle)
Rotates a matrix around the x axis by a given angle.
GLuint createProgram(const char *vertexSource, const char *fragmentSource)
GLsizei GLenum const void * indices
GLuint loadShader(GLenum shaderType, const char *shaderSource)
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simplecube_NativeLibrary_step(JNIEnv *env, jobject obj)
void matrixTranslate(float *matrix, float x, float y, float z)
Takes in a 4 * 4 matrix and translates it by the vector defined by x y and z.
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simplecube_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
GLuint projectionLocation
void matrixRotateY(float *matrix, float angle)
Rotates a matrix around the y axis by a given angle.