在Windows上编译在Ubuntu中创建的opengl程序

时间:2013-11-05 11:18:34

标签: opengl

我在Ubuntu中使用opengl创建了一个程序。现在,我想从中生成.exe文件。如果我在Windows中使用g ++编译器并编译它会有可能吗?

2 个答案:

答案 0 :(得分:1)

取决于您的代码。通常,设备上下文以不同的方式在每个平台上创建

视窗:

hRC_ = wglCreateContextAttribsARB(hDC_, 0, attributes);  // Create and OpenGL 3.x context based on the given attributes

if(hRC_ == 0)
{
    GLenum error = glGetError();
    // ... handle error
}

wglMakeCurrent(nullptr, nullptr); // Remove the temporary context from being active
wglDeleteContext(tempContext); // Delete the temporary OpenGL 2.1 context
wglMakeCurrent(hDC_, hRC_); // Make our OpenGL 3.0 context current

X11 / GLX:

const char *glxExts = glXQueryExtensionsString( display_, DefaultScreen( display_ ) );
if(isExtensionSupported( glxExts, "GLX_ARB_create_context" )) // If the OpenGL 3.x context creation extension is available
{
    glXCreateContextAttribsARB = (GLXContext(*)(Display* dpy, GLXFBConfig config, GLXContext share_context, Bool direct, const int *attrib_list))glXGetProcAddressARB((GLubyte*)"glXCreateContextAttribsARB");

    hRC_ = glXCreateContextAttribsARB(display_, fbConfigs_[0], 0, true, attributes);  // Create and OpenGL 3.x context based on the given attributes

    if(!glXMakeCurrent(display_, window_, hRC_))
    {
        throw BlueFramework::BlueCore::Exception("Making context current failed.");
    }; // Make our OpenGL 3.0 context current

    GLenum err = glewInit(); // Enable GLEW

    if (GLEW_OK != err) // If GLEW fails
    {
        std::cerr << err << std::endl;
        std::cerr << "Error: " << glewGetString(err) << std::endl;
        throw BlueFramework::BlueCore::Exception("GLEW is not initialized!");
    }
}

移交给XXXCreateContextAttribs的属性也存在差异:

视窗:

WGL_CONTEXT_MAJOR_VERSION_ARB, majorVersion_, // Set the MAJOR version of OpenGL
WGL_CONTEXT_MINOR_VERSION_ARB, miniorVersion_, // Set the MINOR version of OpenGL
WGL_CONTEXT_FLAGS_ARB,
WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB // Set our OpenGL context to be forward com
WGL_CONTEXT_PROFILE_MASK_ARB,
WGL_CONTEXT_CORE_PROFILE_BIT_ARB
...

X11 / GLX:

GLX_CONTEXT_MAJOR_VERSION_ARB, majorVersion_, // Set the MAJOR version of OpenGL
GLX_CONTEXT_MINOR_VERSION_ARB, miniorVersion_, // Set the MINOR version of OpenGL
GLX_CONTEXT_FLAGS_ARB,
GLX_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB // Set our OpenGL context to be forward compatible
//#ifdef _DEBUG
|   GLX_CONTEXT_DEBUG_BIT_ARB
//#endif
,
GLX_CONTEXT_PROFILE_MASK_ARB,
GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
0
...

设备创建后,OpenGL依赖代码几乎相同。要在Win中创建渲染上下文,您需要创建一个Window句柄。

看起来像这样:

// Register class
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = (WNDPROC)RenderWindow::controlEventsProcessor;//WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = nullptr;//LoadIcon( hInstance, ( LPCTSTR )IDI_TUTORIAL1 );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
if(fullscreen_)
{
    wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
}
else
{
    wcex.hbrBackground = ( HBRUSH )( COLOR_WINDOW + 1 );
}
wcex.lpszMenuName = NULL;

wcex.lpszClassName = wstrWindowClassName_.c_str(); //ss.str().c_str();
wcex.hIconSm = nullptr;//LoadIcon( wcex.hInstance, ( LPCTSTR )IDI_TUTORIAL1 );

if( !RegisterClassEx( &wcex ) )
{
    BLUE_LOG_STREAM_EX(
        "BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow", 
        buw::eLogSeverityLevel::Error) 
        << "RegisterClassEx failed";

    return E_FAIL;
}

// Create window
hInstance = hInstance;

RECT rc = { 0, 0, width, height };
if(!AdjustWindowRect( &rc, WS_OVERLAPPEDWINDOW, FALSE ))
{
    BLUE_LOG_STREAM_EX(
        "BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow", 
        buw::eLogSeverityLevel::Error) 
        << "AdjustWindowRect failed";
}

if(fullscreen_)
{
    hWnd_ = CreateWindowEx(0,
        wstrWindowClassName_.c_str(),//L"BlueRenderWindow",//ss.str().c_str(), 
        L"Render Window", 
        WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
        0, 0,    // the starting x and y positions should be 0
        rc.right - rc.left, 
        rc.bottom - rc.top, 
        nullptr, 
        nullptr, 
        hInstance,
        nullptr);
}
else
{
    hWnd_ = CreateWindowEx(0,
        wstrWindowClassName_.c_str(),//L"BlueRenderWindow",//ss.str().c_str(), 
        L"Render Window", 
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 
        CW_USEDEFAULT, 
        rc.right - rc.left, 
        rc.bottom - rc.top, 
        nullptr, 
        nullptr, 
        hInstance,
        nullptr);
}

X11 / GLX中的计数器部分可能如下所示:

int attribs[] = {
    GLX_RENDER_TYPE,      GLX_RGBA_BIT,
    GLX_X_RENDERABLE,     True,
    GLX_DRAWABLE_TYPE,    GLX_WINDOW_BIT,
    GLX_X_VISUAL_TYPE,    GLX_TRUE_COLOR,
    GLX_DOUBLEBUFFER,     True,
    GLX_RED_SIZE,         8,
    GLX_BLUE_SIZE,        8,
    GLX_GREEN_SIZE,       8,
    GLX_ALPHA_SIZE,       8,
    GLX_DEPTH_SIZE,       24,
    GLX_STENCIL_SIZE,     8,
    0L //= None
};

display_ = XOpenDisplay(nullptr);
if(display_ == nullptr) 
{
    BLUE_LOG_STREAM_EX(
        "BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow", 
        buw::eLogSeverityLevel::Error) 
        << "Cannot connect to X-Server";

    return false;
}
int screenNr = XDefaultScreen (display_);


int numConfigs = 0;
    //  glXCreateContextAttribsARB = (GLXContext(*)(Display* dpy, GLXFBConfig      config, GLXContext share_context, Bool direct, const int *attrib_list))glXGetProcAddressARB((GLubyte*)"glXCreateContextAttribsARB");

glXChooseFBConfig = (GLXFBConfig*(*)(Display *dpy, int screen, const int *attrib_list, int *nelements))glXGetProcAddressARB((GLubyte*)"glXChooseFBConfig");

glXGetVisualFromFBConfig = (XVisualInfo*(*)(Display *dpy, GLXFBConfig config))glXGetProcAddressARB((GLubyte*)"glXGetVisualFromFBConfig");

fbConfigs_ = glXChooseFBConfig(display_, XDefaultScreen(display_), attribs, &numConfigs);
if(fbConfigs_ == nullptr) 
{
    BLUE_LOG_STREAM_EX(
        "BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow", 
        buw::eLogSeverityLevel::Error) 
        << "Failed to get framebuffer config";

    return false;
}   

XVisualInfo* vi = glXGetVisualFromFBConfig(display_, fbConfigs_[0]);
if(vi == nullptr) 
{
    BLUE_LOG_STREAM_EX(
        "BlueFramework::BlueGraphicsEngine::RenderWindow::initWindow", 
        buw::eLogSeverityLevel::Error) 
        << "No appropriate visual found (X-Server)";
    return false;
}

bool doubleBuffered = true;

Colormap cmap = XCreateColormap (display_, RootWindow (display_, vi->screen),
                  vi->visual, AllocNone);

XSetWindowAttributes attributes;
attributes.background_pixmap = 0L;
attributes.colormap = cmap;
attributes.border_pixel = 0;

答案 1 :(得分:0)

简单的方法是使用cygwinfreeglut,以及glew