我试图创建一个被天空盒包围的随机地形,然后将其应用于纹理。我有2个可互换运行的着色器程序,但似乎无法使天空盒出现在屏幕上,而地形却是正常渲染的。
地形实质上是x-z平面上的网格,每个顶点都有一个由噪声算法生成的y值。网格为正方形,且侧面为const TREE_DATA = {
Groceries: {
'Almond Meal flour': null,
'Organic eggs': null,
'Protein Powder': null,
Fruits: {
Apple: null,
Berries: ['Blueberry', 'Raspberry'],
Orange: null
}
},
Reminders: [
'Cook dinner',
'Read the Material Design spec',
'Upgrade Application to Angular'
]
};
。我通过在SIDE
和x
值上使用等于z
的偏移量来使地形居中。
天空盒是一个立方体,还具有侧面SIDE/2
,因此它应该与其中的网格完全匹配,并且应该可见,因为地形顶点的SIDE
值比y
甚至是SIDE
。
这是代码:
SIDE/2
顶点着色器和片段着色器的代码如下,并且进行编译和链接时没有错误:
GLfloat skyboxVertices[24] = {
-HALF_SIDE, HALF_SIDE, -HALF_SIDE, //A = 1
HALF_SIDE, HALF_SIDE, -HALF_SIDE, //B = 2
HALF_SIDE, -HALF_SIDE, -HALF_SIDE, //C = 3
-HALF_SIDE, -HALF_SIDE, -HALF_SIDE, //D = 4
-HALF_SIDE, HALF_SIDE, HALF_SIDE, //E = 5
HALF_SIDE, HALF_SIDE, HALF_SIDE, //F = 6
HALF_SIDE, -HALF_SIDE, HALF_SIDE, //G = 7
-HALF_SIDE, -HALF_SIDE, HALF_SIDE //H = 8
};
//generating & binding skybox VAO
glGenVertexArrays(1, &skyboxVAO);
glBindVertexArray(skyboxVAO);
//creating & binding VBO for skybox vertices
glGenBuffers(1, &skyboxVBO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices),
skyboxVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
//3 indices per triangle, 2 triangles per side, 6 sides for a cube
//for a total of 36
totalSkyboxElements = 36;
GLuint skyboxElements[36]{
0, 1, 3, 3, 2, 0, //front
4, 5, 1, 1, 0, 4, //up
4, 5, 6, 6, 7, 4, //back
7, 6, 3, 3, 2, 7, //bottom
1, 5, 6, 6, 3, 1, //right
0, 4, 7, 7, 2, 0 //left
};
//creating & binding VBO for skybox vertices
glGenBuffers(1, &skyboxElementsVBO);
glBindBuffer(GL_ARRAY_BUFFER, skyboxElementsVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxElements),
skyboxElements, GL_STATIC_DRAW);
最后,在主循环中,代码如下。 //vertex shader
#version 330 core
layout(location = 0) in vec3 position;
void main()
{
gl_Position = vec4(position, 1.0);
}
//fragment shader
#version 330 core
out vec4 color;
void main()
{
color = vec4(1.0, 1.0, 1.0, 1.0);
}
指天空盒,shaderProgramSB
指地形
shaderProgram
我尝试先渲染地形,然后再渲染天空盒,但是结果始终是相同的。地形正常渲染,天空盒根本不渲染。我一直在搜寻,但似乎找不到问题所在。任何帮助将不胜感激。