我正在尝试用立方体映射制作一个吝啬。而且我已经将立方体“涂上”了,但没有纹理。我不知道问题是,当我加载我的纹理时,就像它没有加载任何信息。 或者因为它没有纹理坐标...但是在我发现的关于立方体贴图的教程和解释中,它们不需要任何纹理坐标。
如你所见,它正在绘制立方体,但一切都是黑色的。
#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改为颜色,这就是它的样子!
答案 0 :(得分:1)
您永远不会将任何数据加载到立方体贴图中。您拨打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个枚举值。