📄 oglwindow.cpp
字号:
#include <assert.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include "../gpucontext.hpp"#include "oglfunc.hpp"#include "oglwindow.hpp"using namespace brook;static const char window_name[] = "Brook GL Render Window";static void appendVendorAttribs( int iAttribList[4][4][64], float fAttribList[4][16], int piAttribList[4][16], const int (*viAttribList)[4][64], const float (*vfAttribList)[4][16], const int (*vpiAttribList)[4][16]) { int i,j,k,m; for (m=0; m<4; m++) { for (i=0; i<4; i++) { for (j=0; j<63; j+=2) { GPUAssert(j<64, "Error: no room for base attribs"); if (iAttribList[m][i][j] == 0 && iAttribList[m][i][j+1] == 0) break; } if (viAttribList) { for (k=0; k<63; k+=2) { GPUAssert(j<64, "Error: no room for vendor attribs"); iAttribList[m][i][j++] = (*viAttribList)[i][k]; iAttribList[m][i][j++] = (*viAttribList)[i][k+1]; if ((*viAttribList)[i][k] == 0 && (*viAttribList)[i][k+1] == 0) break; } } } } for (i=0; i<4; i++) { for (j=0; j<16; j+=2) { GPUAssert(j<16, "Error: no room for base attribs"); if (fAttribList[i][j] == 0.0f && fAttribList[i][j+1] == 0.0f) break; } if (vfAttribList) { for (k=0; k<16; k+=2) { GPUAssert(j<16, "Error: no room for vendor attribs"); fAttribList[i][j++] = (*vfAttribList)[i][k]; fAttribList[i][j++] = (*vfAttribList)[i][k+1]; if ((*vfAttribList)[i][k] == 0.0f && (*vfAttribList)[i][k+1] == 0.0f) break; } } for (j=0; j<16; j+=2) { GPUAssert(j<16, "Error: no room for base attribs"); if (piAttribList[i][j] == 0 && piAttribList[i][j+1] == 0) break; } if (vpiAttribList) { for (k=0; k<16; k+=2) { GPUAssert(j<16, "Error: no room for vendor attribs"); piAttribList[i][j++] = (*vpiAttribList)[i][k]; piAttribList[i][j++] = (*vpiAttribList)[i][k+1]; if ((*vpiAttribList)[i][k] == 0 && (*vpiAttribList)[i][k+1] == 0) break; } } }}#ifdef WIN32#ifndef WGL_ARB_pixel_format#define WGL_ACCELERATION_ARB 0x2003#define WGL_SUPPORT_OPENGL_ARB 0x2010#define WGL_DOUBLE_BUFFER_ARB 0x2011#define WGL_COLOR_BITS_ARB 0x2014#define WGL_RED_BITS_ARB 0x2015#define WGL_GREEN_BITS_ARB 0x2017#define WGL_BLUE_BITS_ARB 0x2019#define WGL_DEPTH_BITS_ARB 0x2022#define WGL_ALPHA_BITS_ARB 0x201B#define WGL_STENCIL_BITS_ARB 0x2023#define WGL_AUX_BUFFERS_ARB 0x2024#define WGL_FULL_ACCELERATION_ARB 0x2027#endif#ifndef WGL_ARB_pbuffer#define WGL_DRAW_TO_PBUFFER_ARB 0x202D#endif#define CRGBAX(c,r,g,b,a,x) \ WGL_DRAW_TO_PBUFFER_ARB, GL_TRUE, \ WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB, \ WGL_DEPTH_BITS_ARB, 0,\ WGL_STENCIL_BITS_ARB, 0,\ WGL_DOUBLE_BUFFER_ARB, GL_FALSE, \ WGL_SUPPORT_OPENGL_ARB, GL_TRUE, \ WGL_AUX_BUFFERS_ARB, x, \ WGL_COLOR_BITS_ARB, c, \ WGL_RED_BITS_ARB, r, \ WGL_GREEN_BITS_ARB, g, \ WGL_BLUE_BITS_ARB, b, \ WGL_ALPHA_BITS_ARB, a#define BASEATTRIB(x) \{CRGBAX(32,32,0,0,0,x), 0, 0}, \{CRGBAX(64,32,32,0,0,x), 0, 0}, \{CRGBAX(96,32,32,32,0,x), 0, 0}, \{CRGBAX(128,32,32,32,32,x), 0, 0}static const int baseiAttribList[4][4][64] = { {BASEATTRIB(0)}, {BASEATTRIB(1)}, {BASEATTRIB(2)}, {BASEATTRIB(3)} };static const floatbasefAttribList[4][16] = { {0.0f,0.0f}, {0.0f,0.0f}, {0.0f,0.0f}, {0.0f,0.0f}}; static intbasepiAttribList[4][16] = { {0, 0}, {0, 0}, {0, 0}, {0, 0} };static HWNDcreate_window (void) { HINSTANCE hinstance; WNDCLASS wc; DWORD window_style; HWND hwnd; /* * These parameters are useless since the window is never shown nor * rendered into. */ const int window_width = 10; const int window_height = 10; const int window_x = 0; const int window_y = 0; hinstance = GetModuleHandle( NULL ); // Create the window class if (!GetClassInfo(hinstance, window_name, &wc)) { wc.style = CS_OWNDC; wc.lpfnWndProc = (WNDPROC) DefWindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hinstance; wc.hIcon = LoadIcon( NULL, IDI_APPLICATION ); wc.hCursor = LoadCursor( NULL, IDC_ARROW ); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = window_name; ATOM result = RegisterClass( &wc ); assert (result); } window_style = ( WS_CLIPSIBLINGS | WS_CLIPCHILDREN ); window_style |= WS_POPUP; // Create the window hwnd = CreateWindow( window_name, window_name, window_style, window_x, window_y, window_width, window_height, NULL, NULL, hinstance, NULL ); if (!hwnd) GPUError ("Failed to create window"); return hwnd;}/* * bSetupPixelFormat -- * * This function creates a default GL context which is never really used by * Brook but is needed to construct a pbuffer */static BOOLbSetupPixelFormat(HDC hdc){ int status; PIXELFORMATDESCRIPTOR *ppfd; PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd 1, // version number PFD_DRAW_TO_WINDOW | // support window PFD_SUPPORT_OPENGL | // support OpenGL PFD_DOUBLEBUFFER, // double buffered PFD_TYPE_RGBA, // RGBA type 0, // 24-bit color depth 0, 0, 0, 0, 0, 0, // color bits ignored 0, // no alpha buffer 0, // shift bit ignored 0, // no accumulation buffer 0, 0, 0, 0, // accum bits ignored 0, // set depth buffer 0, // set stencil buffer 0, // no auxiliary buffer PFD_MAIN_PLANE, // main layer 0, // reserved 0, 0, 0 // layer masks ignored }; int pixelformat; ppfd = &pfd; pixelformat = ChoosePixelFormat( hdc, ppfd ); GPUAssert(pixelformat, "ChoosePixelFormat failed"); status = SetPixelFormat(hdc, pixelformat, ppfd); GPUAssert(status, "SetPixelFormat failed"); return TRUE;}OGLWindow::OGLWindow() { BOOL status; /* Create a window */ hwnd = create_window(); hwindowdc = GetDC(hwnd); /* Initialize the initial window GL_context */ status = bSetupPixelFormat(hwindowdc); GPUAssert(status, "Unable to set window pixel format"); hglrc_window = wglCreateContext(hwindowdc); GPUAssert(hglrc_window, "Unable to create window GL context"); status = wglMakeCurrent(hwindowdc, hglrc_window); GPUAssert(status, "Unable to make current the window GL context"); initglfunc(); hglrc = NULL; hpbuffer = NULL; hpbufferdc = NULL;}OGLWindow::~OGLWindow() { wglMakeCurrent(hwindowdc, NULL); wglDeleteContext(hglrc_window); if (hglrc) wglDeleteContext(hglrc); if (hpbuffer) wglDestroyPbufferARB(hpbuffer); DeleteDC(hwindowdc); DestroyWindow(hwnd);}void OGLWindow::initPbuffer( const int (*viAttribList)[4][64], const float (*vfAttribList)[4][16], const int (*vpiAttribList)[4][16]) { int (*iAttribList)[4][64] = (int (*)[4][64]) malloc (sizeof(baseiAttribList)); float (*fAttribList)[16] = (float (*)[16]) malloc (sizeof(basefAttribList)); unsigned int numFormats; BOOL status; memcpy(iAttribList, baseiAttribList, sizeof(baseiAttribList)); memcpy(fAttribList, basefAttribList, sizeof(basefAttribList)); memcpy(piAttribList, basepiAttribList, sizeof(basepiAttribList)); /* Initialize gl functions */ initglfunc(); /* Append vendor specific attribs */ appendVendorAttribs( iAttribList, fAttribList, piAttribList, viAttribList, vfAttribList, vpiAttribList); for (int ncomp = 0; ncomp < 4; ncomp++) { /* Fetch the pixel formats for pbuffers */ for (int i=0; i<4; i++) { status = wglChoosePixelFormatARB(hwindowdc, iAttribList[ncomp][i], fAttribList[i], 1, &(pixelformat[ncomp][i]), &numFormats); GPUAssert(numFormats > 0 && status, "ChoosePixelFormat failed to find a pbuffer format"); } } currentPbufferComponents = 4; currentPbufferWidth = 16; currentPbufferHeight = 16; currentPbufferOutputs = 1; hpbuffer = wglCreatePbufferARB(hwindowdc, pixelformat [currentPbufferOutputs-1] [currentPbufferComponents-1], currentPbufferWidth, currentPbufferHeight, piAttribList[currentPbufferComponents-1]); if (!hpbuffer) GPUError("Failed to create float pbuffer"); hpbufferdc = wglGetPbufferDCARB (hpbuffer); if (!hpbufferdc) GPUError("Failed to get pbuffer DC"); hglrc = wglCreateContext( hpbufferdc ); if (!hglrc) GPUError("Failed to create GL context"); if (!wglMakeCurrent( hpbufferdc, hglrc )) GPUError("Failed to bind GL context");}boolOGLWindow::bindPbuffer(unsigned int width, unsigned int height, unsigned int numOutputs, unsigned int numComponents) { unsigned int i; bool switched_contexts = false; /* If the pbuffer of the right size is already active, ** return immediately */ if (currentPbufferComponents == numComponents && currentPbufferOutputs >= numOutputs && currentPbufferWidth >= width && currentPbufferHeight >= height) return false; GPUAssert(hpbufferdc, "hpbufferdc = NULL"); GPUAssert(numComponents > 0 && numComponents <= 4, "Cannot hand pbuffers other than 1-4 components"); GPUAssert(width <= 2048 || height <= 2048,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -