|
| 1 | +#include <stdio.h> |
| 2 | +#include <fcntl.h> |
| 3 | +#include <stdlib.h> |
| 4 | +#include <string.h> |
| 5 | +#include <math.h> |
| 6 | +#include <assert.h> |
| 7 | +#include <unistd.h> |
| 8 | +#include "bcm_host.h" |
| 9 | +#include "dashgl.h" |
| 10 | + |
| 11 | +#define check() assert(glGetError() == 0) |
| 12 | + |
| 13 | +uint32_t GScreenWidth; |
| 14 | +uint32_t GScreenHeight; |
| 15 | +EGLDisplay GDisplay; |
| 16 | +EGLSurface GSurface; |
| 17 | +EGLContext GContext; |
| 18 | + |
| 19 | +void dgl_init_graphics() { |
| 20 | + |
| 21 | + bcm_host_init(); |
| 22 | + int32_t success = 0; |
| 23 | + EGLBoolean result; |
| 24 | + EGLint num_config; |
| 25 | + |
| 26 | + static EGL_DISPMANX_WINDOW_T nativewindow; |
| 27 | + |
| 28 | + DISPMANX_ELEMENT_HANDLE_T dispman_element; |
| 29 | + DISPMANX_DISPLAY_HANDLE_T dispman_display; |
| 30 | + DISPMANX_UPDATE_HANDLE_T dispman_update; |
| 31 | + VC_RECT_T dst_rect; |
| 32 | + VC_RECT_T src_rect; |
| 33 | + |
| 34 | + static const EGLint attribute_list[] = { |
| 35 | + EGL_RED_SIZE, 8, |
| 36 | + EGL_GREEN_SIZE, 8, |
| 37 | + EGL_BLUE_SIZE, 8, |
| 38 | + EGL_ALPHA_SIZE, 8, |
| 39 | + EGL_DEPTH_SIZE, 16, // You need this line for depth buffering to work |
| 40 | + EGL_SURFACE_TYPE, EGL_WINDOW_BIT, |
| 41 | + EGL_NONE |
| 42 | + }; |
| 43 | + |
| 44 | + static const EGLint context_attributes[] = { |
| 45 | + EGL_CONTEXT_CLIENT_VERSION, 2, |
| 46 | + EGL_NONE |
| 47 | + }; |
| 48 | + EGLConfig config; |
| 49 | + |
| 50 | + // get an EGL display connection |
| 51 | + GDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 52 | + assert(GDisplay!=EGL_NO_DISPLAY); |
| 53 | + check(); |
| 54 | + |
| 55 | + // initialize the EGL display connection |
| 56 | + result = eglInitialize(GDisplay, NULL, NULL); |
| 57 | + assert(EGL_FALSE != result); |
| 58 | + check(); |
| 59 | + |
| 60 | + // get an appropriate EGL frame buffer configuration |
| 61 | + result = eglChooseConfig(GDisplay, attribute_list, &config, 1, &num_config); |
| 62 | + assert(EGL_FALSE != result); |
| 63 | + check(); |
| 64 | + |
| 65 | + // get an appropriate EGL frame buffer configuration |
| 66 | + result = eglBindAPI(EGL_OPENGL_ES_API); |
| 67 | + assert(EGL_FALSE != result); |
| 68 | + check(); |
| 69 | + |
| 70 | + // create an EGL rendering context |
| 71 | + GContext = eglCreateContext(GDisplay, config, EGL_NO_CONTEXT, context_attributes); |
| 72 | + assert(GContext!=EGL_NO_CONTEXT); |
| 73 | + check(); |
| 74 | + |
| 75 | + // create an EGL window surface |
| 76 | + success = graphics_get_display_size(0 /* LCD */, &GScreenWidth, &GScreenHeight); |
| 77 | + assert( success >= 0 ); |
| 78 | + |
| 79 | + dst_rect.x = 0; |
| 80 | + dst_rect.y = 0; |
| 81 | + dst_rect.width = GScreenWidth; |
| 82 | + dst_rect.height = GScreenHeight; |
| 83 | + |
| 84 | + src_rect.x = 0; |
| 85 | + src_rect.y = 0; |
| 86 | + src_rect.width = GScreenWidth << 16; |
| 87 | + src_rect.height = GScreenHeight << 16; |
| 88 | + |
| 89 | + dispman_display = vc_dispmanx_display_open( 0 /* LCD */); |
| 90 | + dispman_update = vc_dispmanx_update_start( 0 ); |
| 91 | + |
| 92 | + dispman_element = vc_dispmanx_element_add ( dispman_update, dispman_display, |
| 93 | + 0/*layer*/, &dst_rect, 0/*src*/, |
| 94 | + &src_rect, DISPMANX_PROTECTION_NONE, 0 /*alpha*/, 0/*clamp*/, (DISPMANX_TRANSFORM_T)0/*transform*/); |
| 95 | + |
| 96 | + nativewindow.element = dispman_element; |
| 97 | + nativewindow.width = GScreenWidth; |
| 98 | + nativewindow.height = GScreenHeight; |
| 99 | + vc_dispmanx_update_submit_sync( dispman_update ); |
| 100 | + |
| 101 | + check(); |
| 102 | + |
| 103 | + GSurface = eglCreateWindowSurface( GDisplay, config, &nativewindow, NULL ); |
| 104 | + assert(GSurface != EGL_NO_SURFACE); |
| 105 | + check(); |
| 106 | + |
| 107 | + // connect the context to the surface |
| 108 | + result = eglMakeCurrent(GDisplay, GSurface, GSurface, GContext); |
| 109 | + assert(EGL_FALSE != result); |
| 110 | + check(); |
| 111 | + |
| 112 | + // Set background color and clear buffers |
| 113 | + glClearColor(0.15f, 0.25f, 0.35f, 1.0f); |
| 114 | + glClear( GL_COLOR_BUFFER_BIT ); |
| 115 | + |
| 116 | + glViewport ( 0, 0, GScreenWidth, GScreenHeight ); |
| 117 | + check(); |
| 118 | + |
| 119 | +} |
| 120 | + |
| 121 | +void dgl_update_screen() { |
| 122 | + eglSwapBuffers(GDisplay,GSurface); |
| 123 | +} |
| 124 | + |
| 125 | +void dgl_set_viewport() { |
| 126 | + glViewport ( 0, 0, GScreenWidth, GScreenHeight ); |
| 127 | +} |
| 128 | + |
| 129 | +static const char *eglGetErrorStr() { |
| 130 | + |
| 131 | + switch (eglGetError()) { |
| 132 | + case EGL_SUCCESS: |
| 133 | + return "The last function succeeded without error."; |
| 134 | + case EGL_NOT_INITIALIZED: |
| 135 | + return "EGL is not initialized, or could not be initialized, for the " |
| 136 | + "specified EGL display connection."; |
| 137 | + case EGL_BAD_ACCESS: |
| 138 | + return "EGL cannot access a requested resource (for example a context " |
| 139 | + "is bound in another thread)."; |
| 140 | + case EGL_BAD_ALLOC: |
| 141 | + return "EGL failed to allocate resources for the requested operation."; |
| 142 | + case EGL_BAD_ATTRIBUTE: |
| 143 | + return "An unrecognized attribute or attribute value was passed in the " |
| 144 | + "attribute list."; |
| 145 | + case EGL_BAD_CONTEXT: |
| 146 | + return "An EGLContext argument does not name a valid EGL rendering " |
| 147 | + "context."; |
| 148 | + case EGL_BAD_CONFIG: |
| 149 | + return "An EGLConfig argument does not name a valid EGL frame buffer " |
| 150 | + "configuration."; |
| 151 | + case EGL_BAD_CURRENT_SURFACE: |
| 152 | + return "The current surface of the calling thread is a window, pixel " |
| 153 | + "buffer or pixmap that is no longer valid."; |
| 154 | + case EGL_BAD_DISPLAY: |
| 155 | + return "An EGLDisplay argument does not name a valid EGL display " |
| 156 | + "connection."; |
| 157 | + case EGL_BAD_SURFACE: |
| 158 | + return "An EGLSurface argument does not name a valid surface (window, " |
| 159 | + "pixel buffer or pixmap) configured for GL rendering."; |
| 160 | + case EGL_BAD_MATCH: |
| 161 | + return "Arguments are inconsistent (for example, a valid context " |
| 162 | + "requires buffers not supplied by a valid surface)."; |
| 163 | + case EGL_BAD_PARAMETER: |
| 164 | + return "One or more argument values are invalid."; |
| 165 | + case EGL_BAD_NATIVE_PIXMAP: |
| 166 | + return "A NativePixmapType argument does not refer to a valid native " |
| 167 | + "pixmap."; |
| 168 | + case EGL_BAD_NATIVE_WINDOW: |
| 169 | + return "A NativeWindowType argument does not refer to a valid native " |
| 170 | + "window."; |
| 171 | + case EGL_CONTEXT_LOST: |
| 172 | + return "A power management event has occurred. The application must " |
| 173 | + "destroy all contexts and reinitialise OpenGL ES state and " |
| 174 | + "objects to continue rendering."; |
| 175 | + break; |
| 176 | + } |
| 177 | + |
| 178 | + return "Unknown error!"; |
| 179 | + |
| 180 | +} |
| 181 | + |
| 182 | + |
| 183 | + |
| 184 | +GLuint dgl_create_shader(const char *filename, GLenum type) { |
| 185 | + |
| 186 | + FILE *fp; |
| 187 | + int file_len; |
| 188 | + char *source; |
| 189 | + |
| 190 | + fp = fopen(filename, "rb"); |
| 191 | + if(!fp) { |
| 192 | + fprintf(stderr, "Could not open %s for reading\n", filename); |
| 193 | + return 0; |
| 194 | + } |
| 195 | + |
| 196 | + fseek(fp, 0, SEEK_END); |
| 197 | + file_len = ftell(fp); |
| 198 | + fseek(fp, 0, SEEK_SET); |
| 199 | + |
| 200 | + source = (char*)malloc(file_len + 1); |
| 201 | + fread(source, file_len, 1, fp); |
| 202 | + fclose(fp); |
| 203 | + source[file_len] = '\0'; |
| 204 | + |
| 205 | + const GLchar *sources[] = { |
| 206 | + #ifdef GL_ES_VERSION_2_0 |
| 207 | + "#version 100\n", //OpenGL ES 2.0 |
| 208 | + #else |
| 209 | + "#version 120\n", // OpenGL 2.1 |
| 210 | + #endif |
| 211 | + source |
| 212 | + }; |
| 213 | + |
| 214 | + GLuint shader = glCreateShader(type); |
| 215 | + //glShaderSource(shader, 2, sources, NULL); |
| 216 | + glShaderSource(shader, 2, sources, NULL); |
| 217 | + glCompileShader(shader); |
| 218 | + |
| 219 | + free((void*)source); |
| 220 | + |
| 221 | + GLint compile_ok; |
| 222 | + glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_ok); |
| 223 | + |
| 224 | + printf("Compile okay: %d\n", compile_ok); |
| 225 | + if(compile_ok == GL_FALSE) { |
| 226 | + fprintf(stderr, "%s: ", filename); |
| 227 | + dgl_print_log(shader); |
| 228 | + glDeleteShader(shader); |
| 229 | + return 0; |
| 230 | + } |
| 231 | + |
| 232 | + printf("Shader value: %d\n", shader); |
| 233 | + return shader; |
| 234 | + |
| 235 | +} |
| 236 | + |
| 237 | +GLuint dgl_create_program(const char *vertex, const char *fragment) { |
| 238 | + |
| 239 | + GLuint vs = dgl_create_shader(vertex, GL_VERTEX_SHADER); |
| 240 | + GLuint fs = dgl_create_shader(fragment, GL_FRAGMENT_SHADER); |
| 241 | + if(vs == 0 || fs == 0) { |
| 242 | + return 0; |
| 243 | + } |
| 244 | + |
| 245 | + GLint link_ok; |
| 246 | + |
| 247 | + GLuint program = glCreateProgram(); |
| 248 | + glAttachShader(program, vs); |
| 249 | + glAttachShader(program, fs); |
| 250 | + glLinkProgram(program); |
| 251 | + glGetProgramiv(program, GL_LINK_STATUS, &link_ok); |
| 252 | + if(!link_ok) { |
| 253 | + fprintf(stderr, "Program Link Error: "); |
| 254 | + dgl_print_log(program); |
| 255 | + return 0; |
| 256 | + } |
| 257 | + |
| 258 | + return program; |
| 259 | + |
| 260 | +} |
| 261 | + |
| 262 | +void dgl_print_log(GLuint object) { |
| 263 | + |
| 264 | + char *log; |
| 265 | + GLint log_length = 0; |
| 266 | + |
| 267 | + if(glIsShader(object)) { |
| 268 | + glGetShaderiv(object, GL_INFO_LOG_LENGTH, &log_length); |
| 269 | + } else if(glIsProgram(object)) { |
| 270 | + glGetProgramiv(object, GL_INFO_LOG_LENGTH, &log_length); |
| 271 | + } else { |
| 272 | + fprintf(stderr, "Not a shader or program\n"); |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + log = (char*)malloc(log_length); |
| 277 | + |
| 278 | + if(glIsShader(object)) { |
| 279 | + glGetShaderInfoLog(object, log_length, NULL, log); |
| 280 | + } else if(glIsProgram(object)) { |
| 281 | + glGetProgramInfoLog(object, log_length, NULL, log); |
| 282 | + } |
| 283 | + |
| 284 | + fprintf(stderr, "%s\n", log); |
| 285 | + free(log); |
| 286 | + |
| 287 | +} |
| 288 | + |
0 commit comments