开发者

Pass textures in shader

开发者 https://www.devze.com 2023-04-10 16:48 出处:网络
The problem is that I pass two textures in shader, but the picture on 3-d model is the same for both sampler2D variables开发者_运维技巧 (It is with image \"normal.jpg\", the first one.). I\'ll be very

The problem is that I pass two textures in shader, but the picture on 3-d model is the same for both sampler2D variables开发者_运维技巧 (It is with image "normal.jpg", the first one.). I'll be very grateful for help. Here is the code:

GLuint _textures[2];

glEnable(GL_TEXTURE_2D);

glGenTextures(2, _textures);
glActiveTexture(GL_TEXTURE0);
bump = [self loadTextureWithName: @"normal.jpg"];
//bump = [self loadTextureWithName: @"specular.jpg"];
glUniform1i(bump, 0);

glActiveTexture(GL_TEXTURE1);
diffuse = [self loadTextureWithName2: @"diffuse.jpg"];
glUniform1i(diffuse, 0);


- (GLuint) loadTextureWithName: (NSString*) filename{

CGImageRef textureImage = [UIImage imageNamed: filename].CGImage;
NSInteger texWidth = CGImageGetWidth(textureImage);
NSInteger texHeight = CGImageGetHeight(textureImage);
GLubyte *textureData =  (GLubyte *)malloc(texWidth * texHeight * 4*sizeof(GLubyte));
memset((void*) textureData, 0, texWidth * texHeight * 4*sizeof(GLubyte));
CGContextRef textureContext = CGBitmapContextCreate(textureData,texWidth,texHeight,8, texWidth * 4,CGImageGetColorSpace(textureImage),kCGImageAlphaPremultipliedLast);

CGContextDrawImage(textureContext,CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight),textureImage);

CGContextRelease(textureContext);
//glActiveTexture(GL_TEXTURE0);
//glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

//glGenTextures(1, &TEX);
glBindTexture(GL_TEXTURE_2D, _textures[0]);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
free(textureData);
//texId = [NSNumber numberWithInt:TEX];
//[textureImage release];//
//[textureContext release];//
return _textures[0];
}

- (GLuint) loadTextureWithName2: (NSString*) filename{

CGImageRef textureImage = [UIImage imageNamed: filename].CGImage;
NSInteger texWidth = CGImageGetWidth(textureImage);
NSInteger texHeight = CGImageGetHeight(textureImage);
GLubyte *textureData =  (GLubyte *)malloc(texWidth * texHeight * 4*sizeof(GLubyte));
memset((void*) textureData, 0, texWidth * texHeight * 4*sizeof(GLubyte));
CGContextRef textureContext = CGBitmapContextCreate(textureData,texWidth,texHeight,8, texWidth * 4,         CGImageGetColorSpace(textureImage),kCGImageAlphaPremultipliedLast);

CGContextDrawImage(textureContext,CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight),textureImage);

CGContextRelease(textureContext);
//glActiveTexture(GL_TEXTURE1);
//glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

//glGenTextures(1, &TEX);
glBindTexture(GL_TEXTURE_2D, _textures[1]);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
free(textureData);

return _textures[1];
}


These two lines:

glUniform1i(bump, 0);
glUniform1i(diffuse, 0);

Appear to point both of your sampler2Ds to the first texture unit. I'd imagine you want:

glUniform1i(diffuse, 1);

To set a sampler2D you pass it the texture unit that it should read from.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号