diff options
author | Dan Gudmundsson <[email protected]> | 2010-10-06 14:05:36 +0200 |
---|---|---|
committer | Dan Gudmundsson <[email protected]> | 2010-11-15 10:13:05 +0100 |
commit | a9f305c51ebccefc3ce6725b4f90700a7d250287 (patch) | |
tree | 64f522982e763eb5eda411196413a15704db36c4 /lib/wx/c_src/wxe_gl.cpp | |
parent | b48be79573171e530d97cdf078488eb66fc23f30 (diff) | |
download | otp-a9f305c51ebccefc3ce6725b4f90700a7d250287.tar.gz otp-a9f305c51ebccefc3ce6725b4f90700a7d250287.tar.bz2 otp-a9f305c51ebccefc3ce6725b4f90700a7d250287.zip |
Split opengl functionality into separate dynamic library
The idea is to build a separate opengl library which can be reused
by other erlang applications.
Diffstat (limited to 'lib/wx/c_src/wxe_gl.cpp')
-rw-r--r-- | lib/wx/c_src/wxe_gl.cpp | 335 |
1 files changed, 68 insertions, 267 deletions
diff --git a/lib/wx/c_src/wxe_gl.cpp b/lib/wx/c_src/wxe_gl.cpp index 63dd68fa5e..eb4462047f 100644 --- a/lib/wx/c_src/wxe_gl.cpp +++ b/lib/wx/c_src/wxe_gl.cpp @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 2008-2009. All Rights Reserved. + * Copyright Ericsson AB 2008-2010. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in @@ -19,303 +19,104 @@ #include <stdio.h> #include <string.h> +#include <dlfcn.h> #include "wxe_impl.h" - -#include "wxe_gl.h" +#include "wxe_return.h" #define WX_DEF_EXTS -#include "gen/gl_fdefs.h" -#include "gen/gl_finit.h" -#include "gen/glu_finit.h" /* **************************************************************************** * Opengl context management * * ****************************************************************************/ -int gl_initiated = FALSE; +int erl_gl_initiated = FALSE; ErlDrvTermData gl_active = 0; wxeGLC glc; -void setActiveGL(ErlDrvTermData caller, wxGLCanvas *canvas) -{ - if(gl_initiated == FALSE) { - initOpenGL(); - init_tess(); - gl_initiated = TRUE; - } - gl_active = caller; - glc[caller] = canvas; -} - -void deleteActiveGL(wxGLCanvas *canvas) -{ - gl_active = 0; - wxeGLC::iterator it; - for(it = glc.begin(); it != glc.end(); ++it) { - if(it->second == canvas) { - it->second = (wxGLCanvas *) 0; - } - } -} - -/* **************************************************************************** - * OPENGL INITIALIZATION - *****************************************************************************/ +typedef void (*WXE_GL_DISPATCH) (int, char *, ErlDrvPort, ErlDrvTermData, char **); +WXE_GL_DISPATCH wxe_gl_dispatch; #ifdef _WIN32 void * dlsym(HMODULE Lib, const char *func) { - void * funcp; - if((funcp = (void *) GetProcAddress(Lib, func))) - return funcp; - else - return (void *) wglGetProcAddress(func); + funcp = (void *) GetProcAddress(Lib, func); + return funcp; } #endif -int initOpenGL() -{ -#ifdef _MACOSX - char * DLName = "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib"; - void * LIBhandle = dlopen(DLName, RTLD_LAZY); -#elif defined(_WIN32) - WCHAR * DLName = wxT("opengl32.dll"); - HMODULE LIBhandle = LoadLibrary(DLName); -#else - char * DLName = (char *) "libGL.so"; - void * LIBhandle = dlopen(DLName, RTLD_LAZY); -#endif - // fprintf(stderr, "Loading GL: %s\r\n", (const char*)DLName); - void * func = NULL; - int i; - - if(LIBhandle) { - for(i=0; gl_fns[i].name != NULL; i++) { - 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) = (void *) &gl_error; - // fprintf(stderr, "GL Skipped %s and %s \r\n", gl_fns[i].name, gl_fns[i].alt); - }; +void wxe_initOpenGL(wxeReturn rt, char *bp) { + void * LIBhandle; + int (*init_opengl) (); + + if(erl_gl_initiated == FALSE) { + if((LIBhandle = dlopen(bp, RTLD_LAZY))) { + *(void **) (&init_opengl) = dlsym(LIBhandle, "egl_init_opengl"); + wxe_gl_dispatch = (WXE_GL_DISPATCH) dlsym(LIBhandle, "egl_dispatch"); + if(init_opengl && wxe_gl_dispatch) { + (*init_opengl)(); + rt.addAtom((char *) "ok"); + rt.add(wxString::FromAscii("initiated")); + rt.addTupleCount(2); + erl_gl_initiated = TRUE; } else { - * (void **) (gl_fns[i].func) = (void *) &gl_error; - // fprintf(stderr, "GL Skipped %s \r\n", gl_fns[i].name); + wxString msg; + msg.Printf(wxT("In library: ")); + msg += wxString::FromAscii(bp); + msg += wxT(" functions: "); + if(!init_opengl) + msg += wxT("egl_init_opengl "); + if(!wxe_gl_dispatch) + msg += wxT("egl_dispatch "); + rt.addAtom((char *) "error"); + rt.add(msg); + rt.addTupleCount(2); } - } } -#ifdef _WIN32 - FreeLibrary(LIBhandle); -#else - dlclose(LIBhandle); -#endif - // fprintf(stderr, "OPENGL library is loaded\r\n"); } else { - wxString msg; - msg.Printf(wxT("Could NOT load OpenGL library: ")); -#ifdef _WIN32 - msg += DLName; -#else - msg += wxString::FromAscii((char *)DLName); -#endif - send_msg("error", &msg); - }; - -#ifdef _MACOSX - DLName = "/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGLU.dylib"; - LIBhandle = dlopen(DLName, RTLD_LAZY); -#elif defined(_WIN32) - DLName = wxT("glu32.dll"); - LIBhandle = LoadLibrary(DLName); -#else - DLName = (char *) "libGLU.so"; - LIBhandle = dlopen(DLName, RTLD_LAZY); -#endif - // fprintf(stderr, "Loading GL: %s\r\n", (const char*)DLName); - func = NULL; - - if(LIBhandle) { - for(i=0; glu_fns[i].name != NULL; i++) { - if((func = dlsym(LIBhandle, glu_fns[i].name))) { - * (void **) (glu_fns[i].func) = func; - } else { - if(glu_fns[i].alt != NULL) { - if((func = dlsym(LIBhandle, glu_fns[i].alt))) { - * (void **) (glu_fns[i].func) = func; - } else { - * (void **) (glu_fns[i].func) = (void *) &gl_error; - // fprintf(stderr, "GLU Skipped %s\r\n", glu_fns[i].alt); - }; - } else { - * (void **) (glu_fns[i].func) = (void *) &gl_error; - // fprintf(stderr, "GLU Skipped %s\r\n", glu_fns[i].name); - } - } - } -#ifdef _WIN32 - FreeLibrary(LIBhandle); -#else - dlclose(LIBhandle); -#endif - // fprintf(stderr, "GLU library is loaded\r\n"); - } else { - wxString msg; - msg.Printf(wxT("Could NOT load OpenGL GLU library: ")); -#ifdef _WIN32 - msg += DLName; -#else - msg += wxString::FromAscii((char *)DLName); -#endif - send_msg("error", &msg); - }; - return 0; -} - -void gl_error() { - int AP = 0; ErlDrvTermData rt[8]; - rt[AP++] = ERL_DRV_ATOM; rt[AP++] = driver_mk_atom((char *)"_wxe_error_"); - rt[AP++] = ERL_DRV_INT; rt[AP++] = (int) gl_error_op; - rt[AP++] = ERL_DRV_ATOM; rt[AP++] = driver_mk_atom((char *)"undef"); - rt[AP++] = ERL_DRV_TUPLE; rt[AP++] = 3; - driver_send_term(WXE_DRV_PORT,gl_active,rt,AP); -} - -/* ******************************************************************************* - * GLU Tesselation special - * ******************************************************************************/ - -static GLUtesselator* tess; -static GLdouble* tess_coords; -static GLdouble* tess_alloc_vertex; -static int* tess_vertices; - -void CALLBACK -wxe_ogla_vertex(GLdouble* coords) -{ - /* fprintf(stderr, "%d\r\n", (int) (coords - tess_coords) / 3); */ - - *tess_vertices++ = (int) (coords - tess_coords) / 3; -} - -void CALLBACK -wxe_ogla_edge_flag(GLboolean flag) -{ + rt.addAtom((char *) "ok"); + rt.add(wxString::FromAscii("already initilized")); + rt.addTupleCount(2); + } + rt.send(); } -void CALLBACK -wxe_ogla_error(GLenum errorCode) +void setActiveGL(ErlDrvTermData caller, wxGLCanvas *canvas) { - const GLubyte *err; - err = gluErrorString(errorCode); - wxString msg; - msg.Printf(wxT("Tesselation error: %d: "), (int)errorCode); - msg += wxString::FromAscii((char *) err); - send_msg("error", &msg); + gl_active = caller; + glc[caller] = canvas; } -void CALLBACK -wxe_ogla_combine(GLdouble coords[3], - void* vertex_data[4], - GLfloat w[4], - void **dataOut) +void deleteActiveGL(wxGLCanvas *canvas) { - GLdouble* vertex = tess_alloc_vertex; - - tess_alloc_vertex += 3; - -#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]); + gl_active = 0; + wxeGLC::iterator it; + for(it = glc.begin(); it != glc.end(); ++it) { + if(it->second == canvas) { + it->second = (wxGLCanvas *) 0; } } - fprintf(stderr, "\r\n"); - fprintf(stderr, "%g %g %g\r\n", vertex[0], vertex[1], vertex[2]); -#endif - - vertex[0] = coords[0]; - vertex[1] = coords[1]; - vertex[2] = coords[2]; - *dataOut = vertex; -} - -void init_tess() -{ - tess = gluNewTess(); - - gluTessCallback(tess, GLU_TESS_VERTEX, (GLUfuncptr) wxe_ogla_vertex); - gluTessCallback(tess, GLU_TESS_EDGE_FLAG, (GLUfuncptr) wxe_ogla_edge_flag); - gluTessCallback(tess, GLU_TESS_COMBINE, (GLUfuncptr) wxe_ogla_combine); - gluTessCallback(tess, GLU_TESS_ERROR, (GLUfuncptr) wxe_ogla_error); - -} - -void exit_tess() -{ - gluDeleteTess(tess); } -int wxe_tess_impl(char* buff, ErlDrvTermData caller) -{ - ErlDrvBinary* bin; - int i; - int num_vertices = * (int *) buff; buff += 8; // Align - GLdouble *n = (double *) buff; buff += 8*3; - - GLdouble* new_vertices; - bin = driver_alloc_binary(num_vertices*6*sizeof(GLdouble)); - new_vertices = tess_coords = (double *) bin->orig_bytes; - memcpy(tess_coords,buff,num_vertices*3*sizeof(GLdouble)); - tess_alloc_vertex = tess_coords + num_vertices*3; - -#if 0 - fprintf(stderr, "n=%d\r\n", num_vertices); -#endif - int *vertices; - vertices = (int *) driver_alloc(sizeof(int) * 16*num_vertices); - - tess_vertices = vertices; - - gluTessNormal(tess, n[0], n[1], n[2]); - gluTessBeginPolygon(tess, 0); - gluTessBeginContour(tess); - for (i = 0; i < num_vertices; i++) { - gluTessVertex(tess, tess_coords+3*i, tess_coords+3*i); - } - gluTessEndContour(tess); - gluTessEndPolygon(tess); - - int n_pos = (tess_vertices - vertices); - - int AP = 0; ErlDrvTermData *rt; - rt = (ErlDrvTermData *) driver_alloc(sizeof(ErlDrvTermData) * (13+n_pos*2)); - rt[AP++]=ERL_DRV_ATOM; rt[AP++]=driver_mk_atom((char *) "_wxe_result_"); - - for(i=0; i < n_pos; i++) { - rt[AP++] = ERL_DRV_INT; rt[AP++] = (int) vertices[i]; +void gl_dispatch(int op, char *bp,ErlDrvTermData caller,WXEBinRef *bins[]){ + if(caller != gl_active) { + wxGLCanvas * current = glc[caller]; + if(current) { gl_active = caller; current->SetCurrent();} + else { + ErlDrvTermData rt[] = // Error msg + {ERL_DRV_ATOM, driver_mk_atom((char *) "_wxe_error_"), + ERL_DRV_INT, op, + ERL_DRV_ATOM, driver_mk_atom((char *) "no_gl_context"), + ERL_DRV_TUPLE,3}; + driver_send_term(WXE_DRV_PORT,caller,rt,8); + return ; + } }; - rt[AP++] = ERL_DRV_NIL; rt[AP++] = ERL_DRV_LIST; rt[AP++] = n_pos+1; - - rt[AP++] = ERL_DRV_BINARY; rt[AP++] = (ErlDrvTermData) bin; - rt[AP++] = (tess_alloc_vertex-new_vertices)*sizeof(GLdouble); rt[AP++] = 0; - - rt[AP++] = ERL_DRV_TUPLE; rt[AP++] = 2; // Return tuple {list, Bin} - rt[AP++] = ERL_DRV_TUPLE; rt[AP++] = 2; // Result tuple - - driver_send_term(WXE_DRV_PORT,caller,rt,AP); -// fprintf(stderr, "List %d %d %d \r\n", -// n_pos, -// (tess_alloc_vertex-new_vertices)*sizeof(GLdouble), -// num_vertices*6*sizeof(GLdouble)); - driver_free_binary(bin); - driver_free(vertices); - driver_free(rt); - return 0; + char * bs[3]; + for(int i=0; i<3; i++) { + if(bins[i]) + bs[i] = bins[i]->base; + else + bs[i] = NULL; + } + wxe_gl_dispatch(op, bp, WXE_DRV_PORT, caller, bs); } |