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 "House.h"
22 #include "ShaderLighting.h"
23
24 #include <string.h>
25 #include <math.h>
26 #include <limits.h>
27 #include <stdlib.h>
28
29 #include <iostream>
30 using std::cout;
31 using std::endl;
32
33 #include <GLES2/gl2.h>
34
House(vec3f position,vec3f size,vec4f houseColor,ShaderBase * pShader,TextureLoader * houseTexture,float numElements)35 House::House(vec3f position, vec3f size, vec4f houseColor, ShaderBase* pShader, TextureLoader* houseTexture, float numElements)
36 : m_position(position)
37 , m_size(size)
38 , m_color(houseColor)
39 , numElements(numElements)
40 , m_pShader(pShader)
41 {
42 /*
43 // 5-------------6
44 // /| /|
45 // / | / |
46 // 1-------------2 |
47 // | | | |
48 // | | | | x
49 // | | | | | z
50 // | 4----------|--7 | /
51 // | / | / |/
52 // |/ |/ ------y
53 // 0-------------3
54 */
55
56 float height = 0.1 + 1.5 * random() / INT_MAX;
57
58 if(houseTexture != nullptr){
59 texture = houseTexture;
60 withTexture = true;
61 // use square houses (1.0 x 1.0) when using textures
62 height = 1.0f;
63 }
64
65 m_index[0] = vec3u(7, 4, 0); // bottom
66 m_index[1] = vec3u(0, 3, 7); // bottom
67 m_index[2] = vec3u(3, 2, 6); // right
68 m_index[3] = vec3u(6, 7, 3); // right
69 m_index[4] = vec3u(7, 6, 5); // back
70 m_index[5] = vec3u(5, 4, 7); // back
71 m_index[6] = vec3u(4, 5, 1); // left
72 m_index[7] = vec3u(1, 0, 4); // left
73 m_index[8] = vec3u(5, 6, 2); // top
74 m_index[9] = vec3u(2, 1, 5); // top
75 m_index[10] = vec3u(0, 1, 2); // front
76 m_index[11] = vec3u(2, 3, 0); // front
77
78
79 m_vertex[0].x = 0.0f; m_vertex[0].y = 0.0f; m_vertex[0].z = 1.0f;
80 m_vertex[1].x = 1.0f; m_vertex[1].y = 0.0f; m_vertex[1].z = 1.0f;
81 m_vertex[2].x = 1.0f; m_vertex[2].y = height; m_vertex[2].z = 1.0f;
82 m_vertex[3].x = 0.0f; m_vertex[3].y = height; m_vertex[3].z = 1.0f;
83 m_vertex[4].x = 0.0f; m_vertex[4].y = 0.0f; m_vertex[4].z = 0.0f;
84 m_vertex[5].x = 1.0f; m_vertex[5].y = 0.0f; m_vertex[5].z = 0.0f;
85 m_vertex[6].x = 1.0f; m_vertex[6].y = height; m_vertex[6].z = 0.0f;
86 m_vertex[7].x = 0.0f; m_vertex[7].y = height; m_vertex[7].z = 0.0f;
87 }
88
~House()89 House::~House()
90 {
91 }
92
render()93 void House::render()
94 {
95 if (withTexture) {
96 GLuint textureID = texture->getId();
97 ((ShaderTexture *)m_pShader)->use(&m_position, textureID);
98 } else {
99 m_pShader->use(&m_position, &m_color);
100 }
101
102 // draw
103 glEnableVertexAttribArray(0);
104 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
105 if(not withTexture) {
106 glDrawElements(GL_TRIANGLES, 3 * sizeof(m_index)/sizeof(m_index[0]), GL_UNSIGNED_SHORT, m_index);
107 } else {
108 for(int i = 0; i < 6; i++) {
109 // change texture mapping for each size of the house
110 // numElements == number of textures used per side of a house
111 if(i*2 == 0.0) { // left
112 m_texCoords[0].x = numElements;
113 m_texCoords[0].y = 0.0;
114
115 m_texCoords[7].x = 0.0;
116 m_texCoords[7].y = numElements;
117
118 m_texCoords[4].x = 0.0;
119 m_texCoords[4].y = 0.0;
120
121 m_texCoords[3].x = numElements;
122 m_texCoords[3].y = numElements;
123 } else if(i*2 == 10.0) { // front
124 m_texCoords[0].x = 0.0;
125 m_texCoords[0].y = 0.0;
126
127 m_texCoords[1].x = numElements;
128 m_texCoords[1].y = 0.0;
129
130 m_texCoords[2].x = numElements;
131 m_texCoords[2].y = numElements;
132
133 m_texCoords[3].x = 0.0;
134 m_texCoords[3].y = numElements;
135 } else if(i*2 == 8.0) { // right
136 m_texCoords[5].x = numElements;
137 m_texCoords[5].y = 0.0;
138
139 m_texCoords[1].x = 0.0;
140 m_texCoords[1].y = 0.0;
141
142 m_texCoords[2].x = 0.0;
143 m_texCoords[2].y = numElements;
144
145 m_texCoords[6].x = numElements;
146 m_texCoords[6].y = numElements;
147 } else {
148 memset(m_texCoords, 0, sizeof(m_texCoords));
149 }
150 glEnableVertexAttribArray(0);
151 glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
152 ((ShaderTexture *)m_pShader)->setTexCoords(m_texCoords);
153 glDrawElements(GL_TRIANGLE_FAN, 6, GL_UNSIGNED_SHORT, &m_index[i*2]);
154 }
155 }
156 }
157
update(int currentTimeInMs,int lastFrameTime)158 void House::update(int currentTimeInMs, int lastFrameTime)
159 {
160 (void)currentTimeInMs; //prevent warning
161
162 m_position.z += 0.0005f * (GLfloat)lastFrameTime;
163
164 if (m_position.z > 3.0)
165 {
166 m_position.z -= 15.0 * 2.0 * 1.0;
167 }
168 }
169