File CMakeLists.txt
...
target_link_libraries(MyCPlusPlusActivity
android
native_app_glue
EGL
GLESv2
GLESv1_CM
log)
...
C++
#include <GLES2/gl2.h>
#include <GLES/gl.h>
...
void MyGame::DrawGraphic_OpenGL()
{
// green color
glClearColor(0.72f, 0.87f, 0.55f, 1);
glClear(GL_COLOR_BUFFER_BIT);
// draw line
XYZ points[] = {
{0, -0.5, 0}, // point1
{-1, 0.5, 0}, // point2
{+1, 0.5, 0}, // point3
};
RGBA colors[] = {
{0.0f, 0.0f, 1.0f, 1.0f}, // blue color for point1
{1.0f, 0.0f, 0.0f, 1.0f}, // red color for point2
{0.0f, 1.0f, 0.0f, 1.0f}, // green color for point3
};
glLineWidth(10);
DrawGraphic_Primitive(points, colors, 3, GL_TRIANGLES);
eglSwapBuffers(m_Display, m_Surface);
}
void MyGame::DrawGraphic_Primitive(XYZ* points, RGBA* colors, int count, int primitiveType)
{
glVertexPointer(sizeof(points[0])/sizeof(GLfloat) /*=3 it is count of fields: x,y,z*/, GL_FLOAT, 0, points);
glColorPointer(sizeof(colors[0])/sizeof(GLfloat) /*=4 it is count of fields: red,green,blue, alpha*/ , GL_FLOAT, 0, colors);
glDrawArrays(primitiveType, 0 /*first index to draw*/, count /*count of points to draw*/);
}
bool MyGame::InitGraphic_OpenGL(ANativeWindow* pWindow)
{
...
EGLint contextAttribs[] =
{
EGL_CONTEXT_CLIENT_VERSION, 2,
EGL_CONTEXT_CLIENT_VERSION, 1,
}
...
// enable draw lines and colors
glEnableClientState(GL_VERTEX_ARRAY);// allow to use glVertexPointer
glEnableClientState(GL_COLOR_ARRAY); // allow to use glColorPointer
return true;
}
...
include <android/input.h>
#include <android/asset_manager.h>
#include <EGL/egl.h>
#include <GLES/gl.h>
typedef struct
{
GLfloat red; // [0...1]
GLfloat green; // [0...1]
GLfloat blue; // [0...1]
GLfloat alpha; // [0...1]
} RGBA;
typedef struct
{
GLfloat x;
GLfloat y;
GLfloat z;
} XYZ;
class MyGame
{
...
// OpenGL
public: void DrawGraphic_Primitive(XYZ* points, RGBA* colors, int count, int primitiveType);
...
}
// draw line
XYZ points[] = {
{0, -0.5, 0}, // point1
{-1, 0.5, 0}, // point2
{+1, 0.5, 0}, // point3
};
RGBA colors[] = {
{0.0f, 0.0f, 1.0f, 1.0f}, // blue color for point1
{1.0f, 0.0f, 0.0f, 1.0f}, // red color for point2
{0.0f, 1.0f, 0.0f, 1.0f}, // green color for point3
};