SDL OpenGL C++问题

发布于 2024-12-07 01:03:48 字数 1824 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

牵你的手,一向走下去 2024-12-14 01:03:49

我认为 glOrtho 可能设置错误。最后 2 个参数是近剪裁平面和远剪裁平面。如果您不想夹住桨,则近平面应小于零。你的远平面应该大于零。所以试试这个:

glOrtho( 0, width_of_screen, height_of_screen, 0, -1, 1 );

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:

glOrtho( 0, width_of_screen, height_of_screen, 0, -1, 1 );
桃扇骨 2024-12-14 01:03:49

现在您已经排除了错误,只需检查两件事:

  1. 尝试删除 glTranslatef 调用。距离可能太远,您看不到场景。

  2. 您是否按顺时针顺序创建顶点?我无法通过您使用的变量来判断,但如果 paddle_height > > 0 或 paddle_width < 0,它不会是顺时针的(它需要是)。

Just 2 things to check now that you've ruled out errors:

  1. Try removing the glTranslatef call. It may be too far off for you to see the scene.

  2. 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).

你是年少的欢喜 2024-12-14 01:03:49

尝试在您的投影中使用此方法(如果您希望 0,0,0 位于中心)。

double range = 4.0; // Set this to whatever you want for the range of the display.
double ratio = double(displayWidth) / double(displayHeight);
glOrtho(-range* ratio, range* ratio, -range, range, -1.0, 1.0);

暂时尝试渲染一个从 -1.0,-1.0 到 1.0,1.0 的正方形,看看您的显示器是否正常工作。

Try this for your projection instead (if you want 0,0,0 at your center).

double range = 4.0; // Set this to whatever you want for the range of the display.
double ratio = double(displayWidth) / double(displayHeight);
glOrtho(-range* ratio, range* ratio, -range, range, -1.0, 1.0);

Temporarily try to render a square from -1.0,-1.0 to 1.0,1.0 to see if your display is working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文