summaryrefslogtreecommitdiff
path: root/lib/wx/c_src/egl_impl.c
blob: 868b32ab790b16d10b281cec081e4156bddce5d9 (plain)
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
 * %CopyrightBegin%
 *
 * Copyright Ericsson AB 2011-2023. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * %CopyrightEnd%
 */

#include <stdio.h>
#include <string.h>

#ifdef _WIN32
#include <windows.h>
#endif

#include "egl_impl.h"

#define WX_DEF_EXTS
#include "gen/gl_fdefs.h"

void init_tess();
void exit_tess();
int load_gl_functions();

/* ****************************************************************************
 * OPENGL INITIALIZATION
 *
 * Function initializer and loader, part of erl_gl dynamic library.
 * Also supports gl_nif files
 *
 * NOTE: Must function loading must be done after the context is set and in
 *       the gui thread
 *****************************************************************************/

ERL_NIF_TERM EGL_ATOM_OK;
ERL_NIF_TERM EGL_ATOM_REPLY;
ERL_NIF_TERM EGL_ATOM_ERROR;
ERL_NIF_TERM EGL_ATOM_BADARG;

static ErlNifFunc egl_funcs[] =
{
    {"lookup_func_nif", 1, egl_lookup_func_func}
};
static int egl_init(ErlNifEnv *env, void **priv_data, ERL_NIF_TERM arg)
{
    EGL_ATOM_OK = enif_make_atom(env, "ok");
    EGL_ATOM_BADARG = enif_make_atom(env, "badarg");
    EGL_ATOM_REPLY = enif_make_atom(env, "_egl_result_");
    EGL_ATOM_ERROR = enif_make_atom(env, "_egl_error_");

    return 0;
}

ERL_NIF_INIT(gl, egl_funcs, egl_init, NULL, NULL, NULL)

int egl_get_float(ErlNifEnv* env, ERL_NIF_TERM term, GLfloat* dp)
{
    double temp;
    if(enif_get_double(env, term, &temp)) {
        *dp = (GLfloat) temp;
        return 1;
    } else return 0;
}

int egl_get_short(ErlNifEnv* env, ERL_NIF_TERM term, GLshort* dp)
{
    int temp;
    if(enif_get_int(env, term, &temp)) {
        *dp = (GLshort) temp;
        return 1;
    } else return 0;
}

int egl_get_ushort(ErlNifEnv* env, ERL_NIF_TERM term, GLushort* dp)
{
    unsigned int temp;
    if(enif_get_uint(env, term, &temp)) {
        *dp = (GLushort) temp;
        return 1;
    } else return 0;
}

int egl_get_byte(ErlNifEnv* env, ERL_NIF_TERM term, GLbyte* dp)
{
    int temp;
    if(enif_get_int(env, term, &temp)) {
        *dp = (GLbyte) temp;
        return 1;
    } else return 0;
}

int egl_get_ubyte(ErlNifEnv* env, ERL_NIF_TERM term, GLubyte* dp)
{
    unsigned int temp;
    if(enif_get_uint(env, term, &temp)) {
        *dp = (GLubyte) temp;
        return 1;
    } else return 0;
}

int egl_get_word(ErlNifEnv* env, ERL_NIF_TERM term, egl_word* dp)
{
    if(sizeof(egl_word) == sizeof(int))
        return enif_get_int(env, term, (signed int *) dp);
    else
        return enif_get_int64(env, term, (ErlNifSInt64 *) dp);
}

int egl_get_ptr(ErlNifEnv* env, ERL_NIF_TERM term, void** dp)
{
    if(sizeof(void *) == sizeof(int))
        return enif_get_uint(env, term, (unsigned int *) dp);
    else
        return enif_get_uint64(env, term, (ErlNifUInt64 *) dp);
}

void egl_badarg(ErlNifEnv* env, ErlNifPid *self, int op, const char * argc) {
    const char * func;
    func = gl_fns[op-GLE_LIB_START].name;
    enif_send(NULL, self, env,
              enif_make_tuple3(env, EGL_ATOM_ERROR,
                               enif_make_tuple2(env, enif_make_int(env, op),
                                                enif_make_string(env, func, ERL_NIF_LATIN1)),
                               enif_make_tuple2(env, EGL_ATOM_BADARG,
                                                enif_make_string(env, argc, ERL_NIF_LATIN1))));
}

void * egl_lookup_func(int op)
{
    return gl_fns[op-GLE_LIB_START].nif_cb;
}

const char * egl_lookup_func_desc(int op)
{
    return gl_fns[op-GLE_LIB_START].name;
}


ERL_NIF_TERM egl_lookup_func_func(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
    egl_uword func = 0;
    unsigned int which;
    if(!(enif_get_uint(env, argv[0], &which)) && !(which == 1 || which == 2))
        return enif_make_badarg(env);
    if(which == 1)
        func = (egl_uword) egl_lookup_func;
    if(which == 2)
        func = (egl_uword) egl_lookup_func_desc;
    if(sizeof(void *) == sizeof(unsigned int))
        return enif_make_uint(env, (unsigned int) func);
    else
        return enif_make_uint64(env, (ErlNifUInt64) func);
}

#ifdef _WIN32
#define RTLD_LAZY 0
#define OPENGL_LIB L"opengl32.dll"
#define OPENGLU_LIB L"glu32.dll"
typedef HMODULE DL_LIB_P;
typedef WCHAR DL_CHAR;
void * dlsym(HMODULE Lib, const char *func) {
  void * p;
  p = (void *) wglGetProcAddress(func);
  if(p == 0 || (p == (void *) 0x1) || (p == (void *) 0x2)
     || (p == (void *) 0x3) || (p == (void *) -1) ) {
      p = (void *) GetProcAddress(Lib, func);
  }
  return p;
}

HMODULE dlopen(const WCHAR *DLL, int unused) {
  return LoadLibrary(DLL);
}

void dlclose(HMODULE Lib) {
  FreeLibrary(Lib);
}

#else
typedef void * DL_LIB_P;
typedef char DL_CHAR;
# ifdef _MACOSX
#  define OPENGL_LIB "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib"
#  define OPENGLU_LIB "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib"
# else
#  define OPENGL_LIB "libGL.so.1"
#  define OPENGLU_LIB "libGLU.so.1"
# endif
#endif

/* NOTE: Must be done after the context is set and in the gui thread */
int egl_load_functions() {
    DL_CHAR * DLName;
    DL_LIB_P LIBhandle;
    void * func = NULL;
    int i;

#ifdef _WIN32
    if(!wglGetCurrentContext())
        enif_fprintf(stderr, "wglGetCurrentContext is not set this will not work\r\n");
#endif

    /* Load GLU functions */
    DLName = (DL_CHAR *) OPENGLU_LIB;
    LIBhandle = dlopen(DLName, RTLD_LAZY);
    // fprintf(stderr, "Loading GLU: %s\r\n", (const char*)DLName);
    func = NULL;

    if(LIBhandle) {
        for(i=0; i < (GLE_GL_FUNC_START-GLE_LIB_START); i++) {
            if(gl_fns[i].func) {
                if((func = dlsym(LIBhandle, gl_fns[i].name))) {
                    * (void **) (gl_fns[i].func) = func;
                } else {
                    if(gl_fns[i].alt != NULL) {
                        if((func = dlsym(LIBhandle, gl_fns[i].alt))) {
                            * (void **) (gl_fns[i].func) = func;
                        } else {
                            * (void **) (gl_fns[i].func) = NULL;
                            gl_fns[i].nif_cb = NULL;
                            // fprintf(stderr, "GLU Skipped %s\r\n", glu_fns[i].alt);
                        };
                    } else {
                        * (void **) (gl_fns[i].func) = NULL;
                        gl_fns[i].nif_cb = NULL;
                        // fprintf(stderr, "GLU Skipped %s\r\n", glu_fns[i].name);
                    }
                }
            }
        }
        // dlclose(LIBhandle);
        // fprintf(stderr, "GLU library is loaded\r\n");
    } else {
        for(i=0; i < GLE_GL_FUNC_START; i++) {
            gl_fns[i].nif_cb = NULL;
        }
        fprintf(stderr, "Could NOT load OpenGL GLU library: %s\r\n", (char *) DLName);
    };

    /* Load GL functions */

    DLName = (DL_CHAR *) OPENGL_LIB;
    LIBhandle = dlopen(DLName, RTLD_LAZY);
    func = NULL;

    if(LIBhandle) {
        for(; i <= (GLE_GL_FUNC_LAST-GLE_LIB_START); i++) {
            if(gl_fns[i].func) {
                if((func = dlsym(LIBhandle, gl_fns[i].name))) {
                    * (void **) (gl_fns[i].func) = func;
                    // fprintf(stderr, "GL LOADED %s \r\n", gl_fns[i].name);
                } else {
                    if(gl_fns[i].alt != NULL) {
                        if((func = dlsym(LIBhandle, gl_fns[i].alt))) {
                            * (void **) (gl_fns[i].func) = func;
                            // fprintf(stderr, "GL LOADED %s \r\n", gl_fns[i].alt);
                        } else {
                            * (void **) (gl_fns[i].func) = NULL;
                            gl_fns[i].nif_cb = NULL;
                            // fprintf(stderr, "GL Skipped %s and %s \r\n", gl_fns[i].name, gl_fns[i].alt);
                        };
                    } else {
                        * (void **) (gl_fns[i].func) = NULL;
                        gl_fns[i].nif_cb = NULL;
                        // fprintf(stderr, "GL Skipped %s \r\n", gl_fns[i].name);
                    }
                }
            }
        }
        // dlclose(LIBhandle);
        // fprintf(stderr, "OPENGL library is loaded\r\n");
    } else {
        for(i=0; i <= (GLE_GL_FUNC_LAST-GLE_LIB_START); i++) {
            gl_fns[i].nif_cb = NULL;
        }
        fprintf(stderr, "Could NOT load OpenGL library: %s\r\n", (char *) DLName);
    };

    return 0;
}

/* *******************************************************************************
 * GLU Tessellation special
 * ******************************************************************************/

static GLUtesselator* tess;

typedef struct {
    GLdouble * tess_coords;
    int alloc_n;
    int alloc_max;

    int * tess_index_list;
    int index_n;
    int index_max;

    int error;
} egl_tess_data;

#define NEED_MORE_ALLOC 1
#define NEED_MORE_INDEX 2

static egl_tess_data egl_tess;

void CALLBACK
egl_ogla_vertex(GLdouble* coords)
{
    /* fprintf(stderr, "%d\r\n", (int) (coords - tess_coords) / 3); */
    if(egl_tess.index_n < egl_tess.index_max) {
        egl_tess.tess_index_list[egl_tess.index_n] = (int) (coords - egl_tess.tess_coords) / 3;
        egl_tess.index_n++;
    }
    else
        egl_tess.error = NEED_MORE_INDEX;
}

void CALLBACK
egl_ogla_combine(GLdouble coords[3],
                 void* vertex_data[4],
                 GLfloat w[4],
                 void **dataOut)
{
    GLdouble* vertex = &egl_tess.tess_coords[egl_tess.alloc_n];
    if(egl_tess.alloc_n < egl_tess.alloc_max) {
        egl_tess.alloc_n += 3;
        vertex[0] = coords[0];
        vertex[1] = coords[1];
        vertex[2] = coords[2];
        *dataOut = vertex;

#if 0
        fprintf(stderr, "combine: ");
        int i;
        for (i = 0; i < 4; i++) {
            if (w[i] > 0.0) {
                fprintf(stderr, "%d(%g) ", (int) vertex_data[i], w[i]);
            }
        }
        fprintf(stderr, "\r\n");
        fprintf(stderr, "%g %g %g\r\n", vertex[0], vertex[1], vertex[2]);
#endif

    } else {
        egl_tess.error = NEED_MORE_ALLOC;
        *dataOut = NULL;
    }
}

void CALLBACK
egl_ogla_edge_flag(GLboolean flag)
{
}

void CALLBACK
egl_ogla_error(GLenum errorCode)
{
    // const GLubyte *err;
    // err = gluErrorString(errorCode);
    // fprintf(stderr, "Tessellation error: %d: %s\r\n", (int) errorCode, err);
}

void init_tess()
{
    tess = gluNewTess();

    gluTessCallback(tess, GLU_TESS_VERTEX,     (GLUfuncptr) egl_ogla_vertex);
    gluTessCallback(tess, GLU_TESS_EDGE_FLAG,  (GLUfuncptr) egl_ogla_edge_flag);
    gluTessCallback(tess, GLU_TESS_COMBINE,    (GLUfuncptr) egl_ogla_combine);
    gluTessCallback(tess, GLU_TESS_ERROR,      (GLUfuncptr) egl_ogla_error);

}

void exit_tess()
{
    gluDeleteTess(tess);
}

void erl_tess_impl(ErlNifEnv* env, ErlNifPid *self, ERL_NIF_TERM argv[])
{
    int i, a;
    unsigned int num_vertices;
    GLdouble n[3], *vs;
    ErlNifBinary bin;
    const ERL_NIF_TERM *tuple;
    ERL_NIF_TERM vs_l, vs_h, vs_t, reply;

    int a_max = 2;
    int i_max = 6;

    if(!enif_get_tuple(env, argv[0], &a, &tuple) && a != 3) Badarg(5009, "Normal");
    if(!enif_get_double(env, tuple[0], &n[0])) Badarg(5009,"Normal");
    if(!enif_get_double(env, tuple[1], &n[1])) Badarg(5009,"Normal");
    if(!enif_get_double(env, tuple[2], &n[2])) Badarg(5009,"Normal");

    if(!enif_get_list_length(env, argv[1], &num_vertices)) Badarg(5009,"Vs");
    egl_tess.alloc_max = a_max*num_vertices*3;
    egl_tess.error = 0;
    enif_alloc_binary(egl_tess.alloc_max*sizeof(GLdouble), &bin);
    vs = (GLdouble *) bin.data;
    egl_tess.tess_coords = vs;

    vs_l = argv[1];
    while(enif_get_list_cell(env,  vs_l, &vs_h, &vs_t)) {
        if(!enif_get_tuple(env, vs_h, &a, &tuple) && a != 3) Badarg(5009,"Vs");
        if(!enif_get_double(env, tuple[0], vs++)) Badarg(5009,"Normal");
        if(!enif_get_double(env, tuple[1], vs++)) Badarg(5009,"Normal");
        if(!enif_get_double(env, tuple[2], vs++)) Badarg(5009,"Normal");
        vs_l = vs_t;
    }
    egl_tess.index_max = i_max*3*num_vertices;
    egl_tess.tess_index_list = (int *) enif_alloc(sizeof(int) * egl_tess.index_max);

    egl_tess.index_n = 0;
    egl_tess.alloc_n = num_vertices*3;

    gluTessNormal(tess, n[0], n[1], n[2]);
    gluTessBeginPolygon(tess, 0);
    gluTessBeginContour(tess);
    for (i = 0; i < num_vertices; i++) {
        gluTessVertex(tess, egl_tess.tess_coords+3*i, egl_tess.tess_coords+3*i);
    }
    gluTessEndContour(tess);
    gluTessEndPolygon(tess);

    vs_t = enif_make_list(env, 0);
    i=egl_tess.index_n;
    while(i > 0) {
        i--;
        vs_t = enif_make_list_cell(env, enif_make_int(env, egl_tess.tess_index_list[i]), vs_t);
    };

    enif_realloc_binary(&bin, egl_tess.alloc_n*sizeof(GLdouble));
    reply = enif_make_tuple2(env, vs_t, enif_make_binary(env, &bin));
    enif_send(NULL, self, env, enif_make_tuple2(env, EGL_ATOM_REPLY, reply));
    /* fprintf(stderr, "List %d: %d %d %d \r\n",  */
    /* 	  res, */
    /* 	  n_pos,  */
    /* 	  (tess_alloc_vertex-new_vertices)*sizeof(GLdouble),  */
    /* 	  num_vertices*6*sizeof(GLdouble)); */
    enif_free(egl_tess.tess_index_list);
}