Developing with the SDK Overlay

In this tutorial, you will learn how to:

  • How to render overlay on the scene and control the interaction with the overlay.

Contents

Tutorial

Include the overlay’s header file “wvr/wvr_overlay.h”.

After executing WVR_RenderInit(), get the overlay ID through WVR_GenOverlay().

#include <wvr/wvr_overlay.h>
int32_t mOverlayId;

void main() {
    WVR_RenderInit();

    // After WVR_RenderInit();
    WVR_GenOverlay(&mOverlayId);
}

After get the overlay id, set texture id which you want to show on scene.

#include <wvr/wvr_overlay.h>
int32_t mOverlayId;

void main() {
    GLuint textureId = 0;
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_2D, textureId);
    ...
    glBindTexture(GL_TEXTURE_2D, 0);
    uint32_t textureWidth = 100;
    uint32_t textureHeight = 100;
    const WVR_OverlayTexture_t texture = { textureId, textureWidth,
                                          textureHeight };
    WVR_SetOverlayTextureId(mOverlayId, &texture);
}

Assign the event handle method, and register the overlay event callback for interact with the overlay by controller.

#include <wvr/wvr_overlay.h>
int32_t mOverlayId;
WVR_OverlayInputEvent_t mOverlayEventCallback;

void handleInputEvent(int32_t id, WVR_EventType type, WVR_InputId inputId) {
    ....  // Perform action
}

void main() {
    mOverlayEventCallback.overlayId = mOverlayId;
    mOverlayEventCallback.callback = handleInputEvent;
    WVR_RegisterOverlayInputEvent(&mOverlayEventCallback);
}

Set the position of overlay.

void main() {
    const WVR_OverlayPosition position = {0.0f, 0.0f, -1.0f};
    WVR_SetOverlayFixedPosition(mOverlayId, &position);
}

Hide or show the overlay.

void main() {
    WVR_ShowOverlay(mOverlayId);
    WVR_HideOverlay(mOverlayId);
}

Release the resource of overlay if overlay is no longer needed.

void onDestroy() {
    WVR_UnregisterOverlayInputEvent(&mOverlayEventCallback);
    WVR_DelOverlay(mOverlayId);
}

Please refer to the following sample, the behavior of this sample is: press touchpad first time -> show overlay, presse touchpad second time -> hide overlay.

#include <wvr/wvr_overlay.h>
int32_t mOverlayId = -1;
GLuint mTextureId = 0;
WVR_OverlayInputEvent_t mOverlayEventCallback;

void handleInputEvent(int32_t id, WVR_EventType type, WVR_InputId inputId) {
    if (type == WVR_EventType_ButtonUnpressed) {
        if (inputId == WVR_InputId_Alias1_Touchpad) {
            if (WVR_IsOverlayShow(id))
                WVR_HideOverlay(id);
            else
                WVR_ShowOverlay(id);
        }
    }
}

void initOverlay() {
    WVR_GenOverlay(&mOverlayId);

    glGenTextures(1, &mTextureId);
    glBindTexture(GL_TEXTURE_2D, mTextureId);
    ...
    glBindTexture(GL_TEXTURE_2D, 0);
    uint32_t textureWidth = 100;
    uint32_t textureHeight = 100;
    const WVR_OverlayTexture_t texture = { mTextureId, textureWidth,
                                          textureHeight };
    WVR_SetOverlayTextureId(mOverlayId, &texture);

    mOverlayEventCallback.overlayId = mOverlayId;
    mOverlayEventCallback.callback = handleInputEvent;
    WVR_RegisterOverlayInputEvent(&mOverlayEventCallback);
}

void releaseOverlay() {
    WVR_UnregisterOverlayInputEvent(&mOverlayEventCallback);
    WVR_DelOverlay(mOverlayId);
    mOverlayId = -1;
    glDeleteTextures(1, &mTextureId);
    mTextureId = 0;
}

bool renderFrame() {
    // Overlay will be released after android suspend, so check the validity of overlay id per frame.
    if (!WVR_IsOverlayValid(mOverlayId)) {
        releaseOverlay();
        initOverlay();
    }
    ...
}

void main() {
    WVR_RenderInit();
    // After WVR_RenderInit();
    initOverlay();
    while (renderFrame()) {
        ...
    }
    releaseOverlay();
}