立方体贴图未显示纹理 (OpenGL ES)

Cube Map is not showing a texture (OpenGL ES)

我正在尝试使用立方体贴图来节省开支。我已经有了立方体"painted",但是没有纹理。我不知道问题是否在于,当我加载我的纹理时,就像它没有加载任何信息一样。 或者因为它没有纹理坐标...但是在我发现的有关立方体贴图的教程和解释中,它们不需要任何纹理坐标。

如你所见,它正在绘制立方体,但所有内容都是黑色的。

#define SIZE 1.0

GLfloat VERTICES[] = {
    -SIZE,  SIZE, -SIZE,
    -SIZE, -SIZE, -SIZE,
    SIZE, -SIZE, -SIZE,
    SIZE, -SIZE, -SIZE,
    SIZE,  SIZE, -SIZE,
    -SIZE,  SIZE, -SIZE,

    -SIZE, -SIZE,  SIZE,
    -SIZE, -SIZE, -SIZE,
    -SIZE,  SIZE, -SIZE,
    -SIZE,  SIZE, -SIZE,
    -SIZE,  SIZE,  SIZE,
    -SIZE, -SIZE,  SIZE,

    SIZE, -SIZE, -SIZE,
    SIZE, -SIZE,  SIZE,
    SIZE,  SIZE,  SIZE,
    SIZE,  SIZE,  SIZE,
    SIZE,  SIZE, -SIZE,
    SIZE, -SIZE, -SIZE,

    -SIZE, -SIZE,  SIZE,
    -SIZE,  SIZE,  SIZE,
    SIZE,  SIZE,  SIZE,
    SIZE,  SIZE,  SIZE,
    SIZE, -SIZE,  SIZE,
    -SIZE, -SIZE,  SIZE,

    -SIZE,  SIZE, -SIZE,
    SIZE,  SIZE, -SIZE,
    SIZE,  SIZE,  SIZE,
    SIZE,  SIZE,  SIZE,
    -SIZE,  SIZE,  SIZE,
    -SIZE,  SIZE, -SIZE,

    -SIZE, -SIZE, -SIZE,
    -SIZE, -SIZE,  SIZE,
    SIZE, -SIZE, -SIZE,
    SIZE, -SIZE, -SIZE,
    -SIZE, -SIZE,  SIZE,
    SIZE, -SIZE,  SIZE
};

@interface CubeMap (){
    GLuint textureId;
    NSString *face[6];
    ObjLoader *objLoader;

    GLuint skyboxVAO, skyboxVBO;

    Shader *shader;
    GLuint program;

    //Unifroms
    GLuint u_modelViewProjectionMatrix;
    GLuint u_texture;
}

@property GLfloat* VertexData;
@property uint ByteSize;


@end

@implementation CubeMap

-   (void)initCubeMape{

    shader = [[Shader alloc] init];
    program = [shader standartProgramaVertPath:@"cubemap" VertType:@"vert" FragPath:@"cubemap" FragType:@"frag"];

    //Texture Locations
    face[0] = @"back";
    face[1] = @"back";
    face[2] = @"back";
    face[3] = @"back";
    face[4] = @"back";
    face[5] = @"back";

    //Uniforms
    glUseProgram(program);
    u_modelViewProjectionMatrix = glGetUniformLocation(program, "modelViewProjectionMatrix");
    u_texture = glGetUniformLocation(program, "cubeMap");

    glEnable(GL_TEXTURE_CUBE_MAP);
    glGenTextures(1, &textureId);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);

    for(int i = 0; i < 6; i++) {

        NSString *path = [[NSBundle mainBundle] pathForResource:@"back" ofType:@"jpg"];
        NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
        UIImage *image = [[UIImage alloc] initWithData:texData];
        if (image == nil){
            NSLog(@"Image could not been load");
        }


        size_t width = CGImageGetWidth(image.CGImage);
        size_t height = CGImageGetHeight(image.CGImage);
        //    NSLog(@"texture image w,h: %zu, %zu", width, height);
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        void *imageData = malloc( height * width * 4 );
        CGContextRef context0 = CGBitmapContextCreate( imageData, width, height, 8, 4 * width, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big );
        CGColorSpaceRelease( colorSpace );
        CGContextClearRect( context0, CGRectMake( 0, 0, width, height ) );
        CGContextTranslateCTM( context0, 0, height - height );
        CGContextDrawImage( context0, CGRectMake( 0, 0, width, height ), image.CGImage );

        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)width, (int)height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);



        CGContextRelease(context0);
        free(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);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

    glBindTexture(GL_TEXTURE_CUBE_MAP, 0);



    /*********************************
     ***********Buffering*************/

    // Setup skybox VAO
    glGenVertexArrays(1, &skyboxVAO);
    glGenBuffers(1, &skyboxVBO);
    glBindVertexArray(skyboxVAO);
    glBindBuffer(GL_ARRAY_BUFFER, skyboxVBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
    glBindVertexArray(0);


}

-   (void)update{

}

-   (void)render{
    glDepthMask(GL_FALSE);
    glDepthFunc(GL_LEQUAL);
    glBindVertexArray(skyboxVAO);
    glUseProgram(program);

    //bind Texture
    glEnable(GL_TEXTURE_CUBE_MAP);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_CUBE_MAP, textureId);
    glUniform1i(u_texture, 0);

    glUniformMatrix4fv(u_modelViewProjectionMatrix, 1, 0, _modelViewProj.m);


    glDrawArrays(GL_TRIANGLES, 0, 36);
    glBindVertexArray(0);
    glDepthFunc(GL_LESS);
    glDepthMask(GL_TRUE);

}

@end

只是为了测试,所有面孔都在加载相同的图像。

顶点着色器:

attribute vec3 position;

varying highp vec3 vTexCoord;

uniform mat4 modelViewProjectionMatrix;

void main()
{
    vTexCoord = position.xyz;
    gl_Position = modelViewProjectionMatrix * vec4(position, 1.0);
}

片段着色器:

uniform samplerCube cubeMap;

varying highp vec3 vTexCoord;

void main()
{
    lowp vec4 texCol = textureCube(cubeMap, vTexCoord);

    lowp vec4 color = vec4(vTexCoord, 1.0);

    gl_FragColor = texCol;
}

这就是它的样子,如果我将 gl_FragColor 更改为颜色!

您永远不会将任何数据加载到立方体贴图中。您在哪里进行 glTexImage2D() 调用:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, (int)width, (int)height, 0,
             GL_RGBA, GL_UNSIGNED_BYTE, imageData);

目标 GL_TEXTURE_2D 指定您要将数据加载到常规 2D 纹理中,这不是您正在使用的纹理类型。

要将数据加载到立方体贴图中,您必须使用立方体面作为目标:

glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, (int)width, (int)height, 0,
             GL_RGBA, GL_UNSIGNED_BYTE, imageData);

立方体面的枚举值是连续的,因此如果您向第一个面添加索引,则可以保证获得面的 6 个枚举值。