c - SDL+OpenGL 在 linux 下工作但在 windows 下不工作,不是编译器/链接器问题

标签 c windows linux opengl sdl

好吧,关于我一直从事的项目,我遇到了一个非常奇怪的问题。该项目是一个 3D 引擎(分别使用 SDL 和 openGL 进行输入处理和图形处理)。在过去的几个月里,我一直在 linux(Ubuntu 10.10) 中从事这个项目,最近决定将代码移植到 Windows 7 环境中。我选择 SDL 的主要原因之一是它独立于操作系统处理窗口创建,这意味着从一个操作系统移植到另一个操作系统应该相当简单。我在编译或链接时没有遇到问题,问题是完全相同的代码显然没有理由以两种完全不同的方式运行。

首先,一个差异的可视化示例:

http://i.stack.imgur.com/RNcCs.jpg

它不应该这样看,首先左下角的紫色 revtanglers 应该是文本,如果你仔细观察你可以看到一些点被渲染,我使用的是几何、顶点和像素着色器:

http://i.stack.imgur.com/1Y9km.jpg

它应该看起来更像这样,就像在 linux 中一样。我所说的那些点是这些中的草:

http://i.stack.imgur.com/Hzmuy.jpg

http://i.stack.imgur.com/HYCMh.jpg

它还在 Linux 中以 600+ FPS 的速度运行,着色器工作正常,一切都完美无缺。我添加了一些代码来检查 Windows 中的 FPS(按下按钮时将当前 fps 发送到日志文件)并且它在禁用着色器的情况下以 20-30 FPS 运行。

我对此感到非常困惑,因为我使用的代码实际上没有什么不同,唯一的区别在于我使用 openGL 的方式:在 Windows 中我使用 GLEW 来处理我的扩展,而在 Linux 中我使用#define GL_GLEXT_PROTOTYPES . 我环顾四周,似乎我正在使用的代码应该在 linux 和 windows 上工作,尽管每当我在 windows 中运行我的程序时,我都会遇到这些非常奇怪的效果。

问题是:

  • View 似乎卡在正交投影中(当它设置为透视时)
  • 着色器、混合、帧缓冲区/VBO、纹理都无法正常工作。
  • 极度延迟。

我认为我唯一可能做错的地方是初始化 SDL/OpenGL/GLEW。

我直接使用 GLEW,因为我最初链接到的库给我带来了问题。

//SAE_GL.h
/**Header Guard**/
#ifndef __SAE_OPENGL__
#define __SAE_OPENGL__

/**Include**/
///gl extensions
#ifdef __WIN32
    #define GLEW_STATIC
    #include "GL/glew.h"
#else
    #define GL_GLEXT_PROTOTYPES
#endif

///opengl
#include "GL/gl.h"
#include "GL/glu.h"

///SAE Dependencies
#include "SAE_Core.h"

窗口管理器(WM.h 仅包含声明并包括 SAE_GL.h、SAE_SDL.h 和 SAE_Core.h)

#include "SAE_WM.h"

namespace SAE{

bool SDL_Application::SAE_QUIT_BOOL_DEFAULT = false;
bool SDL_Application::SAE_MENU_BOOL_DEFAULT = false;
bool SDL_Application::SAE_VERBOSE_FLAG = false;

///Constructor
SDL_Application::SDL_Application(){

    ///Video Info
    screen      = (SDL_Surface*)NULL;
    videoInfo   = (SDL_VideoInfo*)NULL;

    VIEW_MODE               = SAE_PERSPECTIVE;
    VIEW_DIM                = SAE_3D;

    defaultScreenInfo = SAE_DEFAULT_SCREENINFO_HOLDER;
    currentScreenInfo = SAE_DEFAULT_SCREENINFO_HOLDER;

    numResolutions = 0;

    ///Set up our video flags
    videoFlags  = SDL_OPENGL;          ///OpenGL
    videoFlags |= SDL_GL_DOUBLEBUFFER; ///Double Buffering
    videoFlags |= SDL_HWPALETTE;       ///Harware Palette
    videoFlags |= SDL_RESIZABLE;       ///Window Resizing



    ///Callback setup
    setResizeFunction       ( SDL_DResize );
    setRunFunction          ( SDL_DRun    );
    setEventFunction        ( SDL_DEvent  );
    setCleanupFunction      ( SDL_DClean  );
    setMenuManagerFunction  ( SDL_DMenu   );

    SAE_QUIT_FLAG           = &SAE_QUIT_BOOL_DEFAULT;
    SAE_MENU_FLAG           = &SAE_MENU_BOOL_DEFAULT;


/////////////////////////////////////////////////

    ///FPS Handler setup
    time        = 0;
    frames      = 0;




/////////////////////////////////////////////////


/////////////////////////////////////////////////

/////////////////////////////////////////////////
/////////////////////////////////////////////////
}

void SDL_Application::init(int argc, char** argv){
    ///Redirect output to log
    FILE* tempb;
    tempb = freopen( "logfile.dat", "a+", stderr );

    fprintf(stderr,"\n===================================================================================================\n");
    fprintf(stderr,"\n***Initializing program essentials***\n");

    bool create_fonts = false;

    for(int i = 0; i < argc; i++){
        if(strncmp(argv[i], "-v",10)==0) SAE_VERBOSE_FLAG = true;
        if(strncmp(argv[i], "--genfonts",20)==0){
            videoFlags |= SDL_NOFRAME;
            create_fonts = true;
        }
    }

    ///Initialize Everything in SDL
    if(be_verbose)fprintf(stderr,"----SDL        : ");
    if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ){
        if(be_verbose){
            fprintf( stderr, "Failed\n");
            fprintf( stderr, "--------Critical Error: SDL init failed: %s\n", SDL_GetError( ) );
        }
        fprintf( stderr, "****The program encountered a critical error - exiting...****");
        exit( 0 );
    }else{
        if(be_verbose)fprintf( stderr, "Success :)\n" );
    }

    ///Get Video Info
    videoInfo = ( SDL_VideoInfo* )SDL_GetVideoInfo( );
    if(be_verbose)fprintf(stderr,"----Video Info : ");

    if( !videoInfo ){
        if(be_verbose){
            fprintf( stderr, "Failed\n");
            fprintf( stderr, "--------Critical Error: Can't query video info: %s\n", SDL_GetError() );
        }
        fprintf( stderr, "\n****The program encountered a critical error - exiting...****");
        exit( 0 );
    } else {
        ///Setup our default width and height
        defaultScreenInfo.Width  = videoInfo->current_w;
        defaultScreenInfo.Height = videoInfo->current_h;
        if(be_verbose)fprintf( stderr, "Success :)\n");
    }

//////////////////////////////////////////////////////////////////////////////////////////////////

    ///Sets up what type of surface we're using
    if(be_verbose) fprintf(stderr,"----FrameBuffer: ");
    if( videoInfo -> hw_available ){
        videoFlags |= SDL_HWSURFACE;
    }
    else{
        videoFlags |= SDL_SWSURFACE;
    }

/////////

    ///Sets up hardware blitting
    if( videoInfo -> blit_hw ){
        videoFlags |= SDL_HWACCEL;
    }

/////////

    ///Enable Double Buffering
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE,   16);
    ///Startup the screen

    if(create_fonts){
        screen = SDL_SetVideoMode( SAE_DEFAULT_TEXTURE_WIDTH,
                                   SAE_DEFAULT_TEXTURE_HEIGHT,
                                   SAE_DEFAULT_SCREEN_BYTES_PER_PIXEL,
                                   videoFlags );
    }else{
        screen = SDL_SetVideoMode( SAE_DEFAULT_SCREEN_WIDTH,
                                   SAE_DEFAULT_SCREEN_HEIGHT,
                                   SAE_DEFAULT_SCREEN_BYTES_PER_PIXEL,
                                   videoFlags );
        }

    if ( !screen ){
        if(be_verbose)fprintf( stderr, "Critical Error: Video mode not supported: %s\n", SDL_GetError());
        fprintf( stderr, "\n****The program encountered a critical error - exiting...****");
        exit(0);
    } else {
        if(be_verbose) fprintf( stderr, "Success :)\n");
    }

//////////////////////////////////////////////////////////////////////////////////////////////////

    SDL_PixelFormat format;
    SAE_ScreenInfo max;
    format.BitsPerPixel = SAE_COMPATABILITY_SCREEN_BYTES_PER_PIXEL;

////////

    ///Get supported screen resolutions
    SDL_Rect** modes = SDL_ListModes(&format, videoFlags|SDL_FULLSCREEN);

    if(be_verbose)fprintf(stderr,"----Resolutions: ");
    if (modes == (SDL_Rect**)0) {
        if(be_verbose)fprintf(stderr,"Critical Error: No resolutions available!\n");
        fprintf( stderr, "\n****The program encountered a critical error - exiting...****");
        exit(-1);
    }else{
        if(be_verbose)fprintf(stderr,"Success :)\n");
    }

    if (modes == (SDL_Rect**)-1) {
        max.Width = videoInfo->current_w;
        max.Height = videoInfo->current_h;
    }else{
        for (int i=0; modes[i]; ++i){
            max.Width = modes[i]->w;
            max.Height = modes[i]->h;
        }
    }

    for(int i = 0; i < 23; i+=2){
        if(SAE_SCREEN_RESOLUTIONS[i] <= max.Width && SAE_SCREEN_RESOLUTIONS[i+1] <= max.Height)
        {
            supportedResolutions[numResolutions].Width = SAE_SCREEN_RESOLUTIONS[i];
            supportedResolutions[numResolutions].Height = SAE_SCREEN_RESOLUTIONS[i+1];
            numResolutions++;
        }
    }

    ///Initialize openGL
    if(be_verbose)fprintf(stderr,"----OpenGL     : ");
    if(!initializeOpenGL()){
        if(be_verbose)fprintf(stderr,"Critical Error: openGL init failed, exiting\n");
        fprintf( stderr, "\n****The program encountered a critical error - exiting...****");
        exit(0);
    } else {
        if(be_verbose)fprintf(stderr,"Success :)\n\n");
    }
/////////////////////////////////////////////////
    fprintf(stderr,"***------------SUCCESS------------***\n");
////////////////////////////////////////////////

    if(be_verbose){
        fprintf(stderr,"\n***---Current Video Information---***\n");
        fprintf( stderr, "----Software Surface: %s\n", (videoFlags&SDL_SWSURFACE )?"ENABLED":"DISABLED");
        fprintf( stderr, "----Hardware Surface: %s\n", (videoFlags&SDL_HWSURFACE )?"ENABLED":"DISABLED");
        fprintf( stderr, "----Hardware Palette: %s\n", (videoFlags&SDL_HWPALETTE )?"ENABLED":"DISABLED");
        fprintf( stderr, "----Double Buffering: %s\n", (videoFlags&SDL_DOUBLEBUF )?"ENABLED":"DISABLED");
        fprintf( stderr, "----openGL Doublebuf: %s\n", (videoFlags&SDL_GL_DOUBLEBUFFER )?"ENABLED":"DISABLED");
        fprintf( stderr, "----Fullscreen      : %s\n", (videoFlags&SDL_FULLSCREEN)?"ENABLED":"DISABLED");
        fprintf( stderr, "----OpenGL          : %s\n", (videoFlags&SDL_OPENGL    )?"ENABLED":"DISABLED");
        fprintf( stderr, "----Resizable       : %s\n", (videoFlags&SDL_RESIZABLE )?"ENABLED":"DISABLED");
        fprintf( stderr, "----No Frame        : %s\n", (videoFlags&SDL_NOFRAME   )?"ENABLED":"DISABLED");
    }
    SDL_EnableKeyRepeat(500,250);
    time = SDL_GetTicks();
    resizeWindow(SAE_DEFAULT_SCREEN_WIDTH, SAE_DEFAULT_SCREEN_HEIGHT);
    time = SDL_GetTicks();
}

这是我设置 openGL/GLEW 以供使用的地方。

///Initialize openGL
bool SDL_Application::initializeOpenGL(){
/////////////////////////////////////////////////

    ///Initialize GLEW and load extensions
    #ifdef __WIN32
    GLenum status = glewInit();

    if( status != GLEW_OK ){
    ///Glew Initialization went wrong
        fprintf(stderr, "Error: %s\n", glewGetErrorString( status ));
        return false;
    } else {
        fprintf(stderr, "GLEW successfully initialized!\n");
    }
    #endif

/////////////////////////////////////////////////

    ///Sets clear color and clear depth
    float clearColor[]={0.0f,0.0f,0.0f,0.0f};
    glClearColor( clearColor[0], clearColor[1], clearColor[2], clearColor[3] );
    glClearDepth( 1.0f );

/////////
    ///DEPTH FUNCTION NEEDS CHECKING
    glDepthFunc ( GL_LEQUAL );
    glBlendEquation(GL_FUNC_ADD);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glShadeModel( GL_SMOOTH );
/////////
    glEnable( GL_DEPTH_TEST );      ///Depth Testting
    glEnable( GL_CULL_FACE);        ///Face Culling
/////////////////////////////////////////////////

    return true;

/////////////////////////////////////////////////

}

我还包括我的窗口大小调整函数,因为它是相对的。

void SDL_Application::resizeWindow(int width, int height){
/////////////////////////////////////////////////
        SDL_Event myEvent;
        myEvent.type = SDL_VIDEORESIZE;
        myEvent.resize.type = SDL_VIDEORESIZE;
        myEvent.resize.w = width;
        myEvent.resize.h = height;
        SDL_PushEvent(&myEvent);

    }

void SDL_Application::ResizeWindow(int width, int height){
/////////////////////////////////////////////////
    double v_min = (VIEW_DIM == SAE_2D)?1.0:  0.1 * SAE_UNIT_MEASURE;
    double v_max = (VIEW_DIM == SAE_2D)?1.0: 1000.0 * SAE_UNIT_MEASURE;
    ///Prevents divide by zero
    if( height <= 0 ) height = 1;

    ///Set aspect ratio
    double ratio = (double)width/(double)height;
    currentScreenInfo.Width = width;
    currentScreenInfo.Height = height;
/////////

    ///Load and clear our projection matrix
    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();

    ///Set the entire window as our viewport
    glViewport( 0, 0, currentScreenInfo.Width, currentScreenInfo.Height );

    if(VIEW_MODE == SAE_PERSPECTIVE)
    {
        ///Set up a perspective projection
        gluPerspective( 70, ratio, v_min, v_max );
    }else{
        ///Gives us an orthographic perspective
        glOrtho( v_min, v_max, v_min, v_max, v_min, v_max);

    }



/////////

    ///Set what we're looking at, into the screen
    gluLookAt(0, 0, 0,
              0, 0, -1,
              0.0f, 1.0f, 0.0f);

/////////

    ///Do our user-set resize function and resize the screen

    SAEResize( currentScreenInfo.Width, currentScreenInfo.Height);
    screen = SDL_SetVideoMode( currentScreenInfo.Width, currentScreenInfo.Height, SAE_DEFAULT_SCREEN_BYTES_PER_PIXEL, videoFlags);
/////////////////////////////////////////////////
}

我想我一定是遗漏了什么,或者我不知道 SDL/OpenGL/GLEW 如何在 Windows 上工作。

编辑:看来我的问题可能得不到具体答案,我想我应该重写我的代码以使用 DirectX,嗯,没什么大不了的。

最佳答案

我不认为这是你的问题的主要原因,但是:为什么这么多人,包括 OP 在窗口大小调整处理程序中设置投影矩阵?为什么?设置矩阵全部进入显示功能。

此外,投影矩阵只能用于设置“镜头”。一个人不得使用投影矩阵设置 View 位置。 gluLookAt 投影矩阵模式:又大又胖 NoGo!

您说您正在使用着色器。那么你为什么要使用固定函数矩阵运算呢?将矩阵作为制服传递。这也立即解决了在不属于调整大小处理程序的地方做事的问题。

关于c - SDL+OpenGL 在 linux 下工作但在 windows 下不工作,不是编译器/链接器问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12984429/

相关文章:

c - 如何通过C中的PID监视外部进程的事件?

c - 有没有办法在 Linux 的 32 位程序中获取 64 位 time_t?

C语言不区分大小写地统计某个字符在文件中出现的次数

windows - 在 Windows 命令提示符中查找已安装应用程序的日期和时间

javascript - 如果 Deeplink url 不起作用,请将用户发送到下载页面

linux - 为什么 Linux "sort -u"比 Excel 快这么多

c - C中的暂停功能不响应回车键

c - 如何在 C 中复制此字符串?

c - 静态函数对另一个文件可见

windows - Win32_Product 的替代品?