我想实现一个天空盒,但不知何故没有渲染。 在初始化中,我只需构建一个立方体的3个位置,从(-1,-1,-1)到(1,1,1)。纹理也在加载,可能并将它们添加到立方体贴图中。 通过渲染我只传递ModelViewProj,但没有应用它的平移,缩放或任何东西,只有投影。
有人知道为什么它不起作用吗?我忘了什么或者理解错了吗?
初始VBO:
_VertexData = skyboxVertices;
_ByteSize = sizeof(GLfloat) * 3;
_NumTriangles = 36;
glEnable(GL_DEPTH_TEST);
glGenVertexArraysOES(1, &_vertexArray);
glBindVertexArrayOES(_vertexArray);
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(skyboxVertices), skyboxVertices, GL_STATIC_DRAW);
glEnableVertexAttribArray(GLKVertexAttribPosition);
glVertexAttribPointer(GLKVertexAttribPosition, 3, GL_FLOAT, GL_FALSE, 32, BUFFER_OFFSET(0));
glBindVertexArrayOES(0);
[self loadTextureFromImage:path Type:typ];
加载纹理:
glGenTextures(1, &(_TextureID));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _TextureID);
for (int face = 0; face < 6; ++face) {
[self getImage:[NSString stringWithFormat:@"%i",face]];
glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imageData);
}
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);
// glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,(int)width,(int)height,0,GL_RGBA,GL_UNSIGNED_BYTE,imageData);
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
free(imageData);
绘制Skymap:
glDepthMask(GL_FALSE);
glBindVertexArrayOES(_vertexArray);
glUseProgram(prog);
glEnable(GL_TEXTURE_CUBE_MAP);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, _TextureID);
GLuint projMatrix = glGetUniformLocation(prog, "modelViewProjectionMatrix");
GLuint normalMatrix = glGetUniformLocation(prog, "normalMatrix");
GLuint samplerCube =glGetUniformLocation(prog, "cubeMap");
glUniformMatrix4fv(projMatrix, 1, 0, _modelViewProj.m);
glUniform1i(samplerCube, 0);
glDrawArrays(GL_TRIANGLES, 0, _NumTriangles);
glDepthMask(GL_TRUE);
顶点着色器:
attribute vec4 position;
attribute vec3 texCoord;
attribute vec3 normal;
varying lowp vec3 vTexCoord;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewProjectionMatrix;
uniform mat3 normalMatrix;
void main()
{
vTexCoord = texCoord;
gl_Position = modelViewProjectionMatrix * position;
}
Fragment Shader:
uniform samplerCube cubeMap;
varying lowp vec3 vTexCoord;
void main()
{
lowp vec4 texCol = textureCube(cubeMap, vTexCoord);
lowp vec4 color = vec4(1.0,0.0,0.0,1.0);
gl_FragColor = vec4(texCol.rgba);
}
这是洞信息
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
};
答案 0 :(得分:1)
您的立方体贴图生成代码有错误:
for (int face = 1; face <= 6; ++face) {
你应该从0到5运行它,否则你不会为GL_TEXTURE_CUBE_MAP_POSITIVE_X上传任何内容,你将为GL_TEXTURE_CUBE_MAP_POSITIVE_X + 6上传一些无效的内容。
您应该显示着色器,因为那里也可能存在错误。
修改强>
缓冲区创建似乎不正确。由于你有一个位置,texCoord和一个普通的属性,你应该有3个顶点属性指针,每个指针一个:
glEnableVertexAttribArray(0); //position
glEnableVertexAttribArray(1); //texcoord
glEnableVertexAttribArray(2); //normal
glVertexAttribPointer(0, 4, GL_FLOAT, false, vertexByteSize, 0); //position
glVertexAttribPointer(1, 3, GL_FLOAT, false, vertexByteSize, (void*)(4 * 4)); //texCoord
glVertexAttribPointer(2, 3, GL_FLOAT, false, vertexByteSize, (void*)(7 * 4)); //normal
注意我使用0 1 2作为属性位置,在您的实现中,您应该使用glGetAttribLocation()获取它们;
此外,您的变量_NumTriangles应该命名为_NumVertices,因为它包含顶点的数量(一个立方体有12个三角形 - > 12 * 3 = 36个顶点被上传)。
也可能有一些错误,包含数据的缓冲区也可以复制吗?