opengl quad不渲染

时间:2013-10-27 18:53:14

标签: c++ opengl sdl

我有一个书中的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;
}

1 个答案:

答案 0 :(得分: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通过缓冲区交换,清除等保持其所有矩阵的状态......状态不会因为你开始一个新帧而消失。