Browse Source

python: adapt to GL shader renaming.

pull/11/head
Vladimír Vondruš 5 years ago
parent
commit
431074a776
  1. 41
      doc/python/magnum.shaders.rst
  2. 2
      doc/python/pages/changelog.rst
  3. 204
      src/python/magnum/shaders.cpp
  4. 40
      src/python/magnum/test/test_shaders_gl.py

41
doc/python/magnum.shaders.rst

@ -23,43 +23,43 @@
DEALINGS IN THE SOFTWARE.
..
.. py:class:: magnum.shaders.Flat2D
.. py:class:: magnum.shaders.FlatGL2D
:data POSITION: Vertex position
:data TEXTURE_COORDINATES: 2D texture coordinates
:data COLOR3: Three-component vertex color
:data COLOR4: Four-component vertex color
.. py:class:: magnum.shaders.Flat3D
.. py:class:: magnum.shaders.FlatGL3D
:data POSITION: Vertex position
:data TEXTURE_COORDINATES: 2D texture coordinates
:data COLOR3: Three-component vertex color
:data COLOR4: Four-component vertex color
.. py:property:: magnum.shaders.Flat2D.alpha_mask
.. py:property:: magnum.shaders.FlatGL2D.alpha_mask
:raise AttributeError: If the shader was not created with
:ref:`Flags.ALPHA_MASK`
.. py:property:: magnum.shaders.Flat3D.alpha_mask
.. py:property:: magnum.shaders.FlatGL3D.alpha_mask
:raise AttributeError: If the shader was not created with
:ref:`Flags.ALPHA_MASK`
.. py:function:: magnum.shaders.Flat2D.bind_texture
.. py:function:: magnum.shaders.FlatGL2D.bind_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.TEXTURED`
.. py:function:: magnum.shaders.Flat3D.bind_texture
.. py:function:: magnum.shaders.FlatGL3D.bind_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.TEXTURED`
.. py:class:: magnum.shaders.VertexColor2D
.. py:class:: magnum.shaders.VertexColorGL2D
:data POSITION: Vertex position
:data COLOR3: Three-component vertex color
:data COLOR4: Four-component vertex color
.. py:class:: magnum.shaders.VertexColor3D
.. py:class:: magnum.shaders.VertexColorGL3D
:data POSITION: Vertex position
:data COLOR3: Three-component vertex color
:data COLOR4: Four-component vertex color
.. py:class:: magnum.shaders.Phong
.. py:class:: magnum.shaders.PhongGL
:data POSITION: Vertex position
:data NORMAL: Normal direction
:data TANGENT: Tangent direction
@ -72,36 +72,35 @@
:data NORMAL_MATRIX: (Instanced) normal matrix
:data TEXTURE_OFFSET: (Instanced) texture offset
.. py:property:: magnum.shaders.Phong.normal_texture_scale
.. py:property:: magnum.shaders.PhongGL.normal_texture_scale
:raise AttributeError: If the shader was not created with
:ref:`Flags.NORMAL_TEXTURE`
.. py:property:: magnum.shaders.Phong.alpha_mask
.. py:property:: magnum.shaders.PhongGL.alpha_mask
:raise AttributeError: If the shader was not created with
:ref:`Flags.ALPHA_MASK`
.. py:property:: magnum.shaders.Phong.texture_matrix
.. py:property:: magnum.shaders.PhongGL.texture_matrix
:raise AttributeError: If the shader was not created with
:ref:`Flags.TEXTURE_TRANSFORMATION`
.. py:property:: magnum.shaders.Phong.light_positions
.. py:property:: magnum.shaders.PhongGL.light_positions
:raise ValueError: If list length is different from :ref:`light_count`
.. py:property:: magnum.shaders.Phong.light_colors
.. py:property:: magnum.shaders.PhongGL.light_colors
:raise ValueError: If list length is different from :ref:`light_count`
.. py:property:: magnum.shaders.Phong.light_ranges
.. py:property:: magnum.shaders.PhongGL.light_ranges
:raise ValueError: If list length is different from :ref:`light_count`
.. py:function:: magnum.shaders.Phong.bind_ambient_texture
.. py:function:: magnum.shaders.PhongGL.bind_ambient_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.AMBIENT_TEXTURE`
.. py:function:: magnum.shaders.Phong.bind_diffuse_texture
.. py:function:: magnum.shaders.PhongGL.bind_diffuse_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.DIFFUSE_TEXTURE`
.. py:function:: magnum.shaders.Phong.bind_specular_texture
.. py:function:: magnum.shaders.PhongGL.bind_specular_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.SPECULAR_TEXTURE`
.. py:function:: magnum.shaders.Phong.bind_normal_texture
.. py:function:: magnum.shaders.PhongGL.bind_normal_texture
:raise AttributeError: If the shader was not created with
:ref:`Flags.NORMAL_TEXTURE`
.. py:function:: magnum.shaders.Phong.bind_textures
.. py:function:: magnum.shaders.PhongGL.bind_textures
:raise AttributeError: If the shader was not created with any of
:ref:`Flags.AMBIENT_TEXTURE`, :ref:`Flags.DIFFUSE_TEXTURE`,
:ref:`Flags.SPECULAR_TEXTURE` or :ref:`Flags.NORMAL_TEXTURE`

2
doc/python/pages/changelog.rst

@ -44,7 +44,7 @@ Changelog
- Exposed :ref:`gl.Renderer.Feature.CLIP_DISTANCEn <gl.Renderer.Feature.CLIP_DISTANCE0>`
values that are new since 2020.06
- Exposed new instancing, normal-mapping-related and lighting features in
:ref:`shaders.Phong`
:ref:`shaders.PhongGL`
- Renamed all helper ``Python.h`` headers to ``PythonBindings.h`` to avoid
issues with shitty IDE indexers such as Eclipse, confusing these with
Python's ``<Python.h>``

204
src/python/magnum/shaders.cpp

@ -31,9 +31,9 @@
#include <Magnum/Math/Color.h>
#include <Magnum/Math/Matrix3.h>
#include <Magnum/Math/Matrix4.h>
#include <Magnum/Shaders/Flat.h>
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Shaders/VertexColor.h>
#include <Magnum/Shaders/FlatGL.h>
#include <Magnum/Shaders/PhongGL.h>
#include <Magnum/Shaders/VertexColorGL.h>
#include "Corrade/PythonBindings.h"
@ -48,34 +48,34 @@ template<class T> void anyShader(PyNonDestructibleClass<T, GL::AbstractShaderPro
c.def("draw", static_cast<void(GL::AbstractShaderProgram::*)(GL::Mesh&)>(&GL::AbstractShaderProgram::draw), "Draw a mesh");
}
template<UnsignedInt dimensions> void flat(PyNonDestructibleClass<Shaders::Flat<dimensions>, GL::AbstractShaderProgram>& c) {
template<UnsignedInt dimensions> void flat(PyNonDestructibleClass<Shaders::FlatGL<dimensions>, GL::AbstractShaderProgram>& c) {
/* Attributes */
c.attr("TEXTURE_COORDINATES") = GL::DynamicAttribute{typename Shaders::Flat<dimensions>::TextureCoordinates{}};
c.attr("COLOR3") = GL::DynamicAttribute{typename Shaders::Flat<dimensions>::Color3{}};
c.attr("COLOR4") = GL::DynamicAttribute{typename Shaders::Flat<dimensions>::Color4{}};
c.attr("TEXTURE_COORDINATES") = GL::DynamicAttribute{typename Shaders::FlatGL<dimensions>::TextureCoordinates{}};
c.attr("COLOR3") = GL::DynamicAttribute{typename Shaders::FlatGL<dimensions>::Color3{}};
c.attr("COLOR4") = GL::DynamicAttribute{typename Shaders::FlatGL<dimensions>::Color4{}};
/* Methods */
c
.def(py::init<typename Shaders::Flat<dimensions>::Flag>(), "Constructor",
py::arg("flags") = typename Shaders::Flat<dimensions>::Flag{})
.def(py::init<typename Shaders::FlatGL<dimensions>::Flag>(), "Constructor",
py::arg("flags") = typename Shaders::FlatGL<dimensions>::Flag{})
/* Using lambdas to avoid method chaining getting into signatures */
.def_property_readonly("flags", [](Shaders::Flat<dimensions>& self) {
return typename Shaders::Flat<dimensions>::Flag(UnsignedByte(self.flags()));
.def_property_readonly("flags", [](Shaders::FlatGL<dimensions>& self) {
return typename Shaders::FlatGL<dimensions>::Flag(UnsignedByte(self.flags()));
}, "Flags")
.def_property("transformation_projection_matrix", nullptr, &Shaders::Flat<dimensions>::setTransformationProjectionMatrix,
.def_property("transformation_projection_matrix", nullptr, &Shaders::FlatGL<dimensions>::setTransformationProjectionMatrix,
"Transformation and projection matrix")
.def_property("color", nullptr, &Shaders::Flat<dimensions>::setColor, "Color")
.def_property("alpha_mask", nullptr, [](Shaders::Flat<dimensions>& self, Float mask) {
if(!(self.flags() & Shaders::Flat<dimensions>::Flag::AlphaMask)) {
.def_property("color", nullptr, &Shaders::FlatGL<dimensions>::setColor, "Color")
.def_property("alpha_mask", nullptr, [](Shaders::FlatGL<dimensions>& self, Float mask) {
if(!(self.flags() & Shaders::FlatGL<dimensions>::Flag::AlphaMask)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with alpha mask enabled");
throw py::error_already_set{};
}
self.setAlphaMask(mask);
}, "Alpha mask")
.def("bind_texture", [](Shaders::Flat<dimensions>& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::Flat<dimensions>::Flag::Textured)) {
.def("bind_texture", [](Shaders::FlatGL<dimensions>& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::FlatGL<dimensions>::Flag::Textured)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with texturing enabled");
throw py::error_already_set{};
}
@ -86,10 +86,10 @@ template<UnsignedInt dimensions> void flat(PyNonDestructibleClass<Shaders::Flat<
anyShader(c);
}
template<UnsignedInt dimensions> void vertexColor(PyNonDestructibleClass<Shaders::VertexColor<dimensions>, GL::AbstractShaderProgram>& c) {
template<UnsignedInt dimensions> void vertexColor(PyNonDestructibleClass<Shaders::VertexColorGL<dimensions>, GL::AbstractShaderProgram>& c) {
/* Attributes */
c.attr("COLOR3") = GL::DynamicAttribute{typename Shaders::VertexColor<dimensions>::Color3{}};
c.attr("COLOR4") = GL::DynamicAttribute{typename Shaders::VertexColor<dimensions>::Color4{}};
c.attr("COLOR3") = GL::DynamicAttribute{typename Shaders::VertexColorGL<dimensions>::Color3{}};
c.attr("COLOR4") = GL::DynamicAttribute{typename Shaders::VertexColorGL<dimensions>::Color4{}};
/* Methods */
c
@ -97,7 +97,7 @@ template<UnsignedInt dimensions> void vertexColor(PyNonDestructibleClass<Shaders
/* Using lambdas to avoid method chaining getting into signatures */
.def_property("transformation_projection_matrix", nullptr, &Shaders::VertexColor<dimensions>::setTransformationProjectionMatrix,
.def_property("transformation_projection_matrix", nullptr, &Shaders::VertexColorGL<dimensions>::setTransformationProjectionMatrix,
"Transformation and projection matrix");
anyShader(c);
@ -116,28 +116,28 @@ void shaders(py::module_& m) {
/* 2D/3D flat shader */
{
PyNonDestructibleClass<Shaders::Flat2D, GL::AbstractShaderProgram> flat2D{m,
"Flat2D", "2D flat shader"};
PyNonDestructibleClass<Shaders::Flat3D, GL::AbstractShaderProgram> flat3D{m,
"Flat3D", "3D flat shader"};
flat2D.attr("POSITION") = GL::DynamicAttribute{Shaders::Flat2D::Position{}};
flat3D.attr("POSITION") = GL::DynamicAttribute{Shaders::Flat3D::Position{}};
PyNonDestructibleClass<Shaders::FlatGL2D, GL::AbstractShaderProgram> flatGL2D{m,
"FlatGL2D", "2D flat OpenGL shader"};
PyNonDestructibleClass<Shaders::FlatGL3D, GL::AbstractShaderProgram> flatGL3D{m,
"FlatGL3D", "3D flat shader"};
flatGL2D.attr("POSITION") = GL::DynamicAttribute{Shaders::FlatGL2D::Position{}};
flatGL3D.attr("POSITION") = GL::DynamicAttribute{Shaders::FlatGL3D::Position{}};
/* The flags are currently the same type for both 2D and 3D and pybind
doesn't want to have a single type registered twice, so doing it
this way instead */
py::enum_<Shaders::Flat2D::Flag> flags{flat2D, "Flags", "Flags"};
py::enum_<Shaders::FlatGL2D::Flag> flags{flatGL2D, "Flags", "Flags"};
flags
.value("TEXTURED", Shaders::Flat2D::Flag::Textured)
.value("ALPHA_MASK", Shaders::Flat2D::Flag::AlphaMask)
.value("VERTEX_COLOR", Shaders::Flat3D::Flag::AlphaMask)
.value("NONE", Shaders::Flat3D::Flag{})
.value("TEXTURED", Shaders::FlatGL2D::Flag::Textured)
.value("ALPHA_MASK", Shaders::FlatGL2D::Flag::AlphaMask)
.value("VERTEX_COLOR", Shaders::FlatGL3D::Flag::AlphaMask)
.value("NONE", Shaders::FlatGL3D::Flag{})
/* TODO: OBJECT_ID, once multiple FB outputs and mapDraw is exposed */
;
flat3D.attr("Flags") = flags;
flatGL3D.attr("Flags") = flags;
flat(flat2D);
flat(flat3D);
flat(flatGL2D);
flat(flatGL3D);
corrade::enumOperators(flags);
@ -145,79 +145,79 @@ void shaders(py::module_& m) {
/* 2D/3D vertex color shader */
{
PyNonDestructibleClass<Shaders::VertexColor2D, GL::AbstractShaderProgram> vertexColor2D{m,
"VertexColor2D", "2D vertex color shader"};
PyNonDestructibleClass<Shaders::VertexColor3D, GL::AbstractShaderProgram> vertexColor3D{m,
"VertexColor3D", "3D vertex color shader"};
vertexColor2D.attr("POSITION") = GL::DynamicAttribute{Shaders::VertexColor2D::Position{}};
vertexColor3D.attr("POSITION") = GL::DynamicAttribute{Shaders::VertexColor3D::Position{}};
vertexColor(vertexColor2D);
vertexColor(vertexColor3D);
PyNonDestructibleClass<Shaders::VertexColorGL2D, GL::AbstractShaderProgram> vertexColorGL2D{m,
"VertexColorGL2D", "2D vertex color shader"};
PyNonDestructibleClass<Shaders::VertexColorGL3D, GL::AbstractShaderProgram> vertexColorGL3D{m,
"VertexColorGL3D", "3D vertex color shader"};
vertexColorGL2D.attr("POSITION") = GL::DynamicAttribute{Shaders::VertexColorGL2D::Position{}};
vertexColorGL3D.attr("POSITION") = GL::DynamicAttribute{Shaders::VertexColorGL3D::Position{}};
vertexColor(vertexColorGL2D);
vertexColor(vertexColorGL3D);
}
/* Phong shader */
{
PyNonDestructibleClass<Shaders::Phong, GL::AbstractShaderProgram> phong{m,
"Phong", "Phong shader"};
phong.attr("POSITION") = GL::DynamicAttribute{Shaders::Phong::Position{}};
phong.attr("NORMAL") = GL::DynamicAttribute{Shaders::Phong::Normal{}};
phong.attr("TANGENT") = GL::DynamicAttribute{Shaders::Phong::Tangent{}};
phong.attr("TANGENT4") = GL::DynamicAttribute{Shaders::Phong::Tangent4{}};
phong.attr("BITANGENT") = GL::DynamicAttribute{Shaders::Phong::Bitangent{}};
phong.attr("TEXTURE_COORDINATES") = GL::DynamicAttribute{Shaders::Phong::TextureCoordinates{}};
phong.attr("COLOR3") = GL::DynamicAttribute{Shaders::Phong::Color3{}};
phong.attr("COLOR4") = GL::DynamicAttribute{Shaders::Phong::Color4{}};
PyNonDestructibleClass<Shaders::PhongGL, GL::AbstractShaderProgram> phongGL{m,
"PhongGL", "Phong shader"};
phongGL.attr("POSITION") = GL::DynamicAttribute{Shaders::PhongGL::Position{}};
phongGL.attr("NORMAL") = GL::DynamicAttribute{Shaders::PhongGL::Normal{}};
phongGL.attr("TANGENT") = GL::DynamicAttribute{Shaders::PhongGL::Tangent{}};
phongGL.attr("TANGENT4") = GL::DynamicAttribute{Shaders::PhongGL::Tangent4{}};
phongGL.attr("BITANGENT") = GL::DynamicAttribute{Shaders::PhongGL::Bitangent{}};
phongGL.attr("TEXTURE_COORDINATES") = GL::DynamicAttribute{Shaders::PhongGL::TextureCoordinates{}};
phongGL.attr("COLOR3") = GL::DynamicAttribute{Shaders::PhongGL::Color3{}};
phongGL.attr("COLOR4") = GL::DynamicAttribute{Shaders::PhongGL::Color4{}};
/* TODO: OBJECT_ID attribute, once multiple FB outputs and mapDraw is
exposed */
phong.attr("TRANSFORMATION_MATRIX") = GL::DynamicAttribute{Shaders::Phong::TransformationMatrix{}};
phong.attr("NORMAL_MATRIX") = GL::DynamicAttribute{Shaders::Phong::NormalMatrix{}};
phong.attr("TEXTURE_OFFSET") = GL::DynamicAttribute{Shaders::Phong::TextureOffset{}};
phongGL.attr("TRANSFORMATION_MATRIX") = GL::DynamicAttribute{Shaders::PhongGL::TransformationMatrix{}};
phongGL.attr("NORMAL_MATRIX") = GL::DynamicAttribute{Shaders::PhongGL::NormalMatrix{}};
phongGL.attr("TEXTURE_OFFSET") = GL::DynamicAttribute{Shaders::PhongGL::TextureOffset{}};
py::enum_<Shaders::Phong::Flag> flags{phong, "Flags", "Flags"};
py::enum_<Shaders::PhongGL::Flag> flags{phongGL, "Flags", "Flags"};
flags
.value("AMBIENT_TEXTURE", Shaders::Phong::Flag::AmbientTexture)
.value("DIFFUSE_TEXTURE", Shaders::Phong::Flag::DiffuseTexture)
.value("SPECULAR_TEXTURE", Shaders::Phong::Flag::SpecularTexture)
.value("NORMAL_TEXTURE", Shaders::Phong::Flag::NormalTexture)
.value("ALPHA_MASK", Shaders::Phong::Flag::AlphaMask)
.value("VERTEX_COLOR", Shaders::Phong::Flag::VertexColor)
.value("BITANGENT", Shaders::Phong::Flag::Bitangent)
.value("TEXTURE_TRANSFORMATION", Shaders::Phong::Flag::TextureTransformation)
.value("AMBIENT_TEXTURE", Shaders::PhongGL::Flag::AmbientTexture)
.value("DIFFUSE_TEXTURE", Shaders::PhongGL::Flag::DiffuseTexture)
.value("SPECULAR_TEXTURE", Shaders::PhongGL::Flag::SpecularTexture)
.value("NORMAL_TEXTURE", Shaders::PhongGL::Flag::NormalTexture)
.value("ALPHA_MASK", Shaders::PhongGL::Flag::AlphaMask)
.value("VERTEX_COLOR", Shaders::PhongGL::Flag::VertexColor)
.value("BITANGENT", Shaders::PhongGL::Flag::Bitangent)
.value("TEXTURE_TRANSFORMATION", Shaders::PhongGL::Flag::TextureTransformation)
/* TODO: OBJECT_ID, once multiple FB outputs and mapDraw is exposed */
.value("INSTANCED_TRANSFORMATION", Shaders::Phong::Flag::InstancedTransformation)
.value("INSTANCED_TEXTURE_OFFSET", Shaders::Phong::Flag::InstancedTextureOffset)
.value("NONE", Shaders::Phong::Flag{})
.value("INSTANCED_TRANSFORMATION", Shaders::PhongGL::Flag::InstancedTransformation)
.value("INSTANCED_TEXTURE_OFFSET", Shaders::PhongGL::Flag::InstancedTextureOffset)
.value("NONE", Shaders::PhongGL::Flag{})
;
corrade::enumOperators(flags);
phong
.def(py::init<Shaders::Phong::Flag, UnsignedInt>(), "Constructor",
py::arg("flags") = Shaders::Phong::Flag{},
phongGL
.def(py::init<Shaders::PhongGL::Flag, UnsignedInt>(), "Constructor",
py::arg("flags") = Shaders::PhongGL::Flag{},
py::arg("light_count") = 1)
.def_property_readonly("flags", [](Shaders::Phong& self) {
return Shaders::Phong::Flag(UnsignedShort(self.flags()));
.def_property_readonly("flags", [](Shaders::PhongGL& self) {
return Shaders::PhongGL::Flag(UnsignedShort(self.flags()));
}, "Flags")
.def_property_readonly("light_count", &Shaders::Phong::lightCount,
.def_property_readonly("light_count", &Shaders::PhongGL::lightCount,
"Light count")
.def_property("ambient_color", nullptr,
&Shaders::Phong::setAmbientColor, "Ambient color")
&Shaders::PhongGL::setAmbientColor, "Ambient color")
.def_property("diffuse_color", nullptr,
&Shaders::Phong::setDiffuseColor, "Diffuse color")
&Shaders::PhongGL::setDiffuseColor, "Diffuse color")
.def_property("specular_color", nullptr,
&Shaders::Phong::setSpecularColor, "Specular color")
&Shaders::PhongGL::setSpecularColor, "Specular color")
.def_property("shininess", nullptr,
&Shaders::Phong::setShininess, "Shininess")
.def_property("normal_texture_scale", nullptr, [](Shaders::Phong& self, Float scale) {
if(!(self.flags() & Shaders::Phong::Flag::NormalTexture)) {
&Shaders::PhongGL::setShininess, "Shininess")
.def_property("normal_texture_scale", nullptr, [](Shaders::PhongGL& self, Float scale) {
if(!(self.flags() & Shaders::PhongGL::Flag::NormalTexture)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with normal texture enabled");
throw py::error_already_set{};
}
self.setNormalTextureScale(scale);
}, "Normal texture scale")
.def_property("alpha_mask", nullptr, [](Shaders::Phong& self, Float mask) {
if(!(self.flags() & Shaders::Phong::Flag::AlphaMask)) {
.def_property("alpha_mask", nullptr, [](Shaders::PhongGL& self, Float mask) {
if(!(self.flags() & Shaders::PhongGL::Flag::AlphaMask)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with alpha mask enabled");
throw py::error_already_set{};
}
@ -225,20 +225,20 @@ void shaders(py::module_& m) {
self.setAlphaMask(mask);
}, "Alpha mask")
.def_property("transformation_matrix", nullptr,
&Shaders::Phong::setTransformationMatrix, "Transformation matrix")
&Shaders::PhongGL::setTransformationMatrix, "Transformation matrix")
.def_property("normal_matrix", nullptr,
&Shaders::Phong::setNormalMatrix, "Normal matrix")
&Shaders::PhongGL::setNormalMatrix, "Normal matrix")
.def_property("projection_matrix", nullptr,
&Shaders::Phong::setProjectionMatrix, "Projection matrix")
.def_property("texture_matrix", nullptr, [](Shaders::Phong& self, const Matrix3& matrix) {
if(!(self.flags() & Shaders::Phong::Flag::TextureTransformation)) {
&Shaders::PhongGL::setProjectionMatrix, "Projection matrix")
.def_property("texture_matrix", nullptr, [](Shaders::PhongGL& self, const Matrix3& matrix) {
if(!(self.flags() & Shaders::PhongGL::Flag::TextureTransformation)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with texture transformation enabled");
throw py::error_already_set{};
}
self.setTextureMatrix(matrix);
}, "Texture matrix")
.def_property("light_positions", nullptr, [](Shaders::Phong& self, const std::vector<Vector4>& positions) {
.def_property("light_positions", nullptr, [](Shaders::PhongGL& self, const std::vector<Vector4>& positions) {
if(positions.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(positions.size()));
throw py::error_already_set{};
@ -246,7 +246,7 @@ void shaders(py::module_& m) {
self.setLightPositions(positions);
}, "Light positions")
.def_property("light_colors", nullptr, [](Shaders::Phong& self, const std::vector<Color3>& colors) {
.def_property("light_colors", nullptr, [](Shaders::PhongGL& self, const std::vector<Color3>& colors) {
if(colors.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(colors.size()));
throw py::error_already_set{};
@ -254,7 +254,7 @@ void shaders(py::module_& m) {
self.setLightColors(colors);
}, "Light colors")
.def_property("light_specular_colors", nullptr, [](Shaders::Phong& self, const std::vector<Color3>& colors) {
.def_property("light_specular_colors", nullptr, [](Shaders::PhongGL& self, const std::vector<Color3>& colors) {
if(colors.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(colors.size()));
throw py::error_already_set{};
@ -262,7 +262,7 @@ void shaders(py::module_& m) {
self.setLightSpecularColors(colors);
}, "Light specular colors")
.def_property("light_ranges", nullptr, [](Shaders::Phong& self, const std::vector<Float>& ranges) {
.def_property("light_ranges", nullptr, [](Shaders::PhongGL& self, const std::vector<Float>& ranges) {
if(ranges.size() != self.lightCount()) {
PyErr_Format(PyExc_ValueError, "expected %u items but got %u", self.lightCount(), UnsignedInt(ranges.size()));
throw py::error_already_set{};
@ -271,40 +271,40 @@ void shaders(py::module_& m) {
self.setLightRanges(ranges);
}, "Light ranges")
.def("bind_ambient_texture", [](Shaders::Phong& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::Phong::Flag::AmbientTexture)) {
.def("bind_ambient_texture", [](Shaders::PhongGL& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::PhongGL::Flag::AmbientTexture)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with ambient texture enabled");
throw py::error_already_set{};
}
self.bindAmbientTexture(texture);
}, "Bind an ambient texture")
.def("bind_diffuse_texture", [](Shaders::Phong& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::Phong::Flag::DiffuseTexture)) {
.def("bind_diffuse_texture", [](Shaders::PhongGL& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::PhongGL::Flag::DiffuseTexture)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with diffuse texture enabled");
throw py::error_already_set{};
}
self.bindDiffuseTexture(texture);
}, "Bind a diffuse texture")
.def("bind_specular_texture", [](Shaders::Phong& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::Phong::Flag::SpecularTexture)) {
.def("bind_specular_texture", [](Shaders::PhongGL& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::PhongGL::Flag::SpecularTexture)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with specular texture enabled");
throw py::error_already_set{};
}
self.bindSpecularTexture(texture);
}, "Bind a specular texture")
.def("bind_normal_texture", [](Shaders::Phong& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::Phong::Flag::NormalTexture)) {
.def("bind_normal_texture", [](Shaders::PhongGL& self, GL::Texture2D& texture) {
if(!(self.flags() & Shaders::PhongGL::Flag::NormalTexture)) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with normal texture enabled");
throw py::error_already_set{};
}
self.bindNormalTexture(texture);
}, "Bind a normal texture")
.def("bind_textures", [](Shaders::Phong& self, GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal) {
if(!(self.flags() & (Shaders::Phong::Flag::AmbientTexture|Shaders::Phong::Flag::DiffuseTexture|Shaders::Phong::Flag::SpecularTexture|Shaders::Phong::Flag::NormalTexture))) {
.def("bind_textures", [](Shaders::PhongGL& self, GL::Texture2D* ambient, GL::Texture2D* diffuse, GL::Texture2D* specular, GL::Texture2D* normal) {
if(!(self.flags() & (Shaders::PhongGL::Flag::AmbientTexture|Shaders::PhongGL::Flag::DiffuseTexture|Shaders::PhongGL::Flag::SpecularTexture|Shaders::PhongGL::Flag::NormalTexture))) {
PyErr_SetString(PyExc_AttributeError, "the shader was not created with any textures enabled");
throw py::error_already_set{};
}
@ -313,7 +313,7 @@ void shaders(py::module_& m) {
}, "Bind textures", py::arg("ambient") = nullptr, py::arg("diffuse") = nullptr, py::arg("specular") = nullptr, py::arg("normal") = nullptr)
;
anyShader(phong);
anyShader(phongGL);
}
}

40
src/python/magnum/test/test_shaders_gl.py

@ -32,16 +32,16 @@ from . import GLTestCase, setUpModule
from magnum import *
from magnum import gl, shaders
class Flat(GLTestCase):
class FlatGL(GLTestCase):
def test_init(self):
a = shaders.Flat3D()
self.assertEqual(a.flags, shaders.Flat3D.Flags.NONE)
a = shaders.FlatGL3D()
self.assertEqual(a.flags, shaders.FlatGL3D.Flags.NONE)
b = shaders.Flat3D(shaders.Flat3D.Flags.TEXTURED|shaders.Flat3D.Flags.ALPHA_MASK)
self.assertEqual(b.flags, shaders.Flat3D.Flags.TEXTURED|shaders.Flat3D.Flags.ALPHA_MASK)
b = shaders.FlatGL3D(shaders.FlatGL3D.Flags.TEXTURED|shaders.FlatGL3D.Flags.ALPHA_MASK)
self.assertEqual(b.flags, shaders.FlatGL3D.Flags.TEXTURED|shaders.FlatGL3D.Flags.ALPHA_MASK)
def test_uniforms_bindings(self):
a = shaders.Flat3D(shaders.Flat3D.Flags.TEXTURED|shaders.Flat3D.Flags.ALPHA_MASK)
a = shaders.FlatGL3D(shaders.FlatGL3D.Flags.TEXTURED|shaders.FlatGL3D.Flags.ALPHA_MASK)
a.color = (0.5, 1.0, 0.9)
a.transformation_projection_matrix = Matrix4.translation(Vector3.x_axis())
a.alpha_mask = 0.3
@ -51,7 +51,7 @@ class Flat(GLTestCase):
a.bind_texture(texture)
def test_uniforms_bindings_errors(self):
a = shaders.Flat2D()
a = shaders.FlatGL2D()
with self.assertRaisesRegex(AttributeError, "the shader was not created with alpha mask enabled"):
a.alpha_mask = 0.3
@ -59,33 +59,33 @@ class Flat(GLTestCase):
with self.assertRaisesRegex(AttributeError, "the shader was not created with texturing enabled"):
a.bind_texture(texture)
class VertexColor(GLTestCase):
class VertexColorGL(GLTestCase):
def test_init(self):
a = shaders.VertexColor2D()
b = shaders.VertexColor3D()
a = shaders.VertexColorGL2D()
b = shaders.VertexColorGL3D()
def test_uniforms(self):
a = shaders.VertexColor2D()
a = shaders.VertexColorGL2D()
a.transformation_projection_matrix = (
Matrix3.translation(Vector2.x_axis())@
Matrix3.rotation(Deg(35.0)))
class Phong(GLTestCase):
class PhongGL(GLTestCase):
def test_init(self):
a = shaders.Phong()
self.assertEqual(a.flags, shaders.Phong.Flags.NONE)
a = shaders.PhongGL()
self.assertEqual(a.flags, shaders.PhongGL.Flags.NONE)
self.assertEqual(a.light_count, 1)
b = shaders.Phong(shaders.Phong.Flags.DIFFUSE_TEXTURE|shaders.Phong.Flags.ALPHA_MASK)
self.assertEqual(b.flags, shaders.Phong.Flags.DIFFUSE_TEXTURE|shaders.Phong.Flags.ALPHA_MASK)
b = shaders.PhongGL(shaders.PhongGL.Flags.DIFFUSE_TEXTURE|shaders.PhongGL.Flags.ALPHA_MASK)
self.assertEqual(b.flags, shaders.PhongGL.Flags.DIFFUSE_TEXTURE|shaders.PhongGL.Flags.ALPHA_MASK)
self.assertEqual(b.light_count, 1)
c = shaders.Phong(shaders.Phong.Flags.NONE, 3)
self.assertEqual(c.flags, shaders.Phong.Flags.NONE)
c = shaders.PhongGL(shaders.PhongGL.Flags.NONE, 3)
self.assertEqual(c.flags, shaders.PhongGL.Flags.NONE)
self.assertEqual(c.light_count, 3)
def test_uniforms_bindings(self):
a = shaders.Phong(shaders.Phong.Flags.ALPHA_MASK|shaders.Phong.Flags.AMBIENT_TEXTURE|shaders.Phong.Flags.DIFFUSE_TEXTURE|shaders.Phong.Flags.SPECULAR_TEXTURE|shaders.Phong.Flags.NORMAL_TEXTURE|shaders.Phong.Flags.TEXTURE_TRANSFORMATION, 2)
a = shaders.PhongGL(shaders.PhongGL.Flags.ALPHA_MASK|shaders.PhongGL.Flags.AMBIENT_TEXTURE|shaders.PhongGL.Flags.DIFFUSE_TEXTURE|shaders.PhongGL.Flags.SPECULAR_TEXTURE|shaders.PhongGL.Flags.NORMAL_TEXTURE|shaders.PhongGL.Flags.TEXTURE_TRANSFORMATION, 2)
a.diffuse_color = (0.5, 1.0, 0.9)
a.transformation_matrix = Matrix4.translation(Vector3.x_axis())
a.projection_matrix = Matrix4.zero_init()
@ -106,7 +106,7 @@ class Phong(GLTestCase):
a.bind_textures(ambient=texture, diffuse=texture, specular=texture, normal=texture)
def test_uniforms_bindings_errors(self):
a = shaders.Phong()
a = shaders.PhongGL()
with self.assertRaisesRegex(AttributeError, "the shader was not created with normal texture enabled"):
a.normal_texture_scale = 0.3
with self.assertRaisesRegex(AttributeError, "the shader was not created with alpha mask enabled"):

Loading…
Cancel
Save