Steward
分享是一種喜悅、更是一種幸福
手機 - Motorola XT897 - Sailfish OS 4.4.0.68 - Wayland (OpenGL ES 2.0) - Draw Triangle
參考資訊:
https://jan.newmarch.name/Wayland/index.html
https://wayland.freedesktop.org/docs/html/apa.html
https://bugaevc.gitbooks.io/writing-wayland-clients/content/
https://gist.github.com/Miouyouyou/ca15af1c7f2696f66b0e013058f110b4
main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | #include <string.h> #include <stdlib.h> #include <unistd.h> #include <wayland-server.h> #include <wayland-client.h> #include <wayland-client-protocol.h> #include <wayland-egl.h> #include <EGL/egl.h> #include <EGL/eglplatform.h> #include <GLES2/gl2.h> #define LCD_W 540 #define LCD_H 960 struct wl_shell * wl_shell = NULL ; struct wl_region * wl_region = NULL ; struct wl_display * wl_display = NULL ; struct wl_surface * wl_surface = NULL ; struct wl_registry * wl_registry = NULL ; struct wl_egl_window * wl_egl_window = NULL ; struct wl_compositor * wl_compositor = NULL ; struct wl_shell_surface * wl_shell_surface = NULL ; const char *vShaderSrc = "attribute vec4 pos; \n" "void main() { \n" " gl_Position = pos; \n" "}\n" ; const char *fShaderSrc = "precision mediump float; \n" "void main() { \n" " gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0); \n" "}\n" ; static void global_registry_handler ( void * data , struct wl_registry *registry, uint32_t id, const char * interface , uint32_t version) { if ( strcmp ( interface , "wl_compositor" ) == 0) { wl_compositor = wl_registry_bind (registry, id, & wl_compositor_interface , 1); } else if ( strcmp ( interface , "wl_shell" ) == 0) { wl_shell = wl_registry_bind (registry, id, & wl_shell_interface , 1); } } static void global_registry_remover ( void * data , struct wl_registry *registry, uint32_t id) { } const struct wl_registry_listener listener = { global_registry_handler , global_registry_remover }; int main ( int argc, char **argv) { wl_display = wl_display_connect ( NULL ); wl_registry = wl_display_get_registry ( wl_display ); wl_registry_add_listener ( wl_registry , &listener, NULL ); wl_display_dispatch ( wl_display ); wl_display_roundtrip ( wl_display ); wl_surface = wl_compositor_create_surface ( wl_compositor ); wl_shell_surface = wl_shell_get_shell_surface ( wl_shell , wl_surface ); wl_shell_surface_set_toplevel ( wl_shell_surface ); wl_region = wl_compositor_create_region ( wl_compositor ); wl_region_add ( wl_region , 0, 0, LCD_W , LCD_H ); wl_surface_set_opaque_region ( wl_surface , wl_region ); wl_egl_window = wl_egl_window_create ( wl_surface , LCD_W , LCD_H ); EGLContext egl_context; EGLSurface egl_surface; EGLint egl_numConfigs; EGLint egl_majorVersion; EGLint egl_minorVersion; EGLConfig egl_config; EGLint egl_attribs[] = { EGL_SURFACE_TYPE , EGL_WINDOW_BIT , EGL_RENDERABLE_TYPE , EGL_OPENGL_ES2_BIT , EGL_RED_SIZE , 8, EGL_GREEN_SIZE , 8, EGL_BLUE_SIZE , 8, EGL_NONE }; EGLint egl_context_attribs[] = { EGL_CONTEXT_CLIENT_VERSION , 2, EGL_NONE , EGL_NONE }; EGLDisplay egl_display = eglGetDisplay ( wl_display ); eglInitialize (egl_display, &egl_majorVersion, &egl_minorVersion); eglGetConfigs (egl_display, NULL , 0, &egl_numConfigs); eglChooseConfig (egl_display, egl_attribs, &egl_config, 1, &egl_numConfigs); egl_surface = eglCreateWindowSurface (egl_display, egl_config, wl_egl_window , NULL ); egl_context = eglCreateContext (egl_display, egl_config, EGL_NO_CONTEXT , egl_context_attribs); eglMakeCurrent (egl_display, egl_surface, egl_surface, egl_context); wl_display_dispatch_pending ( wl_display ); glViewport (0, 0, LCD_W , LCD_H ); glClearColor (0.0, 0.0, 0.0, 1.0); glClear ( GL_COLOR_BUFFER_BIT ); GLuint vShader = 0; GLuint fShader = 0; GLuint pObject = 0; vShader = glCreateShader ( GL_VERTEX_SHADER ); glShaderSource (vShader, 1, &vShaderSrc, NULL ); glCompileShader (vShader); fShader = glCreateShader ( GL_FRAGMENT_SHADER ); glShaderSource (fShader, 1, &fShaderSrc, NULL ); glCompileShader (fShader); pObject = glCreateProgram (); glAttachShader (pObject, vShader); glAttachShader (pObject, fShader); glLinkProgram (pObject); glUseProgram (pObject); GLfloat v[] = { 0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f }; glViewport (0, 0, LCD_W , LCD_H ); glClear ( GL_COLOR_BUFFER_BIT ); glVertexAttribPointer (0, 3, GL_FLOAT , GL_FALSE , 0, v); glEnableVertexAttribArray (0); glDrawArrays ( GL_TRIANGLES , 0, 3); eglSwapBuffers (egl_display, egl_surface); usleep (3000000); eglDestroySurface (egl_display, egl_surface); eglDestroyContext (egl_display, egl_context); wl_egl_window_destroy ( wl_egl_window ); eglTerminate (egl_display); wl_region_destroy ( wl_region ); wl_shell_surface_destroy ( wl_shell_surface ); wl_shell_destroy ( wl_shell ); wl_surface_destroy ( wl_surface ); wl_compositor_destroy ( wl_compositor ); wl_registry_destroy ( wl_registry ); wl_display_disconnect ( wl_display ); return 0; } |
編譯、執行
$ gcc main.c -o main -lwayland-client -lwayland-egl -lEGL -lGLESv2 $ ./main