/* * OpenHMD - Free and Open Source API and drivers for immersive technology. * Copyright (C) 2013 Fredrik Hultin. * Copyright (C) 2013 Jakob Bornecrantz. * Distributed under the Boost 1.0 licence, see LICENSE for full text. */ /* OpenGL Test - GL Helper Functions Implementation */ #include "gl.h" #include #include #include #include #include #ifdef __unix #include #endif #ifndef M_PI #define M_PI 3.14159265359 #endif void init_gl(gl_ctx* ctx, struct lvrcInstance * compositor) { memset(ctx, 0, sizeof(gl_ctx)); bool bSwapChainCreated = lvrcInitSwapChain(compositor); if (!bSwapChainCreated) { fprintf(stderr, "swap chain not created\n"); exit(-1); } static const EGLint attribs[] = { EGL_RED_SIZE, 8, EGL_GREEN_SIZE, 8, EGL_BLUE_SIZE, 8, //EGL_ALPHA_SIZE, 8, //EGL_DEPTH_SIZE, 16, EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT, EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_NONE }; static const EGLint context_attributes[] = { EGL_CONTEXT_CLIENT_VERSION, 2, EGL_NONE }; // // Get Native Display EGLNativeDisplayType nativeDisplay = lvrcSwapChainGetNativeDisplay(compositor); if (!nativeDisplay) { fprintf(stderr, "invalid native display\n"); exit(-1); } // // get an EGL display connection ctx->display = eglGetDisplay(nativeDisplay); if (EGL_NO_DISPLAY == ctx->display) { fprintf(stderr, "eglGetDisplay() failed\n"); exit(-1); } // // initialize the EGL display connection EGLint major, minor; if (!eglInitialize(ctx->display, &major, &minor)) { fprintf(stderr, "eglInitialize() failed\n"); exit(-1); } // // bind OpenGL API eglBindAPI(EGL_OPENGL_API); // // get an appropriate EGL frame buffer configuration EGLint num_config; EGLConfig configs[128]; if (!eglChooseConfig(ctx->display, attribs, configs, 128, &num_config)) { fprintf(stderr, "eglChooseConfig() failed\n"); exit(-1); } EGLConfig config = configs[0]; // FIXME // // create context ctx->context = eglCreateContext(ctx->display, config, EGL_NO_CONTEXT, context_attributes); if (EGL_NO_CONTEXT == ctx->context) { fprintf(stderr, "failed to create context\n"); exit(-1); } // // Get Native Window EGLNativeWindowType nativeWindow = lvrcSwapChainGetNativeWindow(compositor); if (!nativeWindow) { fprintf(stderr, "invalid native window\n"); exit(-1); } // // Create Surface ctx->surface = eglCreateWindowSurface(ctx->display, config, nativeWindow, NULL); if (EGL_NO_SURFACE == ctx->surface) { fprintf(stderr, "eglCreateWindowSurface() failed\n"); exit(-1); } // // Set current context if (!eglMakeCurrent(ctx->display, ctx->surface, ctx->surface, ctx->context)) { fprintf(stderr, "failed to make context current\n"); exit(-1); } ctx->w = lvrcSwapChainGetWidth(compositor); ctx->h = lvrcSwapChainGetHeight(compositor); ctx->is_fullscreen = 1; // Disable ctrl-c catching on Linux (and OS X?) #ifdef __unix signal(SIGINT, SIG_DFL); #endif printf("OpenGL Renderer: %s\n", glGetString(GL_RENDERER)); printf("OpenGL Vendor: %s\n", glGetString(GL_VENDOR)); printf("OpenGL Version: %s\n", glGetString(GL_VERSION)); // == Initialize OpenGL == glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_ALPHA_TEST); glLoadIdentity(); glShadeModel(GL_SMOOTH); glDisable(GL_DEPTH_TEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glEnable(GL_POLYGON_SMOOTH); glLoadIdentity(); glViewport(0, 0, ctx->w, ctx->h); } void ortho(gl_ctx* ctx) { glMatrixMode(GL_PROJECTION); //glPushMatrix(); glLoadIdentity(); glOrtho(0.0f, ctx->w, ctx->h, 0.0f, -1.0f, 1.0f); glMatrixMode(GL_MODELVIEW); //glPushMatrix(); glLoadIdentity(); glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH); glDisable(GL_MULTISAMPLE); } void draw_cube() { glBegin(GL_QUADS); glVertex3f( 0.5f, 0.5f, -0.5f); /* Top Right Of The Quad (Top) */ glVertex3f( -0.5f, 0.5f, -0.5f); /* Top Left Of The Quad (Top) */ glVertex3f( -0.5f, 0.5f, 0.5f); /* Bottom Left Of The Quad (Top) */ glVertex3f( 0.5f, 0.5f, 0.5f); /* Bottom Right Of The Quad (Top) */ glVertex3f( 0.5f, -0.5f, 0.5f); /* Top Right Of The Quad (Botm) */ glVertex3f( -0.5f, -0.5f, 0.5f); /* Top Left Of The Quad (Botm) */ glVertex3f( -0.5f, -0.5f, -0.5f); /* Bottom Left Of The Quad (Botm) */ glVertex3f( 0.5f, -0.5f, -0.5f); /* Bottom Right Of The Quad (Botm) */ glVertex3f( 0.5f, 0.5f, 0.5f); /* Top Right Of The Quad (Front) */ glVertex3f( -0.5f, 0.5f, 0.5f); /* Top Left Of The Quad (Front) */ glVertex3f( -0.5f, -0.5f, 0.5f); /* Bottom Left Of The Quad (Front) */ glVertex3f( 0.5f, -0.5f, 0.5f); /* Bottom Right Of The Quad (Front) */ glVertex3f( 0.5f, -0.5f, -0.5f); /* Bottom Left Of The Quad (Back) */ glVertex3f( -0.5f, -0.5f, -0.5f); /* Bottom Right Of The Quad (Back) */ glVertex3f( -0.5f, 0.5f, -0.5f); /* Top Right Of The Quad (Back) */ glVertex3f( 0.5f, 0.5f, -0.5f); /* Top Left Of The Quad (Back) */ glVertex3f( -0.5f, 0.5f, 0.5f); /* Top Right Of The Quad (Left) */ glVertex3f( -0.5f, 0.5f, -0.5f); /* Top Left Of The Quad (Left) */ glVertex3f( -0.5f, -0.5f, -0.5f); /* Bottom Left Of The Quad (Left) */ glVertex3f( -0.5f, -0.5f, 0.5f); /* Bottom Right Of The Quad (Left) */ glVertex3f( 0.5f, 0.5f, -0.5f); /* Top Right Of The Quad (Right) */ glVertex3f( 0.5f, 0.5f, 0.5f); /* Top Left Of The Quad (Right) */ glVertex3f( 0.5f, -0.5f, 0.5f); /* Bottom Left Of The Quad (Right) */ glVertex3f( 0.5f, -0.5f, -0.5f); /* Bottom Right Of The Quad (Right) */ glEnd(); } void create_fbo(int eye_width, int eye_height, GLuint* fbo, GLuint* color_tex, GLuint* depth_tex) { glGenTextures(1, color_tex); glGenTextures(1, depth_tex); glGenFramebuffers(1, fbo); glBindTexture(GL_TEXTURE_2D, *color_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, eye_width, eye_height, 0, GL_RGBA, GL_UNSIGNED_INT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glBindTexture(GL_TEXTURE_2D, *depth_tex); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, eye_width, eye_height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, *fbo); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, *color_tex, 0); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, *depth_tex, 0); GLenum status = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT); if(status != GL_FRAMEBUFFER_COMPLETE_EXT){ printf("failed to create fbo %x\n", status); } glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); }