c++ - OpenGL:天空盒放大太多

标签 c++ opengl skybox

我正在尝试在游戏中实现天空盒。我使用的图像是:

enter image description here

不幸的是,它被极大地放大了,只显示了纹理的一些像素。它看起来像这样:

enter image description here

这是我创建天空盒的代码:

SkyBox::SkyBox()

{
    programID = LoadShaders("Resources/Shaders/skybox.vert", "Resources/Shaders/skybox.frag");

    pID = glGetUniformLocation(programID, "P");
    vID = glGetUniformLocation(programID, "V");

    float points[] =
    {
        -1.0f,  1.0f, -1.0f,
        -1.0f, -1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,
        1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f, -1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

        1.0f, -1.0f, -1.0f,
        1.0f, -1.0f,  1.0f,
        1.0f,  1.0f,  1.0f,
        1.0f,  1.0f,  1.0f,
        1.0f,  1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,

        -1.0f, -1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        1.0f,  1.0f,  1.0f,
        1.0f,  1.0f,  1.0f,
        1.0f, -1.0f,  1.0f,
        -1.0f, -1.0f,  1.0f,

        -1.0f,  1.0f, -1.0f,
        1.0f,  1.0f, -1.0f,
        1.0f,  1.0f,  1.0f,
        1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f,  1.0f,
        -1.0f,  1.0f, -1.0f,

        -1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
        1.0f, -1.0f, -1.0f,
        1.0f, -1.0f, -1.0f,
        -1.0f, -1.0f,  1.0f,
        1.0f, -1.0f,  1.0f
    };
    glGenBuffers (1, &vbo);
    glBindBuffer (GL_ARRAY_BUFFER, vbo);
    glBufferData (GL_ARRAY_BUFFER, sizeof (points), points, GL_STATIC_DRAW);

    const char *pth = "/Users/uonibr/Documents/Programming/Space Shooter/Space Shooter/Space Shooter/Resources/space.jpg";

    createCubeMap(pth, pth, pth, pth, pth, pth);
}


bool load_cube_map_side (GLuint texture, GLenum side_target, const char* file_name)
{
    glBindTexture (GL_TEXTURE_CUBE_MAP, texture);

    int x, y;
    unsigned char*  image_data = SOIL_load_image(file_name, &x, &y, 0, SOIL_LOAD_RGBA);
    if (!image_data) {
        fprintf (stderr, "ERROR: could not load %s\n", file_name);
        return false;
    }
    // non-power-of-2 dimensions check
    if ((x & (x - 1)) != 0 || (y & (y - 1)) != 0) {
        fprintf (
                 stderr, "WARNING: image %s is not power-of-2 dimensions\n", file_name
                 );
    }

    // copy image data into 'target' side of cube map
    glTexImage2D (
                  side_target,
                  0,
                  GL_RGBA,
                  x,
                  y,
                  0,
                  GL_RGBA,
                  GL_UNSIGNED_BYTE,
                  image_data
                  );
    free (image_data);
    return true;
}


void SkyBox::createCubeMap ( const char* front, const char* back, const char* top, const char* bottom, const char* left,const char* right)
{
    // generate a cube-map texture to hold all the sides
    glActiveTexture (GL_TEXTURE0);
    glGenTextures (1, &cubeMap);

    // load each image and copy into a side of the cube-map texture
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, front));
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, back));
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, top));
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, bottom));
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, left));
    assert(load_cube_map_side (cubeMap, GL_TEXTURE_CUBE_MAP_POSITIVE_X, right));

    // format cube map texture
    glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
    glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri (GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}

下面是天空盒的渲染代码:

void SkyBox::render(const Camera &camera) const
{
    glDepthMask (GL_FALSE);
    glUseProgram (programID);
    glActiveTexture (GL_TEXTURE0);
    glBindTexture (GL_TEXTURE_CUBE_MAP, cubeMap);
    glUniformMatrix4fv(pID, 1, GL_FALSE, &camera.getProjectionMatrix()[0][0]);
    glm::mat4 view = camera.getRotationMatrix();
    glUniformMatrix4fv(vID, 1, GL_FALSE, &view[0][0]);
    glEnableVertexAttribArray (0);
    // 1st attribute buffer : vertices
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glVertexAttribPointer(
                          0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
                          3,                  // size
                          GL_FLOAT,           // type
                          GL_FALSE,           // normalized?
                          0,                  // stride
                          (void*)0            // array buffer offset
                          );

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glDrawArrays (GL_TRIANGLES, 0, 36);

    glDisableVertexAttribArray(0);
    glDepthMask (GL_TRUE);
    glCullFace(GL_BACK);

}

片段着色器很简单:

#version 330 core

in vec3 texcoords;
uniform samplerCube cube_texture;
out vec4 frag_color;

void main () {
    frag_color = texture (cube_texture, texcoords);
}

和顶点着色器一样:

#version 330 core

in vec3 vp;
uniform mat4 P, V;
out vec3 texcoords;

void main () {
    texcoords = vp;
    gl_Position = P * V * vec4 (vp, 1.0);

}

编辑: 这是我生成投影矩阵的代码:

glm::mat4 Camera::getProjectionMatrix() const
{
    return glm::perspective(FoV, 4.0f / 3.0f, 0.1f, 100.0f);
}

这是我的 FoV:

 float FoV = 3.14159 * 65. / 180;

最佳答案

我确定了这个问题: 当我应该通过度数时,我将弧度传递给 glm::perspective() 函数。因此,正如对问题的评论,答案是小 FoV

关于c++ - OpenGL:天空盒放大太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28128443/

相关文章:

unity3d - 在Unity5中哪里可以找到skybox包?

c++ - OpenGL 不显示天空盒纹理?

c++ - 指向 C++ 类成员函数的指针作为全局函数的参数?

c++ - 为什么 C++ && 运算符需要优先级

c++ - 我如何指向 std::set 的成员,以便我可以判断该元素是否已被删除?

c++ - OpenGL 变换具有不同轴多次旋转的对象

c - 我正在尝试用 C 中的 openGL 创建一个天空盒,但只得到黑屏

C++11 基于范围的循环 : How does it really work

opengl - 不了解 gluOrtho2D 函数

c++ - 在 C++.NET 中使用 OpenGL 并在视口(viewport)上绘制