~ubuntu-branches/ubuntu/oneiric/blobandconquer/oneiric

« back to all changes in this revision

Viewing changes to src/misc/opengl.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Guus Sliepen
  • Date: 2008-06-15 12:04:29 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080615120429-5ss7cbb4z9mpywj5
Tags: 0.95-1
New upstream release. Closes: #486310

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2006 Parallel Realities
 
3
 
 
4
This program is free software; you can redistribute it and/or
 
5
modify it under the terms of the GNU General Public License
 
6
as published by the Free Software Foundation; either version 2
 
7
of the License, or (at your option) any later version.
 
8
 
 
9
This program is distributed in the hope that it will be useful,
 
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
12
 
 
13
See the GNU General Public License for more details.
 
14
 
 
15
You should have received a copy of the GNU General Public License
 
16
along with this program; if not, write to the Free Software
 
17
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
18
 
 
19
*/
 
20
                
 
21
// Just contains lots of misc drawing related functions...
 
22
 
 
23
#include "opengl.h"
 
24
 
 
25
bool withinViewableRange(Entity *entity)
 
26
{
 
27
        if (entity->cluster != -1)
 
28
        {
 
29
                if (isClusterVisible(entity->cluster) == 0)
 
30
                {
 
31
                        return false;
 
32
                }
 
33
        }
 
34
        
 
35
        return camera->frustum.containsEntity(entity);
 
36
}
 
37
 
 
38
void drawCyclinder(Vector position, int segments, float size, float height, GLColor c)
 
39
{
 
40
        double f1, f2;
 
41
        Vector v1, v2;
 
42
 
 
43
        glEnable(GL_BLEND);
 
44
        glEnable(GL_TEXTURE_2D);
 
45
        glDepthMask(GL_FALSE);
 
46
        
 
47
        glBindTexture(GL_TEXTURE_2D, textureManager->getTexture("textures/game/fire.png")->data);
 
48
        
 
49
        for (int i = 0 ; i < segments ; i++)
 
50
        {
 
51
                f1 = (i * 2 * PI);
 
52
                f1 -= graphics->getAnimTimer() * 0.05;
 
53
                f1 /= segments;
 
54
                
 
55
                f2 = ((i + 1) * 2 * PI);
 
56
                f2 -= graphics->getAnimTimer() * 0.05;
 
57
                f2 /= segments;
 
58
                
 
59
                v1 = position;
 
60
                v1.x += (cos(f1) * size);
 
61
                v1.y += (sin(f1) * size);
 
62
                
 
63
                v2 = position;
 
64
                v2.x += (cos(f2) * size);
 
65
                v2.y += (sin(f2) * size);
 
66
                
 
67
                glColor4fv(c.color);
 
68
                
 
69
                glPushMatrix();
 
70
                {
 
71
                        glBegin(GL_QUADS);
 
72
                        {
 
73
                                glTexCoord2f(0.0, 1.0); glVertex3f(v1.x, v1.y, v1.z);
 
74
                                glTexCoord2f(1.0, 1.0); glVertex3f(v2.x, v2.y, v2.z);
 
75
                                glTexCoord2f(1.0, 0.0); glVertex3f(v2.x, v2.y, v2.z + height);
 
76
                                glTexCoord2f(0.0, 0.0); glVertex3f(v1.x, v1.y, v1.z + height);
 
77
                        }
 
78
                        glEnd();
 
79
                }
 
80
                glPopMatrix();
 
81
        }
 
82
        
 
83
        glDepthMask(GL_TRUE);
 
84
}
 
85
 
 
86
void drawBox(Vector position, float width, float length, float height, float tcm)
 
87
{
 
88
        width /= 2;
 
89
        length /= 2;
 
90
        height /= 2;
 
91
        
 
92
        glPushMatrix();
 
93
        {
 
94
                glTranslatef(position.x, position.y, position.z);
 
95
                
 
96
                glBegin(GL_QUADS);
 
97
                {
 
98
                        glTexCoord2f(tcm, tcm); glVertex3f( width, length, -height);
 
99
                        glTexCoord2f(0.0, tcm); glVertex3f(-width, length, -height);
 
100
                        glTexCoord2f(0.0, 0.0); glVertex3f(-width, length, height);
 
101
                        glTexCoord2f(tcm, 0.0); glVertex3f( width, length, height);
 
102
        
 
103
                        glTexCoord2f(0.0, 0.0); glVertex3f( width,-length, height);
 
104
                        glTexCoord2f(tcm, 0.0); glVertex3f(-width,-length, height);
 
105
                        glTexCoord2f(tcm, tcm); glVertex3f(-width,-length, -height);
 
106
                        glTexCoord2f(0.0, tcm); glVertex3f( width,-length, -height);
 
107
        
 
108
                        // Top
 
109
                        glTexCoord2f(0.0, 0.0); glVertex3f( width, length, height);
 
110
                        glTexCoord2f(tcm, 0.0); glVertex3f(-width, length, height);
 
111
                        glTexCoord2f(tcm, tcm); glVertex3f(-width,-length, height);
 
112
                        glTexCoord2f(0.0, tcm); glVertex3f( width,-length, height);
 
113
        
 
114
                        // Bottom
 
115
                        glTexCoord2f(0.0, 0.0); glVertex3f( width,-length, -height);
 
116
                        glTexCoord2f(tcm, 0.0); glVertex3f(-width,-length, -height);
 
117
                        glTexCoord2f(tcm, tcm); glVertex3f(-width, length, -height);
 
118
                        glTexCoord2f(0.0, tcm); glVertex3f( width, length, -height);
 
119
        
 
120
                        glTexCoord2f(tcm, 0.0); glVertex3f(-width, length,  height);
 
121
                        glTexCoord2f(tcm, tcm); glVertex3f(-width, length, -height);
 
122
                        glTexCoord2f(0.0, tcm); glVertex3f(-width,-length, -height);
 
123
                        glTexCoord2f(0.0, 0.0); glVertex3f(-width,-length,  height);
 
124
        
 
125
                        glTexCoord2f(0.0, tcm); glVertex3f( width, length, -height);
 
126
                        glTexCoord2f(0.0, 0.0); glVertex3f( width, length,  height);
 
127
                        glTexCoord2f(tcm, 0.0); glVertex3f( width,-length,  height);
 
128
                        glTexCoord2f(tcm, tcm); glVertex3f( width,-length, -height);
 
129
                }
 
130
                glEnd();
 
131
        }
 
132
        glPopMatrix();
 
133
}
 
134
 
 
135
void drawBoundingBox(Entity *entity)
 
136
{
 
137
        glColor4f(0.75, 0.75, 1.0, 0.25);
 
138
        
 
139
        glDisable(GL_TEXTURE_2D);
 
140
        glEnable(GL_BLEND);
 
141
        
 
142
        drawBox(entity->position, entity->boundingBox.maxs.x * 2, entity->boundingBox.maxs.y * 2, entity->boundingBox.maxs.z * 2, 1);
 
143
        
 
144
        glDisable(GL_BLEND);
 
145
        glEnable(GL_TEXTURE_2D);
 
146
}
 
147
 
 
148
void drawSelectedUnit()
 
149
{
 
150
        Point point;
 
151
        float dir = 1;
 
152
        float c = 0.5;
 
153
        
 
154
        if ((player->target == NULL) || (game->cutsceneType != CUTSCENE_NONE))
 
155
        {
 
156
                return;
 
157
        }
 
158
        
 
159
        if (dir == 1)
 
160
        {
 
161
                c += 0.01 * engine->getTimeDifference(TD_ANIMATION);
 
162
                
 
163
                if (c >= 0.75)
 
164
                {
 
165
                        dir = 0;
 
166
                }
 
167
        }
 
168
        else
 
169
        {
 
170
                c -= 0.01 * engine->getTimeDifference(TD_ANIMATION);
 
171
                
 
172
                if (c <= 0.25)
 
173
                {
 
174
                        dir = 1;
 
175
                }
 
176
        }
 
177
        
 
178
        point = graphics->convertToScreenCoords(player->target->position);
 
179
        
 
180
        graphics->setMode(MODE_2D);
 
181
        
 
182
        glColor4f(0.0, 1.0, 0.0, c);
 
183
        
 
184
        if (game->autoFaceTimer > 0)
 
185
        {
 
186
                glColor4f(1.0, 0.0, 0.0, c);
 
187
        }
 
188
        
 
189
        graphics->blit(textureManager->getTexture("textures/game/targetter.png"), (int)point.x, (int)point.y, true);
 
190
        graphics->setMode(MODE_3D);
 
191
}
 
192
 
 
193
void drawSkyDome(Vector position, int size, Texture *texture)
 
194
{
 
195
        glDepthMask(GL_FALSE);
 
196
        
 
197
        glDisable(GL_FOG);
 
198
        
 
199
        glDisable(GL_BLEND);
 
200
        glEnable(GL_TEXTURE_2D);
 
201
        
 
202
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
 
203
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
 
204
        
 
205
        glBindTexture(GL_TEXTURE_2D, texture->data);
 
206
        
 
207
        drawBox(position, 24, 24, 24, mission->skySphereSize);
 
208
        
 
209
        glEnable(GL_FOG);
 
210
        
 
211
        glDepthMask(GL_TRUE);
 
212
        
 
213
        glDisable(GL_TEXTURE_GEN_S);
 
214
        glDisable(GL_TEXTURE_GEN_T);
 
215
}
 
216
 
 
217
void enableMultiTexturing()
 
218
{
 
219
        glPushAttrib(GL_ALL_ATTRIB_BITS);
 
220
 
 
221
        /* Unit 0 */
 
222
 
 
223
        glActiveTextureARB(GL_TEXTURE0_ARB);
 
224
 
 
225
        glEnable(GL_TEXTURE_2D);
 
226
 
 
227
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 
228
 
 
229
        /* Unit 1 */
 
230
 
 
231
        glActiveTextureARB(GL_TEXTURE1_ARB);
 
232
 
 
233
        glEnable(GL_TEXTURE_2D);
 
234
        
 
235
        /* Set up environment to draw textures, lightmaps etc. */
 
236
        
 
237
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE_EXT);
 
238
 
 
239
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB_EXT, GL_PREVIOUS_EXT);
 
240
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB_EXT, GL_SRC_COLOR);
 
241
 
 
242
        glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB_EXT, GL_MODULATE);
 
243
 
 
244
        glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB_EXT, GL_TEXTURE);
 
245
        glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND1_RGB_EXT, GL_SRC_COLOR);
 
246
 
 
247
        glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_EXT, 2.0f);
 
248
        
 
249
        /* Set back to unit 0 */
 
250
        
 
251
        glActiveTextureARB(GL_TEXTURE0_ARB);
 
252
 
 
253
        glEnableClientState(GL_VERTEX_ARRAY);
 
254
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
255
 
 
256
        glClientActiveTextureARB(GL_TEXTURE1_ARB);
 
257
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
258
 
 
259
        glClientActiveTextureARB(GL_TEXTURE0_ARB);
 
260
}
 
261
 
 
262
void finishMultiTexturing()
 
263
{
 
264
        glActiveTextureARB(GL_TEXTURE1_ARB);
 
265
        glDisable(GL_TEXTURE_2D);
 
266
        
 
267
        glActiveTextureARB(GL_TEXTURE0_ARB);
 
268
        
 
269
        glClientActiveTextureARB(GL_TEXTURE1_ARB);
 
270
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
271
        glClientActiveTextureARB(GL_TEXTURE0_ARB);
 
272
 
 
273
        glDisableClientState(GL_VERTEX_ARRAY);
 
274
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
 
275
 
 
276
        glPopAttrib();
 
277
}
 
278
 
 
279
void renderScaledTexture(Texture *texture)
 
280
{
 
281
        glEnable(GL_TEXTURE_2D);
 
282
        
 
283
        float scaleX = graphics->screen->w;
 
284
        scaleX /= texture->iw;
 
285
        
 
286
        float scaleY = graphics->screen->h;
 
287
        scaleY /= texture->ih;
 
288
        
 
289
        glBindTexture(GL_TEXTURE_2D, texture->data);
 
290
        
 
291
        glBegin(GL_QUADS);
 
292
        {
 
293
                glTexCoord2f(0.0f, 0.0f); glVertex2f(0, 0);
 
294
                glTexCoord2f(1.0f, 0.0f); glVertex2f(texture->tw * scaleX, 0);
 
295
                glTexCoord2f(1.0f, 1.0f); glVertex2f(texture->tw * scaleX, texture->th * scaleY);
 
296
                glTexCoord2f(0.0f, 1.0f); glVertex2f(0, texture->th * scaleY);
 
297
        }
 
298
        glEnd();
 
299
}