我想在我的片段着色器中获得不同的纹理。但不知何故,它不会工作。它只得到其中之一。
我开始渲染:
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);
}