我在GLSL片段着色器(OpenGL ES 2.0)中传递具有NxM大小的纹理作为采样器.从相邻纹理元素读取纹素数据的正确方法是什么?我在片段着色器中没有"变化的"纹理坐标.我只能使用片段坐标来读取纹理信息.
以下是我的着色器,我不确定它是否实际读取数据:
precision mediump float;
uniform sampler2D Sampler;
#define OFFSET 1.0
void main()
{
vec2 T = gl_FragCoord.xy;
//Find neighboring velocities:
vec2 N = texture2D(Sampler,vec2(T.x,T.y+OFFSET)).xy;
vec2 S = texture2D(Sampler,vec2(T.x,T.y-OFFSET)).xy;
vec2 E = texture2D(Sampler,vec2(T.x+OFFSET,T.y)).xy;
vec2 W = texture2D(Sampler,vec2(T.x-OFFSET,T.y)).xy;
}
Run Code Online (Sandbox Code Playgroud)
对于NxM尺寸纹理,OFFSET值应该是1.0还是其他值?
我试图在iOS平台上将UIImage读入纹理.我发现StackOverflow上的代码片段可以解决这个问题,但问题是当我显示纹理时它显示为镜像倒置.
int numComponents = 4;
UIImage* image = [UIImage imageNamed:@"Test.png"];
CGImageRef imageRef = [image CGImage];
int width = CGImageGetWidth(imageRef);
int height = CGImageGetHeight(imageRef);
//Allocate texture data
GLubyte* textureData = (GLubyte *)malloc(width * height * numComponents);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(textureData, width, height,
bitsPerComponent, bytesPerRow, colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
CGContextRelease(context);
//texture setup
GLuint textureID;
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, …Run Code Online (Sandbox Code Playgroud) 以下行在iOS 6模拟器上运行良好,但在iOS 6设备上不起作用.可能有什么不对?如何解决这个问题?非常感谢.
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
Run Code Online (Sandbox Code Playgroud)