-1

我正在玩一些 OpenGL 和 SDL。目前,我正在尝试让 HUD 显示在一些 3D 旋转立方体上。但是,当我在 3d 形状之后添加代码以绘制 2d 四边形时,我得到了黑屏死机。现在根本没有渲染,包括以前工作的 3d 形状,所以这似乎不是深度问题。

我在做什么:

_SETUP_

Make a new SDL window.

Enable lighting and make a new light. 

Enable depth.

_GAMELOOP_

Switch the matrix to GL_PROJECTION and make a new perspective view.

Switch back to the GL_MODELVIEW matrix.

Draw some shapes.

Switch the matrix back to GL_PROJECTION and make a new ortho view.

Switch back to the GL_MODELVIEW matrix.

Draw a 2d shape.

代码(有点乱,我很抱歉)绘制了一个 10x10 的旋转立方体墙并让它们改变颜色:

int main(int argc, char **argv)
{
srand(12);

int x = 0, i, j, k;
Coordinate ang, pos;
Color color, colorMod;
SDL_Event event;
bool quit = false;

color.r = 0;
color.g = 0;
color.b = 0;

colorMod.r = .01;
colorMod.g = .05;
colorMod.b = .03;

pos.x = -5;
pos.y = -5;
pos.z = -40.0f;

SDL_Init(SDL_INIT_EVERYTHING);

SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

glClearColor(0, 0, 0, 0);
glClearDepth(1.0f);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0); 

GLfloat lightpos[] = {10, 10, 10, 1};
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);

glEnable(GL_COLOR_MATERIAL);

while(quit == false)
{    
    //Draw some cubes
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(40.0, 800.0/600.0, 0.5, 20.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);   
    glLoadIdentity();                                    

    for(i=0;i<10;i++)
    {
        pos.x += 2;

        for(j=0;j<10;j++)
        {
            pos.y += 2;
            glPushMatrix();
            drawACube(ang, pos, color);
            glPopMatrix();
        }

        pos.y = -10;
    }

    pos.x = -10; 

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 600, 0, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glPushMatrix();

    glBegin(GL_QUADS);

    glColor3f(color.r, color.g, color.b);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, -1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);

    glEnd();

    glPopMatrix();  

    //Swap and FPS delay
    SDL_GL_SwapBuffers();

    SDL_Delay(10);

    //Makes the rotation happen   
    ang.x += .7;
    ang.y += .1;
    ang.z += .1;

    //Monitor for a quit event
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            quit = true;
        }
    }

    //Do the color stuff
    if(color.r > 1)
        colorMod.r = -.01;
    else if(color.r < 0)
        colorMod.r = .01;

    if(color.g > 1)
        colorMod.g = -.05;
    else if(color.g < 0)
        colorMod.g = .05;

    if(color.b > 1)
        colorMod.b = -.03;
    else if(color.b < 0)
        colorMod.b = .03;

    color.r += colorMod.r;
    color.g += colorMod.g;
    color.b += colorMod.b;
}

SDL_Quit();

return 0;
}

void drawACube(Coordinate ang, Coordinate pos, Color color)
{    
glTranslatef(pos.x, pos.y, pos.z);
glRotatef(ang.x, 1.0f, 0.0f, 0.0f);
glRotatef(ang.y, 0.0f, 1.0f, 0.0f);
glRotatef(ang.z, 0.0f, 0.0f, 1.0f); 


glBegin(GL_QUADS);

//Top
glNormal3d(0, 1, 0);
glColor3f(color.r, color.g, color.b);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);


//Bottom
glNormal3d(0, -1, 0);
glColor3f(color.r, color.g, color.b);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

//Front
glNormal3d(0, 0, 1);
glColor3f(color.r, color.g, color.b);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

//Back
glNormal3d(0, 0, -1);
glColor3f(color.r, color.g, color.b);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);

//Left side
glNormal3d(-1, 0, 0);
glColor3f(color.r, color.g, color.b);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);

//Right side   
glNormal3d(1, 0, 0);
glColor3f(color.r, color.g, color.b);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);

glEnd();
}

编辑:我尝试注释掉 2d 部分,并遇到了上述相同的问题。还更改了上面的代码以删除颜色缓冲区的错误清除(没有解决问题,但以后可能会出现问题

解决方案:在顶部有一个不正确的 gluPerspective 行,它搞砸了一切。感谢您的快速回复,我为错误道歉

4

1 回答 1

1

您的渲染代码中有一个glClear(GL_COLOR_BUFFER_BIT|…),抹去了您之前绘制的所有内容。对于 HUD,您可能只想清除深度缓冲区,但保留颜色。

附带说明一下,您应该在绘制立方体之前移动照明设置。您可能不希望该照明用于绘制 HUD,因此您将在绘制 HUD 之前禁用/更改它。

于 2013-11-05T15:05:20.200 回答