Permalink
Browse files

procedural texture generation added

  • Loading branch information...
1 parent 30851e3 commit 1ed46bf9c8e3f376530e3d3bfd79c0d9edbc8978 @haqu committed Jun 9, 2011
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 4DA7120913A0E9AD00DD891F /* noise.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DA7120813A0E9AD00DD891F /* noise.png */; };
4DFD2EAF139CCCA0001E1480 /* QuartzCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EAE139CCCA0001E1480 /* QuartzCore.framework */; };
4DFD2EB1139CCCA0001E1480 /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EB0139CCCA0001E1480 /* OpenGLES.framework */; };
4DFD2EB3139CCCA0001E1480 /* OpenAL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4DFD2EB2139CCCA0001E1480 /* OpenAL.framework */; };
@@ -122,13 +123,13 @@
4DFD3012139CCCA6001E1480 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFD3011139CCCA6001E1480 /* AppDelegate.m */; };
4DFD3015139CCCA6001E1480 /* RootViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFD3014139CCCA6001E1480 /* RootViewController.m */; };
4DFD3018139CCCA6001E1480 /* HelloWorldLayer.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFD3017139CCCA6001E1480 /* HelloWorldLayer.m */; };
- 4DFE49BF139F33CE00973A5D /* stripes.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DFE49BE139F33CE00973A5D /* stripes.png */; };
4DFE49C2139F4FBF00973A5D /* background.png in Resources */ = {isa = PBXBuildFile; fileRef = 4DFE49C1139F4FBF00973A5D /* background.png */; };
4DFE49C5139F507200973A5D /* Terrain.m in Sources */ = {isa = PBXBuildFile; fileRef = 4DFE49C4139F507200973A5D /* Terrain.m */; };
4DFE49C7139F6A7900973A5D /* README.md in Resources */ = {isa = PBXBuildFile; fileRef = 4DFE49C6139F6A7900973A5D /* README.md */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
+ 4DA7120813A0E9AD00DD891F /* noise.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = noise.png; sourceTree = "<group>"; };
4DFD2EAA139CCCA0001E1480 /* Game.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Game.app; sourceTree = BUILT_PRODUCTS_DIR; };
4DFD2EAE139CCCA0001E1480 /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
4DFD2EB0139CCCA0001E1480 /* OpenGLES.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGLES.framework; path = System/Library/Frameworks/OpenGLES.framework; sourceTree = SDKROOT; };
@@ -364,7 +365,6 @@
4DFD3016139CCCA6001E1480 /* HelloWorldLayer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HelloWorldLayer.h; sourceTree = "<group>"; };
4DFD3017139CCCA6001E1480 /* HelloWorldLayer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HelloWorldLayer.m; sourceTree = "<group>"; };
4DFD3019139CCCA6001E1480 /* GameConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GameConfig.h; sourceTree = "<group>"; };
- 4DFE49BE139F33CE00973A5D /* stripes.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = stripes.png; sourceTree = "<group>"; };
4DFE49C1139F4FBF00973A5D /* background.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = background.png; sourceTree = "<group>"; };
4DFE49C3139F507200973A5D /* Terrain.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Terrain.h; sourceTree = "<group>"; };
4DFE49C4139F507200973A5D /* Terrain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Terrain.m; sourceTree = "<group>"; };
@@ -445,8 +445,8 @@
4DFD2EBF139CCCA0001E1480 /* Resources */ = {
isa = PBXGroup;
children = (
+ 4DA7120813A0E9AD00DD891F /* noise.png */,
4DFE49C1139F4FBF00973A5D /* background.png */,
- 4DFE49BE139F33CE00973A5D /* stripes.png */,
4DFD2EC0139CCCA0001E1480 /* Default.png */,
4DFD2EC2139CCCA0001E1480 /* fps_images.png */,
4DFD2EC4139CCCA0001E1480 /* Icon-72.png */,
@@ -828,9 +828,9 @@
4DFD2ECD139CCCA0001E1480 /* Icon.png in Resources */,
4DFD2ECF139CCCA0001E1480 /* Icon@2x.png in Resources */,
4DFD2ED2139CCCA1001E1480 /* iTunesArtwork in Resources */,
- 4DFE49BF139F33CE00973A5D /* stripes.png in Resources */,
4DFE49C2139F4FBF00973A5D /* background.png in Resources */,
4DFE49C7139F6A7900973A5D /* README.md in Resources */,
+ 4DA7120913A0E9AD00DD891F /* noise.png in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -27,6 +27,55 @@ + (CCScene*) scene
return scene;
}
+- (CCSprite*) generateBackground {
+
+ CGSize textureSize = CGSizeMake(screenW, screenH);
+
+ ccColor3B c = (ccColor3B){140,205,221};
+
+ CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize.width height:textureSize.height];
+ [rt beginWithClear:(float)c.r/256.0f g:(float)c.g/256.0f b:(float)c.b/256.0f a:1];
+
+ // layer 1: gradient
+
+ float gradientAlpha = 0.2f;
+
+ glDisable(GL_TEXTURE_2D);
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+
+ CGPoint vertices[4];
+ ccColor4F colors[4];
+ int nVertices = 0;
+
+ vertices[nVertices] = CGPointMake(0, 0);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
+ vertices[nVertices] = CGPointMake(textureSize.width, 0);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
+ vertices[nVertices] = CGPointMake(0, textureSize.height);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
+ vertices[nVertices] = CGPointMake(textureSize.width, textureSize.height);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
+
+ glVertexPointer(2, GL_FLOAT, 0, vertices);
+ glColorPointer(4, GL_FLOAT, 0, colors);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
+
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glEnable(GL_TEXTURE_2D);
+
+ // layer 2: noise
+
+ CCSprite *s = [CCSprite spriteWithFile:@"noise.png"];
+ [s setBlendFunc:(ccBlendFunc){GL_DST_COLOR, GL_ZERO}];
+ s.position = ccp(textureSize.width/2, textureSize.height/2);
+ glColor4f(1,1,1,1);
+ [s visit];
+
+ [rt end];
+
+ return [CCSprite spriteWithTexture:rt.sprite.texture];
+}
+
// on "init" you need to initialize your instance
- (id) init
{
@@ -39,8 +88,8 @@ - (id) init
screenW = size.width;
screenH = size.height;
- self.background = [CCSprite spriteWithFile:@"background.png"];
- background_.position = ccp(screenW/2, screenH/2);
+ self.background = [self generateBackground];
+ background_.position = ccp(240,160);
[self addChild:background_];
self.terrain = [Terrain new];
View
@@ -5,6 +5,7 @@
#import "Terrain.h"
@interface Terrain()
+- (void) generateStripes;
- (void) generateHills;
- (void) updateHillVisibleVertices;
- (void) offsetChanged;
@@ -17,13 +18,10 @@ @implementation Terrain
- (id) init {
if ((self = [super init])) {
- self.stripes = [CCSprite spriteWithFile:@"stripes.png"];
- ccTexParams tp = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_CLAMP_TO_EDGE};
- [stripes_.texture setTexParameters:&tp];
-
scrolling = NO;
offsetX = 0;
-
+
+ [self generateStripes];
[self generateHills];
[self scheduleUpdate];
@@ -69,21 +67,100 @@ - (void) update:(ccTime)dt {
}
}
+#define kMaxStripes 20
+
+- (void) generateStripes {
+
+ int textureSize = 512;
+ int nStripes = 6;
+ ccColor3B c1 = (ccColor3B){86,155,30};
+ ccColor3B c2 = (ccColor3B){123,195,56};
+ float gradientAlpha = 0.5f;
+
+ CCRenderTexture *rt = [CCRenderTexture renderTextureWithWidth:textureSize height:textureSize];
+ [rt beginWithClear:(float)c1.r/256.0f g:(float)c1.g/256.0f b:(float)c1.b/256.0f a:1];
+
+ glDisable(GL_TEXTURE_2D);
+ glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+ glDisableClientState(GL_COLOR_ARRAY);
+
+ // layer 1: stripes
+
+ CGPoint vertices[kMaxStripes*6];
+ int nVertices = 0;
+ float x1 = -textureSize;
+ float x2;
+ float y1 = textureSize;
+ float y2 = 0;
+ float dx = textureSize*2 / nStripes;
+ float stripeWidth = dx/2;
+ for (int i=0; i<=nStripes; i++) {
+ x2 = x1 + textureSize;
+ vertices[nVertices++] = CGPointMake(x1, y1);
+ vertices[nVertices++] = CGPointMake(x1+stripeWidth, y1);
+ vertices[nVertices++] = CGPointMake(x2, y2);
+ vertices[nVertices++] = vertices[nVertices-2];
+ vertices[nVertices++] = vertices[nVertices-2];
+ vertices[nVertices++] = CGPointMake(x2+stripeWidth, y2);
+ x1 += dx;
+ }
+
+ glColor4ub(c2.r, c2.g, c2.b, 255);
+ glVertexPointer(2, GL_FLOAT, 0, vertices);
+ glDrawArrays(GL_TRIANGLES, 0, (GLsizei)nVertices);
+
+ // layer 2: gradient
+
+ glEnableClientState(GL_COLOR_ARRAY);
+
+ ccColor4F colors[4];
+ nVertices = 0;
+ vertices[nVertices] = CGPointMake(0, 0);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
+ vertices[nVertices] = CGPointMake(textureSize, 0);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, 0};
+ vertices[nVertices] = CGPointMake(0, textureSize);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
+ vertices[nVertices] = CGPointMake(textureSize, textureSize);
+ colors[nVertices++] = (ccColor4F){0, 0, 0, gradientAlpha};
+
+ glVertexPointer(2, GL_FLOAT, 0, vertices);
+ glColorPointer(4, GL_FLOAT, 0, colors);
+ glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)nVertices);
+
+ glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+ glEnable(GL_TEXTURE_2D);
+
+ // layer 3: noise
+
+ CCSprite *s = [CCSprite spriteWithFile:@"noise.png"];
+ [s setBlendFunc:(ccBlendFunc){GL_DST_COLOR, GL_ZERO}];
+ s.position = ccp(textureSize/2, textureSize/2);
+ glColor4f(1,1,1,1);
+ [s visit];
+
+ [rt end];
+
+ self.stripes = [CCSprite spriteWithTexture:rt.sprite.texture];
+ ccTexParams tp = {GL_NEAREST, GL_NEAREST, GL_REPEAT, GL_CLAMP_TO_EDGE};
+ [stripes_.texture setTexParameters:&tp];
+}
+
- (void) generateHills {
// random key points
- srand(1);
+ srand(5);
nHillKeyPoints = kMaxHillKeyPoints;
// nHillKeyPoints = 5;
- float x = 0, y = 160, dy, ny;
+ float x = 0, y = 120, dy, ny;
float sign = -1;
float paddingTop = 100;
float paddingBottom = 20;
for (int i=0; i<nHillKeyPoints; i++) {
hillKeyPoints[i] = CGPointMake(x, y);
- x += random()%40+160;
+ x += rand()%40+160;
while(true) {
- dy = random()%80+40;
+ dy = rand()%80+40;
ny = y + dy*sign;
if(ny < 320-paddingTop && ny > paddingBottom) break;
}
View
Deleted file not rendered
@@ -76,12 +76,12 @@ simple macro that swaps 2 variables
/** @def CCRANDOM_MINUS1_1
returns a random float between -1 and 1
*/
-#define CCRANDOM_MINUS1_1() ((random() / (float)0x3fffffff )-1.0f)
+#define CCRANDOM_MINUS1_1() ((rand() / (float)0x3fffffff )-1.0f)
/** @def CCRANDOM_0_1
returns a random float between 0 and 1
*/
-#define CCRANDOM_0_1() ((random() / (float)0x7fffffff ))
+#define CCRANDOM_0_1() ((rand() / (float)0x7fffffff ))
/** @def CC_DEGREES_TO_RADIANS
converts degrees to radians
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
Deleted file not rendered
View
@@ -2,11 +2,11 @@ This is an attempt to implement key features of the [Tiny Wings][1] game.
[1]: http://itunes.apple.com/us/app/tiny-wings/id417817520?mt=8
-![Tiny Wings Hills](http://haqu.net/wip/tiny-wings-1.png)
+![Tiny Wings Hills](http://haqu.net/wip/tiny-wings-2.png)
## Video demos
-- [procedural hills][2]
+- [procedural hills][2] [2011-06-08]
[2]: http://www.youtube.com/watch?v=k1RQtB10hN0

0 comments on commit 1ed46bf

Please sign in to comment.