0

我想在我的片段着色器中获得不同的纹理。但不知何故,它不会工作。它只得到其中之一。

我开始渲染:

glBindVertexArrayOES(_heighMap.vertexArray);

//Get Uniforms
GLuint mapProj = glGetUniformLocation(_mapProgram, "modelViewProjectionMatrix");
GLuint mapView = glGetUniformLocation(_mapProgram, "modelViewMatrix");
GLuint mapNormal = glGetUniformLocation(_mapProgram, "normalMatrix");
GLuint map2D0 =glGetUniformLocation(_mapProgram, "uSampler0");
GLuint map2D1 =glGetUniformLocation(_mapProgram, "uSampler1");

// bind a texture
glEnable(GL_TEXTURE_2D);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, _heighMap.textureID0);
glUniform1i(map2D0, 0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, _heighMap.textureID1);  
glUniform1i(map2D1, 1);

//Lade Object
glUseProgram(_mapProgram);
glUniformMatrix4fv(mapProj, 1, 0, _heighMap.projectionMatix.m);
glUniformMatrix3fv(mapNormal, 1, 0, _heighMap.normalMatrix.m);

glDrawElements(GL_TRIANGLE_STRIP, _heighMap.sizeVertexIndeces,  GL_UNSIGNED_INT, _heighMap.vertexIndeces);

glActiveTexture(GL_TEXTURE0);

我的片段着色器没什么特别的:

uniform sampler2D uSampler0;
uniform sampler2D uSampler1;

varying lowp vec2 vTexCoord;

void main()
{
    lowp vec4 texCol = texture2D(uSampler1, vTexCoord);

    gl_FragColor = vec4(texCol.rgba);
}

如果我得到纹理,只是在那里测试。

在这里我得到了我的纹理:

-(void)loadMapImages:(NSString *)p : (NSString *)type TexID:(uint *)textureID count:(uint)c{

    NSString *path = [[NSBundle mainBundle] pathForResource:p ofType:type];
    NSData *texData = [[NSData alloc] initWithContentsOfFile:path];
    UIImage *image = [[UIImage alloc] initWithData:texData];

    size_t width = CGImageGetWidth(image.CGImage);
    size_t height = CGImageGetHeight(image.CGImage);

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

    GLuint tex;
    switch (c) {
        case 0:
            tex = GL_TEXTURE0;
            break;
        case 1:
            tex = GL_TEXTURE1;
            break;
        default:
            break;
    }
    glActiveTexture(tex);
    glBindTexture(GL_TEXTURE_2D, *textureID);

    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

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

    glBindTexture(GL_TEXTURE_2D, tex);

    CGContextRelease(context0);

    free(imageData);

}
4

1 回答 1

1

在调用 glUseProgram 之前,您正在传递采样器的制服

尝试将所有呼叫转移到:

glGetUniformLocation(*);

也:

glUniform1i(map2D0, 0);
glUniform1i(map2D1, 1);

在你调用 glUseProgram(_mapProgram); 之后

**我假设您只获得绑定到位置 0 的纹理,因为您的采样器都获得了默认的 0 值

于 2015-06-18T05:15:22.980 回答