ReturnValue EGLWindow::Init() { EGL_CHECK( m_display = eglGetDisplay(EGL_DEFAULT_DISPLAY) ); if (m_display == EGL_NO_DISPLAY) { LOGE("EGLDisplay::Init() failed to get default display"); return RETURN_VALUE_KO; } EGLint result = 0; EGL_CHECK( result = eglInitialize(m_display, &m_majorVersion, &m_minorVersion) ); LOGI("EGL version: %d.%d", m_majorVersion, m_minorVersion); if (!result) { LOGE("EGLDisplay::Init() unable to initialize display"); return RETURN_VALUE_KO; } return RETURN_VALUE_OK; }
ReturnValue EGLWindow::CreateSurface(NativeWindowType &window) { EGL_CHECK( m_surface = eglCreateWindowSurface(m_display, m_config, window, NULL) ); if (m_surface == EGL_NO_SURFACE) { LOGE("EGLDisplay::CreateSurface() failed to create surface"); return RETURN_VALUE_KO; } m_haveSurface = true; return RETURN_VALUE_OK; }
ReturnValue EGLWindow::GetFormat(EGLint &format) const { EGLint result = 0; EGL_CHECK( result = eglGetConfigAttrib(m_display, m_config, EGL_NATIVE_VISUAL_ID, &format) ); if (!result) { LOGE("EGLDisplay::GetFormat() unable to configure window format"); return RETURN_VALUE_KO; } return RETURN_VALUE_OK; }
ReturnValue EGLWindow::SwapBuffers() { EGLint result = 0; EGL_CHECK( result = eglSwapBuffers(m_display, m_surface) ); if(result != EGL_TRUE) { LOGE("EGLWindow::SwapBuffers() error"); return RETURN_VALUE_KO; } return RETURN_VALUE_OK; }
ReturnValue EGLWindow::CreateContext() { const EGLint contextAttrib[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGL_CHECK( m_context = eglCreateContext(m_display, m_config, EGL_NO_CONTEXT, contextAttrib) ); if (m_context == EGL_NO_CONTEXT) { LOGE("EGLDisplay::CreateContext() failed to create context"); return RETURN_VALUE_KO; } m_haveContext = true; return RETURN_VALUE_OK; }
void Win32Window::Create(const int& uiWidth, const int& uiHeight) { HDC hDisplay; HWND hWindow; /* EGL Configuration */ EGLint aEGLAttributes[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint aEGLContextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLConfig aEGLConfigs[1]; EGLint cEGLConfigs; hDisplay = EGL_DEFAULT_DISPLAY; sEGLDisplay = EGL_CHECK(eglGetDisplay(hDisplay)); EGL_CHECK(eglInitialize(sEGLDisplay, NULL, NULL)); EGL_CHECK(eglChooseConfig(sEGLDisplay, aEGLAttributes, aEGLConfigs, 1, &cEGLConfigs)); if (cEGLConfigs == 0) { printf("No EGL configurations were returned.\n"); exit(-1); } hWindow = create_window(uiWidth, uiHeight); sEGLSurface = EGL_CHECK(eglCreateWindowSurface(sEGLDisplay, aEGLConfigs[0], (EGLNativeWindowType)hWindow, NULL)); if (sEGLSurface == EGL_NO_SURFACE) { printf("Failed to create EGL surface.\n"); exit(-1); } sEGLContext = EGL_CHECK(eglCreateContext(sEGLDisplay, aEGLConfigs[0], EGL_NO_CONTEXT, aEGLContextAttributes)); if (sEGLContext == EGL_NO_CONTEXT) { printf("Failed to create EGL context.\n"); exit(-1); } EGL_CHECK(eglMakeCurrent(sEGLDisplay, sEGLSurface, sEGLSurface, sEGLContext)); }
ReturnValue EGLWindow::ChooseConfig(EGLint redSize, EGLint greenSize, EGLint blueSize, EGLint depthSize) { EGLint numConfigs = 0; EGLint result = 0; const EGLint attributes[] = { EGL_RENDERABLE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, redSize, EGL_GREEN_SIZE, greenSize, EGL_BLUE_SIZE, blueSize, EGL_DEPTH_SIZE, depthSize, EGL_NONE }; EGL_CHECK( result = eglChooseConfig(m_display, attributes, &m_config, 1, &numConfigs) ); if(!result || (numConfigs <= 0)) { LOGE("EGLDisplay::ChooseConfig() unable to select display configuration"); return RETURN_VALUE_KO; } return RETURN_VALUE_OK; }
bool Game::InitDisplay() { /* * Here specify the attributes of the desired configuration. * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen windows */ const EGLint attribs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_DEPTH_SIZE, 24, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, //Maybe EGL_CONFORMANT for windows and Android? EGL_NONE }; EGLint attrib_list[]= { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RENDERABLE_TYPE, 0, EGL_NONE}; EGLint w, h, format; EGLint numConfigs; EGLConfig config[64]; EGLSurface surface; EGLContext context; EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); int maj; int min; eglInitialize(display, &maj, &min); Log::Debug("EGLInitialise", "EGL Major:%d Minor:%d", maj, min); /* Here, the application chooses the configuration it desires. In this * sample, we have a very simplified selection process, where we pick * the first EGLConfig that matches our criteria */ eglChooseConfig(display, attribs, &config[0], 64, &numConfigs); for(int i = 0; i < numConfigs; i++) { int val[1]; eglGetConfigAttrib(display, config[i], EGL_CONFORMANT, &val[0]); Log::Debug("EGLInitialise", "EGL_CONFORMANT: %d", val[0]); Log::Debug("EGLInitialise", "GL2: %d", val[0] & EGL_OPENGL_ES2_BIT); Log::Debug("EGLInitialise", "GL1: %d", val[0] & EGL_OPENGL_ES_BIT); } if(numConfigs == 0) { Log::Error("EGLInitialise", "No EGL configs were returned."); return true; } /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). * As soon as we picked a EGLConfig, we can safely reconfigure the * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */ eglGetConfigAttrib(display, config[0], EGL_NATIVE_VISUAL_ID, &format); #ifdef ANDROID ANativeWindow_setBuffersGeometry(mDisplay.app->window, 0, 0, format); //TODO! surface = eglCreateWindowSurface(display, config[0], mDisplay.app->window, NULL); #endif #ifdef _WIN32 mDisplay.WindowHandle = create_window(mDisplay.Width, mDisplay.Height); surface = EGL_CHECK(eglCreateWindowSurface(display, config[0], (EGLNativeWindowType)mDisplay.WindowHandle, NULL)); #endif #ifdef __QNX__ int screen_format = SCREEN_FORMAT_RGBX8888; int usage = SCREEN_USAGE_OPENGL_ES2 | SCREEN_USAGE_ROTATION; int screen_resolution[2]; screen_display_mode_t screen_mode; int size[2]; int angle = std::atoi(std::getenv("ORIENTATION")); screen_create_window(&mDisplay.qnx_screen_win, mDisplay.qnx_screen_context); screen_set_window_property_iv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_FORMAT, &screen_format); screen_set_window_property_iv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_USAGE, &usage); screen_get_window_property_pv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_DISPLAY, (void **)&mDisplay.qnx_screen_disp); screen_get_display_property_iv(mDisplay.qnx_screen_disp, SCREEN_PROPERTY_SIZE, screen_resolution); screen_get_display_property_pv(mDisplay.qnx_screen_disp, SCREEN_PROPERTY_MODE, (void**)&screen_mode); screen_get_window_property_iv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_BUFFER_SIZE, size); int buffer_size[2] = {size[0], size[1]}; if ((angle == 0) || (angle == 180)) { if (((screen_mode.width > screen_mode.height) && (size[0] < size[1])) || ((screen_mode.width < screen_mode.height) && (size[0] > size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else if ((angle == 90) || (angle == 270)){ if (((screen_mode.width > screen_mode.height) && (size[0] > size[1])) || ((screen_mode.width < screen_mode.height && size[0] < size[1]))) { buffer_size[1] = size[0]; buffer_size[0] = size[1]; } } else { return true; } screen_set_window_property_iv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_BUFFER_SIZE, buffer_size); screen_set_window_property_iv(mDisplay.qnx_screen_win, SCREEN_PROPERTY_ROTATION, &angle); screen_create_window_buffers(mDisplay.qnx_screen_win, 2); surface = EGL_CHECK(eglCreateWindowSurface(display, config[0], (EGLNativeWindowType)mDisplay.qnx_screen_win, NULL)); #endif if(surface == EGL_NO_SURFACE) { Log::Error("EGLInitialise", "EGL Surface creation failed."); return true; } const EGLint attribs2[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; context = eglCreateContext(display, config[0], NULL, attribs2); if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) { Log::Error("EGLInitialise", "Unable to eglMakeCurrent"); return true; } #ifdef __QNX__ eglSwapInterval(display, 1); #endif eglQuerySurface(display, surface, EGL_WIDTH, &w); eglQuerySurface(display, surface, EGL_HEIGHT, &h); mDisplay.Display = display; mDisplay.Context = context; mDisplay.Surface = surface; mDisplay.Width = w; mDisplay.Height = h; mTP->camera->SetResolution(Vector2f((float)mDisplay.Width, (float)mDisplay.Height)); // Initialize GL state. GL_CHECK(glViewport(0, 0, w, h)); GL_CHECK(glEnable(GL_DEPTH_TEST)); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GL_CHECK(glEnable(GL_BLEND)); GL_CHECK(glEnable(GL_ALPHA)); Effect::CacheTick(*mTP, EffectClearLevel::Initialise); Texture::CacheTick(*mTP, TextureClearLevel::Initialise); Font::CacheTick(*mTP, FontClearLevel::Initialise); //Initialise graphics for(std::vector<GameObject*>::iterator it = mGO.begin(); it != mGO.end(); ++it) { (*it)->InitialiseGraphics(*mTP); } return false; }
static DFBResult InitGL( Test *test ) { EGLint major, minor, nconfigs; EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_RED_SIZE, 1, EGL_GREEN_SIZE, 1, EGL_BLUE_SIZE, 1, EGL_ALPHA_SIZE, 1, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint context_attrs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE, EGL_NONE }; EGLint surface_attrs[] = { EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE }; EGLNativeDisplayType disp = EGL_DEFAULT_DISPLAY; #define EGL_CHECK(cmd) \ /*fprintf(stderr, "CALLING %s...\n", #cmd);*/ \ if (cmd) { \ fprintf(stderr, "!!! %s failed\n", #cmd); \ goto quit; \ } // get display EGL_CHECK((display = eglGetDisplay(disp)) == EGL_NO_DISPLAY) // init EGL_CHECK(!eglInitialize(display, &major, &minor)) // get configs EGL_CHECK(!eglGetConfigs(display, configs, 2, &nconfigs)) // choose config EGL_CHECK(!eglChooseConfig(display, attribs, configs, 2, &nconfigs)) // create a surface EGL_CHECK((surface = eglCreateWindowSurface(display, configs[0], test->primary, surface_attrs)) == EGL_NO_SURFACE) EGL_CHECK(eglBindAPI(EGL_OPENGL_ES_API) != EGL_TRUE) // create context EGL_CHECK((context = eglCreateContext(display, configs[0], EGL_NO_CONTEXT, context_attrs)) == EGL_NO_CONTEXT) EGL_CHECK(eglMakeCurrent(display, surface, surface, context) != EGL_TRUE) eglSwapInterval( display, 1 ); /* Setup the viewport */ glViewport( 0, 0, (GLint) test->size.w, (GLint) test->size.h ); return DFB_OK; quit: return DFB_FAILURE; }
int main(int argc, char **argv) { EGLDisplay sEGLDisplay; EGLContext sEGLContext; EGLSurface sEGLSurface; /* EGL Configuration */ EGLint aEGLAttributes[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, EGL_DEPTH_SIZE, 16, EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, EGL_NONE }; EGLint aEGLContextAttributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; EGLConfig aEGLConfigs[1]; EGLint cEGLConfigs; #ifdef _WIN32 MSG sMessage; #else XSetWindowAttributes win_attrs; int attrs[64], idx = 0, num_config = 0; int major, minor; Colormap colormap; XVisualInfo *pVisual; XEvent e; #endif GLint iLocPosition = 0; GLint iLocColour, iLocTexCoord, iLocNormal, iLocMVP; GLint iLocXangle, iLocYangle, iLocZangle; GLint iLocAspect, iLocLightPos, iLocSampler, iLocSampler2; GLuint uiProgram, uiFragShader, uiVertShader; GLenum myTex, myTex2; int bDone = 0; const unsigned int uiWidth = 640; const unsigned int uiHeight = 480; int iXangle = 0, iYangle = 0, iZangle = 0; float aTBNmatrix1[9], aTBNmatrix2[9]; float aLightPos[] = { 0.0f, 0.0f, -1.0f }; // Light is nearest camera. unsigned char *myPixels = calloc(1, 128*128*4); // Holds texture data. unsigned char *myPixels2 = calloc(1, 128*128*4); // Holds texture data. float aRotate[16], aModelView[16], aPerspective[16], aMVP[16]; int i; /* EGL Init */ #ifdef _WIN32 hDisplay = EGL_DEFAULT_DISPLAY; #else hDisplay = XOpenDisplay(NULL); if (!hDisplay) { printf("Could not open display\n"); exit(-1); } #endif sEGLDisplay = EGL_CHECK(eglGetDisplay(hDisplay)); EGL_CHECK(eglInitialize(sEGLDisplay, NULL, NULL)); EGL_CHECK(eglChooseConfig(sEGLDisplay, aEGLAttributes, aEGLConfigs, 1, &cEGLConfigs)); if (cEGLConfigs == 0) { printf("No EGL configurations were returned.\n"); exit(-1); } #ifdef _WIN32 hWindow = create_window(uiWidth, uiHeight); #else hWindow = create_window("OpenGL ES 2.0 Example on a Linux Desktop", uiWidth, uiHeight, hDisplay, sEGLDisplay, aEGLConfigs[0], &colormap, &pVisual); #endif sEGLSurface = EGL_CHECK(eglCreateWindowSurface(sEGLDisplay, aEGLConfigs[0], (EGLNativeWindowType)hWindow, NULL)); if (sEGLSurface == EGL_NO_SURFACE) { printf("Failed to create EGL surface.\n"); exit(-1); } sEGLContext = EGL_CHECK(eglCreateContext(sEGLDisplay, aEGLConfigs[0], EGL_NO_CONTEXT, aEGLContextAttributes)); if (sEGLContext == EGL_NO_CONTEXT) { printf("Failed to create EGL context.\n"); exit(-1); } EGL_CHECK(eglMakeCurrent(sEGLDisplay, sEGLSurface, sEGLSurface, sEGLContext)); /* Shader Initialisation */ process_shader(&uiVertShader, "shader.vert", GL_VERTEX_SHADER); process_shader(&uiFragShader, "shader.frag", GL_FRAGMENT_SHADER); /* Create uiProgram (ready to attach shaders) */ uiProgram = GL_CHECK(glCreateProgram()); /* Attach shaders and link uiProgram */ GL_CHECK(glAttachShader(uiProgram, uiVertShader)); GL_CHECK(glAttachShader(uiProgram, uiFragShader)); GL_CHECK(glLinkProgram(uiProgram)); /* Get attribute locations of non-fixed attributes like colour and texture coordinates. */ iLocPosition = GL_CHECK(glGetAttribLocation(uiProgram, "av4position")); iLocColour = GL_CHECK(glGetAttribLocation(uiProgram, "av3colour")); #ifdef DEBUG printf("iLocPosition = %i\n", iLocPosition); printf("iLocColour = %i\n", iLocColour); #endif /* Get uniform locations */ iLocMVP = GL_CHECK(glGetUniformLocation(uiProgram, "mvp")); #ifdef DEBUG printf("iLocMVP = %i\n", iLocMVP); #endif GL_CHECK(glUseProgram(uiProgram)); /* Enable attributes for position, colour and texture coordinates etc. */ GL_CHECK(glEnableVertexAttribArray(iLocPosition)); GL_CHECK(glEnableVertexAttribArray(iLocColour)); /* Populate attributes for position, colour and texture coordinates etc. */ GL_CHECK(glVertexAttribPointer(iLocPosition, 3, GL_FLOAT, GL_FALSE, 0, aVertices)); GL_CHECK(glVertexAttribPointer(iLocColour, 3, GL_FLOAT, GL_FALSE, 0, aColours)); GL_CHECK(glEnable(GL_CULL_FACE)); GL_CHECK(glEnable(GL_DEPTH_TEST)); #ifndef _WIN32 XSelectInput(hDisplay, hWindow, KeyPressMask | ExposureMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | VisibilityChangeMask | ButtonPressMask | ButtonReleaseMask | StructureNotifyMask); #endif /* Enter event loop */ while (!bDone) { #ifdef _WIN32 if(PeekMessage(&sMessage, NULL, 0, 0, PM_REMOVE)) { if(sMessage.message == WM_QUIT) { bDone = 1; } else { TranslateMessage(&sMessage); DispatchMessage(&sMessage); } } #else while (XPending(hDisplay) > 0) { XNextEvent(hDisplay, &e); if (e.type == ButtonPress) { bDone = 1; } } #endif /* * Do some rotation with Euler angles. It is not a fixed axis as * quaterions would be, but the effect is cool. */ rotate_matrix(iXangle, 1.0, 0.0, 0.0, aModelView); rotate_matrix(iYangle, 0.0, 1.0, 0.0, aRotate); multiply_matrix(aRotate, aModelView, aModelView); rotate_matrix(iZangle, 0.0, 1.0, 0.0, aRotate); multiply_matrix(aRotate, aModelView, aModelView); /* Pull the camera back from the cube */ aModelView[14] -= 2.5; perspective_matrix(45.0, (double)uiWidth/(double)uiHeight, 0.01, 100.0, aPerspective); multiply_matrix(aPerspective, aModelView, aMVP); GL_CHECK(glUniformMatrix4fv(iLocMVP, 1, GL_FALSE, aMVP)); iXangle += 3; iYangle += 2; iZangle += 1; if(iXangle >= 360) iXangle -= 360; if(iXangle < 0) iXangle += 360; if(iYangle >= 360) iYangle -= 360; if(iYangle < 0) iYangle += 360; if(iZangle >= 360) iZangle -= 360; if(iZangle < 0) iZangle += 360; GL_CHECK(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT)); GL_CHECK(glDrawArrays(GL_TRIANGLES, 0, 36)); if (!eglSwapBuffers(sEGLDisplay, sEGLSurface)) { printf("Failed to swap buffers.\n"); } #ifdef _WIN32 Sleep(20); #else usleep(20000); #endif } /* Cleanup shaders */ GL_CHECK(glUseProgram(0)); GL_CHECK(glDeleteShader(uiVertShader)); GL_CHECK(glDeleteShader(uiFragShader)); GL_CHECK(glDeleteProgram(uiProgram)); /* EGL clean up */ EGL_CHECK(eglMakeCurrent(sEGLDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)); EGL_CHECK(eglDestroySurface(sEGLDisplay, sEGLSurface)); EGL_CHECK(eglDestroyContext(sEGLDisplay, sEGLContext)); EGL_CHECK(eglTerminate(sEGLDisplay)); #ifndef _WIN32 /* X windows clean up */ XDestroyWindow(hDisplay, hWindow); XFreeColormap(hDisplay, colormap); XFree(pVisual); XCloseDisplay(hDisplay); #endif return 0; }