Initial Version of LayerManagementService added.
[profile/ivi/layer-management.git] / LayerManagerService / include / Shader.h
1 /***************************************************************************
2 *
3 * Copyright 2010 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
20 #ifndef _SHADER_H_
21 #define _SHADER_H_
22
23 #include <ShaderProgram.h>
24 #include <ShaderUniform.h>
25 #include <string>
26 #include <map>
27
28 /**
29  * Represents a shader instance.
30  *
31  * It stores a set of uniform parameters and refers to an OpenGL program object.
32  * Some uniform variables are pre-defined, like surface position and size, 
33  * opacity, etc... Additionally, there may be user-defined uniforms.
34  */
35 class Shader
36 {
37 public:
38         /**
39          * Creates a new shader instance by vertex and fragment shader name.
40          * @param vertName   File name of vertex shader.
41          * @param fragName   File name of fragment shader.
42          * @return new Shader instance, NULL if shader could not be loaded, compiled or linked.
43          */
44         static Shader* createShader(const std::string& vertName, const std::string& fragName);
45
46         /**
47          * Destructor
48          */
49         ~Shader();
50
51         /**
52          * @return Unique ID of this instance
53          */
54         int getId(void) const
55         {
56                 return _id;
57         }
58
59         /**
60          * Start using this shader for rendering.
61          */
62         void use(void) const
63         {
64                 _program.use();
65         }
66
67         /**
68          * Set a uniform.
69          */
70         void setUniform(const ShaderUniform& uniform);
71
72         /**
73          * Update uniform values.
74          * Please note that this method doesn't update the standard uniforms
75          * used for position, size, etc... They need to be set separately
76          * by loadCommonUniforms().
77          */
78         void loadUniforms(void) const;
79         
80         /**
81          * Load uniform values for common surface properties, like position, 
82          * size, opacity, etc...
83          *
84          * @param uniforms   Uniform values
85          */
86         void loadCommonUniforms(const ShaderProgram::CommonUniforms& uniforms) const
87         {
88                 _program.loadCommonUniforms(uniforms);
89         }
90
91 private:
92         /**
93          * Private constructor.
94          * New instances of this class are supposed to be created by createShader(...).
95          *
96          * @param program  Program object to be used     
97          */
98         Shader(ShaderProgram& program);
99
100 private:
101         /// unique shader ID
102         const int _id;
103
104         /// reference to OpenGL program object used by this shader instance
105         ShaderProgram& _program;
106
107         /// a map of user-defined uniforms
108         std::map<std::string,ShaderUniform> _uniformMap;
109         
110         /// next unique ID
111         static int _nextId;
112 };
113
114 #endif /* _SHADER_H_ */
115