OpenGL ES 2.0 cubemap is not showing a texture
I wanted to implement a skybox, but somehow there is nothing rendering. In the Initialization I simply build load the 3 Positioncoords of a cube from (-1,-1,-1) to (1, 1, 1). Textures are also loading, probably and adding them to the cube map. By the rendering i only passing the ModelViewProj but the are no Translation, scaling or anything applied to it, only the Projection.
Does someone see why it doesn't work? Did I forget something or understand something wrong?
Init 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];
Loading Textures:
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);
drawing 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);
Vertex Shader:
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);
}
This is the hole Information
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
};
Your cubemap generating code has an error:
for (int face = 1; face <= 6; ++face) {
You should run this from 0 to 5, or else you wont upload anything for GL_TEXTURE_CUBE_MAP_POSITIVE_X, and you'll upload something for GL_TEXTURE_CUBE_MAP_POSITIVE_X+6, which is invalid.
You should show your shaders, as there may be an error there too.
EDIT
Buffer creation seems to be incorrect. As you have a position, texCoord and a normal attributes, you should have 3 vertex attrib pointers, one for each:
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
NOTE i am using 0 1 2 for attribute locations, in your implementation you should fetch them with glGetAttribLocation();
Also, your variable _NumTriangles should be named _NumVertices, as it holds the number of vertices (a cube has 12 triangles --> 12*3 = 36 vertices are uploaded).
Also there may be some error with the buffer containing the data could you copy that too?
链接地址: http://www.djcxy.com/p/33910.html上一篇: iOS将对象渲染到纹理