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