public static Texture Checkerboard(int w, int h, int cellbits, uint oddColor, uint evenColor) { Texture t = new Texture(w,h); int pos = 0; for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) t.Pixel[pos++] = (((((x>>cellbits)+(y>>cellbits))&1)==0) ? evenColor : oddColor ); return t; }
public static Texture Perlin(int w, int h, float persistency, float density, int samples, int scale) { TextureFactory.initNoiseBuffer(); Texture t = new Texture(w,h); int pos = 0; float wavelength = (float) ((w > h) ? w : h )/density; for(int y = 0; y < h; y++) for(int x = 0; x < w; x++) t.Pixel[pos++] = (uint)((float) scale*TextureFactory.perlin2d(x,y,wavelength,persistency,samples)); return t; }
public static Texture BlendTopDown(Texture top, Texture down) { down.Resize(top.Width,top.Height); Texture t = new Texture(top.Width,top.Height); int pos = 0; int alpha; for(int y = 0; y < top.Height; y++) { alpha = 255*y/(top.Height-1); for(int x = 0; x < top.Width; x++) { t.Pixel[pos] = Colors.Transparency(down.Pixel[pos],top.Pixel[pos],alpha); pos++; } } return t; }
public Texture Add(Texture additive) { for(int i = this.Width*this.Height-1; i >= 0; i--) this.Pixel[i] = Colors.Add(this.Pixel[i],additive.Pixel[i]); return this; }
public Texture Sub(Texture subtractive) { for(int i = this.Width*this.Height-1; i >= 0; i--) this.Pixel[i] = Colors.Sub(this.Pixel[i],subtractive.Pixel[i]); return this; }
public Texture Put(Texture newData) { Array.Copy(newData.Pixel,0,this.Pixel,0,this.Width*this.Height); return this; }
public Texture Multiply(Texture multiplicative) { for(int i = this.Width*this.Height-1; i >= 0; i--) this.Pixel[i] = Colors.Multiply(this.Pixel[i],multiplicative.Pixel[i]); return this; }
public Texture Mix(Texture newData) { for(int i = this.Width*this.Height-1; i >= 0; i--) this.Pixel[i] = Colors.Mix(this.Pixel[i],newData.Pixel[i]); return this; }
public Texture GetClone() { Texture t = new Texture(this.Width,this.Height); for(int i = 0; i < this.Pixel.Length; i++) t.Pixel[i] = this.Pixel[i]; return t; }
public static int GenerateHeightMap(Texture texture, float dxz, float dy, int granularity, out int idBuffer, out int idN) { int w = texture.Width; int h = texture.Height; int lnx = (w/granularity); int lnz = (h/granularity); idN = (lnx*(lnz-0x01))<<0x01; int[] idData = new int[idN]; dxz *= granularity; int k = 0x00; for(int i = (lnz-0x01)*lnx-0x01, j = i+lnx; i >= 0x00;) { idData[k++] = j--; idData[k++] = i--; } GL.GenBuffers(0x01, out idBuffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, idBuffer); GL.BufferData(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(int)*idData.Length), idData, BufferUsageHint.StaticDraw); float[] xntData = new float[(lnx*lnz)<<0x03]; float vy, ty, dyx, dyz, ntx = 1.0f/(lnx-0x01), nty = 1.0f/(lnz-0x01), nn, dxzsq = 4.0f*dxz*dxz; uint[] pixel = texture.Pixel; float factor = dy/255.0f; k = 0x00; int l = 0x00; float x0 = -0.5f*dxz*(lnx-0x01), z0 = -0.5f*dxz*(lnz-0x01); for(int i = 0x00; i < lnz; i++) { vy = dxz*i+z0; ty = nty*i; l = w*i*granularity; for(int j = 0x00; j < lnx; j++) { xntData[k++] = dxz*j+x0; xntData[k++] = (pixel[l]&0xff)*factor; xntData[k++] = vy; if(i > 0x00 && j > 0x00 && i < (lnz-0x01) && j < (lnx-0x01)) { //fill dyx = (pixel[l-0x01]&0xff)-(pixel[l+0x01]&0xff)*factor; dyz = (pixel[l-w]&0xff)-(pixel[l+w]&0xff)*factor; nn = (float)(1.0d/Math.Sqrt(dxzsq+dyx*dyx+dyz*dyz)); xntData[k++] = dyx*nn; xntData[k++] = 2.0f*dxz*nn; xntData[k++] = dyz*nn; } else { //border xntData[k++] = 0.0f; xntData[k++] = 1.0f; xntData[k++] = 0.0f; } xntData[k++] = ntx*j; xntData[k++] = ty; l += granularity; } } int xntBuff; GL.GenBuffers(0x01, out xntBuff); GL.BindBuffer(BufferTarget.ArrayBuffer, xntBuff); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float)*xntData.Length), xntData, BufferUsageHint.StaticDraw); return xntBuff; }