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"
42 "attribute vec3 vertexNormal;\n"
44 "varying vec3 fragColour;\n"
45 "uniform mat4 projection;\n"
46 "uniform mat4 modelView;\n"
50 " vec3 transformedVertexNormal = normalize((modelView * vec4(vertexNormal, 0.0)).xyz);"
51 " vec3 inverseLightDirection = normalize(vec3(0.0, 1.0, 1.0));\n"
52 " fragColour = vec3(0.0);\n"
56 " vec3 diffuseLightIntensity = vec3(1.0, 1.0, 1.0);\n"
57 " vec3 vertexDiffuseReflectionConstant = vertexColour;\n"
58 " float normalDotLight = max(0.0, dot(transformedVertexNormal, inverseLightDirection));\n"
59 " fragColour += normalDotLight * vertexDiffuseReflectionConstant * diffuseLightIntensity;\n"
63 " vec3 ambientLightIntensity = vec3(0.1, 0.1, 0.1);\n"
64 " vec3 vertexAmbientReflectionConstant = vertexColour;\n"
65 " fragColour += vertexAmbientReflectionConstant * ambientLightIntensity;\n"
69 " vec3 inverseEyeDirection = normalize(vec3(0.0, 0.0, 1.0));\n"
70 " vec3 specularLightIntensity = vec3(1.0, 1.0, 1.0);\n"
71 " vec3 vertexSpecularReflectionConstant = vec3(1.0, 1.0, 1.0);\n"
72 " float shininess = 2.0;\n"
73 " vec3 lightReflectionDirection = reflect(vec3(0) - inverseLightDirection, transformedVertexNormal);\n"
74 " float normalDotReflection = max(0.0, dot(inverseEyeDirection, lightReflectionDirection));\n"
75 " fragColour += pow(normalDotReflection, shininess) * vertexSpecularReflectionConstant * specularLightIntensity;\n"
78 " /* Make sure the fragment colour is between 0 and 1. */"
79 " clamp(fragColour, 0.0, 1.0);\n"
81 " gl_Position = projection * modelView * vertexPosition;\n"
86 "precision mediump float;\n"
87 "varying vec3 fragColour;\n"
90 " gl_FragColor = vec4(fragColour, 1.0);\n"
95 GLuint shader = glCreateShader(shaderType);
98 glShaderSource(shader, 1, &shaderSource, NULL);
99 glCompileShader(shader);
101 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
102 if (compiled != GL_TRUE)
105 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
109 char * logBuffer = (
char*) malloc(infoLen);
111 if (logBuffer != NULL)
113 glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
114 LOGE(
"Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
119 glDeleteShader(shader);
131 if (vertexShader == 0)
137 if (fragmentShader == 0)
146 glAttachShader(program, vertexShader);
147 glAttachShader(program, fragmentShader);
148 glLinkProgram(program);
149 GLint linkStatus = GL_FALSE;
150 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
151 if(linkStatus != GL_TRUE)
154 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
157 char* logBuffer = (
char*) malloc(bufLength);
159 if (logBuffer != NULL)
161 glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
162 LOGE(
"Could not link program:\n%s\n", logBuffer);
167 glDeleteProgram(program);
193 LOGE (
"Could not create program");
207 glEnable(GL_DEPTH_TEST);
209 glViewport(0, 0, width, height);
317 GLushort
indices[] = {0, 2, 4, 0, 4, 1, 1, 4, 3, 2, 3, 4,
318 5, 7, 9, 5, 9, 6, 6, 9, 8, 7, 8, 9,
319 10, 12, 14, 10, 14, 11, 11, 14, 13, 12, 13, 14,
320 15, 17, 19, 15, 19, 16, 16, 19, 18, 17, 18, 19,
321 20, 22, 24, 20, 24, 21, 21, 24, 23, 22, 23, 24,
322 25, 27, 29, 25, 29, 26, 26, 29, 28, 27, 28, 29
328 glClearColor(0.0
f, 0.0
f, 0.0
f, 1.0
f);
329 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
353 glDrawElements(GL_TRIANGLES, 72, GL_UNSIGNED_SHORT,
indices);
366 JNIEnv * env, jobject obj, jint
width, jint
height);
368 JNIEnv * env, jobject obj);
372 JNIEnv * env, jobject obj, jint
width, jint
height)
378 JNIEnv * env, jobject obj)
static const char glVertexShader[]
GLuint vertexNormalLocation
float projectionMatrix[16]
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_lighting_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
float modelViewMatrix[16]
GLint GLsizei GLsizei height
void matrixIdentityFunction(float *matrix)
Takes a 4 * 4 and sets the elements to the Identity function.
static const char glFragmentShader[]
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.
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
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_lighting_NativeLibrary_step(JNIEnv *env, jobject obj)
GLuint loadShader(GLenum shaderType, const char *shaderSource)
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
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.