1 /***************************************************************************
2 *
3 * Copyright 2010,2011 BMW Car IT GmbH
4 * Copyright (C) 2018 Advanced Driver Information Technology Joint Venture GmbH
5 *
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 ****************************************************************************/
20 #include "MockNavi.h"
21
22 #include "House.h"
23 #include "Street.h"
24 #include "Ground.h"
25 #include "Car.h"
26 #include "Sky.h"
27 #include "configuration.h"
28
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #define CITY_GRID_SIZE 1.0f
34
MockNavi(float fps,float animationSpeed,SurfaceConfiguration * config)35 MockNavi::MockNavi(float fps, float animationSpeed, SurfaceConfiguration* config)
36 : OpenGLES2App(fps, animationSpeed, config)
37 , m_camera(vec3f(-1.5 * CITY_GRID_SIZE, -0.1, 0.0), vec3f(0.0, 0.0, 0.0), config->surfaceWidth, config->surfaceHeight)
38 , m_houseCount(15)
39 , nosky(config->nosky)
40 {
41 generateCity();
42 }
43
~MockNavi()44 MockNavi::~MockNavi()
45 {
46 delete pShader;
47 delete pShaderTexture;
48 delete pShaderGradient;
49 }
50
update(int currentTimeInMs,int lastFrameTime)51 void MockNavi::update(int currentTimeInMs, int lastFrameTime)
52 {
53 m_camera.update(currentTimeInMs, lastFrameTime);
54
55 list<IUpdateable*>::const_iterator iter = m_updateList.begin();
56 list<IUpdateable*>::const_iterator iterEnd = m_updateList.end();
57
58 for (; iter != iterEnd; ++iter)
59 {
60 (*iter)->update(currentTimeInMs, lastFrameTime);
61 }
62 }
63
render()64 void MockNavi::render()
65 {
66 list<IRenderable*>::const_iterator iter = m_renderList.begin();
67 list<IRenderable*>::const_iterator iterEnd = m_renderList.end();
68
69 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
70
71 for (; iter != iterEnd; ++iter)
72 {
73 (*iter)->render();
74 }
75 }
76
generateCity()77 void MockNavi::generateCity()
78 {
79 float* projection = m_camera.getViewProjectionMatrix();
80 pShader = new ShaderLighting(projection);
81 pShaderTexture = new ShaderTexture(projection);
82 pShaderGradient = new ShaderGradient();
83 TextureLoader* carTexture = new TextureLoader;
84 bool carTextureLoaded = carTexture->loadBMP((texturePath + std::string("/car.bmp")).c_str());
85 TextureLoader* streetTexture = new TextureLoader;
86 bool streetTextureLoaded = streetTexture->loadBMP((texturePath + std::string("/street.bmp")).c_str());
87 list<TextureLoader*> houseTextures;
88 for(int i = 0; i < m_houseCount; i++){
89 TextureLoader* houseTexture = new TextureLoader;
90 std::string houseTexturePath = texturePath;
91 houseTexturePath.append("/skyscrapers/facade0");
92 houseTexturePath.append(std::to_string(i));
93 houseTexturePath.append(".bmp");
94 bool houseTextureLoaded = houseTexture->loadBMP(houseTexturePath.c_str());
95 if(not houseTextureLoaded){
96 break;
97 }
98 houseTextures.push_back(houseTexture);
99 }
100
101 // generate sky
102 if (not nosky) {
103 vec4f skyColor(0.0, 0.0, 1.0, 1.0);
104 vec3f skyPosition = vec3f(-1.0, -1.0, 1.0);
105 vec3f skySize = vec3f(2.0, 2.0, 0.0);
106 Sky* sky = new Sky(skyPosition, skySize, skyColor, pShaderGradient);
107 m_renderList.push_back(sky);
108 m_updateList.push_back(sky);
109 }
110
111 // generate base plate
112 vec4f groundColor(0.8, 0.8, 0.6, 1.0);
113 vec3f position = vec3f(0.0, -0.001, 0.0);
114 vec3f size = vec3f(CITY_GRID_SIZE * 3, 0.0, -CITY_GRID_SIZE * 2.0 * m_houseCount);
115 Ground* ground = new Ground(position, size, groundColor, pShader);
116 m_renderList.push_back(ground);
117
118 // generate street z direction
119 vec4f streetColor(0.0, 0.0, 0.0, 1.0);
120 vec3f streetPosition = vec3f(0.6 * CITY_GRID_SIZE, 0.001, 0.0);
121 vec3f streetSize = vec3f(CITY_GRID_SIZE * 0.6, 0.0, -CITY_GRID_SIZE * 2.0 * m_houseCount * 200.0);
122 Street* obj = nullptr;
123 if(streetTextureLoaded){
124 obj = new Street(streetPosition, streetSize, streetColor, pShaderTexture, streetTexture);
125 }else{
126 obj = new Street(streetPosition, streetSize, streetColor, pShader);
127 }
128 m_renderList.push_back(obj);
129 m_updateList.push_back(obj);
130
131 // generate streets x direction
132 for (int z = 1; z < m_houseCount; ++z)
133 {
134 vec4f streetColor(0.0, 0.0, 0.0, 1.0);
135 vec3f streetPosition = vec3f(0.0, 0.0, 0.6 - z * CITY_GRID_SIZE);
136 vec3f streetSize = vec3f(CITY_GRID_SIZE * 3, 0.0, CITY_GRID_SIZE * 0.6);
137 Street* obj = nullptr;
138 if(streetTextureLoaded){
139 obj = new Street(streetPosition, streetSize, streetColor, pShaderTexture, streetTexture);
140 }else{
141 obj = new Street(streetPosition, streetSize, streetColor, pShader);
142 }
143 m_renderList.push_back(obj);
144 m_updateList.push_back(obj);
145 }
146
147 // generate car
148 vec3f carSize(0.2f, 0.2f, 0.3f);
149 vec4f carColor(0.7, 0.3, 0.3, 1.0);
150 Car* car = nullptr;
151 if(carTextureLoaded){
152 vec3f carPosition(1.39 * CITY_GRID_SIZE, 0.002, -0.3);
153 car = new Car(carPosition, carSize, carColor, pShaderTexture, carTexture);
154 } else {
155 vec3f carPosition(1.4 * CITY_GRID_SIZE, 0.002, -0.3);
156 car = new Car(carPosition, carSize, carColor, pShader);
157 }
158 m_renderList.push_back(car);
159
160 // generate houses
161 vec4f houseColor(0.6, 0.6, 0.8, 1.0);
162 for (int x = 0; x < 2; ++x)
163 {
164 for (int z = 0; z < m_houseCount; ++z)
165 {
166 float posx = x * 2.0 * CITY_GRID_SIZE;
167 float posy = 0.0;
168 float posz = -z * 2.0 * CITY_GRID_SIZE;
169
170 vec3f housePosition(posx, posy, posz);
171 vec3f houseSize(1.0, 1.0, 1.0);
172
173 TextureLoader* houseTexture = houseTextures.front();
174
175 House* obj = nullptr;
176
177 if(houseTexture == nullptr){
178 obj = new House(housePosition, houseSize, houseColor, pShader);
179 } else {
180 houseTextures.pop_front();
181 obj = new House(housePosition, houseSize, houseColor, pShaderTexture, houseTexture);
182 houseTextures.push_back(houseTexture);
183 }
184
185 m_renderList.push_back(obj);
186 m_updateList.push_back(obj);
187 }
188 }
189 }
190