【发布时间】:2018-08-13 22:03:43
【问题描述】:
没有可见的天空盒,但代码似乎工作正常。
这就是我终于看到的:
我是 OpenGL 新手,所以这可能是 GL 代码的绑定或位置问题。完整代码可以访问:https://github.com/ZigoRiloo96/Riloo_Engine
我这里有 Skybox 类:
class Skybox
{
public:
Skybox();
~Skybox();
void Draw(const glm::mat4& view, const glm::mat4& projection);
private:
unsigned int cubemapTexture;
GLuint VAO;
GLuint VBO;
Shader* shader;
GLuint loadCubemap(std::vector<std::string> faces);
};
着色器:
#version 330 core
layout (location = 0) in vec3 aPos;
out vec3 TexCoords;
uniform mat4 projection;
uniform mat4 view;
void main()
{
TexCoords = aPos;
vec4 pos = projection * view * vec4(aPos, 1.0);
gl_Position = pos.xyww;
}
#version 330 core
out vec4 FragColor;
in vec3 TexCoords;
uniform samplerCube skybox;
void main()
{
FragColor = texture(skybox, TexCoords);
}
及类实现:
Skybox::Skybox()
{
GLfloat skyboxVertices[108] =
{
// positions
-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
};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), &skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
std::vector<std::string> faces
{
"Resources/skybox/right.jpg",
"Resources/skybox/left.jpg",
"Resources/skybox/top.jpg",
"Resources/skybox/bottom.jpg",
"Resources/skybox/front.jpg",
"Resources/skybox/back.jpg"
};
cubemapTexture = loadCubemap(faces);
shader = new Shader("Resources/Shaders/skybox.vp", "Resources/Shaders/skybox.frag");
shader->Use();
shader->setInt("skybox", 0);
}
Skybox::~Skybox()
{
}
void Skybox::Draw(const glm::mat4& view, const glm::mat4& projection)
{
glDepthFunc(GL_LEQUAL);
shader->Use();
shader->setMat4("view", glm::mat4(glm::mat3(view)));
shader->setMat4("projection", projection);
// skybox cube
glBindVertexArray(VAO);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, cubemapTexture);
glDrawArrays(GL_TRIANGLES, 0, 36);
glBindVertexArray(0);
glDepthFunc(GL_LESS);
}
GLuint Skybox::loadCubemap(std::vector<std::string> faces)
{
unsigned int textureID;
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
int width, height, nrChannels;
for (unsigned int i = 0; i < faces.size(); i++)
{
unsigned char *data = stbi_load(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);
stbi_image_free(data);
}
else
{
printf("Stb can't load this sh*t: %s\n", faces[i].c_str());
stbi_image_free(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;
}
【问题讨论】:
-
确保 a) 你可以渲染一个简单的多边形,使其可见 b) 天空盒面向你,即它没有显示在外面 b) 你实际上在天空盒内
-
以上包括解决任何问题的基本全局方法:ericlippert.com/2014/03/21/find-a-simpler-problem
-
我删除了 C 标签,如果您重新插入它,请确保就这个问题说明 C 是什么。而且,如果对大多数读者来说很明显,对不起。
-
见swift sphere combine star data 它使用旧式 gl 但这并不重要。 Yunnosch 是对的,您要么有错误的矩阵,要么有错误的缠绕规则。尝试在没有纹理的情况下渲染你的天空盒几何体,它可能在你的视锥体之外(太远或太近),或者在相机后面。也可以试试
glDisable(GL_CULL_FACE);。 -
也可能是你的近剪裁平面(见投影矩阵)太近了。它最多应该为 1,因为天空盒的半径为 1。
标签: c++ opengl rendering skybox