-
Notifications
You must be signed in to change notification settings - Fork 1
/
SeaSkyTerrain.cpp
165 lines (137 loc) · 4.85 KB
/
SeaSkyTerrain.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
#include "SeaSkyTerrain.h"
SeaSkyTerrain::SeaSkyTerrain(const std::string &skyfile, const std::string &seaFile)
{
//Load Both The Sea And Sky Texture
this->seaTextureId = loadExternalTextures(seaFile);
this->skyTextureId = loadExternalTextures(skyfile);
//Set Up Sea Object
this->disksea =gluNewQuadric(); // Create A Pointer To The Quadric Object
gluQuadricTexture(disksea , GL_TRUE); // Create Texture Coords
gluQuadricNormals(disksea, GLU_SMOOTH); // Create Smooth Normals
//Set Up Sky Object
this->cylinderSky=gluNewQuadric(); // Create A Pointer To The Quadric Object
animate = 0;
setAnimate(false);
setRenderMode(true);
}
void SeaSkyTerrain::draw()
{
if (this->getNeedsRedraw() == true){
glPushMatrix();
setUpLighting();
if (renderMode == true){
glDisable(GL_TEXTURE_2D);
glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
}
else {
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
}
setUpMaterialLighting();
//Sea
//Render Sky
glRotated(-90,1,0,0);
glColor3fv(seaBlue);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, this->seaTextureId);
gluQuadricTexture(this->disksea, getRenderMode() ? GLU_FALSE : GLU_TRUE);
gluQuadricNormals(this->disksea, GLU_SMOOTH);
gluDisk(this->disksea,0,500,35,35);
glTranslated(0,0,-1);
glRotated(angle,0,0,1);
glColor3f(1,0,0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glBindTexture(GL_TEXTURE_2D, this->skyTextureId);
gluQuadricTexture(this->cylinderSky, getRenderMode() ? GLU_FALSE : GLU_TRUE);
gluQuadricNormals(this->cylinderSky, GLU_SMOOTH);
gluCylinder(this->cylinderSky,500,200,400,35,35);
setNeedsRedraw(false);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
}
bool SeaSkyTerrain::nextFrame()
{
if (getAnimate() == true){
if (angle >= 360.0 )
angle=0.0;
else
angle+=angle_increment;
}
setNeedsRedraw(true);
return true ;
}
bool SeaSkyTerrain::getRenderMode() const
{
return renderMode;
}
void SeaSkyTerrain::setRenderMode(bool value)
{
renderMode = value;
}
void SeaSkyTerrain::setUpMaterialLighting()
{
float matSpec[] = { 1.0, 1.0, 1,0, 1.0 };
float matShine[] = { 50 };
// Material properties shared by all enviorment stuff in the scene
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, matSpec);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, matShine);
//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,black);
// Enable color material mode:
// The ambient and diffuse color of the front faces will track the color set by glColor().
//if (!getRenderMode()){
//glDisable(GL_COLOR_MATERIAL);
//glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT,white);
//glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,white);
//}
//else {
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
//} // glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, black);
}
bool SeaSkyTerrain::getNeedsRedraw() const
{
return needsRedraw;
}
void SeaSkyTerrain::setNeedsRedraw(bool value)
{
needsRedraw = value;
}
void SeaSkyTerrain::setUpLighting()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
float lightAmb[] = { 0.0, 0.0, 0.0, 1.0 };
float lightDifAndSpec0[] = { 1, 1, 1, 0.0 };
float lightPos0[] = { 1,1,0, 0};
//draw Sphere Centre 0,0
float globAmb[] = { 0.2, 0.2, 0.2, 1.0 };
//Light Position
glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
// Light0 properties.
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDifAndSpec0);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightDifAndSpec0);
//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
//
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globAmb); // Global ambient light.
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE); // Enable local viewpoint
glShadeModel(GL_SMOOTH);
//Distance Light
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.0);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.0);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 1.0);
//Enable normalization
glEnable(GL_NORMALIZE);
}
bool SeaSkyTerrain::getAnimate() const
{
return animate;
}
void SeaSkyTerrain::setAnimate(bool value)
{
animate = value;
}
float SeaSkyTerrain::angle_increment = 0.1;