TDME2 1.9.121
EntityShaderParameters.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <string>
5
6#include <tdme/tdme.h>
8
9using std::map;
10using std::string;
11using std::to_string;
12
14
15/**
16 * TDME2 engine entity shader parameters
17 * @author Andreas Drewke
18 * @version $Id$
19 */
21{
22private:
23 map<string, ShaderParameter> parameters;
24 string shaderId;
25
26 mutable string hash;
27 mutable bool changed { false };
28
29 /**
30 * Compute shader parameters hash
31 */
33
34public:
35 /**
36 * Public constructor
37 */
39 }
40
41 /**
42 * Destructor
43 */
45 }
46
47 /**
48 * Set shader
49 * @param shaderId shader id
50 */
51 void setShader(const string& shaderId) {
52 if (this->shaderId == shaderId) return;
53 parameters.clear();
54 hash = shaderId + ";";
55 changed = false;
56 this->shaderId = shaderId;
57 }
58
59 /**
60 * Returns shader parameter for given parameter name, if the value does not exist, the default will be returned
61 * @param shaderId shader id
62 * @param parameterName parameter name
63 * @return shader parameter
64 */
65 const ShaderParameter getShaderParameter(const string& parameterName) const;
66
67 /**
68 * Set shader parameter for given parameter name
69 * @param shaderId shader id
70 * @param parameterName parameter name
71 * @param paraemterValue parameter value
72 */
73 void setShaderParameter(const string& parameterName, const ShaderParameter& parameterValue);
74
75 /**
76 * Set shader parameter for given parameter name
77 * @param shaderId shader id
78 * @param parameterName parameter name
79 * @param paraemterValue parameter value
80 */
81 void setShaderParameter(const string& parameterName, const string& parameterValueString);
82
83 /**
84 * @return shader parameters hash
85 */
86 const string& getShaderParametersHash() const {
87 if (changed == true) {
88 // TODO: md5 or something
89 hash = shaderId + ";";
90 for (auto& it: parameters) {
91 hash+= it.first;
92 hash+= "=";
93 auto& parameterValue = it.second;
94 switch(parameterValue.getType()) {
96 break;
98 hash+= to_string(parameterValue.getBooleanValue());
99 break;
101 hash+= to_string(parameterValue.getIntegerValue());
102 break;
104 hash+= to_string(static_cast<int>(parameterValue.getFloatValue() * 100.0f));
105 break;
107 {
108 auto& shaderParameterArray = parameterValue.getVector2Value().getArray();
109 for (auto i = 0; i < shaderParameterArray.size(); i++) {
110 if (i != 0) hash+= ",";
111 hash+= to_string(static_cast<int>(shaderParameterArray[i] * 100.0f));
112 }
113 }
114 break;
116 {
117 auto& shaderParameterArray = parameterValue.getVector3Value().getArray();
118 for (auto i = 0; i < shaderParameterArray.size(); i++) {
119 if (i != 0) hash+= ",";
120 hash+= to_string(static_cast<int>(shaderParameterArray[i] * 100.0f));
121 }
122 }
123 break;
125 {
126 auto& shaderParameterArray = parameterValue.getVector4Value().getArray();
127 for (auto i = 0; i < shaderParameterArray.size(); i++) {
128 if (i != 0) hash+= ",";
129 hash+= to_string(static_cast<int>(shaderParameterArray[i] * 100.0f));
130 }
131 }
132 break;
133 default:
134 break;
135 }
136 hash+= ";";
137 }
138 changed = false;
139 }
140 return hash;
141 }
142};
TDME2 engine entity shader parameters.
map< string, ShaderParameter > parameters
void computeHash()
Compute shader parameters hash.
void setShaderParameter(const string &parameterName, const ShaderParameter &parameterValue)
Set shader parameter for given parameter name.
void setShader(const string &shaderId)
Set shader.
const ShaderParameter getShaderParameter(const string &parameterName) const
Returns shader parameter for given parameter name, if the value does not exist, the default will be r...
Shader parameter model class.