ios - iPad 纹理加载差异(32 位与 64 位)

标签 ios objective-c opengl-es core-graphics

我正在开发一个绘图应用程序,我注意到在 32 位 iPad 和 64 位 iPad 上加载的纹理存在显着差异。

这是在 32 位 iPad 上绘制的纹理:

enter image description here

这是在 64 位 iPad 上绘制的纹理:

enter image description here

64 位是我想要的,但似乎丢失了一些数据?

我用这段代码创建了一个默认画笔纹理:

UIGraphicsBeginImageContext(CGSizeMake(64, 64));
CGContextRef defBrushTextureContext = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(defBrushTextureContext);

size_t num_locations = 3;
CGFloat locations[3] = { 0.0, 0.8, 1.0 };
CGFloat components[12] = { 1.0,1.0,1.0, 1.0,
    1.0,1.0,1.0, 1.0,
    1.0,1.0,1.0, 0.0 };
CGColorSpaceRef myColorspace = CGColorSpaceCreateDeviceRGB();
CGGradientRef myGradient = CGGradientCreateWithColorComponents (myColorspace, components, locations, num_locations);

CGPoint myCentrePoint = CGPointMake(32, 32);
float myRadius = 20;

CGGradientDrawingOptions options = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;
CGContextDrawRadialGradient (UIGraphicsGetCurrentContext(), myGradient, myCentrePoint,
                             0, myCentrePoint, myRadius,
                             options);

CFRelease(myGradient);
CFRelease(myColorspace);
UIGraphicsPopContext();

[self setBrushTexture:UIGraphicsGetImageFromCurrentImageContext()];

UIGraphicsEndImageContext();

然后像这样实际设置画笔纹理:

-(void) setBrushTexture:(UIImage*)brushImage{
// save our current texture.
currentTexture = brushImage;

// first, delete the old texture if needed
if (brushTexture){
    glDeleteTextures(1, &brushTexture);
    brushTexture = 0;
}

// fetch the cgimage for us to draw into a texture
CGImageRef brushCGImage = brushImage.CGImage;

// Make sure the image exists
if(brushCGImage) {
    // Get the width and height of the image
    GLint width = CGImageGetWidth(brushCGImage);
    GLint height = CGImageGetHeight(brushCGImage);

    // Texture dimensions must be a power of 2. If you write an application that allows users to supply an image,
    // you'll want to add code that checks the dimensions and takes appropriate action if they are not a power of 2.

    // Allocate  memory needed for the bitmap context
    GLubyte* brushData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));
    // Use  the bitmatp creation function provided by the Core Graphics framework.
    CGContextRef brushContext = CGBitmapContextCreate(brushData, width, height, 8, width * 4, CGImageGetColorSpace(brushCGImage), kCGImageAlphaPremultipliedLast);
    // After you create the context, you can draw the  image to the context.
    CGContextDrawImage(brushContext, CGRectMake(0.0, 0.0, (CGFloat)width, (CGFloat)height), brushCGImage);
    // You don't need the context at this point, so you need to release it to avoid memory leaks.
    CGContextRelease(brushContext);

    // Use OpenGL ES to generate a name for the texture.
    glGenTextures(1, &brushTexture);
    // Bind the texture name.
    glBindTexture(GL_TEXTURE_2D, brushTexture);
    // Set the texture parameters to use a minifying filter and a linear filer (weighted average)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // Specify a 2D texture image, providing the a pointer to the image data in memory
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, brushData);
    // Release  the image data; it's no longer needed
    free(brushData);
}
}

更新:

我已经将 CGFloats 更新为 GLfloats 但没有成功。也许这个渲染代码有问题?

if(frameBuffer){
    // draw the stroke element
    [self prepOpenGLStateForFBO:frameBuffer];
    [self prepOpenGLBlendModeForColor:element.color];
    CheckGLError();
}

// find our screen scale so that we can convert from
// points to pixels
GLfloat scale = self.contentScaleFactor;

// fetch the vertex data from the element
struct Vertex* vertexBuffer = [element generatedVertexArrayWithPreviousElement:previousElement forScale:scale];

glLineWidth(2);

// if the element has any data, then draw it
if(vertexBuffer){
    glVertexPointer(2, GL_FLOAT, sizeof(struct Vertex), &vertexBuffer[0].Position[0]);
    glColorPointer(4, GL_FLOAT, sizeof(struct Vertex), &vertexBuffer[0].Color[0]);
    glTexCoordPointer(2, GL_FLOAT, sizeof(struct Vertex), &vertexBuffer[0].Texture[0]);
    glDrawArrays(GL_TRIANGLES, 0, (GLint)[element numberOfSteps] * (GLint)[element numberOfVerticesPerStep]);
    CheckGLError();
}

if(frameBuffer){
    [self unprepOpenGLState];
}

顶点结构如下:

struct Vertex{
    GLfloat Position[2];    // x,y position
    GLfloat Color [4];      // rgba color
    GLfloat Texture[2];    // x,y texture coord
};

更新:

该问题实际上似乎并非基于 32 位或 64 位,而是 A7 GPU 和 GL 驱动程序有所不同。我通过在 64 位 iPad 上运行 32 位构建和 64 位构建发现了这一点。纹理最终在应用程序的两个版本中看起来完全相同。

最佳答案

我希望你检查两件事。

  1. 检查 OpenGL 中的 alpha 混合逻辑(或选项)。

  2. 检查与拖动速度成正比的插值逻辑。

看来你没有第二个或者没有效果,这是绘图应用程序所必需的

关于ios - iPad 纹理加载差异(32 位与 64 位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21689336/

相关文章:

ios - 如何在其 subview 之一正在动画时释放 UIView?

objective-c - 在 ios6 中,将 pageViewController 的 gestureRecognizers 委托(delegate)设置为 viewController 会导致崩溃

ios - AVFoundation:视频到 OpenGL 纹理工作 - 如何播放和同步音频?

c++ - 如何尽早离开 OBJ-C 的世界并在 iOS OpenGL ES 应用程序中进入现有的 C++ 代码库?

ios - 如何将 subview 的 subview 置于顶部?

ios - 我无法在 iOS 中从另一个文件执行选择器

objective-c - 如何用 KissXML 编写 CDATA?

ios - 如何在 OpenGL ES 2.0 中使用可分离滤镜着色器?

iOS - 将 NSDictionary 复制到 SQLite3

ios - 在 UITextView 中将 NSMutableArray 对象显示为文本