English 中文(简体)
How to generate Perlin Noise on an iPhone
原标题:

I want to create an animated perlin noise on the iPhone, so I can ultimately do something like this: http://dl.dropbox.com/u/1977230/example.png

I ve looked and looked, but can t find anything similar or a way to actually display a Perlin Noise.

I ve been told to look at OpenGL ES, but even searching for an example of Perlin noise or a lava/plasma effect doesn t result in anything.

I d really appreciate some help on this one.

Thanks guys, Andre

最佳回答

Well, first study the Perlin Noise algorithm itself. http://en.wikipedia.org/wiki/Perlin_noise looks just the best place to take off.

Once you have the RGBA data of this effect of yours, the nasty thing begins.

There are two options basically.

  • 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);   
    

Ah, I miss the good old video is on $A000 days :)

问题回答

I started an open source project on Github that you can use for generating Perlin noise. It supports full 4-dimensional (x,y,z,t) Perlin generation. It also includes a project with a sandbox app to play with texture ideas. http://czgarrett.com/code/2011/05/18/perlin-noise-generator-for-ios.html

Now there is another Perlin noise function for GLSL shader that doesn t require lookup textures: https://github.com/ashima/webgl-noise/tree/master/src. This should work on iPhones.





相关问题
How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

Iphone NSTimer Issue

Hi I am new to objective c. I am trying to make an app for iphone. I have a button on my view, and the click on which the function playSound is called. This is working properly. It does plays the ...

Include a .txt file in a .h in C++?

I have a number of places where I need to re-use some template code. Many classes need these items In a .h could I do something like: #include <xxx.txt> and place all of this code in the ....

Iterating over string/strlen with umlauted characters

This is a follow-up to my previous question . I succeeded in implementing the algorithm for checking umlauted characters. The next problem comes from iterating over all characters in a string. I do ...

Xcode open two editor windows with same file

Is it possible to open the same file in two separate windows in Xcode. I can open a file in one window and the same file in the main Xcode editor window, but I wanted two separate fulltime editor ...

Forcing code signing refresh in Xcode

In our environment, we share resources across multiple projects and platforms. When building for iPhone, only a subset of those resources are needed. Since that subset is still considerable, we have ...

热门标签