Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef GLES3JNI_H
     18 #define GLES3JNI_H 1
     19 
     20 #include <android/log.h>
     21 #include <math.h>
     22 
     23 #if DYNAMIC_ES3
     24 #include "gl3stub.h"
     25 #else
     26 #include <GLES3/gl3.h>
     27 #endif
     28 
     29 #define DEBUG 1
     30 
     31 #define LOG_TAG "GLES3JNI"
     32 #define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
     33 #if DEBUG
     34 #define ALOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
     35 #else
     36 #define ALOGV(...)
     37 #endif
     38 
     39 // ----------------------------------------------------------------------------
     40 // Types, functions, and data used by both ES2 and ES3 renderers.
     41 // Defined in gles3jni.cpp.
     42 
     43 #define MAX_INSTANCES_PER_SIDE 16
     44 #define MAX_INSTANCES   (MAX_INSTANCES_PER_SIDE * MAX_INSTANCES_PER_SIDE)
     45 #define TWO_PI          (2.0 * M_PI)
     46 #define MAX_ROT_SPEED   (0.3 * TWO_PI)
     47 
     48 // This demo uses three coordinate spaces:
     49 // - The model (a quad) is in a [-1 .. 1]^2 space
     50 // - Scene space is either
     51 //    landscape: [-1 .. 1] x [-1/(2*w/h) .. 1/(2*w/h)]
     52 //    portrait:  [-1/(2*h/w) .. 1/(2*h/w)] x [-1 .. 1]
     53 // - Clip space in OpenGL is [-1 .. 1]^2
     54 //
     55 // Conceptually, the quads are rotated in model space, then scaled (uniformly)
     56 // and translated to place them in scene space. Scene space is then
     57 // non-uniformly scaled to clip space. In practice the transforms are combined
     58 // so vertices go directly from model to clip space.
     59 
     60 struct Vertex {
     61     GLfloat pos[2];
     62     GLubyte rgba[4];
     63 };
     64 extern const Vertex QUAD[4];
     65 
     66 // returns true if a GL error occurred
     67 extern bool checkGlError(const char* funcName);
     68 extern GLuint createShader(GLenum shaderType, const char* src);
     69 extern GLuint createProgram(const char* vtxSrc, const char* fragSrc);
     70 
     71 // ----------------------------------------------------------------------------
     72 // Interface to the ES2 and ES3 renderers, used by JNI code.
     73 
     74 class Renderer {
     75 public:
     76     virtual ~Renderer();
     77     void resize(int w, int h);
     78     void render();
     79 
     80 protected:
     81     Renderer();
     82 
     83     // return a pointer to a buffer of MAX_INSTANCES * sizeof(vec2).
     84     // the buffer is filled with per-instance offsets, then unmapped.
     85     virtual float* mapOffsetBuf() = 0;
     86     virtual void unmapOffsetBuf() = 0;
     87     // return a pointer to a buffer of MAX_INSTANCES * sizeof(vec4).
     88     // the buffer is filled with per-instance scale and rotation transforms.
     89     virtual float* mapTransformBuf() = 0;
     90     virtual void unmapTransformBuf() = 0;
     91 
     92     virtual void draw(unsigned int numInstances) = 0;
     93 
     94 private:
     95     void calcSceneParams(unsigned int w, unsigned int h, float* offsets);
     96     void step();
     97 
     98     unsigned int mNumInstances;
     99     float mScale[2];
    100     float mAngularVelocity[MAX_INSTANCES];
    101     uint64_t mLastFrameNs;
    102     float mAngles[MAX_INSTANCES];
    103 };
    104 
    105 extern Renderer* createES2Renderer();
    106 extern Renderer* createES3Renderer();
    107 
    108 #endif // GLES3JNI_H
    109