1 /***************************************************************************
2 *
3 * Copyright (C) 2018 Advanced Driver Information Technology Joint Venture GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19 #include "Sky.h"
20 #include "ShaderBase.h"
21 #include <string.h>
22
23 #include <iostream>
24 using std::cout;
25 using std::endl;
26
27 #include <GLES2/gl2.h>
28
Sky(vec3f position,vec3f size,vec4f color,ShaderGradient * shader,float sunriseSpeed,float sunsetSpeed,float daySpeed,float nightSpeed)29 Sky::Sky(vec3f position, vec3f size, vec4f color, ShaderGradient* shader, float sunriseSpeed, float sunsetSpeed, float daySpeed, float nightSpeed)
30 : m_position(position)
31 , m_size(size)
32 , m_color(color)
33 , m_index(0, 1, 2, 3)
34 , skyState(sunrise)
35 , sunriseSpeed(sunriseSpeed)
36 , sunsetSpeed(sunsetSpeed)
37 , daySpeed(daySpeed)
38 , nightSpeed(nightSpeed)
39 , m_pShader(shader)
40 {
41 m_vertex[0].x = m_position.x;
42 m_vertex[0].y = m_position.y;
43 m_vertex[0].z = m_position.z;
44
45 m_vertex[1].x = m_position.x;
46 m_vertex[1].y = m_position.y + m_size.y;
47 m_vertex[1].z = m_position.z;
48
49 m_vertex[2].x = m_position.x + m_size.x;
50 m_vertex[2].y = m_position.y + m_size.y;
51 m_vertex[2].z = m_position.z;
52
53 m_vertex[3].x = m_position.x + m_size.x;
54 m_vertex[3].y = m_position.y;
55 m_vertex[3].z = m_position.z;
56 }
57
render()58 void Sky::render()
59 {
60 m_pShader->use(&m_position, colors);
61
62 // draw
63 glEnableVertexAttribArray(0);
64 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
65 glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, &m_index);
66 }
67
update(int currentTimeInMs,int lastFrameTime)68 void Sky::update(int currentTimeInMs, int lastFrameTime)
69 {
70 (void)currentTimeInMs; //prevent warning
71 (void)lastFrameTime;
72 static float clock = 0;
73
74 if (skyState == sunrise) {
75 if (colors[0].r < 1.0) {
76 colors[0].r += sunriseSpeed;
77 colors[0].g += sunriseSpeed/2;
78
79 colors[1].b += sunriseSpeed/4;
80 colors[2].b += sunriseSpeed/4;
81
82 colors[3].r += sunriseSpeed;
83 colors[3].g += sunriseSpeed/2;
84 } else {
85 skyState = daytime;
86 }
87 } else if (skyState == daytime) {
88 if (clock < 1.0) {
89 clock += daySpeed;
90 } else {
91 clock = 0.0;
92 skyState = sunset;
93 }
94 } else if (skyState == sunset) {
95 if (colors[0].r > 0.0) {
96 colors[0].r -= sunsetSpeed;
97 colors[0].g -= sunsetSpeed/2;
98
99 colors[1].b -= sunsetSpeed/4;
100 colors[2].b -= sunsetSpeed/4;
101
102 colors[3].r -= sunsetSpeed;
103 colors[3].g -= sunsetSpeed/2;
104 } else {
105 skyState = nighttime;
106 }
107 } else if (skyState == nighttime) {
108 if (clock < 1.0) {
109 clock += nightSpeed;
110 } else {
111 clock = 0.0;
112 skyState = sunrise;
113 }
114 }
115 }
116