{"action":"created","issue":{"url":"https://api.github.com/repos/SFML/SFML/issues/983","labels_url":"https://api.github.com/repos/SFML/SFML/issues/983/labels{/name}","comments_url":"https://api.github.com/repos/SFML/SFML/issues/983/comments","events_url":"https://api.github.com/repos/SFML/SFML/issues/983/events","html_url":"https://github.com/SFML/SFML/pull/983","id":109762915,"number":983,"title":"New API to set shader uniforms","user":{"login":"Bromeon","id":708488,"avatar_url":"https://avatars.githubusercontent.com/u/708488?v=3","gravatar_id":"","url":"https://api.github.com/users/Bromeon","html_url":"https://github.com/Bromeon","followers_url":"https://api.github.com/users/Bromeon/followers","following_url":"https://api.github.com/users/Bromeon/following{/other_user}","gists_url":"https://api.github.com/users/Bromeon/gists{/gist_id}","starred_url":"https://api.github.com/users/Bromeon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bromeon/subscriptions","organizations_url":"https://api.github.com/users/Bromeon/orgs","repos_url":"https://api.github.com/users/Bromeon/repos","events_url":"https://api.github.com/users/Bromeon/events{/privacy}","received_events_url":"https://api.github.com/users/Bromeon/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/SFML/SFML/labels/feature","name":"feature","color":"23BA35"},{"url":"https://api.github.com/repos/SFML/SFML/labels/m:sfml-graphics","name":"m:sfml-graphics","color":"cccccc"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/SFML/SFML/milestones/10","html_url":"https://github.com/SFML/SFML/milestones/2.4","labels_url":"https://api.github.com/repos/SFML/SFML/milestones/10/labels","id":1043773,"number":10,"title":"2.4","description":"","creator":{"login":"eXpl0it3r","id":920861,"avatar_url":"https://avatars.githubusercontent.com/u/920861?v=3","gravatar_id":"","url":"https://api.github.com/users/eXpl0it3r","html_url":"https://github.com/eXpl0it3r","followers_url":"https://api.github.com/users/eXpl0it3r/followers","following_url":"https://api.github.com/users/eXpl0it3r/following{/other_user}","gists_url":"https://api.github.com/users/eXpl0it3r/gists{/gist_id}","starred_url":"https://api.github.com/users/eXpl0it3r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eXpl0it3r/subscriptions","organizations_url":"https://api.github.com/users/eXpl0it3r/orgs","repos_url":"https://api.github.com/users/eXpl0it3r/repos","events_url":"https://api.github.com/users/eXpl0it3r/events{/privacy}","received_events_url":"https://api.github.com/users/eXpl0it3r/received_events","type":"User","site_admin":false},"open_issues":25,"closed_issues":19,"state":"open","created_at":"2015-03-29T21:47:10Z","updated_at":"2015-10-05T09:51:06Z","due_on":null,"closed_at":null},"comments":2,"created_at":"2015-10-05T09:41:40Z","updated_at":"2015-10-05T14:40:15Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/SFML/SFML/pulls/983","html_url":"https://github.com/SFML/SFML/pull/983","diff_url":"https://github.com/SFML/SFML/pull/983.diff","patch_url":"https://github.com/SFML/SFML/pull/983.patch"},"body":"\r\n\r\n*[Link to forum thread](http://en.sfml-dev.org/forums/index.php?topic=18146.0).*\r\n\r\nThis pull request implements https://github.com/SFML/SFML/issues/538 and introduces a new API to set uniform variables in `sf::Shader`. The old `setParameter()` overloads are deprecated in favor of new `setUniform()` ones.\r\n\r\nThe `sf::Glsl` namespace is added to SFML. It contains type definitions for types that can be passed to `Shader::setUniform()`. Most types are `typedef`s to existing SFML types (such as `sf::Vector2<T>`), there are however a couple of new ones:\r\n```cpp\r\nsf::Glsl::Mat3  // 3x3 matrix\r\nsf::Glsl::Mat4  // 4x4 matrix\r\nsf::Glsl::Vec4  // 4D float vector\r\nsf::Glsl::Ivec4 // 4D int vector\r\nsf::Glsl::Bvec4 // 4D bool vector\r\n```\r\nI provided implicit conversions from `sf::Transform` and `sf::Color` to these types where appropriate. Where necessary for ambiguity reasons, an explicit conversion must be used:\r\n```cpp\r\nsf::Color color;\r\nshader.setUniform(\"color\", sf::Glsl::Vec4(color)); // pass as 4D vector\r\nshader.setUniform(\"color\", color); // same call, implicit conversion\r\n\r\nsf::Transform transform;\r\nshader.setUniform(\"matrix\", sf::Glsl::Mat3(transform)); // pass as 3x3\r\nshader.setUniform(\"matrix\", sf::Glsl::Mat4(transform)); // pass as 4x4\r\n```\r\n\r\nIn addition to basic GLSL types, arrays are now supported as well. They can be specified with `Shader::setUniformArray()` that takes a pointer to the first element and the length:\r\n```cpp\r\nsf::Glsl::Vec2 positions[5] = ...;\r\nshader.setUniformArray(\"positions\", positions, 5);\r\n```\r\n\r\n-----\r\n#### Design rationale\r\n\r\nAfter extensive thinking about the design (see 2 forum threads), I came to the conclusion that `setUniform()` overloads are more appropriate than named functions `setUniformFloat()`, for the following reasons:\r\n* The call is simpler and we don't force the user to type redundant information.\r\n* We allow polymorphism (a user can write a template to pass uniforms of any type, without knowing the function names).\r\n* The number of provided methods is smaller. We can work with conversions where possible. For example, we don't need to overload `setUniformMat3()` for `sf::Glsl::Mat3`, `float*` and `sf::Transform` separately, one method taking `sf::Glsl::Mat3` is enough.\r\n* It is still possible to enforce type safety at compile time and at runtime. The former can be achieved with a wrapper function that explicitly requires the type to be specified.\r\n* There is no way around dedicated types anyway, otherwise we can't support GLSL arrays (or only in a very low-level way).\r\n\r\n-----\r\n#### Try it out\r\n\r\nA self-contained archive containing a C++ file, two GLSL shader files and an image can be downloaded temporarily from my homepage: www.bromeon.ch/sfml/feature-shader-uniforms.zip"},"comment":{"url":"https://api.github.com/repos/SFML/SFML/issues/comments/145549632","html_url":"https://github.com/SFML/SFML/pull/983#issuecomment-145549632","issue_url":"https://api.github.com/repos/SFML/SFML/issues/983","id":145549632,"user":{"login":"Bromeon","id":708488,"avatar_url":"https://avatars.githubusercontent.com/u/708488?v=3","gravatar_id":"","url":"https://api.github.com/users/Bromeon","html_url":"https://github.com/Bromeon","followers_url":"https://api.github.com/users/Bromeon/followers","following_url":"https://api.github.com/users/Bromeon/following{/other_user}","gists_url":"https://api.github.com/users/Bromeon/gists{/gist_id}","starred_url":"https://api.github.com/users/Bromeon/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Bromeon/subscriptions","organizations_url":"https://api.github.com/users/Bromeon/orgs","repos_url":"https://api.github.com/users/Bromeon/repos","events_url":"https://api.github.com/users/Bromeon/events{/privacy}","received_events_url":"https://api.github.com/users/Bromeon/received_events","type":"User","site_admin":false},"created_at":"2015-10-05T14:40:15Z","updated_at":"2015-10-05T14:40:15Z","body":"Like this? :smiley: "}}