androidcopengl-es-2.0native-activity

GLES 2 Functions Not Working in Visual Studio


I'm using Visual Studio Community 2017, but the native-activity in this version is the same as in Visual Studio Community 2015.

I have just created a new native activity project in Visual Studio 2017, using the default program that loads with this project choice. However, whenever I try to use a function exclusive to open gl es 2, Visual Studio responds with an error like "error : undefined reference to 'glCreateShader' " for each open gl es 2 function that I use. Strangely, the following code works, even though I deleted the line in the pch file that includes open gl es 1:

glClearColor(200, 200, 200, 1);
glClear(GL_COLOR_BUFFER_BIT);

The following function if put into the default native activity project (even after including GLES2/gl2.h, GLES2/gl2ext.h, and GLES2/gl2platform.h) results in the error described above:

int prepareShader(GLuint shaderType, const char * shaderCode) {
GLuint shader = glCreateShader(shaderType);
int len = strlen(shaderCode);
glShaderSource(shader, 1, &shaderCode, &len);

}

I remember a similar error occurring in Android Studio, which was fixed by adding the following to cmakelists.txt:

find_library(gl-lib
         GLESv2 )

find_library(egl-lib
         EGL )

target_link_libraries( # Specifies the target library.
                   native-lib
                   ${egl-lib}
                   ${gl-lib})

My Visual Studio project does not use CMake, however.

Can somebody please tell me how to successfully use open gl es 2 in Visual Studio 2017?


Solution

  • The solution to my problem is to load the open gl functions using eglGetProcAddress. This just means that each opengl es function must be set to a pointer before using them because GLES2/gl2.h does not define the functions (the functions are defined at run time).

    Doing something like this for each function solves the problem. (However, I needed to delete the function prototypes in GLES2/gl2.h for this to work).

    typedef GLuint(GL_APIENTRYP PFNGLCREATESHADERPROC) (GLenum type);
    PFNGLCREATESHADERPROC glCreateShader = (PFNGLCREATESHADERPROC)eglGetProcAddress("glCreateShader");