如何在iPhone上生成Perlin噪声

4
我想在iPhone上创建一个动画柏林噪声,最终可以做到像这样:http://dl.dropbox.com/u/1977230/example.png 我已经搜索了很多,但是找不到类似的东西或者一种实际显示柏林噪声的方法。
有人告诉我要看OpenGL ES,但是即使搜索柏林噪声或岩浆/等离子效果的示例也没有结果。
我真的很需要帮助。
谢谢大家, 安德烈
3个回答

10

首先,学习Perlin噪声算法本身。http://en.wikipedia.org/wiki/Perlin_noise看起来是起飞的最佳地点。

一旦你获得了这种效果的RGBA数据,麻烦的事情就开始了。

基本上有两个选择。

  • Create a UIView subclass and override the draw:(CGRect) method. Use Converting RGB data into a bitmap in Objective-C++ Cocoa wisely to create a CGImage from your data and and draw that image to the current context in draw.

    CGContextDrawImage(UIGraphicsGetCurrentContext(), <#CGRect rect#>, <#CGImageRef image#>);
    

    If this is a still image, you are ok. if it's animating, this might not be the best solution.

  • Get familiar with OpenGL ES on the iPhone. The iPhone SDK's OpenGL ES example is an excellent starting point. Study texture mapping. Once you are familiar with glTexImage2D, use that to load your image.

    The example can be easily extended with the following:

    have these defines:

      GLuint spriteTexture;
      GLubyte *spriteData;  // the perlin noise will be here
      size_t    width, height;
    

    then in the ESRenderer init method create space for the texture:

    - (id) init { ....
    width = 512;  // make sure the texture size is the power of 2
    height = 512;
    
    glGenTextures(1, &spriteTexture);       
    glBindTexture(GL_TEXTURE_2D, spriteTexture);        
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);       
    //free(spriteData); // free this if not used any more
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);   
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);         
    

    In case the noise is periodically updated, update the texture in the render method

            - (void) render { .....
    glBindTexture(GL_TEXTURE_2D, spriteTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);   
    
啊,我怀念那些美好的 $A000 年代的视频 :)

我明白了,谢谢你。 我仍然无法相信没有针对Objective-C / OpenGL ES的Perlin噪声版本... - Andre
1
C函数和C++类可以在Objective-C中使用。 - f3r3nc

6

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接