没有显示SDL的OpenGL窗口

时间:2011-12-10 18:44:16

标签: c linux opengl graphics sdl

我正在使用OpenGL / SDL编写程序,但窗口不会绘制。窗口的条目显示在任务栏和alt +选项卡菜单中,但不显示缩略图。当我点击它时,它被标记为活动,但屏幕上没有任何变化。

我可以验证从我的渲染循环中调用了glClear(GL_COLOR_BUFFER_BIT)SDL_GL_SwapBuffers(),并且SDL_SetVideoMode正在成功,但它仍然无法渲染。这不是共享库问题,因为其他SDL / GL应用程序运行正常。

有什么想法吗?

编辑:

这是图形初始化:

void GraphicsManager::initGraphics() {
    // Access prefs
    PreferencesManager* prefMgr = PreferencesManager::getInstance();
    int width = prefMgr->getIntKey("res_width");
    int height = prefMgr->getIntKey("res_height");

    if(SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
        fprintf(stderr, "Cannot initialize SDL video!\n");
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

    m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
    if(!m_screen) {
        fprintf(stderr, "Error: Invalid screen pointer\n");
        exit(2);
    }

    SDL_ShowCursor(SDL_DISABLE);

    // Setup OpenGL stuffs
    glDisable(GL_DEPTH_TEST); // We're only using 2D
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glViewport(0.0, 0.0, width, height);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width, height, 0.0, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);
    glClear(GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapBuffers();

    // Init FPS stuff
    m_avgFps = 60.0f;
    m_lastUpdate = SDL_GetTicks();
}

这是我的主循环:

while(stateMgr->getState() != GS_QUIT) {
    // Process input events
    inputMgr->processEvents();

    // Update the current system mode
    stateMgr->update();

    // Update GUI state
    guiMgr->update();

    // Render
    gfxMgr->render();
    //char buf[16];
    //snprintf(buf, 16, "FPS: %4.2f", gfxMgr->getFramerate());
    //testWindow->getChild("TestRoot/Framerate")->setText(buf);
    SDL_Delay(10);
}

这是渲染功能:

void GraphicsManager::render() {
    std::list<IRenderCallback*>::iterator rci;

    // Clear the screen
    glClear(GL_COLOR_BUFFER_BIT);

    // Run render callbacks
    /*for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
        (*rci)->preRender();
    }

    // Iterate through all renderables and render them
    std::list<Renderable*>::iterator i;
    for(i=m_renderables.begin();i != m_renderables.end();i++) {
        (*i)->render();
    }

    // Run render callbacks
    for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
        (*rci)->preFlip();
    }*/

    // Flip the buffers
    SDL_GL_SwapBuffers();

    // Update FPS stuff
    Uint32 now = SDL_GetTicks();
    Uint32 timeTaken = now - m_lastUpdate;
    m_lastUpdate = now;
    double seconds = (double)timeTaken / 1000.0f;
    m_avgFps = (1.0f / seconds);

    // Run render callbacks
    for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
        (*rci)->postRender();
    }
}

2 个答案:

答案 0 :(得分:1)

分解。做(主要是)最简单的事情应该有效:

#include <SDL.h>
#include <SDL_opengl.h>
#include <cstdio>
#include <iostream>

using namespace std;

int main( int argc, char** argv )
{
    if(SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
        fprintf(stderr, "Cannot initialize SDL video!\n");
        exit(1);
    }

    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

    int width = 640;
    int height = 480;

    SDL_Surface* m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
    if(!m_screen) {
        fprintf(stderr, "Error: Invalid screen pointer\n");
        exit(2);
    }

    cout << "GL_VERSION  : " << glGetString(GL_VERSION) << endl;
    cout << "GL_VENDOR   : " << glGetString(GL_VENDOR) << endl;
    cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << endl;

    SDL_ShowCursor(SDL_DISABLE);

    // Setup OpenGL stuffs
    glDisable(GL_DEPTH_TEST); // We're only using 2D
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glViewport(0.0, 0.0, width, height);
    glClear(GL_COLOR_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, width, height, 0.0, -1.0f, 1.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //glEnable(GL_TEXTURE_2D);
    glClear(GL_COLOR_BUFFER_BIT);
    SDL_GL_SwapBuffers();

    float angle = 0;
    bool running = true;
    while( running )
    {
        // handle all pending events
        SDL_Event event;
        while( SDL_PollEvent(&event) )
        {
            switch (event.type)
            {
            case SDL_QUIT:  
                running = false; 
                break;
            default:        
                break;
            }
        }

        angle += 1;
        while( angle > 360 )
            angle -= 360;

        // Render
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glPushMatrix();
        glTranslatef( width/2, height/2, 0 );
        glScalef( 150, 150, 0 );

        glRotatef( angle, 0, 0, 1 );
        glColor3ub(255,0,0);
        glBegin(GL_TRIANGLES);
        glVertex2i(0,0);
        glVertex2i(1,0);
        glVertex2i(1,1);
        glEnd();

        glPopMatrix();

        // Flip the buffers
        SDL_GL_SwapBuffers();

        SDL_Delay(10);
    }

    SDL_Quit();
    return 0;
}

如果这样可以完成所有工作(哈!),弄清楚简单程序执行流程与大程序的不同之处。如果它不起作用,你的环境可能会以某种方式搞砸。

GL_VERSION/VENDOR/RENDERER检查系统上打印的内容是什么?

答案 1 :(得分:1)

看起来你将每个像素的位设置为0。

m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);

一般来说,我认为你的情况需要32个。

话虽如此,如果这不能解决您的问题,请尝试致电:

m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER);

这将要求硬件表面和双缓冲。

我也没有看到你这么说:

glViewport(0, 0, width, height);

在你第一次打电话给glLoadIdentity之前。您可能在过去已经这样做了 - 但是,SDL_SetVideoMode可以导致重置GL上下文,这使得重新设置所需的所有OpenGL参数成为一种很好的做法。

然而,有一个更大的担忧。我们专注于您的GL代码,但是可能在此子系统初始化之前(或之后)SDL被破坏。 我想建议您在进入主循环之前调用一次SDL_GetError()只是为了看到一些没有检查回来的错误没有干扰。

使用OpenGL时,SDL还有很多其他问题。我的下一个问题是您的代码流可能多次调用get_surface ...尝试向函数添加静态变量,如下所示:

static int has_init = 0;

has_init++;
if (has_init > 1) {
    // Well that's odd isn't it
}