I've been learning Rust recently and am working on a 3d engine using glfw3 and gl. I've got as far as trying to get a shader program that worked in my C++ engine working on my Rust one but have hit a snag on the implementation of setting shader uniforms.
shader.h:
template <typename T> void SetUniform(const GLchar* name, const T& data);
shader.cpp
template <> void ShaderProgram::SetUniform<int>(const GLchar* name, const int& data) {
glUniform1i(GetUniformLocation(name), data);
}
template <> void ShaderProgram::SetUniform<float>(const GLchar* name, const float& data) {
glUniform1f(GetUniformLocation(name), data);
}
template <> void ShaderProgram::SetUniform(const GLchar* name, const glm::vec2& data) {
glUniform2f(GetUniformLocation(name), data.x, data.y);
}
//etc...
With my limited knowledge of Rust, I tried using a match statement, as it has so far saved me a lot of hassle, to no avail.
pub fn set_uniform<T>(&mut self, value: T){
match T {
u32 => {/*insert gl call here*/},
f32 => {/*insert gl call here*/},
//etc...
_ => {panic!()}
}
}
I also came across traits, but had no luck trying to tweak them to do what I needed. I'd hoped it would have been something like this although I understand the syntax is very wonky.
trait SetUniform<T, Self = ShaderProgram> {
fn set_uniform(self: &mut Self, value: T)
where
T: Sized;
}
Is there any way to do this with just one function name that can take in any type of parameter or should I just give in and make a bunch of differently named set_uniform functions for each different type?
X
which has specific functions (unimplemented) and thenimpl X<Y>
for a givenY
. In your case it's likelyimpl SetUniform<u32> for ...
– Cynicmatch
works on values, not types, so you'll never be able to use it for that. – Oliviero