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: Stefan Lienard |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | #include "mapped_water.h" |
---|
17 | #include "util/loading/load_param.h" |
---|
18 | #include "util/loading/factory.h" |
---|
19 | #include "material.h" |
---|
20 | |
---|
21 | |
---|
22 | CREATE_FACTORY(MappedWater, CL_MAPPED_WATER); |
---|
23 | |
---|
24 | |
---|
25 | MappedWater::MappedWater(const TiXmlElement* root) |
---|
26 | : texture(GL_TEXTURE_2D) |
---|
27 | { |
---|
28 | this->setClassID(CL_MAPPED_WATER, "MappedWater"); |
---|
29 | this->toList(OM_ENVIRON); |
---|
30 | |
---|
31 | if (root != NULL) |
---|
32 | this->loadParams(root); |
---|
33 | |
---|
34 | |
---|
35 | mat.setDiffuseMap(&this->texture, 0); |
---|
36 | // Change the view port to be the size of the texture we will render to |
---|
37 | // HACK |
---|
38 | this->textureSize = 512; |
---|
39 | } |
---|
40 | |
---|
41 | MappedWater::~MappedWater() |
---|
42 | { |
---|
43 | |
---|
44 | } |
---|
45 | |
---|
46 | void MappedWater::loadParams(const TiXmlElement* root) |
---|
47 | { |
---|
48 | WorldEntity::loadParams(root); |
---|
49 | |
---|
50 | LoadParam(root, "waterHeight", this, MappedWater, setHeight); |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | void MappedWater::draw() const |
---|
55 | { |
---|
56 | glPushMatrix(); |
---|
57 | |
---|
58 | /* |
---|
59 | glTranslatef (this->getAbsCoor ().x, |
---|
60 | this->getAbsCoor ().y, |
---|
61 | this->getAbsCoor ().z); |
---|
62 | */ |
---|
63 | glTranslatef(0,this->waterHeight,0); |
---|
64 | |
---|
65 | //glEnable(GL_LIGHTING); |
---|
66 | |
---|
67 | |
---|
68 | //mat.setDiffuseMap("pictures/sky-replace.jpg", GL_TEXTURE_2D, 1); |
---|
69 | //mat.setTransparency(1.0); |
---|
70 | //mat.setDiffuse(1.0, 0, .1); |
---|
71 | |
---|
72 | |
---|
73 | // HACK |
---|
74 | |
---|
75 | //glDisable(GL_BLEND); |
---|
76 | //glActiveTexture(GL_TEXTURE0); |
---|
77 | //glBindTexture(GL_TEXTURE_2D, this->texture); |
---|
78 | |
---|
79 | mat.select(); |
---|
80 | |
---|
81 | glBegin(GL_QUADS); |
---|
82 | glNormal3f(0,1,0); |
---|
83 | glTexCoord2f(0,0); |
---|
84 | //glMultiTexCoord2f(GL_TEXTURE1, 0,0); |
---|
85 | glVertex3f(0,0,0); |
---|
86 | glTexCoord2f(1,0); |
---|
87 | //glMultiTexCoord2f(GL_TEXTURE1, 1,0); |
---|
88 | glVertex3f(100,0,0); |
---|
89 | glTexCoord2f(1,1); |
---|
90 | //glMultiTexCoord2f(GL_TEXTURE1, 1,1); |
---|
91 | glVertex3f(100,0,-100); |
---|
92 | glTexCoord2f(0,1); |
---|
93 | //glMultiTexCoord2f(GL_TEXTURE1, 0,1); |
---|
94 | glVertex3f(0,0,-100); |
---|
95 | glEnd(); |
---|
96 | |
---|
97 | glPopMatrix(); |
---|
98 | } |
---|
99 | |
---|
100 | void MappedWater::tick(float dt) |
---|
101 | { |
---|
102 | |
---|
103 | } |
---|
104 | |
---|
105 | void MappedWater::setHeight(float height) |
---|
106 | { |
---|
107 | this->waterHeight = height; |
---|
108 | } |
---|
109 | |
---|
110 | void MappedWater::activateReflection() |
---|
111 | { |
---|
112 | glPushAttrib(GL_VIEWPORT_BIT); |
---|
113 | // glPushMatrix(); |
---|
114 | |
---|
115 | //glLoadIdentity(); |
---|
116 | glViewport(0,0, textureSize, textureSize); |
---|
117 | |
---|
118 | |
---|
119 | // Clear the color and depth bits, reset the matrix and position our camera. |
---|
120 | |
---|
121 | //g_Camera.Look(); |
---|
122 | |
---|
123 | |
---|
124 | // If our camera is above the water we will render the scene flipped upside down. |
---|
125 | // In order to line up the reflection nicely with the world we have to translate |
---|
126 | // the world to the position of our reflected surface, multiplied by two. |
---|
127 | //if(g_Camera.Position().y > waterHeight) |
---|
128 | //{ |
---|
129 | // Translate the world, then flip it upside down |
---|
130 | // glTranslatef(0.0f, waterHeight*2.0f, 0.0f); |
---|
131 | // glScalef(1.0, -1.0, 1.0); |
---|
132 | |
---|
133 | // Since the world is updside down we need to change the culling to FRONT |
---|
134 | //glCullFace(GL_FRONT); |
---|
135 | |
---|
136 | // Set our plane equation and turn clipping on |
---|
137 | // double plane[4] = {0.0, 1.0, 0.0, -waterHeight}; |
---|
138 | // glEnable(GL_CLIP_PLANE0); |
---|
139 | // glClipPlane(GL_CLIP_PLANE0, plane); |
---|
140 | |
---|
141 | // Render the world upside down and clipped (only render the top flipped). |
---|
142 | // If we don't turn OFF caustics for the reflection texture we get horrible |
---|
143 | // artifacts in the water. That is why we set bRenderCaustics to FALSE. |
---|
144 | //RenderWorld(false); |
---|
145 | |
---|
146 | // Turn clipping off |
---|
147 | // glDisable(GL_CLIP_PLANE0); |
---|
148 | |
---|
149 | // Restore back-face culling |
---|
150 | // glCullFace(GL_BACK); |
---|
151 | //} |
---|
152 | /*else |
---|
153 | { |
---|
154 | // If the camera is below the water we don't want to flip the world, |
---|
155 | // but just render it clipped so only the top is drawn. |
---|
156 | double plane[4] = {0.0, 1.0, 0.0, waterHeight}; |
---|
157 | glEnable(GL_CLIP_PLANE0); |
---|
158 | glClipPlane(GL_CLIP_PLANE0, plane); |
---|
159 | RenderWorld(true); |
---|
160 | glDisable(GL_CLIP_PLANE0); |
---|
161 | }*/ |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | void MappedWater::deactivateReflection() |
---|
166 | { |
---|
167 | //glBindTexture(GL_TEXTURE_2D, texture.getTexture()); |
---|
168 | |
---|
169 | //mat.setDiffuseMap(&texture, 0); |
---|
170 | mat.select(); |
---|
171 | glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize); |
---|
172 | |
---|
173 | |
---|
174 | glPopAttrib(); |
---|
175 | // glPopMatrix(); |
---|
176 | } |
---|
177 | |
---|
178 | void MappedWater::activateRefraction() |
---|
179 | { |
---|
180 | |
---|
181 | } |
---|
182 | |
---|
183 | void MappedWater::deactivateRefraction() |
---|
184 | { |
---|
185 | |
---|
186 | } |
---|