DomiDiDongo
DomiDiDongo

Reputation: 133

Passing 2 Textures into fragmenshader

Im want to get to different textures in my fragment shader. But somehow it won't work. It is only getting one of them.

I'm starting my rendering with :

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

my fragment shader nothing special:

uniform sampler2D uSampler0;
uniform sampler2D uSampler1;

varying lowp vec2 vTexCoord;

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

    gl_FragColor = vec4(texCol.rgba);
}

just testing there if I got the textures.

and here I'm getting my Textures:

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

}

Upvotes: 0

Views: 34

Answers (1)

Gal Kamar
Gal Kamar

Reputation: 173

You are passing the uniforms of your samplers before you call glUseProgram

Try moving all calls to:

glGetUniformLocation(*);

As well as:

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

To be after you call glUseProgram(_mapProgram);

**I am assuming you are getting only the texture bound to location 0 because your samplers are both getting the default 0 values

Upvotes: 1

Related Questions