在着色器中传递纹理
问题是我在着色器中传递了两个纹理,但 3D 模型上的图片对于两个 Sampler2D 变量是相同的(它与图像“normal.jpg”,第一个)。我将非常感谢您的帮助。这是代码:
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];
}
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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这两行:
似乎将两个
sampler2D
都指向第一个纹理单元。我想你想要:要设置一个
sampler2D
,你将它传递给它应该读取的纹理单元。These two lines:
Appear to point both of your
sampler2D
s to the first texture unit. I'd imagine you want:To set a
sampler2D
you pass it the texture unit that it should read from.