我正在使用 3 个纹理。但只有两个纹理在工作。
enum {
UNIFORM_TEXTURE,
UNIFORM_TEXTURE_MAP,
UNIFORM_INPUT1,
NUM_UNIFORMS};
GLint uniforms[NUM_UNIFORMS];
第三个没有显示。这就是我激活纹理的方式:
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// second texture
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, textureName1);
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
// thrid texture
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, textureName2);
glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glUniform1i(uniforms[UNIFORM_TEXTURE], 0);
glUniform1i(uniforms[UNIFORM_TEXTURE_MAP], 1);
glUniform1i(uniforms[UNIFORM_INPUT1],2);
uniforms[UNIFORM_TEXTURE] = glGetUniformLocation(program, "texture");
uniforms[UNIFORM_TEXTURE_MAP] = glGetUniformLocation(program,"texture2");
uniforms[UNIFORM_INPUT1]= glGetUniformLocation(program, "texture3");
在我的着色器中,我只调用第三个纹理来检查它。
highp vec4 texel = texture2D(texture3,texCoordVarying);
highp vec2 coord = 0.5 * (texel.xy / 0.8);
gl_FragColor = vec4(coord,0,0);
这里我如何加载纹理:
- (void)loadTexture:(GLuint *)newTextureName fromFile:(NSString *)fileName {
// Load image from file and get reference
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
CGImageRef imageRef = [image CGImage];
if(imageRef) {
// get width and height
size_t imageWidth = CGImageGetWidth(imageRef);
size_t imageHeight = CGImageGetHeight(imageRef);
GLubyte *imageData = (GLubyte *)malloc(imageWidth * imageHeight * 4);
memset(imageData, 0, (imageWidth * imageHeight * 4));
CGContextRef imageContextRef = CGBitmapContextCreate(imageData, imageWidth, imageHeight, 8, imageWidth * 4, CGImageGetColorSpace(imageRef), kCGImageAlphaPremultipliedLast);
// Make CG system interpret OpenGL style texture coordinates properly by inverting Y axis
CGContextTranslateCTM(imageContextRef, 0, imageHeight);
CGContextScaleCTM(imageContextRef, 1.0, -1.0);
CGContextDrawImage(imageContextRef, CGRectMake(0.0, 0.0, (CGFloat)imageWidth, (CGFloat)imageHeight), imageRef);
CGContextRelease(imageContextRef);
glGenTextures(1, newTextureName);
glBindTexture(GL_TEXTURE_2D, *newTextureName);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
free(imageData);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
}
[image release];
}
- (void)awakeFromNib
{
EAGLContext *aContext = [[EAGLContext alloc] initWithAPI:2];
if (!aContext)
NSLog(@"Failed to create ES context");
else if (![EAGLContext setCurrentContext:aContext])
NSLog(@"Failed to set ES context current");
self.context = aContext;
[aContext release];
[(EAGLView *)self.view setContext:context];
[(EAGLView *)self.view setFramebuffer];
[self loadShaders];
[self loadTexture:&textureName fromFile:@"image.png"];
[self loadTexture:&textureName1 fromFile:@"image1.png"];
[self loadTexture:&textureName2 fromFile:@"image2.png"];
GLint framebufferWidth, framebufferHeight;
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_WIDTH, &framebufferWidth);
glGetRenderbufferParameteriv(GL_RENDERBUFFER, GL_RENDERBUFFER_HEIGHT,&framebufferHeight);
[self drawFrame];
}