-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sky.cpp
224 lines (172 loc) · 7.04 KB
/
Sky.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include "stdafx.h"
#include <string>
#include <string.h>
#include <sstream>
#include <stdexcept>
#include <string>
#include <iostream>
#include <fstream>
#include "Includes\glee\GLee.h"
#include "glut.h"
#include "Sky.h"
#include "Math.h"
#include "TargaImage.h"
#include "Includes\OpenGL.h"
using namespace std;
//#define GL_CLAMP_TO_EDGE 0x812F
Sky::Sky(){
m_size = 50.0f;
for (int idx = 0; idx < 6; idx++)
skyobj[idx] = 0;
}
Sky::~Sky(){
Release();
}
bool Sky::Init(){
// enable 2D texturing
glEnable(GL_TEXTURE_2D);
TargaImage skybox;
glPushMatrix();
// fisrts right
skybox.Load("Side1.tga");
glGenTextures(1, &skyobj[SKY_RIGHT]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_RIGHT]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
// second bottom
skybox.Load("Side2.tga");
glGenTextures(1, &skyobj[SKY_BOTTOM]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_BOTTOM]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
// third left side
skybox.Load("Side3.tga");
glGenTextures(1, &skyobj[SKY_LEFT]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_LEFT]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
// fourth front side
skybox.Load("Side4.tga");
glGenTextures(1, &skyobj[SKY_FRONT]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_FRONT]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
//fifth
skybox.Load("Side5.tga");
glGenTextures(1, &skyobj[SKY_BACK]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_BACK]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
// sixth
skybox.Load("Side6.tga");
glGenTextures(1, &skyobj[SKY_TOP]);
// bind the texture object
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_TOP]);
// minimum required to set the min and mag texture filters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// now that the texture object is bound, specify a texture for it
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, skybox.GetWidth(), skybox.GetHeight(), GL_RGB, GL_UNSIGNED_BYTE, skybox.GetImage());
skybox.Release();
glPopMatrix();
return true;
}
void Sky::Render(Vector3 cameraPos){
Init();
glPushMatrix();
glTranslatef(cameraPos.x, cameraPos.y, cameraPos.z);
glPushAttrib(GL_FOG_BIT | GL_DEPTH_BUFFER_BIT | GL_LIGHTING_BIT);
glDisable(GL_DEPTH_TEST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glColor3f(0.16f, 0.789f, 0.85f);
GLfloat skyColor[] = { 0.0f, 0.0f, 0.5f, 1.0f };
GLfloat skySpecular[] = { 0.0f, 0.0f, 0.5f, 1.0f };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, skyColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, skySpecular);
//Top
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_TOP]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, m_size, m_size);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, m_size, m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_TOP]);
// Bottom
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_BOTTOM]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, -m_size, -m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, -m_size, -m_size);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_BOTTOM]);
// Front
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_FRONT]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, -m_size);
glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, -m_size);
glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_FRONT]);
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_BACK]);
// Back
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_BACK]);
// Right
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_RIGHT]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(m_size, -m_size, m_size);
glTexCoord2f(1.0f, 1.0f); glVertex3f(m_size, m_size, m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(m_size, m_size, -m_size);
glTexCoord2f(0.0f, 0.0f); glVertex3f(m_size, -m_size, -m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_RIGHT]);
// Left
glBindTexture(GL_TEXTURE_2D, skyobj[SKY_LEFT]);
glBegin(GL_QUADS);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-m_size, -m_size, -m_size);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-m_size, m_size, -m_size);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-m_size, m_size, m_size);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-m_size, -m_size, m_size);
glEnd();
glDeleteTextures(1,&skyobj[SKY_LEFT]);
glEnable(GL_DEPTH_TEST);
glPopMatrix();
Release();
}
void Sky::Release(){
for (int i = 0; i < 6; ++i)
glDeleteTextures(6, &skyobj[i]);
}