|
| 1 | +#include <Spout.h> |
| 2 | + |
| 3 | +HWND hWnd; |
| 4 | + |
| 5 | +bool InitGLtexture(GLuint &texID, unsigned int width, unsigned int height) |
| 6 | +{ |
| 7 | + // printf("testApp::InitGLtexture %dx%d\n", width, height); |
| 8 | + |
| 9 | + if(texID != 0) glDeleteTextures(1, &texID); |
| 10 | + |
| 11 | + glGenTextures(1, &texID); |
| 12 | + glBindTexture(GL_TEXTURE_2D, texID); |
| 13 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); |
| 14 | + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 15 | + glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 16 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 17 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 18 | + glBindTexture(GL_TEXTURE_2D, 0); |
| 19 | + |
| 20 | + return true; |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +void bitmapDataToGL(GLuint &texID, int width, int height, GLvoid * pixels) |
| 25 | +{ |
| 26 | + glBindTexture(GL_TEXTURE_2D, texID); |
| 27 | + |
| 28 | + |
| 29 | + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,GL_BGRA_EXT, GL_UNSIGNED_BYTE,pixels); |
| 30 | + |
| 31 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // GL_LINEAR); |
| 32 | + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // GL_LINEAR); |
| 33 | + |
| 34 | + glBindTexture(GL_TEXTURE_2D, 0); |
| 35 | + glDisable(GL_TEXTURE_2D); |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +void GLToBitmapData(GLuint &texID, int width, int height, GLvoid * pixels) |
| 40 | +{ |
| 41 | + glBindTexture(GL_TEXTURE_2D, texID); |
| 42 | + glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels); |
| 43 | + glBindTexture(GL_TEXTURE_2D,1); |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +//GL INIT / CONTEXT CREATION |
| 48 | +BOOL CALLBACK EnumProc(HWND hwnd, LPARAM lParam) |
| 49 | +{ |
| 50 | + DWORD windowID; |
| 51 | + GetWindowThreadProcessId(hwnd, &windowID); |
| 52 | + |
| 53 | + if (windowID == lParam) |
| 54 | + { |
| 55 | + printf("Found HWND !\n"); |
| 56 | + hWnd = hwnd; |
| 57 | + |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + return true; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC) |
| 66 | +{ |
| 67 | + PIXELFORMATDESCRIPTOR pfd; |
| 68 | + int iFormat; |
| 69 | + |
| 70 | + // get the device context (DC) |
| 71 | + *hDC = GetDC( hWnd ); |
| 72 | + |
| 73 | + // set the pixel format for the DC |
| 74 | + ZeroMemory( &pfd, sizeof( pfd ) ); |
| 75 | + pfd.nSize = sizeof( pfd ); |
| 76 | + pfd.nVersion = 1; |
| 77 | + pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | |
| 78 | + PFD_DOUBLEBUFFER; |
| 79 | + pfd.iPixelType = PFD_TYPE_RGBA; |
| 80 | + pfd.cColorBits = 24; |
| 81 | + pfd.cDepthBits = 16; |
| 82 | + pfd.iLayerType = PFD_MAIN_PLANE; |
| 83 | + iFormat = ChoosePixelFormat( *hDC, &pfd ); |
| 84 | + SetPixelFormat( *hDC, iFormat, &pfd ); |
| 85 | + |
| 86 | + // create and enable the render context (RC) |
| 87 | + *hRC = wglCreateContext( *hDC ); |
| 88 | + wglMakeCurrent( *hDC, *hRC ); |
| 89 | +} |
| 90 | + |
| 91 | +void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC) |
| 92 | +{ |
| 93 | + wglMakeCurrent( NULL, NULL ); |
| 94 | + wglDeleteContext( hRC ); |
| 95 | + ReleaseDC( hWnd, hDC ); |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +bool InitGL() // All Setup For OpenGL Goes Here |
| 100 | +{ |
| 101 | + |
| 102 | + printf("InitGL with EnabledOpenGL\n"); |
| 103 | + |
| 104 | + //set hWnd with process ID |
| 105 | + DWORD processID = GetCurrentProcessId(); |
| 106 | + EnumWindows(EnumProc, processID); |
| 107 | + |
| 108 | + |
| 109 | + HDC hDC; |
| 110 | + HGLRC glContext; |
| 111 | + EnableOpenGL(hWnd,&hDC,&glContext); |
| 112 | + |
| 113 | + // Determine hardware capabilities now, not later when all is initialized |
| 114 | + // ===================================================== |
| 115 | + //glContext = wglGetCurrentContext(); // should check if opengl context creation succeed |
| 116 | + if(glContext) { |
| 117 | + printf("GL Context creation OK\n"); |
| 118 | + } |
| 119 | + else { |
| 120 | + |
| 121 | + printf("No GL context, GetLastError : %X\n",GetLastError()); |
| 122 | + |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + printf("And we are there !\n"); |
| 127 | + |
| 128 | + return true; // Initialization Went OK |
| 129 | + |
| 130 | +} |
0 commit comments