How do I get EGL and OpenGLES libraries for Ubuntu running on VirtualBox?

469

Solution 1

GLFW, Mesa, Ubuntu 16.04 AMD64

I haven't tried it inside of Virtual Box, but this should work regardless since Mesa has a software implementation.

sudo apt-get install libglfw3-dev libgles2-mesa-dev
gcc glfw_triangle.c -lGLESv2 -lglfw

Output:

Source:

#include <stdio.h>
#include <stdlib.h>

#define GLFW_INCLUDE_ES2
#include <GLFW/glfw3.h>

static const GLuint WIDTH = 800;
static const GLuint HEIGHT = 600;
static const GLchar* vertex_shader_source =
    "#version 100\n"
    "attribute vec3 position;\n"
    "void main() {\n"
    "   gl_Position = vec4(position, 1.0);\n"
    "}\n";
static const GLchar* fragment_shader_source =
    "#version 100\n"
    "void main() {\n"
    "   gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
    "}\n";
static const GLfloat vertices[] = {
        0.0f,  0.5f, 0.0f,
        0.5f, -0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f,
};

GLint common_get_shader_program(const char *vertex_shader_source, const char *fragment_shader_source) {
    enum Consts {INFOLOG_LEN = 512};
    GLchar infoLog[INFOLOG_LEN];
    GLint fragment_shader;
    GLint shader_program;
    GLint success;
    GLint vertex_shader;

    /* Vertex shader */
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_source, NULL);
    glCompileShader(vertex_shader);
    glGetShaderiv(vertex_shader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(vertex_shader, INFOLOG_LEN, NULL, infoLog);
        printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n%s\n", infoLog);
    }

    /* Fragment shader */
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, &fragment_shader_source, NULL);
    glCompileShader(fragment_shader);
    glGetShaderiv(fragment_shader, GL_COMPILE_STATUS, &success);
    if (!success) {
        glGetShaderInfoLog(fragment_shader, INFOLOG_LEN, NULL, infoLog);
        printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n%s\n", infoLog);
    }

    /* Link shaders */
    shader_program = glCreateProgram();
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, fragment_shader);
    glLinkProgram(shader_program);
    glGetProgramiv(shader_program, GL_LINK_STATUS, &success);
    if (!success) {
        glGetProgramInfoLog(shader_program, INFOLOG_LEN, NULL, infoLog);
        printf("ERROR::SHADER::PROGRAM::LINKING_FAILED\n%s\n", infoLog);
    }

    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);
    return shader_program;
}

int main(void) {
    GLuint shader_program, vbo;
    GLint pos;
    GLFWwindow* window;

    glfwInit();
    glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    window = glfwCreateWindow(WIDTH, HEIGHT, __FILE__, NULL, NULL);
    glfwMakeContextCurrent(window);

    printf("GL_VERSION  : %s\n", glGetString(GL_VERSION) );
    printf("GL_RENDERER : %s\n", glGetString(GL_RENDERER) );

    shader_program = common_get_shader_program(vertex_shader_source, fragment_shader_source);
    pos = glGetAttribLocation(shader_program, "position");

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glViewport(0, 0, WIDTH, HEIGHT);

    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    glEnableVertexAttribArray(pos);
    glBindBuffer(GL_ARRAY_BUFFER, 0);

    while (!glfwWindowShouldClose(window)) {
        glfwPollEvents();
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(shader_program);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
    }
    glDeleteBuffers(1, &vbo);
    glfwTerminate();
    return EXIT_SUCCESS;
}

The key line lines of code are:

#define GLFW_INCLUDE_ES2
#include <GLFW/glfw3.h>

GLFW_INCLUDE_ES2 is documented at: http://www.glfw.org/docs/latest/build_guide.html#build_macros and a quick look at the source shows that it forwards to GLES:

 #elif defined(GLFW_INCLUDE_ES2)
  #include <GLES2/gl2.h>
  #if defined(GLFW_INCLUDE_GLEXT)
   #include <GLES2/gl2ext.h>
  #endif

This source seems to be is in the common subset of GLES and OpenGL (like much of GLES), and also compiles with -lGL if we remove the #define GLFW_INCLUDE_ES2.

If we add things which are not in GLES like immediate rendering glBegin, link fails as expected.

See also: https://stackoverflow.com/questions/3809236/how-to-develop-opengl-es-gles-2-0-applications-on-linux/39356268#39356268

Credits: genpfult made the code much more correct.

ARM Mali OpenGL ES SDK

Contains several interesting open source examples + windowing system boilerplate (X11 + EGL).

The build system supports easy cross compilation for ARM / Mali SoCs, but I haven't tested that yet.

The key component included seems to be the "OpenGL ES Emulator" http://malideveloper.arm.com/resources/tools/opengl-es-emulator/ which "maps OpenGL ES 3.2 API calls to the OpenGL API". But that does not ship with source, only precompiled.

Uses a custom enterprisey EULA that appears to be permissive, but yeah, ask your lawyer.

Tested on SDK v2.4.4.

Solution 2

You can search for packages and package contents with apt-cache:

> apt-cache search opengles 
mesa-utils-extra - Miscellaneous Mesa utilies (opengles, egl)

The output says that OpenGLES probably is in the package mesa-utils-extra. Mesa 3D has a project page for OpenGLES and writes there:

Mesa implements OpenGL ES 1.1 and OpenGL ES 2.0. More informations about OpenGL ES can be found at http://www.khronos.org/opengles/.

EGL is also built into Mesa:

> apt-cache search mesa | grep -i egl
mesa-utils-extra - Miscellaneous Mesa utilies (opengles, egl)
libegl1-mesa - free implementation of the EGL API -- runtime
libegl1-mesa-dbg - free implementation of the EGL API -- debugging symbols
libegl1-mesa-dev - free implementation of the EGL API -- development files
libegl1-mesa-drivers - free implementation of the EGL API -- hardware drivers
libegl1-mesa-drivers-dbg - free implementation of the EGL API -- driver debugging symbols

So you need to install mesa-utils-extra and probably also libegl1-mesa.

Solution 3

Since the question has been asked, a package appeared and could help :

sudo apt-get install libgles2-mesa-dev

Solution 4

Try ARM OpenGL ES 2.0 Emulator, I myself haven't managed to make OpenGL ES 2.0 work, but 1.1 seems to run fine(simpleApp demo). As I understand, it's supposed to be hardware accelerated as the emulator uses platform GL libraries and mesa3d is(not sure though) accelerated.

There's also libgles2-mesa - but unfortunately I couldn't make it work. es2gears/es2tri samples crash as well as simpleApp linked against mesa libs.

Share:
469

Related videos on Youtube

Ehsan
Author by

Ehsan

Updated on September 18, 2022

Comments

  • Ehsan
    Ehsan almost 2 years

    I am going to change wordpress permalinks and will do 301 redirect from old to new URL's. But the problem is how can i keep my social sharing counts (like facebook,twitter, google +1) after the URL's are changed. Thanks

    • Jason Southwell
      Jason Southwell over 11 years
      It is very difficult to tell exactly what you are asking, can you be a bit more detailed?
  • vboxuser
    vboxuser over 11 years
    Thank you very much for the reply. But Mesa do not use VirtualBox virtual GPU for hardware acceleration. While running Mesa on VBox it uses software rasterizer. My requirement is to strictly use the virtual box 3D acceleration for Opengles demos.
  • qbi
    qbi over 11 years
    So maybe we should watch this/your question here: forums.virtualbox.org/…