OpenGL,C ++,Skybox,CubeMap

时间:2018-06-25 13:54:41

标签: c++

faces.push_back方法带来此错误

  

“没有构造函数实例“ std :: vector_Ty,_Alloc> :: vector [with_Ty = std :: string,_Alloc = std :: Allocator]匹配参数列表”

加载bmp文件的loadbmp_custom不会导致任何错误,但是却给了我黑屏。

如果相关,我还包括了立方体贴图纹理头文件。

std::vector<std::string>faces
{
    faces.push_back("right.BMP"),
    faces.push_back("left.BMP"),
    faces.push_back("top.BMP"),
    faces.push_back("bottom.BMP"),
    faces.push_back("front.BMP"),           
    faces.push_back("back.BMP")

    //loadBMP_custom("right.BMP");
    //loadBMP_custom("left.BMP");
    //loadBMP_custom("top.BMP");
    //loadBMP_custom("bottom.BMP");
    //loadBMP_custom("front.BMP");
    //loadBMP_custom("back.BMP");
};
GLuint CubemapTexture = loadCubemap(faces);

这是hpp文件。

#pragma once
#include <iostream>
#include "SOIL.h"
#define GLEW_STATIC
#include <GL/glew.h>
#include <vector>

static GLuint loadCubemap(std::vector<std::string> faces)
{
    unsigned int textureID;
    glGenTextures(1, &textureID);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);

    int width, height, nrChannels;

    for (GLuint i = 0; i < faces.size(); i++)
    {
        unsigned char *data = SOIL_load_image(faces[i].c_str(), &width, &height, 
                              &nrChannels, 0);
        if (data) 
        {
            glTexImage2D(
                GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
                0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
            SOIL_free_image_data(data);
        }
        else
        {
            //std::cout << "Cubemap texture failed to load at path:" <<faces[i] << std::endl;
            printf("Cubemap texture failed to load at path:");

            //std::cout << &faces <<std::endl;

            SOIL_free_image_data(data);
        }
    }

    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    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);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    return textureID;
}

1 个答案:

答案 0 :(得分:1)

您不需要重复调​​用push_back来初始化vector。请改用vector<T>::vector(std::initializer_list<T>)构造函数:

std::vector<std::string> faces
{
    "right.BMP",
    "left.BMP",
    "top.BMP",
    "bottom.BMP",
    "front.BMP",
    "back.BMP"
};