Browse Source

Draw image

Girish Ramakrishnan 6 years ago
parent
commit
0ff04d24f7
5 changed files with 148 additions and 1 deletions
  1. 1 0
      AndroidManifest.xml
  2. 142 0
      jni/renderer.cpp
  3. 3 0
      jni/renderer.h
  4. 1 1
      res/values/strings.xml
  5. 1 0
      src/in/forwardbias/gldroid/MainActivity.java

+ 1 - 0
AndroidManifest.xml

@@ -3,6 +3,7 @@
       package="in.forwardbias.gldroid"
       android:versionCode="1"
       android:versionName="1.0">
+    <uses-feature android:glEsVersion="0x00020000" />
     <application android:label="@string/app_name" >
         <activity android:name="MainActivity"
                   android:label="@string/app_name">

+ 142 - 0
jni/renderer.cpp

@@ -1,5 +1,11 @@
 #include "renderer.h"
 
+#include <android/log.h>
+//#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "Renderer", __VA_ARGS__)
+#define LOGE(...)
+#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "Renderer", __VA_ARGS__)
+
+#include <time.h>
 #include <GLES2/gl2.h>
 #include <GLES2/gl2ext.h>
 #include <GLES2/gl2platform.h>
@@ -12,9 +18,115 @@ Renderer::~Renderer()
 {
 }
 
+static void printLog(GLuint obj)
+{
+    int infologLength = 0;
+    int maxLength = 0;
+ 
+    if (glIsShader(obj)) {
+        glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &maxLength);
+    } else {
+        glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &maxLength);
+    }
+
+    if (maxLength == 0) maxLength = 1024; // some drivers return 0
+
+    char *infoLog = new char[maxLength];
+ 
+    if (glIsShader(obj)) {
+        glGetShaderInfoLog(obj, maxLength, &infologLength, infoLog);
+    } else {
+        glGetProgramInfoLog(obj, maxLength, &infologLength, infoLog);
+    }
+
+    LOGI("LOG : [%s]\n",infoLog);
+    delete [] infoLog;
+}
+
+static GLuint createShader(int type, char *source)
+{
+    GLuint shader = glCreateShader(type);
+    if (shader == 0) return 0;
+
+    glShaderSource(shader, 1, (const GLchar **) &source, NULL /* null terminated */);
+    glCompileShader(shader);
+
+    GLint status = 0;
+    glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
+    if (status == GL_TRUE) return shader;
+
+    LOGI("Error creating %s shader", type == GL_VERTEX_SHADER ? "vertex" : "fragment");
+    printLog(shader);
+    return 0;
+}
+
+static GLuint createProgram(char *vshaderSource, char *fshaderSource)
+{
+    GLuint vshader = createShader(GL_VERTEX_SHADER, vshaderSource);
+    GLuint fshader = createShader(GL_FRAGMENT_SHADER, fshaderSource);
+    
+    if (vshader == 0 || fshader == 0) return 0;
+
+    GLuint program = glCreateProgram();
+    glAttachShader(program, vshader);
+    glAttachShader(program, fshader);
+    glLinkProgram(program);
+
+    GLint status;
+    glGetProgramiv(program, GL_LINK_STATUS, &status);
+    if (status == GL_TRUE) return program;
+
+    LOGI("Error Linking Program");
+    printLog(program);
+    return 0;
+}
+
 void Renderer::init()
 {
     glClearColor(1.0, 1.0, 0.0, 1.0);
+
+    GLuint buffers[2];
+    glGenBuffers(2, buffers);
+
+    GLfloat vertices[] = {
+        -0.5, -0.5,
+        0.5, -0.5,
+        -0.5, 0.5,
+        0.5, 0.5
+    };
+    glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
+    glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), vertices, GL_STATIC_DRAW);
+    m_vertexArrayBuffer = buffers[0];
+
+    GLfloat texCoords[] = {
+        0, 0,
+        1, 0,
+        0, 1,
+        1, 1
+    };
+    glBindBuffer(GL_ARRAY_BUFFER, buffers[1]);
+    glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), texCoords, GL_STATIC_DRAW);
+    m_texCoordArrayBuffer = buffers[1];
+
+    char *vertexShader =
+        "attribute vec2 a_vertex;"
+        "attribute vec2 a_texCoord;"
+        "varying vec2 v_texCoord;"
+        "void main() {"
+        "   gl_Position = vec4(a_vertex, 0, 1);"
+        "   v_texCoord = a_texCoord;"
+        "}";
+
+    char *fragmentShader =
+        "precision mediump float;"
+        "uniform sampler2D u_image;"
+        "varying vec2 v_texCoord;"
+        "void main() {"
+        "   gl_FragColor = texture2D(u_image, v_texCoord);"
+        "}";
+
+    m_program = createProgram(vertexShader, fragmentShader);
+    if (m_program) LOGI("PROGRAM READY TO RUN!!");
 }
 
 void Renderer::resize(int w, int h)
@@ -22,9 +134,39 @@ void Renderer::resize(int w, int h)
     glViewport(0, 0, w, h);
 }
 
+static unsigned long long getTime()
+{
+    timespec t;
+    clock_gettime(CLOCK_MONOTONIC, &t);
+    return (unsigned long long)(t.tv_sec*1000000000ULL) + (unsigned long long)t.tv_nsec;
+}
+
 void Renderer::draw()
 {
     glClear(GL_COLOR_BUFFER_BIT);
+
+    glUseProgram(m_program);
+
+    //unsigned long long current = getTime();
+
+    const int vertexLocation = glGetAttribLocation(m_program, "a_vertex");
+    glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffer);
+    glVertexAttribPointer(vertexLocation, 2 /* component count */, GL_FLOAT, GL_FALSE /* normalized */, 0 /* stride */, 0 /* offset */);
+    glEnableVertexAttribArray(vertexLocation);
+
+    const int texCoordLocation = glGetAttribLocation(m_program, "a_texCoord");
+    glBindBuffer(GL_ARRAY_BUFFER, m_texCoordArrayBuffer);
+    glVertexAttribPointer(texCoordLocation, 2 /* component count */, GL_FLOAT, GL_FALSE /* normalized */, 0 /* stride */, 0 /* offset */);
+    glEnableVertexAttribArray(texCoordLocation);
+
+    const int textureLocation = glGetUniformLocation(m_program, "u_image");
+    glUniform1i(textureLocation, 0);
+    glActiveTexture(GL_TEXTURE0);
+    glBindTexture(GL_TEXTURE_2D, m_texture);
+
+    glDrawArrays(GL_TRIANGLE_STRIP, 0 /* first */, 4 /* count */);
+    //glFinish();
+    //LOGI("Finished: %llu", getTime() - current);
 }
 
 void Renderer::setTexture(int texture)

+ 3 - 0
jni/renderer.h

@@ -1,6 +1,8 @@
 #ifndef RENDERER_H
 #define RENDERER_H
 
+#include <GLES2/gl2.h>
+
 class Renderer
 {
 public:
@@ -13,6 +15,7 @@ public:
     void setTexture(int texture);
 
 private:
+    GLuint m_program, m_texCoordArrayBuffer, m_vertexArrayBuffer;
     int m_texture;
 };
 

+ 1 - 1
res/values/strings.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="app_name">MainActivity</string>
+    <string name="app_name">GlDroid</string>
 </resources>

+ 1 - 0
src/in/forwardbias/gldroid/MainActivity.java

@@ -32,6 +32,7 @@ class MyGLSurfaceView extends GLSurfaceView
 {
     MyGLSurfaceView(Context context) {
         super(context);
+        setEGLContextClientVersion(2);
         MyRenderer renderer = new MyRenderer(context);
         setRenderer(renderer);
     }