• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ShaderTexture.h"
20 
21 const static char* vertexShaderCode =
22         "attribute mediump vec4 a_vertex;                                \
23          uniform mediump mat4 u_modelMatrix;                             \
24          attribute mediump vec2 a_texCoord;                              \
25          varying mediump vec2 v_texCoord;                                \
26          void main(void)                                                 \
27          {                                                               \
28             gl_Position = u_projectionMatrix * u_modelMatrix * a_vertex; \
29             v_texCoord = a_texCoord;                                     \
30          }";
31 
32 const static char* fragmentShaderCode =
33         "varying mediump vec2 v_texCoord;                    \
34          uniform sampler2D s_texture;                        \
35          void main (void)                                    \
36          {                                                   \
37             gl_FragColor = texture2D(s_texture, v_texCoord); \
38          }";
39 
ShaderTexture(float * projectionMatrix)40 ShaderTexture::ShaderTexture(float* projectionMatrix)
41 : ShaderBase(vertexShaderCode, fragmentShaderCode, projectionMatrix)
42 {
43     glUseProgram(shaderProgramId);
44     m_uniformModelMatrix = glGetUniformLocation(shaderProgramId, "u_modelMatrix");
45     m_uniformSampler = glGetUniformLocation(shaderProgramId, "s_texture");
46     attributeTexCoord = glGetAttribLocation(shaderProgramId, "a_texCoord");
47 }
48 
~ShaderTexture()49 ShaderTexture::~ShaderTexture()
50 {
51 }
52 
use(vec3f * position,GLuint textureID)53 void ShaderTexture::use(vec3f* position, GLuint textureID)
54 {
55     ShaderBase::use(position, nullptr);
56 
57     float translation[16];
58     translation[0] = 1.0f;
59     translation[1] = 0.0f;
60     translation[2] = 0.0f;
61     translation[3] = 0.0f;
62 
63     translation[4] = 0.0f;
64     translation[5] = 1.0f;
65     translation[6] = 0.0f;
66     translation[7] = 0.0f;
67 
68     translation[8] = 0.0f;
69     translation[9] = 0.0f;
70     translation[10] = 1.0f;
71     translation[11] = 0.0f;
72 
73     translation[12] = position->x;
74     translation[13] = position->y;
75     translation[14] = position->z;
76     translation[15] = 1.0f;
77 
78     glUseProgram(shaderProgramId);
79     glUniformMatrix4fv(m_uniformModelMatrix, 1, GL_FALSE, translation);
80 
81     // Code for using textures
82     // select active texture unit
83     glActiveTexture(GL_TEXTURE0);
84     glBindTexture(GL_TEXTURE_2D, textureID);
85     // Set the sampler texture unit to 0: GL_TEXTURE0
86     glUniform1i(m_uniformSampler, 0);
87 }
88 
getAttributeTexCoord()89 GLint ShaderTexture::getAttributeTexCoord() {
90     return attributeTexCoord;
91 }
92 
setTexCoords(vec2f * m_texCoords)93 void ShaderTexture::setTexCoords(vec2f * m_texCoords){
94     glEnableVertexAttribArray(attributeTexCoord);
95     glVertexAttribPointer(attributeTexCoord, 2, GL_FLOAT, GL_FALSE, 0, m_texCoords);
96 }
97