0

我正在使用 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];
}
4

2 回答 2

0

好吧,我发现第三个纹理的问题,我像这样添加了另外两个 glTexParameteri:

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

着色器接收第三个纹理。

于 2013-10-09T09:19:05.807 回答
0

我觉得还有其他问题,但对于初学者来说......

您希望在将值上传到这些位置之前获取统一的位置。所以你声明了你的数组

GLint uniforms[NUM_UNIFORMS]

您正在使用数组来存储您的制服位置(也许您可以将其重命名为uniformLocations?)然后您继续调用glUniform1i,使用存储在“制服”中的位置。由于内容未定义,谁知道您正在使用的值发生了什么。

上传统一值后,您可以使用 查询统一位置glGetUniformLocation,这会填充您的数组。您应该查询,然后上传。所以把代码改成这样:

uniforms[UNIFORM_TEXTURE] = glGetUniformLocation(program, "texture");
uniforms[UNIFORM_TEXTURE_MAP] = glGetUniformLocation(program,"texture2");
uniforms[UNIFORM_INPUT1]= glGetUniformLocation(program, "texture3");

glUniform1i(uniforms[UNIFORM_TEXTURE], 0);
glUniform1i(uniforms[UNIFORM_TEXTURE_MAP], 1);
glUniform1i(uniforms[UNIFORM_INPUT1],2);

最重要的是,您可能希望使用 glGetError 来调试您的代码。您可以将它放在代码的特定部分以缩小问题范围,或者创建一个宏,在其中调用 glGetError 并对任何错误进行断言,并在每次 OGL 调用之后插入该宏。

于 2013-10-09T11:06:53.843 回答