Skip to content

Instantly share code, notes, and snippets.

@lag945
Last active July 15, 2022 03:25
Show Gist options
  • Save lag945/77cf3e2d81c5a0cf3fbbd0d01616413b to your computer and use it in GitHub Desktop.
Save lag945/77cf3e2d81c5a0cf3fbbd0d01616413b to your computer and use it in GitHub Desktop.
Desktop 3D with EGL+ANGLE+Ubuntu18.04LTS
#include <X11/Xlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <EGL/egl.h>
#include <EGL/eglplatform.h>
#include <GLES3/gl3.h>
//g++ -out eglX11 eglX11.cpp -lX11 -lEGL-lGLESv2 -I<angle>/include -L<angle>/Release -Wl,-rpath=<angle>/Release
//./eglX11
using namespace std;
void outputGLESInfo() {
cout << "GL_VENDOR = " << glGetString(GL_VENDOR) << "\n";
cout << "GL_RENDERER = " << glGetString(GL_RENDERER) << "\n";
cout << "GL_VERSION = " << glGetString(GL_VERSION) << "\n";
cout << "GL_SHADING_LANGUAGE_VERSION = " << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
//TL;DW
/*cout << "Extensions :\n";
string extBuffer;
stringstream extStream;
extStream << glGetString(GL_EXTENSIONS);
while (extStream >> extBuffer) {
cout << extBuffer << "\n";
}
*/
}
int main(void) {
Display* d;
Window w;
XEvent e;
const char* msg = "Hello, World!";
int s;
d = XOpenDisplay(NULL);
if (d == NULL) {
fprintf(stderr, "Cannot open display\n");
exit(1);
}
s = DefaultScreen(d);
w = XCreateSimpleWindow(d, RootWindow(d, s), 10, 10, 800, 600, 1,
BlackPixel(d, s), WhitePixel(d, s));
XSelectInput(d, w, ExposureMask | KeyPressMask);
XMapWindow(d, w);
EGLDisplay eglDisplay = eglGetDisplay(d);
if (eglDisplay == EGL_NO_DISPLAY) {
cout << "Could not get egl display!" << endl;
return 1;
}
EGLint eglVersionMajor, eglVersionMinor;
eglInitialize(eglDisplay, &eglVersionMajor, &eglVersionMinor);
eglBindAPI(EGL_OPENGL_ES_API);
EGLint configAttributes[] =
{
EGL_RED_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_BLUE_SIZE, 8,
EGL_ALPHA_SIZE, 8,
EGL_DEPTH_SIZE, 24,
EGL_STENCIL_SIZE, 8,
EGL_SAMPLE_BUFFERS, 0,
EGL_SAMPLES, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
EGL_NONE
};
EGLint surfaceAttributes[] = { EGL_NONE };
EGLint contextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE };
EGLint nrOfConfigs;
EGLConfig windowConfig;
eglChooseConfig(eglDisplay, configAttributes, &windowConfig, 2, &nrOfConfigs);
EGLSurface eglSurface = eglCreateWindowSurface(eglDisplay, windowConfig, w, surfaceAttributes);
if (eglSurface == EGL_NO_SURFACE) {
cerr << "Could not create EGL surface : " << eglGetError() << endl;
return 1;
}
EGLContext eglContext = eglCreateContext(eglDisplay, windowConfig, NULL, contextAttributes);
if (eglContext == EGL_NO_CONTEXT) {
cout << "Could not create EGL context : " << eglGetError() << endl;
return 1;
}
cout << "EGL Version = " << eglQueryString(eglDisplay, EGL_VERSION) << "\n";
cout << "EGL Vendor = " << eglQueryString(eglDisplay, EGL_VENDOR) << "\n";
cout << "EGL Client APIs : \n" << eglQueryString(eglDisplay, EGL_CLIENT_APIS) << "\n";
eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext);
outputGLESInfo();
//init scene
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
const char* vertex_shader =
"#version 100\n"
"attribute vec3 vp;"
"void main() {"
" gl_Position = vec4(vp, 1.0);"
"}";
const char* fragment_shader =
"#version 100\n"
"void main() {"
" gl_FragColor = vec4(0.5, 0.0, 0.5, 1.0);"
"}";
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLint vertex_compiled;
glGetShaderiv(vs, GL_COMPILE_STATUS, &vertex_compiled);
if (vertex_compiled != GL_TRUE)
{
GLsizei log_length = 0;
GLchar message[1024];
glGetShaderInfoLog(vs, 1024, &log_length, message);
// Write the error to a log
cout << "vertex_compiled: " << message << endl;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
GLint fragment_compiled;
glGetShaderiv(fs, GL_COMPILE_STATUS, &fragment_compiled);
if (fragment_compiled != GL_TRUE)
{
GLsizei log_length = 0;
GLchar message[1024];
glGetShaderInfoLog(fs, 1024, &log_length, message);
// Write the error to a log
cout << "fragment_compiled: " << message << endl;
}
GLuint shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
while (1) {
while (XPending(d))
{
XNextEvent(d, &e);
}
//render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glUseProgram(shader_programme);
glBindVertexArray(vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 3);
eglSwapBuffers(eglDisplay, eglSurface);
// cout << "in" << endl;
}
XCloseDisplay(d);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment