好的,所以我遇到了一个关于我一直在从事的项目的非常奇怪的问题。该项目是一个 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 中运行我的程序时都会产生这些非常奇怪的效果。
问题将是:
- 视图似乎卡在正交投影中(当它设置为透视时)
- 着色器、混合、帧缓冲区/VBO、纹理不工作。
- 极度延迟。
我认为我可能做错的唯一地方是 Initializing 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,嗯,没什么大不了的。