Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8636 was 8636, checked in by hdavid, 18 years ago

branches/atmospheric_engine: creating clouds with the shader works, animation of the clouds with the shader still not working

File size: 7.4 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  INSPIRED BY http://www.codesampler.com/usersrc/usersrc_6.htm#oglu_sky_dome_shader
15*/
16
17#include "cloud_effect.h"
18
19#include "util/loading/load_param.h"
20#include "util/loading/factory.h"
21#include "util/loading/resource_manager.h"
22
23#include "material.h"
24#include <math.h>
25#include "state.h"
26#include "p_node.h"
27#include "shader.h"
28#include "shell_command.h"
29
30#include "parser/tinyxml/tinyxml.h"
31
32
33using namespace std;
34
35SHELL_COMMAND(activate, CloudEffect, activateCloud);
36SHELL_COMMAND(deactivate, CloudEffect, deactivateCloud);
37
38CREATE_FACTORY(CloudEffect, CL_CLOUD_EFFECT);
39
40CloudEffect::CloudEffect(const TiXmlElement* root)
41{
42  this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
43  //  this->toList(OM_ENVIRON);
44
45  this->init();
46
47  if (root != NULL)
48    this->loadParams(root);
49
50  //if(cloudActivate)
51  this->activate();
52}
53
54CloudEffect::~CloudEffect()
55{
56  this->deactivate();
57}
58
59
60void CloudEffect::init()
61{
62  PRINTF(0)("Initializing CloudEffect\n");
63
64  noise3DTexSize = 64;
65  noise3DTexName = 0;
66
67  this->make3DNoiseTexture();
68
69  glGenTextures(1, &noise3DTexName);
70  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
71
72  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
73  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
74  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
75  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
76  glTexParameterf(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
77
78  glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA,
79               noise3DTexSize, noise3DTexSize, noise3DTexSize,
80               0, GL_RGBA, GL_UNSIGNED_BYTE, noise3DTexPtr);
81
82
83  //cloudMaterial.setDiffuseMap("pictures/sky/cloud1.jpg", GL_TEXTURE_2D, 0);
84
85  coor = 0;
86 
87  shader = new Shader(ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.vert",
88                      ResourceManager::getInstance()->getDataDir() + "/shaders/cloud.frag");
89
90  this->shader->activateShader();
91
92  Shader::Uniform(shader, "Noise").set(0);
93  Shader::Uniform(shader, "SkyColor").set(0.0f, 0.0f, 0.8f);
94  Shader::Uniform(shader, "CloudColor").set(0.8f, 0.8f, 0.8f);
95  Shader::Uniform(shader, "LightPos").set(5.0f, 0.0f, 5.0f);
96  Shader::Uniform(shader, "Scale").set(0.1f);
97 
98  offset = new Shader::Uniform(shader, "Offset");
99
100  this->shader->deactivateShader();
101}
102
103
104void CloudEffect::loadParams(const TiXmlElement* root)
105{
106  WeatherEffect::loadParams(root);
107
108  // LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
109  // LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
110  /*
111  LOAD_PARAM_START_CYCLE(root, element);
112  {
113  LoadParam_CYCLE(element, "option", this, CloudEffect, setCloudOption);
114  }
115  LOAD_PARAM_END_CYCLE(element);*/
116}
117
118
119void CloudEffect::activate()
120{
121  PRINTF(0)( "Activating\n");
122
123  //  this->cloudActivate = true;
124}
125
126void CloudEffect::deactivate()
127{
128  PRINTF(0)("Deactivating CloudEffect\n");
129
130  //  this->cloudActivate = false;
131}
132
133void CloudEffect::draw() const
134{
135  //if (!this->cloudActivate)
136  //  return;
137  glPushAttrib(GL_ENABLE_BIT);
138
139  glDisable(GL_LIGHTING);
140  glDisable(GL_BLEND);
141
142  glEnable(GL_TEXTURE_3D);
143  glBindTexture(GL_TEXTURE_3D, noise3DTexName);
144
145
146  //this->shader->activateShader();
147
148  //glMatrixMode(GL_MODELVIEW);
149  //glColor4f(1,1,1,1);
150  glPushMatrix();
151  //glTranslatef(0,this->waterHeight,0);
152
153  //cloudMaterial.unselect();
154  //cloudMaterial.select();
155
156  this->shader->activateShader();
157  //PRINTF(0)("coor: %f\n", coor);
158  offset->set(coor, coor, coor);
159
160  //glutSolidTeapot(1);
161
162
163  glColor3f(1.0, 1.0, 1.0);
164  glBegin(GL_QUADS);
165
166  glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0f, 0.0f,  0.0f);
167  glTexCoord2f(0.0f, 1.0f); glVertex3f( 0.0f, 10.0f,  0.0f);
168  glTexCoord2f(0.0f, 0.0f); glVertex3f( 10.0f, 10.0f,  0.0f);
169  glTexCoord2f(1.0f, 0.0f); glVertex3f(10.0f, 0.0f,  0.0f);
170
171  glEnd();
172
173  this->shader->deactivateShader();
174
175  //cloudMaterial.unselect();
176
177  glPopMatrix();
178
179  glPopAttrib();
180
181}
182
183void CloudEffect::tick (float dt)
184{
185  //if (!this->cloudActivate)
186  //  return;
187  coor += 10;
188}
189
190void CloudEffect::make3DNoiseTexture()
191{
192  int f, i, j, k, inc;
193  int startFrequency = 4;
194  int numOctaves = 4;
195  double ni[3];
196  double inci, incj, inck;
197  int frequency = startFrequency;
198  GLubyte *ptr;
199  double amp = 0.5;
200
201  if ((noise3DTexPtr = (GLubyte *) malloc(noise3DTexSize *
202                                          noise3DTexSize *
203                                          noise3DTexSize * 4)) == NULL)
204    PRINTF(0)("ERROR: Could not allocate 3D noise texture\n");
205
206  for (f=0, inc=0; f < numOctaves;
207       ++f, frequency *= 2, ++inc, amp *= 0.5)
208  {
209    SetNoiseFrequency(frequency);
210    ptr = noise3DTexPtr;
211    ni[0] = ni[1] = ni[2] = 0;
212
213    inci = 1.0 / (noise3DTexSize / frequency);
214    for (i=0; i<noise3DTexSize; ++i, ni[0] += inci)
215    {
216      incj = 1.0 / (noise3DTexSize / frequency);
217      for (j=0; j<noise3DTexSize; ++j, ni[1] += incj)
218      {
219        inck = 1.0 / (noise3DTexSize / frequency);
220        for (k=0; k<noise3DTexSize; ++k, ni[2] += inck, ptr+= 4)
221        {
222          *(ptr+inc) = (GLubyte) (((noise3(ni)+1.0) * amp)*128.0);
223        }
224      }
225    }
226  }
227}
228
229void CloudEffect::initNoise()
230{
231  int i, j, k;
232
233  srand(30757);
234  for (i = 0 ; i < B ; i++)
235  {
236    p[i] = i;
237    g1[i] = (double)((rand() % (B + B)) - B) / B;
238
239    for (j = 0 ; j < 2 ; j++)
240      g2[i][j] = (double)((rand() % (B + B)) - B) / B;
241    normalize2(g2[i]);
242
243    for (j = 0 ; j < 3 ; j++)
244      g3[i][j] = (double)((rand() % (B + B)) - B) / B;
245    normalize3(g3[i]);
246  }
247
248  while (--i)
249  {
250    k = p[i];
251    p[i] = p[j = rand() % B];
252    p[j] = k;
253  }
254
255  for (i = 0 ; i < B + 2 ; i++)
256  {
257    p[B + i] = p[i];
258    g1[B + i] = g1[i];
259    for (j = 0 ; j < 2 ; j++)
260      g2[B + i][j] = g2[i][j];
261    for (j = 0 ; j < 3 ; j++)
262      g3[B + i][j] = g3[i][j];
263  }
264}
265
266void CloudEffect::SetNoiseFrequency( int frequency)
267{
268  start = 1;
269  B = frequency;
270  BM = B-1;
271}
272
273double CloudEffect::noise3( double vec[3])
274{
275  int bx0, bx1, by0, by1, bz0, bz1, b00, b10, b01, b11;
276  double rx0, rx1, ry0, ry1, rz0, rz1, *q, sy, sz, a, b, c, d, t, u, v;
277  int i, j;
278
279  if (start)
280  {
281    start = 0;
282    initNoise();
283  }
284
285  setup(0, bx0,bx1, rx0,rx1);
286  setup(1, by0,by1, ry0,ry1);
287  setup(2, bz0,bz1, rz0,rz1);
288
289  i = p[ bx0 ];
290  j = p[ bx1 ];
291
292  b00 = p[ i + by0 ];
293  b10 = p[ j + by0 ];
294  b01 = p[ i + by1 ];
295  b11 = p[ j + by1 ];
296
297  t  = s_curve(rx0);
298  sy = s_curve(ry0);
299  sz = s_curve(rz0);
300
301  q = g3[ b00 + bz0 ] ; u = at3(rx0,ry0,rz0);
302  q = g3[ b10 + bz0 ] ; v = at3(rx1,ry0,rz0);
303  a = lerp(t, u, v);
304
305  q = g3[ b01 + bz0 ] ; u = at3(rx0,ry1,rz0);
306  q = g3[ b11 + bz0 ] ; v = at3(rx1,ry1,rz0);
307  b = lerp(t, u, v);
308
309  c = lerp(sy, a, b);
310
311  q = g3[ b00 + bz1 ] ; u = at3(rx0,ry0,rz1);
312  q = g3[ b10 + bz1 ] ; v = at3(rx1,ry0,rz1);
313  a = lerp(t, u, v);
314
315  q = g3[ b01 + bz1 ] ; u = at3(rx0,ry1,rz1);
316  q = g3[ b11 + bz1 ] ; v = at3(rx1,ry1,rz1);
317  b = lerp(t, u, v);
318
319  d = lerp(sy, a, b);
320
321  return lerp(sz, c, d);
322}
323
324void CloudEffect::normalize2( double v[2])
325{
326  double s;
327
328  s = sqrt(v[0] * v[0] + v[1] * v[1]);
329  v[0] = v[0] / s;
330  v[1] = v[1] / s;
331}
332
333void CloudEffect::normalize3( double v[3])
334{
335  double s;
336
337  s = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
338  v[0] = v[0] / s;
339  v[1] = v[1] / s;
340  v[2] = v[2] / s;
341}
342
Note: See TracBrowser for help on using the repository browser.