HelloAPI Header File#

Note

OpenGL Unsigned Integers and Unsigned Bytes are used in the following code snippets.

/*!***************************************************************************
 \File         OpenGLESHelloAPI.h
 \Title        OpenGL ES Hello API Header
 \Author       PowerVR by Imagination, Developer Technology Team.
 \Copyright    Copyright(c) Imagination Technologies Limited.
 \brief        Header file for OpenGLESHelloAPI class.
 *****************************************************************************/
 #pragma once

 #define DYNAMICGLES_NO_NAMESPACE
 #define DYNAMICEGL_NO_NAMESPACE
 #include <DynamicGles.h>
 #include <vector>

 #if defined(SUPPORT_X11)
 #include "X11/Xlib.h"
 #include "X11/Xutil.h"
 #endif

 #if defined(SUPPORT_WAYLAND)
 #include <wayland-client.h>
 #include <wayland-server.h>
 #include <wayland-egl.h>
 #include <linux/input.h>
 #endif

 #if defined(__GBM__)
 #include <xf86drm.h>
 #include <xf86drmMode.h>
 #include <gbm.h>
 #endif

SurfaceData is dependent on platform, so check which platform is currently being used, then define appropriate surface data.

struct SurfaceData
{
   float width = 1280.0f, height = 800.0f;

#if defined(_Win32)
   HDC deviceContext = NULL;
   HWND window = NULL;
#elif defined(ANDROID) || (__ANDROID__)
   ANativeWindow* window;
   EGLNativeDisplayType deviceContext = 0;
#elif defined(APPLE) || defined(SUPPORT_APPLE)
   int deviceContext = (EGLNativeDisplayType)0;
   void* window = NULL;
#elif defined(SUPPORT_X11)
   Display* deviceContext = 0;
   Window window = NULL;
#elif defined(SUPPORT_WAYLAND)
   wl_display* deviceContext = NULL;
   wl_egl_window* window = NULL;
#elif defined(__GBM__)
   gbm_device* deviceContext = NULL;
   gbm_surface* window = NULL;
#else // NullWS
   EGLNativeDisplayType deviceContext = (EGLNativeDisplayType)0;
   EGLNativeWindowType window = (EGLNativeWindowType)0;
   SurfaceData() = default;
#endif
};

static const char* procAddressMessageTypes[] = {
   "INFORMATION: ",
   "ERROR: ",
};

inline void logOutput(bool error, const char* const formatString, va_list argumentList)
{
#if defined(__ANDROID__)

Note

There may be issues displaying 64bit values with this function.

This function will truncate long messages.

   __android_log_vprint(error ? ANDROID_LOG_ERROR : ANDROID_LOG_INFO, "com.powervr.Example", formatString, argumentList);
#elif defined(__QNXNTO__)
   vslogf(1, error ? _SLOG_INFO : _SLOG_ERROR, formatString, argumentList);
#else // Not android Not QNX
   FILE* logfile = fopen("Log.txt", "w");
   static char buffer[4096];
   va_list tempList;
   memset(buffer, 0, sizeof(buffer));
#if (defined _MSC_VER) // Pre VS2013
   tempList = argumentList;
#else
   va_copy(tempList, argumentList);
#endif

   vsnprintf(buffer, 4095, formatString, argumentList);
   fprintf(logfile, "%s%s\n", procAddressMessageTypes[(int)error], buffer);
   fclose(logfile);
#if defined(_WIN32)

   if (IsDebuggerPresent())
   {
       OutputDebugString(procAddressMessageTypes[(int)error]);
       OutputDebugString(buffer);
       OutputDebugString("\n");
   }
   else
#endif
   {
       printf("%s%s\n", procAddressMessageTypes[error], buffer);
   }
#endif
}

inline void Log(bool error, const char* const formatString, ...)
{
   va_list argumentList;
   va_start(argumentList, formatString);
   logOutput(error, formatString, argumentList);
   va_end(argumentList);
}

glm includes for matrix and transformation calculations

#define GLM_ENABLE_EXPERIMENTAL
#include "../../../external/glm/glm.hpp"
#include "../../../external/glm/gtc/type_ptr.hpp"
#include "../../../external/glm/gtx/transform.hpp"

Image data

#define imageWidth 256
#define imageHeight 256

class OpenGLESAPI
{
private:

Functions used to set up EGL state.

bool createEGLDisplay();
bool chooseEGLConfig(EGLConfig& config);
bool createEGLSurface(EGLConfig config);
bool setupEGLContext(EGLConfig config, EGLContext& context);

Handles for the two shaders used to draw the triangle, and the program handle which combines them.

GLuint _fragmentShader, _vertexShader;
GLuint _shaderProgram;

A vertex buffer object to store the model data.

GLuint _vertexBuffer;

Handle to the texture used in application.

GLuint _texture;

Index to bind the attributes to vertex shaders.

const unsigned int _vertexArray = 0;
const unsigned int _textureArray = 1;

Count rotation angle.

float _angle;

OpenGL ES initialisation functions.

void generateTextureData();
bool initializeTexture();
bool initializeShaders();
bool initializeBuffer();

Error check functions.

bool test-egl-error(SurfaceData surfaceData, const char* functionLastCalled);
bool test-gl-error(SurfaceData surfaceData, const char* functionLastCalled);

EGL objects are required to be permanent, for accessing in OpenGL ES functions.

   EGLSurface _eglSurface;
   EGLDisplay _eglDisplay;

public:

Initialise EGL and GLES states.

bool initializeGLES();
bool initializeEGL();

Per frame render.

bool renderScene();
bool swapEGLBuffers();
bool drawFrame();

Release EGL state and objects.

void releaseEGLState();

De-initialise GL state and objects.

void releaseGLState();

Surface data relevant to current build platform.

SurfaceData _surfaceData;

Declaration of image data.

   GLubyte _image[imageWidth][imageHeight][4];
};