• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "Car.h"
22 #include "ShaderBase.h"
23 #include <string.h>
24 
25 #include <iostream>
26 using std::cout;
27 using std::endl;
28 
29 #include <GLES2/gl2.h>
30 
Car(vec3f position,vec3f size,vec4f color,ShaderBase * shader)31 Car::Car(vec3f position, vec3f size, vec4f color, ShaderBase* shader)
32 : m_position(position)
33 , m_size(size)
34 , m_color(color)
35 , m_index(0, 1, 2, 3)
36 , m_pShader(shader)
37 , texture(nullptr)
38 , withTexture(false)
39 {
40 	m_vertex[0].x = 0.0;
41 	m_vertex[0].y = 0.0;
42 	m_vertex[0].z = 0.0;
43 
44 	m_vertex[1].x = 0.1 * m_size.x;
45 	m_vertex[1].y = 0.0;
46 	m_vertex[1].z = -m_size.z;
47 
48 	m_vertex[2].x = 0.9 * m_size.x;
49 	m_vertex[2].y = 0.0;
50 	m_vertex[2].z = -m_size.z;
51 
52 	m_vertex[3].x = m_size.x;
53 	m_vertex[3].y = 0.0;
54 	m_vertex[3].z = 0.0;
55 }
56 
Car(vec3f position,vec3f size,vec4f color,ShaderTexture * shader,TextureLoader * texture)57 Car::Car(vec3f position, vec3f size, vec4f color, ShaderTexture* shader, TextureLoader* texture)
58 : m_position(position)
59 , m_size(size)
60 , m_color(color)
61 , m_index(0, 1, 2, 3)
62 , m_pShader(shader)
63 , texture(texture)
64 , withTexture(true)
65 {
66     m_vertex[0].x = 0.0;
67     m_vertex[0].y = 0.0;
68     m_vertex[0].z = 0.0;
69 
70     m_vertex[1].x = 0.0;
71     m_vertex[1].y = 0.0;
72     m_vertex[1].z = -m_size.z;
73 
74     m_vertex[2].x = m_size.x;
75     m_vertex[2].y = 0.0;
76     m_vertex[2].z = -m_size.z;
77 
78     m_vertex[3].x = m_size.x;
79     m_vertex[3].y = 0.0;
80     m_vertex[3].z = 0.0;
81 
82     m_texCoords[0].x = 0.0;
83     m_texCoords[0].y = 0.0;
84 
85     m_texCoords[1].x = 0.0;
86     m_texCoords[1].y = 1.0;
87 
88     m_texCoords[2].x = 1.0;
89     m_texCoords[2].y = 1.0;
90 
91     m_texCoords[3].x = 1.0;
92     m_texCoords[3].y = 0.0;
93 }
94 
render()95 void Car::render()
96 {
97     // draw
98     if (withTexture) {
99         GLuint textureID = texture->getId();
100         ((ShaderTexture *)m_pShader)->use(&m_position, textureID);
101         ((ShaderTexture *)m_pShader)->setTexCoords(m_texCoords);
102     } else {
103         m_pShader->use(&m_position, &m_color);
104     }
105     glEnableVertexAttribArray(0);
106     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, m_vertex);
107     glDrawElements(GL_TRIANGLE_FAN, 4, GL_UNSIGNED_SHORT, &m_index);
108 }
109