SDL OpenGL C++问题
我正在尝试使用 C++ 中的 SDL 和 OpenGL 制作一个简单的乒乓球游戏,但在屏幕上显示任何类型的 OpenGL 图像时遇到问题,想知道是否有人可以提供帮助:
初始化:
void init_everything()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( width_of_screen, height_of_screen, bpp_of_screen, SDL_OPENGL );
glClearColor( 0, 0, 0, 0 );
// Sets the projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width_of_screen, height_of_screen, 0, 1, -1 );
// initalises the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
SDL_WM_SetCaption( "Pong - By Michael Clover", NULL );
}
在此之后我调用类函数:
void paddle::show()
{
//Move to offset
glTranslatef( x, y, 0 );
//Start quad
glBegin( GL_QUADS );
//Set color to white
glColor4f( 1.0, 1.0, 1.0, 1.0 );
//Draw square
glVertex3f( 0, 0, 0 );
glVertex3f( paddle_width, 0, 0 );
glVertex3f( paddle_width, paddle_height, 0 );
glVertex3f( 0, paddle_height, 0 );
//End quad
glEnd();
//Reset
glLoadIdentity();
}
然后我将其放入我的主函数中:
int main( int argc, char **argv )
{
bool quit = false;
init_everything();
paddle playerpaddle;
while (quit == false )
{
while( SDL_PollEvent( &event ) )
{
playerpaddle.handle_input();
if (event.type == SDL_QUIT)
{
quit = true;
}
}
playerpaddle.move();
glClear( GL_COLOR_BUFFER_BIT );
playerpaddle.show();
SDL_GL_SwapBuffers();
}
clean_up();
return 0;
}
我得到的只是在 640 x 480px SDL 屏幕中设置的恒定黑色背景屏幕。
抱歉有大量文字,我将非常感谢您对这里可能出现的问题的任何见解,我猜我在某个地方犯了一个愚蠢的错误。
I'm trying to make a simple game of pong using SDL and OpenGL in C++ and I'm having trouble displaying any sort of OpenGL image onto the screen and was wondering if anybody could help:
Initialization:
void init_everything()
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_SetVideoMode( width_of_screen, height_of_screen, bpp_of_screen, SDL_OPENGL );
glClearColor( 0, 0, 0, 0 );
// Sets the projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, width_of_screen, height_of_screen, 0, 1, -1 );
// initalises the modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
SDL_WM_SetCaption( "Pong - By Michael Clover", NULL );
}
After this I call the class function:
void paddle::show()
{
//Move to offset
glTranslatef( x, y, 0 );
//Start quad
glBegin( GL_QUADS );
//Set color to white
glColor4f( 1.0, 1.0, 1.0, 1.0 );
//Draw square
glVertex3f( 0, 0, 0 );
glVertex3f( paddle_width, 0, 0 );
glVertex3f( paddle_width, paddle_height, 0 );
glVertex3f( 0, paddle_height, 0 );
//End quad
glEnd();
//Reset
glLoadIdentity();
}
I then put this in my main function:
int main( int argc, char **argv )
{
bool quit = false;
init_everything();
paddle playerpaddle;
while (quit == false )
{
while( SDL_PollEvent( &event ) )
{
playerpaddle.handle_input();
if (event.type == SDL_QUIT)
{
quit = true;
}
}
playerpaddle.move();
glClear( GL_COLOR_BUFFER_BIT );
playerpaddle.show();
SDL_GL_SwapBuffers();
}
clean_up();
return 0;
}
All I get is a constant black background screen that I set within a 640 by 480px SDL screen.
Sorry for the huge amount of text and I would be extremely grateful for any insight on what the problem could be here, I'm guessing I'm making a silly mistake somewhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为 glOrtho 可能设置错误。最后 2 个参数是近剪裁平面和远剪裁平面。如果您不想夹住桨,则近平面应小于零。你的远平面应该大于零。所以试试这个:
I think glOrtho might be set up wrong. The last 2 parameters are near and far clipping planes. Your near plane should be less than zero if you don't want to clip your paddle. And your far plane should be greater than zero. So try this:
现在您已经排除了错误,只需检查两件事:
尝试删除 glTranslatef 调用。距离可能太远,您看不到场景。
您是否按顺时针顺序创建顶点?我无法通过您使用的变量来判断,但如果 paddle_height > > 0 或 paddle_width < 0,它不会是顺时针的(它需要是)。
Just 2 things to check now that you've ruled out errors:
Try removing the glTranslatef call. It may be too far off for you to see the scene.
Are you creating your vertices in a clockwise order? I can't tell by the variables you used, but if paddle_height is > 0 or paddle_width < 0, it's not going to be clockwise (which it needs to be).
尝试在您的投影中使用此方法(如果您希望 0,0,0 位于中心)。
暂时尝试渲染一个从 -1.0,-1.0 到 1.0,1.0 的正方形,看看您的显示器是否正常工作。
Try this for your projection instead (if you want 0,0,0 at your center).
Temporarily try to render a square from -1.0,-1.0 to 1.0,1.0 to see if your display is working.