0

我有一个GL_QUADS来自书本的例子,我扔了一个黑色的,但我仍然不明白,我得到的只是我的清晰的白色屏幕可能是什么问题?

#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
int main(int argc, char* args[])
{
    // Memory usage specifics
    SDL_Init(SDL_INIT_EVERYTHING);
    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 );
    SDL_GL_SetAttribute( SDL_GL_BUFFER_SIZE, 32 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    // name on the window
    SDL_WM_SetCaption( "our first game", NULL );

    //set screen
    SDL_SetVideoMode(600,400,32, SDL_OPENGL);

    // when screen is clear
    glClearColor(1,1,1,1); //red, green, blue, alpha

    //portion to display
    glViewport(0,0,600,400);

    //shade
    glShadeModel(GL_SMOOTH);

    // 3D or 2D
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity(); //saves

    //if only 2D?
    glDisable(GL_DEPTH_TEST);
    //this handles the main loop
    bool isRunning = true;
    //this is for the events
    SDL_Event event;

    //main rectangle
    float myX = 300;
    float myY = 350;
    float width = 50;
    float height = 30;

    //main loop
    while ( isRunning )
    {
        //EVENTS
        while ( SDL_PollEvent(&event) )
        {
            //logic that should happen for certain events
            //quiting the window
            if ( event.type == SDL_QUIT )
            {
                isRunning = false;
            }
            // if ESC pressed
            if ( event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE )
            {
                isRunning = false;
            }
        }
        //LOGIC

        //RENDERING TO THE SCREEN
         glClear(GL_COLOR_BUFFER_BIT);
         //start rendering phase
         glPushMatrix();
         //set the matix
         glOrtho(0,600,400,0,-1,1);
         //black color
         glColor4ub(0,0,0,255);

         //drawing
         //GL_QUADS, GL_POINTS, GL_LINES, GL_LINE_STRIP,
         //GL_LINE_LOOP, GL_QUADS, GL_TRIANGLES, GL_POLIGON

         glBegin(GL_QUADS);
            glVertex2f(myX,myY);
            glVertex2f(myX+width,myY);
            glVertex2f(myX+width,myY+height);
            glVertex2f(myX,myY+height);
         glEnd();



        //render
        SDL_GL_SwapBuffers();
    }
    //house keeping
    SDL_Quit();
    return 0;
}
4

1 回答 1

0

这并不奇怪,由于这一行,您有等待发生的堆栈溢出:

//start rendering phase
glPushMatrix();

每一帧,你将“当前”矩阵推到堆栈的顶部,并且永远不会弹出任何东西。最终你会溢出并且glOrtho(0,600,400,0,-1,1);会有未定义的结果。

我可能会问你为什么首先将矩阵推入堆栈?如果您在每一帧上都覆盖了投影矩阵,您的代码将可以正常工作。

我也对这几行代码感到困惑:

// 3D or 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //saves

glLoadIdentity (...)不保存任何东西,它将一个单位矩阵写入当前矩阵:

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

由于glOrtho (...)乘以当前矩阵,我实际上会用第二个代码替换我提到的第一个代码。这样,您将始终产生相同的结果,而不是乘以矩阵的前一个值。

然而,更重要的是,假设您从未在代码中的其他任何地方修改矩阵,您不必在每次绘制时都设置投影矩阵。OpenGL 在缓冲区交换、清除等过程中保持其所有矩阵的状态......状态不会因为您开始一个新帧而简单地消失。

于 2013-10-27T19:31:24.977 回答