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