0

3 つのテクスチャを使用していますが、2 つのテクスチャしか機能していません。

enum {
UNIFORM_TEXTURE,
UNIFORM_TEXTURE_MAP,
UNIFORM_INPUT1,
NUM_UNIFORMS};
GLint uniforms[NUM_UNIFORMS];

3 番目のものは表示されません。テクスチャを有効にする方法は次のとおりです。

  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");

私のシェーダーでは、3 番目のテクスチャのみを呼び出してチェックしています。

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];
}
4

2 に答える 2

0

さて、私はそのような2つの別のglTexParameteriを追加する3番目のテクスチャの問題を見つけました:

        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);

        glActiveTexture(GL_TEXTURE2);
        glBindTexture(GL_TEXTURE_2D, textureName2);
        glEnable(GL_TEXTURE_2D);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

そして、シェーダーは 3 番目のテクスチャを受け取ります。

于 2013-10-09T09:19:05.807 に答える