Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/atmospheric_engine/src/lib/graphics/effects/cloud_effect.cc @ 7784

Last change on this file since 7784 was 7784, checked in by amaechler, 19 years ago

branches/atmospheric_engine: random animated and generated clouds, veerrryyyy slow hmm

File size: 6.3 KB
Line 
1/*
2        orxonox - the future of 3D-vertical-scrollers
3
4        Copyright (C) 2004 orx
5
6        This program is free software; you can redistribute it and/or modify
7        it under the terms of the GNU General Public License as published by
8        the Free Software Foundation; either version 2, or (at your option)
9        any later version.
10
11### File Specific:
12        main-programmer: hdavid, amaechler
13*/
14
15#include "cloud_effect.h"
16
17#include "util/loading/load_param.h"
18#include "util/loading/factory.h"
19
20#include "glincl.h"
21//#include "graphics_engine.h"
22
23#include "parser/tinyxml/tinyxml.h"
24
25using namespace std;
26
27CREATE_FACTORY(CloudEffect, CL_CLOUD_EFFECT);
28
29CloudEffect::CloudEffect(const TiXmlElement* root)
30{
31        this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
32
33        this->init();
34
35        if (root != NULL)
36                this->loadParams(root);
37
38        this->activate();
39}
40
41CloudEffect::~CloudEffect()
42{
43        this->deactivate();
44}
45
46void CloudEffect::loadParams(const TiXmlElement* root)
47{
48        WeatherEffect::loadParams(root);
49
50        LoadParam(root, "animSpeed", this, CloudEffect, setCloudAnimation);
51
52}
53
54
55bool CloudEffect::init()
56{
57        // default values
58        this->cloudAnimTimeStep = 0;
59
60        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
61        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
62        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
63        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
64
65        // Generate noise map a
66        CloudEffect::genNoiseMap(cloudMap32_a);
67       
68        if (this->cloudAnimTimeStep > 0) {
69                // Generate noise map b
70                CloudEffect::genNoiseMap(cloudMap32_b);
71        }
72}
73
74bool CloudEffect::activate()
75{
76        PRINTF(0)( "Activating CloudEffect\n");
77        if (this->cloudAnimTimeStep == 0) {
78                for (int i = 0; i < 32*32; i++)
79                        cloudMap32_c[i] = cloudMap32_a[i];
80
81                CloudEffect::overlapOctaves();
82                CloudEffect::expFilter();
83                CloudEffect::genCloudTexture();
84        }
85}
86
87bool CloudEffect::deactivate()
88{
89        PRINTF(0)("Deactivating CloudEffect\n");
90}
91
92void CloudEffect::genCloudTexture() {
93        for(int i=0; i<256; i++)
94                for(int j=0; j<256; j++) 
95                {
96                        float color = cloudMap256[i*256+j]; 
97                        cloudTexture[i][j][0] = (char) color;
98                        cloudTexture[i][j][1] = (char) color;
99                        cloudTexture[i][j][2] = (char) color;
100                }
101
102        glGenTextures(2, &texID[0]);
103        glBindTexture(GL_TEXTURE_2D, texID[0]);
104        gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, 256, 256, GL_RGB, GL_UNSIGNED_BYTE, cloudTexture);
105}
106
107void CloudEffect::calcAnimMap(float timer) {
108
109        for (int x=0; x<32*32; x++)
110                cloudMap32_c[x] = (cloudMap32_a[x] * ((this->cloudAnimTimeStep - timer) / this->cloudAnimTimeStep)) + (cloudMap32_b[x] * (timer / this->cloudAnimTimeStep));
111
112}
113
114void CloudEffect::draw() const
115{
116        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
117
118        glPushMatrix();
119        glEnable(GL_TEXTURE_2D);
120
121        glBindTexture(GL_TEXTURE_2D, texID[0]);
122
123        // FIXME : Bind this to the sky - how do I do this?
124        glBegin(GL_QUADS);
125                glTexCoord2f(0.0f, 0.0f); glVertex3f(20, 20,  60);      // Bottom Left Of The Texture and Quad
126                glTexCoord2f(1.0f, 0.0f); glVertex3f(60, 20,  60);      // Bottom Right Of The Texture and Quad
127                glTexCoord2f(1.0f, 1.0f); glVertex3f(60, 60,  60);      // Top Right Of The Texture and Quad
128                glTexCoord2f(0.0f, 1.0f); glVertex3f(20, 60,  60);      // Top Left Of The Texture and Quad
129        glEnd(); 
130
131        glPopMatrix();
132}
133
134void CloudEffect::tick (float dt)
135{
136        if (this->cloudAnimTimeStep > 0) {
137                if (timer >= this->cloudAnimTimeStep) {
138                        timer -= this->cloudAnimTimeStep;
139                        for (int i = 0; i < 32*32; i++)
140                                cloudMap32_a[i] = cloudMap32_b[i];
141                        CloudEffect::genNoiseMap(cloudMap32_b);
142                }
143
144                //map32anim = (map32a * (10 - timer)) + (map32b * timer);
145                CloudEffect::calcAnimMap(timer);
146
147                CloudEffect::overlapOctaves();
148                CloudEffect::expFilter();
149                CloudEffect::genCloudTexture();
150
151                timer += dt;
152        }
153}
154
155/*
156        Random noise generator
157*/
158float CloudEffect::noise(int x, int y, int random)
159{
160                int n = x + y * 57 + random * 131;
161                n = (n<<13) ^ n;
162                return (1.0f - ( (n * (n * n * 15731 + 789221) +
163                                                1376312589)&0x7fffffff)* 0.000000000931322574615478515625f);
164}
165
166/*
167        Set noise for the 32*32 noise map:
168*/
169void CloudEffect::genNoiseMap(float  *map)
170{
171        float temp[34][34];
172
173        int random = rand() % 5000;
174
175        for (int y = 1; y < 33; y++)
176                for (int x = 1; x < 33; x++)
177                        temp[x][y] = 128.0f + CloudEffect::noise(x,  y,  random) * 128.0f;
178
179        // Seamless cloud
180        for (int x=1; x<33; x++)
181        {
182                temp[0][x] = temp[32][x];
183                temp[33][x] = temp[1][x];
184                temp[x][0] = temp[x][32];
185                temp[x][33] = temp[x][1];
186        }
187        temp[0][0] = temp[32][32];
188        temp[33][33] = temp[1][1];
189        temp[0][33] = temp[32][1];
190        temp[33][0] = temp[1][32];
191
192        // We mirror the side and corner elements so our final cloud will be seamless without any ugly borders showing.
193        for (int y=1; y<33; y++)
194                for (int x=1; x<33; x++)
195                {
196                        float center = temp[x][y]/4.0f;
197                        float sides = (temp[x+1][y] + temp[x-1][y] + temp[x][y+1] + temp[x][y-1])/8.0f;
198                        float corners = (temp[x+1][y+1] + temp[x+1][y-1] + temp[x-1][y+1] + temp[x-1][y-1])/16.0f;
199
200                        map[((x-1)*32) + (y-1)] = center + sides + corners;
201                }
202}
203
204/*
205        Interpolation - average the value of each pixel value with that of its neighbors' values.
206*/
207float CloudEffect::interpolate(float x, float y, float  *map)
208{
209        int Xint = (int)x;
210        int Yint = (int)y;
211
212        float Xfrac = x - Xint;
213        float Yfrac = y - Yint;
214
215        int X0 = Xint % 32;
216        int Y0 = Yint % 32;
217        int X1 = (Xint + 1) % 32;
218        int Y1 = (Yint + 1) % 32;
219
220        float bot = map[X0*32 + Y0] + Xfrac * (map[X1*32 + Y0] - map[X0*32 + Y0]);
221        float top = map[X0*32 + Y1] + Xfrac * (map[X1*32 +  Y1] - map[X0*32 + Y1]);
222
223        return (bot + Yfrac * (top - bot));
224}
225
226
227/*
228        Octaves are overlapped together to give cloud more turbulence. We will use four octaves for our cloud.
229*/
230void CloudEffect::overlapOctaves()
231{
232        for (int x=0; x<256*256; x++)
233        {
234                cloudMap256[x] = 0;
235        }
236
237        for (int octave=0; octave<4; octave++)
238                for (int x=0; x<256; x++)
239                        for (int y=0; y<256; y++)
240                        {
241                                float scale = 1 / pow(2, 3-octave);
242                                float noise = CloudEffect::interpolate(x*scale, y*scale , cloudMap32_c);
243
244                                //The octaves are added together with the proper weight factors.
245                                //You could replace pow(2, i) with 1<<i for faster computation
246                                cloudMap256[(y*256) + x] += noise / pow(2, octave);
247                        }
248}
249
250
251/*
252        Filter the noise with exponential function
253*/
254void CloudEffect::expFilter()
255{
256        float cover = 20.0f;
257        float sharpness = 0.95f;
258
259        for (int x=0; x<256*256; x++)
260        {
261                float c = cloudMap256[x] - (255.0f-cover);
262                if (c<0)     c = 0;
263                cloudMap256[x] = 255.0f - ((float)(pow(sharpness, c))*255.0f);
264        }
265}
266
267
Note: See TracBrowser for help on using the repository browser.