OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Native.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2013-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 /* [Includes] */
22 #include <jni.h>
23 #include <android/log.h>
24 
25 #include <GLES2/gl2.h>
26 #include <GLES2/gl2ext.h>
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31 
32 #define LOG_TAG "libNative"
33 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
34 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
35 /* [Includes] */
36 
37 /* [Vertex source] */
38 static const char glVertexShader[] =
39  "attribute vec4 vPosition;\n"
40  "void main()\n"
41  "{\n"
42  " gl_Position = vPosition;\n"
43  "}\n";
44 /* [Vertex source] */
45 
46 /* [Fragment source] */
47 static const char glFragmentShader[] =
48  "precision mediump float;\n"
49  "void main()\n"
50  "{\n"
51  " gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
52  "}\n";
53 /* [Fragment source] */
54 
55 /* [loadShader] */
56 GLuint loadShader(GLenum shaderType, const char* shaderSource)
57 {
58  GLuint shader = glCreateShader(shaderType);
59  if (shader)
60  {
61  glShaderSource(shader, 1, &shaderSource, NULL);
62  glCompileShader(shader);
63 
64  GLint compiled = 0;
65  glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
66 
67  if (!compiled)
68  {
69  GLint infoLen = 0;
70  glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
71 
72  if (infoLen)
73  {
74  char * buf = (char*) malloc(infoLen);
75 
76  if (buf)
77  {
78  glGetShaderInfoLog(shader, infoLen, NULL, buf);
79  LOGE("Could not Compile Shader %d:\n%s\n", shaderType, buf);
80  free(buf);
81  }
82 
83  glDeleteShader(shader);
84  shader = 0;
85  }
86  }
87  }
88 
89  return shader;
90 }
91 /* [loadShader] */
92 
93 /* [createProgram] */
94 GLuint createProgram(const char* vertexSource, const char * fragmentSource)
95 {
96  GLuint vertexShader = loadShader(GL_VERTEX_SHADER, vertexSource);
97  if (!vertexShader)
98  {
99  return 0;
100  }
101 
102  GLuint fragmentShader = loadShader(GL_FRAGMENT_SHADER, fragmentSource);
103  if (!fragmentShader)
104  {
105  return 0;
106  }
107 
108  GLuint program = glCreateProgram();
109 
110  if (program)
111  {
112  glAttachShader(program , vertexShader);
113  glAttachShader(program, fragmentShader);
114 
115  glLinkProgram(program);
116  GLint linkStatus = GL_FALSE;
117 
118  glGetProgramiv(program , GL_LINK_STATUS, &linkStatus);
119 
120  if( linkStatus != GL_TRUE)
121  {
122  GLint bufLength = 0;
123 
124  glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength);
125 
126  if (bufLength)
127  {
128  char* buf = (char*) malloc(bufLength);
129 
130  if (buf)
131  {
132  glGetProgramInfoLog(program, bufLength, NULL, buf);
133  LOGE("Could not link program:\n%s\n", buf);
134  free(buf);
135  }
136  }
137  glDeleteProgram(program);
138  program = 0;
139  }
140  }
141 
142  return program;
143 }
144 /* [createProgram] */
145 
146 /* [setupGraphics] */
149 
150 bool setupGraphics(int w, int h)
151 {
153 
155  {
156  LOGE ("Could not create program");
157  return false;
158  }
159 
160  vPosition = glGetAttribLocation(simpleTriangleProgram, "vPosition");
161 
162  glViewport(0, 0, w, h);
163 
164  return true;
165 }
166 /* [setupGraphics] */
167 
168 /* [renderFrame] */
170  0.0f, 1.0f,
171  -1.0f, -1.0f,
172  1.0f, -1.0f
173 };
174 
176 {
177  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
178  glClear (GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
179  glUseProgram(simpleTriangleProgram);
180  glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0 ,triangleVertices);
181  glEnableVertexAttribArray(vPosition);
182  glDrawArrays(GL_TRIANGLES, 0, 3);
183 }
184 /* [renderFrame] */
185 
186 extern "C"
187 {
189  JNIEnv * env, jobject obj, jint width, jint height);
191  JNIEnv * env, jobject obj);
192 };
193 
194 /* [Native functions] */
196  JNIEnv * env, jobject obj, jint width, jint height)
197 {
198  setupGraphics(width, height);
199 }
200 
202  JNIEnv * env, jobject obj)
203 {
204  renderFrame();
205 }
206 /* [Native functions] */
void setupGraphics(int width, int height)
Definition: Native.cpp:1256
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simpletriangle_NativeLibrary_init(JNIEnv *env, jobject obj, jint width, jint height)
Definition: Native.cpp:195
GLint GLsizei GLsizei height
Definition: gl2ext.h:179
GLuint createProgram(const char *vertexSource, const char *fragmentSource)
Definition: Native.cpp:104
GLfloat GLfloat GLfloat w
Definition: gl2ext.h:2701
GLuint vPosition
Definition: Native.cpp:148
GLfloat GLfloat f
Definition: gl2ext.h:2707
GLuint loadShader(GLenum shaderType, const char *shaderSource)
Definition: Native.cpp:69
GLfloat GLfloat GLfloat GLfloat h
Definition: gl2ext.h:2701
void renderFrame(void)
Definition: Native.cpp:1536
const GLfloat triangleVertices[]
Definition: Native.cpp:169
GLuint simpleTriangleProgram
Definition: Native.cpp:147
#define LOGE(...)
Definition: Native.cpp:34
static const char glFragmentShader[]
Definition: Native.cpp:47
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: gl2ext.h:134
JNIEXPORT void JNICALL Java_com_arm_malideveloper_openglessdk_simpletriangle_NativeLibrary_step(JNIEnv *env, jobject obj)
Definition: Native.cpp:201
GLint GLsizei width
Definition: gl2ext.h:179
typedef GLfloat(GL_APIENTRYP PFNGLGETPATHLENGTHNVPROC)(GLuint path
typedef GLenum(GL_APIENTRYP PFNGLGETGRAPHICSRESETSTATUSKHRPROC)(void)
GLuint program
Definition: gl2ext.h:1475
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
static const char glVertexShader[]
Definition: Native.cpp:38