• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /***************************************************************************
2  *
3  * Copyright 2010,2011 BMW Car IT 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 "ShaderLighting.h"
20 
21 const char* vertexShaderCode =
22 		    "attribute mediump vec4 a_vertex;                                 \
23 		     uniform mediump mat4 u_modelMatrix;                              \
24 		     varying mediump vec4 v_normal;                                   \
25              void main(void)                                                  \
26              {                                                                \
27                  gl_Position = u_projectionMatrix * u_modelMatrix * a_vertex; \
28 		         v_normal = normalize(a_vertex);                              \
29              }";
30 
31 const char* fragmentShaderCode =
32 		    "uniform mediump vec4 u_color;   \
33 		     varying mediump vec4 v_normal;  \
34 		     mediump vec4 lightPosition;     \
35              void main (void)                \
36 		     {                               \
37 		         lightPosition = normalize(vec4(-3.0, -5.0, 10.0, 1.0));   \
38 		         gl_FragColor = max(dot(v_normal, lightPosition), 0.0) * 0.5 * u_color + 0.8 * u_color;   \
39 		         gl_FragColor.a = 1.0;   \
40 		     }";
41 
ShaderLighting(float * projectionMatrix)42 ShaderLighting::ShaderLighting(float* projectionMatrix)
43 : ShaderBase(vertexShaderCode, fragmentShaderCode, projectionMatrix)
44 {
45     glUseProgram(shaderProgramId);
46     m_uniformModelMatrix = glGetUniformLocation(shaderProgramId, "u_modelMatrix");
47     m_uniformColor = glGetUniformLocation(shaderProgramId, "u_color");
48 }
49 
~ShaderLighting()50 ShaderLighting::~ShaderLighting()
51 {
52 }
53 
use(vec3f * position,vec4f * color)54 void ShaderLighting::use(vec3f* position, vec4f* color)
55 {
56 	ShaderBase::use(position, color);
57 
58     float translation[16];
59     translation[0] = 1.0f;
60     translation[1] = 0.0f;
61     translation[2] = 0.0f;
62     translation[3] = 0.0f;
63 
64     translation[4] = 0.0f;
65     translation[5] = 1.0f;
66     translation[6] = 0.0f;
67     translation[7] = 0.0f;
68 
69     translation[8] = 0.0f;
70     translation[9] = 0.0f;
71     translation[10] = 1.0f;
72     translation[11] = 0.0f;
73 
74     translation[12] = position->x;
75     translation[13] = position->y;
76     translation[14] = position->z;
77     translation[15] = 1.0f;
78 
79 	glUseProgram(shaderProgramId);
80 	glUniformMatrix4fv(m_uniformModelMatrix, 1, GL_FALSE, translation);
81     glUniform4fv(m_uniformColor, 1, &color->r);
82 
83 }
84