Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 8179 was 8175, checked in by amaechler, 18 years ago

branches/atmospheric_engine: new cloud implementation using shaders, not yet working - wanna fix it? :) look at source and the demo mentioned there

File size: 5.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        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 "glincl.h"
24#include "material.h"
25#include <math.h>
26#include "state.h"
27#include "p_node.h"
28#include "shader.h"
29
30#include "parser/tinyxml/tinyxml.h"
31
32#include "sglmodel.h"
33
34using namespace std;
35
36CREATE_FACTORY(CloudEffect, CL_CLOUD_EFFECT);
37
38CloudEffect::CloudEffect(const TiXmlElement* root)
39{
40        this->setClassID(CL_CLOUD_EFFECT, "CloudEffect");
41
42        this->init();
43
44        if (root != NULL)
45                this->loadParams(root);
46
47        this->activate();
48}
49
50CloudEffect::~CloudEffect()
51{
52        this->deactivate();
53
54        delete this->cloudMaterial;
55
56        g_sky_model.Delete();
57        Shader::unload(this->skyShader);
58
59}
60
61
62bool CloudEffect::init()
63{
64        PRINTF(1)("Initializing CloudEffect\n");
65
66        // glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
67        glShadeModel(GL_SMOOTH);
68        glEnable(GL_DEPTH_TEST);
69        glEnable(GL_CULL_FACE);
70        glCullFace(GL_BACK);
71        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
72       
73        glLineWidth(3.0f);
74       
75        float diffuse[]  = {1.0f, 1.0f, 1.0f, 1.0f};
76        float position[] = {0.0f, 2.0f, 0.0f, 1.0f};
77        float specular[] = {1.0f, 1.0f, 1.0f, 1.0f};
78       
79        glEnable(GL_LIGHTING);
80        glEnable( GL_LIGHT0);
81        glLightfv(GL_LIGHT0, GL_DIFFUSE,  diffuse);
82        glLightfv(GL_LIGHT0, GL_POSITION, position);
83        glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
84       
85        float shininess = 5.0f;
86        glEnable(GL_COLOR_MATERIAL);
87        glMaterialf( GL_FRONT, GL_SHININESS, shininess);
88        glMaterialfv(GL_FRONT, GL_SPECULAR,  specular);
89
90        this->cloudTexture = "pictures/sky/cloud1.jpg";
91
92        g_sky_model.Load("sky.sgl");
93        g_sky_model.Unitize();
94
95        /* Get the bounding box of the sky dome. */
96        float bbox[3] = {0};
97        g_sky_model.GetDimensions(bbox[0], bbox[1], bbox[2]);
98       
99        // this->skyShader = (Shader*)ResourceManager::getInstance()->load("shaders/sky.vert", SHADER, RP_GAME, "shaders/skyfrag");
100        this->skyShader = new Shader( ResourceManager::getInstance()->getDataDir() + "/shaders/sky.vert", ResourceManager::getInstance()->getDataDir() +"/shaders/sky.frag");
101
102        /* Tell the shader the bounding box of the sky dome. */
103        this->skyShader->bindShader("bbox", bbox, 4);
104
105        //g_cloud_texture = 0;
106
107        g_tint[4] = ( 0.9f, 0.7f, 0.7f, 1.0f );
108        g_scroll = 1.0f;
109        time = 0.0f;
110}
111
112
113void CloudEffect::loadParams(const TiXmlElement* root)
114{
115        WeatherEffect::loadParams(root);
116
117        LoadParam(root, "speed", this, CloudEffect, setCloudAnimation);
118        LoadParam(root, "texture", this, CloudEffect, setCloudTexture);
119}
120
121
122bool CloudEffect::activate()
123{
124        PRINTF(0)( "Activating CloudEffect with Material %s\n", this->cloudTexture.c_str());
125
126        this->cloudMaterial = new Material("Sky");
127        this->cloudMaterial->setIllum(3);
128        this->cloudMaterial->setAmbient(1.0, 1.0, 1.0);
129        this->cloudMaterial->setDiffuseMap(this->cloudTexture);
130}
131
132bool CloudEffect::deactivate()
133{
134        PRINTF(0)("Deactivating CloudEffect\n");
135}
136
137void CloudEffect::draw() const
138{
139
140//      glMatrixMode(GL_MODELVIEW);
141//      glPushMatrix();
142//
143//      // Move sphere along with the camera
144//      Vector r = State::getCameraNode()->getAbsCoor();
145//      glTranslatef(r.x, r.y, r.z);
146//
147//      // Sky movement
148//      glRotatef(mover, 0, 0, 1);
149//
150//      cloudMaterial->select();
151//      gluSphere(this->sphereObj, this->sphereRadius, 20, 20);
152//      glPopMatrix();
153
154        // ******
155
156        glMatrixMode(GL_MODELVIEW);
157        glPushMatrix();
158
159        static float time = 0.0f;
160        // static vector3f eye = {0.0f, 0.0f, 0.0f}, look = {0.0f, 0.0f, 1.0f}, up = {0.0f, 1.0f, 0.0f};
161
162        // glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
163        // glLoadIdentity();
164        // gluLookAt(eye[x], eye[y], eye[z], look[x], look[y], look[z], up[x], up[y], up[z]);
165
166        /* Turn off the depth buffer when rendering the background. */
167        // glDisable(GL_DEPTH_TEST);
168
169        this->skyShader->activateShader();
170
171        /* Select the shader program and update parameters. */
172        /* The "time" parameter controls the cloud scrolling. */
173        this->skyShader->bindShader("time", &time, 1);
174        /* The "horizon" parameter controls the sky tinting. */
175        this->skyShader->bindShader("horizon", g_tint, 4);
176       
177        glActiveTexture(GL_TEXTURE0 + 0);
178        glActiveTexture(GL_TEXTURE0 + 1);
179        /* Load the cloud texture. */
180        //glBindTexture(GL_TEXTURE_2D, g_cloud_texture);
181        this->cloudMaterial->select();
182       
183        /* Render the sky dome. */
184        g_sky_model.Render();
185       
186        /* Unselect the shader. */
187        Shader::deactivateShader();
188
189        /* Turn on the depth buffer when rendering the foreground. */
190        // glEnable(GL_DEPTH_TEST);
191
192        /* Render the forground, for example, a teapot or bunny. */
193        //    glEnable(GL_LIGHTING);
194        //    glColor3f(0.0f, 1.0f, 0.0f);
195        //    glBegin(GL_QUADS);
196        //    glNormal3f( 0.0f,  1.0f,  0.0f);
197        //    glVertex3f( 20.0f, -1.0f,  20.0f);
198        //    glVertex3f( 20.0f, -1.0f, -20.0f);
199        //    glVertex3f(-20.0f, -1.0f, -20.0f);
200        //    glVertex3f(-20.0f, -1.0f,  20.0f);
201        //    glEnd();
202        //    glDisable(GL_LIGHTING);
203
204        glPopMatrix();
205}
206
207void CloudEffect::tick (float dt)
208{
209        // mover = mover + this->cloudSpeed * dt;
210
211        /* Update the timer for scrolling the clouds. */
212        time = time + ((1.0f / 2000.0f) * g_scroll);
213}
214
Note: See TracBrowser for help on using the repository browser.