22 #include <android/log.h>
24 #include <GLES2/gl2.h>
25 #include <GLES2/gl2ext.h>
34 #include <assimp/cimport.h>
35 #include <assimp/scene.h>
39 const struct aiScene*
scene = NULL;
45 #define LOG_TAG "libNative"
46 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
47 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
50 "attribute vec4 vertexPosition;\n"
51 "attribute vec3 vertexColour;\n"
52 "varying vec3 fragColour;\n"
53 "uniform mat4 projection;\n"
54 "uniform mat4 modelView;\n"
57 " gl_Position = projection * modelView * vertexPosition;\n"
58 " fragColour = vertexColour;\n"
62 "precision mediump float;\n"
63 "varying vec3 fragColour;\n"
66 " gl_FragColor = vec4(fragColour, 1.0);\n"
71 GLuint shader = glCreateShader(shaderType);
74 glShaderSource(shader, 1, &shaderSource, NULL);
75 glCompileShader(shader);
77 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
78 if (compiled != GL_TRUE)
81 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
85 char * logBuffer = (
char*) malloc(infoLen);
87 if (logBuffer != NULL)
89 glGetShaderInfoLog(shader, infoLen, NULL, logBuffer);
90 LOGE(
"Could not Compile Shader %d:\n%s\n", shaderType, logBuffer);
95 glDeleteShader(shader);
107 if (vertexShader == 0)
113 if (fragmentShader == 0)
122 glAttachShader(program, vertexShader);
123 glAttachShader(program, fragmentShader);
124 glLinkProgram(program);
125 GLint linkStatus = GL_FALSE;
126 glGetProgramiv(program, GL_LINK_STATUS, &linkStatus);
127 if(linkStatus != GL_TRUE)
130 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
133 char* logBuffer = (
char*) malloc(bufLength);
135 if (logBuffer != NULL)
137 glGetProgramInfoLog(program, bufLength, NULL, logBuffer);
138 LOGE(
"Could not link program:\n%s\n", logBuffer);
143 glDeleteProgram(program);
166 LOGE (
"Could not create program");
177 glEnable(GL_DEPTH_TEST);
179 glViewport(0, 0, width, height);
182 std::string
sphere =
"s 0 0 0 10";
183 scene = aiImportFileFromMemory(sphere.c_str(), sphere.length(), 0,
".nff");
187 LOGE(
"Open Asset Importer could not load scene. \n");
193 int vertices_accumulation = 0;
195 for (
int i = 0; i <
scene->mNumMeshes; i++)
198 for (
int j = 0; j <
scene->mMeshes[i]->mNumVertices; j++)
200 const aiVector3D& vector =
scene->mMeshes[i]->mVertices[j];
212 for (
unsigned int j = 0 ; j <
scene->mMeshes[i]->mNumFaces ; j++)
214 const aiFace&
face =
scene->mMeshes[i]->mFaces[j];
215 indices.push_back(face.mIndices[0] + vertices_accumulation);
216 indices.push_back(face.mIndices[1] + vertices_accumulation);
217 indices.push_back(face.mIndices[2] + vertices_accumulation);
221 vertices_accumulation +=
scene->mMeshes[i]->mNumVertices;
230 glClearColor(0.0
f, 0.0
f, 0.0
f, 1.0
f);
231 glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
253 glDrawElements(GL_TRIANGLES,
indices.size(), GL_UNSIGNED_SHORT, &
indices[0]);
266 JNIEnv * env, jobject obj, jint
width, jint
height);
268 JNIEnv * env, jobject obj);
272 JNIEnv * env, jobject obj, jint
width, jint
height)
278 JNIEnv * env, jobject obj)
static const char glVertexShader[]
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_assetloading_NativeLibrary_step(JNIEnv *env, jobject obj)
const struct aiScene * scene
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.
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_assetloading_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
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 GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
std::vector< GLushort > indices
static const char glFragmentShader[]
std::vector< GLfloat > vertices
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.