22 #include <android/log.h>
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
34 #define LOG_TAG "libNative"
35 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
36 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
40 "attribute vec4 vertexPosition;\n"
41 "attribute vec2 vertexTextureCord;\n"
42 "attribute vec3 vertexNormal;\n"
43 "attribute vec3 vertexColor; \n"
44 "attribute vec3 vertexTangent;\n"
45 "attribute vec3 vertexBiNormal;\n"
46 "varying vec2 textureCord;\n"
47 "varying vec3 varyingColor; \n"
48 "varying vec3 inverseLightDirection;\n"
49 "varying vec3 inverseEyeDirection;\n"
50 "uniform mat4 projection;\n"
51 "uniform mat4 modelView;\n"
54 " vec3 worldSpaceVertex =(modelView * vertexPosition).xyz;"
55 " vec3 transformedVertexNormal = normalize((modelView * vec4(vertexNormal, 0.0)).xyz);"
57 " inverseLightDirection = normalize(vec3(0.0, 0.0, 1.0));\n"
58 " inverseEyeDirection = normalize((vec3(0.0, 0.0, 1.0)- worldSpaceVertex ).xyz);\n"
60 " gl_Position = projection * modelView * vertexPosition;\n"
61 " textureCord = vertexTextureCord;\n"
62 " varyingColor = vertexColor;\n"
64 " vec3 transformedTangent = normalize((modelView * vec4(vertexTangent, 0.0)).xyz);\n"
65 " vec3 transformedBinormal = normalize((modelView * vec4(vertexBiNormal, 0.0)).xyz);\n"
66 " mat3 tangentMatrix = mat3(transformedTangent, transformedBinormal, transformedVertexNormal);\n"
67 " inverseLightDirection =inverseLightDirection * tangentMatrix;\n"
68 " inverseEyeDirection = inverseEyeDirection * tangentMatrix;\n"
73 "precision mediump float;\n"
74 "uniform sampler2D texture;\n"
75 "varying vec2 textureCord;\n"
76 "varying vec3 varyingColor;\n"
77 "varying vec3 inverseLightDirection;\n"
78 "varying vec3 inverseEyeDirection;\n"
79 "varying vec3 transformedVertexNormal;\n"
82 " vec3 fragColor = vec3(0.0,0.0,0.0); \n"
83 " vec3 normal = texture2D(texture, textureCord).xyz;"
84 " normal = normalize(normal * 2.0 -1.0);"
86 " vec3 diffuseLightIntensity = vec3(1.0, 1.0, 1.0);\n"
87 " float normalDotLight = max(0.0, dot(normal, inverseLightDirection));\n"
88 " fragColor += normalDotLight * varyingColor *diffuseLightIntensity;\n"
90 " vec3 ambientLightIntensity = vec3(0.1, 0.1, 0.1);\n"
91 " fragColor += ambientLightIntensity * varyingColor;\n"
93 " vec3 specularLightIntensity = vec3(1.0, 1.0, 1.0);\n"
94 " vec3 vertexSpecularReflectionConstant = vec3(1.0, 1.0, 1.0);\n"
95 " float shininess = 2.0;\n"
96 " vec3 lightReflectionDirection = reflect(vec3(0) - inverseLightDirection, normal);\n"
97 " float normalDotReflection = max(0.0, dot(inverseEyeDirection, lightReflectionDirection));\n"
98 " fragColor += pow(normalDotReflection, shininess) * vertexSpecularReflectionConstant * specularLightIntensity;\n"
99 " /* Make sure the fragment colour is between 0 and 1. */"
100 " clamp(fragColor, 0.0, 1.0);\n"
101 " gl_FragColor = vec4(fragColor,1.0);\n"
107 GLuint shader = glCreateShader(shaderType);
110 glShaderSource(shader, 1, &shaderSource, NULL);
111 glCompileShader(shader);
114 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
116 if (compiled != GL_TRUE)
119 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
123 char * logBuffer = (
char*) malloc(infoLen);
125 if (logBuffer != NULL)
127 glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
128 LOGE(
"Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
133 glDeleteShader(shader);
145 if (vertexShader == 0)
151 if (fragmentShader == 0)
160 glAttachShader(program , vertexShader);
161 glAttachShader(program, fragmentShader);
162 glLinkProgram(program);
163 GLint linkStatus = GL_FALSE;
164 glGetProgramiv(program , GL_LINK_STATUS, &linkStatus);
166 if(linkStatus != GL_TRUE)
170 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
174 char* logBuffer = (
char*) malloc(bufLength);
176 if (logBuffer != NULL)
178 glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
179 LOGE(
"Could not link program:\n%s\n", logBuffer);
184 glDeleteProgram(program);
216 LOGE (
"Could not create program");
234 glEnable(GL_DEPTH_TEST);
236 glViewport(0, 0, width, height);
270 -1.0f, - 1.0f, -1.0f,
406 GLushort
indicies[] = {0, 3, 2, 0, 1, 3, 4, 6, 7, 4, 7, 5, 8, 9, 10, 8, 11, 10, 12, 13, 14, 15, 12, 14, 16, 17, 18, 16, 19, 18, 20, 21, 22, 20, 23, 22};
411 glClearColor(0.0
f, 0.0
f, 0.0
f, 1.0
f);
412 glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
443 glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT,
indicies);
456 JNIEnv * env, jobject obj);
460 JNIEnv * env, jobject obj, jint
width, jint
height)
466 JNIEnv * env, jobject obj)
GLuint vertexNormalLocation
float projectionMatrix[16]
float modelViewMatrix[16]
GLint GLsizei GLsizei height
void matrixIdentityFunction(float *matrix)
Takes a 4 * 4 and sets the elements to the Identity function.
void loadTexture(const char *texture, unsigned int level, unsigned int width, unsigned int height)
Loads a desired texture into memory at an appropriate mipmap level.
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.
GLuint textureCordLocation
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)
static const char glFragmentShader[]
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.
static const char glVertexShader[]
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_normalmapping_NativeLibrary_step(JNIEnv *env, jobject obj)
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_normalmapping_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
GLuint projectionLocation
void matrixRotateY(float *matrix, float angle)
Rotates a matrix around the y axis by a given angle.