有人告诉我要看OpenGL ES,但是即使搜索柏林噪声或岩浆/等离子效果的示例也没有结果。
我真的很需要帮助。
谢谢大家, 安德烈
首先,学习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);
我在Github上开始了一个开源项目,可以用于生成Perlin噪声。它支持完整的4维(x、y、z、t)Perlin生成。它还包括一个带有沙盒应用程序的项目,可用于玩耍纹理想法。http://czgarrett.com/code/2011/05/18/perlin-noise-generator-for-ios.html