0

我正在尝试使用 C 语言中的 OpenGL 创建一个天空盒。我查看了很多教程并查看了很多人的代码,我认为我没有错过任何内容,但还是出现了黑屏。我是不是把整件事都搞错了?

#include <windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <stdio.h>
#include <math.h>

GLuint texture[6];

/**
 * Init function initializing the background to black.
 */
void init()
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glLoadIdentity();   
    glMatrixMode(GL_MODELVIEW | GL_PROJECTION);
    glEnable(GL_DEPTH_TEST);    
}

void reshape(int w, int h)
{
   glViewport(0, 0, (GLsizei) w, (GLsizei) h);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
   glMatrixMode(GL_MODELVIEW);
}

GLuint LoadTexture(const char * filename, int width, int height)
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    //Open the image file
    file = fopen(filename, "rb");
    //If it doesn't open just return 0
    if(file == NULL)
    {
        return 0;
    }

    //Allocate space for data and read
    data = (unsigned char *)malloc(width * height * 3);
    fread(data, width * height * 3, 1, file);

    //Close the file
    fclose(file);

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );

    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );    
    free(data);

    return texture;     
}

void draw()
{
    int width = 512;
    int height = 512;
    int length = 512;

    //start in this coordinates
    int x = 0;
    int y = 0;
    int z = 0;

    //center the square
    x = x - width / 2;
    y = y - height / 2;
    z = z - length / 2;

    // Bind the BACK texture of the sky map to the BACK side of the cube
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_QUADS);      
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y,  z);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z);
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y, z);
    glEnd();

    //FRONT
    glBindTexture(GL_TEXTURE_2D, texture[1]);
    glBegin(GL_QUADS);  
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,  z + length);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z + length); 
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,  z + length);
    glEnd();

    //BOTTOM
    glBindTexture(GL_TEXTURE_2D, texture[4]);
    glBegin(GL_QUADS);      
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,  z);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y,  z + length);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y,  z + length); 
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,  z);
    glEnd();

    //TOP
    glBindTexture(GL_TEXTURE_2D, texture[5]);
    glBegin(GL_QUADS);      
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y + height, z);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y + height, z + length); 
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z + length);
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z);
    glEnd();

    //LEFT
    glBindTexture(GL_TEXTURE_2D, texture[2]);
    glBegin(GL_QUADS);      
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x, y + height, z); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x, y + height, z + length); 
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x, y,  z + length);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x, y,  z);     

    glEnd();

    //RIGHT
    glBindTexture(GL_TEXTURE_2D, texture[3]);
    glBegin(GL_QUADS);  
        glTexCoord2f(0.0f, 0.0f); glVertex3f(x + width, y,  z);
        glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y,  z + length);
        glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, z + length); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(x + width, y + height, z);
    glEnd();
}

void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
    gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
    texture[0] = LoadTexture("back.bmp", 512, 512);
    /*texture[0] = SOIL_load_OGl_texture
    {
        "back.bmp",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y
    };*/
    texture[1] = LoadTexture("front.bmp", 512, 512);
    texture[2] = LoadTexture("left.bmp", 512, 512);
    texture[3] = LoadTexture("right.bmp", 512, 512);
    texture[4] = LoadTexture("cesped.bmp", 512, 512);
    texture[5] = LoadTexture("top.bmp", 512, 512);

    glEnable(GL_TEXTURE_2D);
    glEnable(GL_TEXTURE_GEN_S);
    glEnable(GL_TEXTURE_GEN_T);

    draw();
    glutSwapBuffers();
}

int main(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_ALPHA);
    glutInitWindowSize(1000, 1000);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("blah");

    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

    glutMainLoop();

    return 0;
}
4

2 回答 2

1

问题似乎是,天空盒不在可见区域内。查看投影矩阵的定义时glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);,可以看到沿 z 轴的可见值必须介于 1.5 和 20.0 之间。但是天空盒是从 -256.0 到 256.0 绘制的,它位于远平面/近平面的后面并被剪掉。

对此有两种可能的解决方案:一种可以绘制更小的天空盒,也可以增加可见区域。一般来说,两种情况下的结果是相同的(至少在没有深度测试的情况下渲染天空盒作为第一个对象,通常会这样做)。

根据评论:以像素为单位的纹理大小与几何大小之间没有关系。所以没有必要画一个边长为 512 的立方体。

于 2015-01-28T10:25:42.640 回答
0

有几件事看起来很糟糕:

  • glClear 在没有 GL_DEPTH_BUFFER_BIT 的情况下调用,但启用了深度测试
  • gluLookAt 使用 (0,0,0) 作为眼睛位置和兴趣点,模型视图矩阵看起来不太好;你应该使用盒子的中心作为位置和你想要的任何目标。
  • 您的显示功能正在重新加载图像并重新创建每一帧的纹理!这实际上应该在启动时在您的 init() 函数中一劳永逸地完成。
  • 您不需要启用 GL_TEXTURE_GEN_S 或 GL_TEXTURE_GEN_T,因为 正在 提供纹理坐标。
  • @BDL 所说的:考虑到盒子的大小(512.0),远剪裁平面太近了(20.0)。
于 2015-01-28T14:32:44.230 回答