Skip to content

Commit

Permalink
Ensures depth buffer for ofxAppEmscriptenWindow. Closes #6484 (#6485)
Browse files Browse the repository at this point in the history
#changelog #emscripten
  • Loading branch information
ofTheo authored and arturoc committed Dec 12, 2019
1 parent cf8c500 commit 06a9366
Showing 1 changed file with 33 additions and 8 deletions.
41 changes: 33 additions & 8 deletions addons/ofxEmscripten/src/ofxAppEmscriptenWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
EGLint minorVersion;
EGLConfig config;
EGLint contextAttribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE };
EGLint attribList[] =
std::vector <EGLint> attribList =
{
EGL_RED_SIZE, EGL_DONT_CARE,
EGL_GREEN_SIZE, EGL_DONT_CARE,
Expand All @@ -70,6 +70,18 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
EGL_SAMPLE_BUFFERS, EGL_DONT_CARE,
EGL_NONE
};

// We'll try these depth sizes in order ending with EGL_DONT_CARE if we don't get anything higher.
std::vector <EGLint> depthPreference = {24, 16, EGL_DONT_CARE};

// Find the index for the value EGL_DEPTH_SIZE uses, so we can try a few different values till we get a successful config.
int attribListDepthIndex = -1;
for(int i = 0; i < attribList.size(); i++){
if( attribList[i] == EGL_DEPTH_SIZE ){
attribListDepthIndex = i+1;
break;
}
}

// Get Display
display = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY);
Expand All @@ -89,14 +101,27 @@ void ofxAppEmscriptenWindow::setup(const ofGLESWindowSettings & settings){
ofLogError() << "couldn't get configs";
return;
}

// Choose the config based on our attribute list
// Try higher EGL_DEPTH_SIZE first
for(int i = 0; i < depthPreference.size(); i++){
// Set EGL_DEPTH_SIZE
attribList[attribListDepthIndex] = depthPreference[i];

// Try out that depth value
if ( !eglChooseConfig(display, &attribList[0], &config, 1, &numConfigs) ){

// Finally fail like we did before if no preference works
if( depthPreference[i] == EGL_DONT_CARE ){
ofLogError() << "couldn't choose display";
return;
}

ofLogNotice("ofxAppEmscriptenWindow") << "Got " << numConfigs << " display configs";

// Choose config
if ( !eglChooseConfig(display, attribList, &config, 1, &numConfigs) ){
ofLogError() << "couldn't choose display";
return;
}
}else{
// Got a good configuration. Stop searching.
break;
}
}

// Create a surface
surface = eglCreateWindowSurface(display, config, NULL, NULL);
Expand Down

0 comments on commit 06a9366

Please sign in to comment.