Browse Source

python: expose the (almost) full Phong shader.

Except textures.
pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
0db853a483
  1. 4
      doc/python/conf.py
  2. 71
      src/python/magnum/shaders.cpp
  3. 22
      src/python/magnum/test/test_shaders_gl.py

4
doc/python/conf.py

@ -32,6 +32,10 @@ magnum.shaders.VertexColor3D.__annotations__ = {}
magnum.shaders.VertexColor3D.__annotations__['POSITION'] = magnum.gl.Attribute
magnum.shaders.VertexColor3D.__annotations__['COLOR3'] = magnum.gl.Attribute
magnum.shaders.VertexColor3D.__annotations__['COLOR4'] = magnum.gl.Attribute
magnum.shaders.Phong.__annotations__ = {}
magnum.shaders.Phong.__annotations__['POSITION'] = magnum.gl.Attribute
magnum.shaders.Phong.__annotations__['TEXTURE_COORDINATES'] = magnum.gl.Attribute
magnum.shaders.Phong.__annotations__['NORMAL'] = magnum.gl.Attribute
PROJECT_TITLE = 'Magnum'
PROJECT_SUBTITLE = 'Python docs'

71
src/python/magnum/shaders.cpp

@ -24,8 +24,12 @@
*/
#include <pybind11/pybind11.h>
#include <pybind11/stl.h> /* for vector arguments */
#include <Corrade/Containers/ArrayViewStl.h>
#include <Magnum/Shaders/Phong.h>
#include <Magnum/Shaders/VertexColor.h>
#include "corrade/EnumOperators.h"
#include "magnum/bootstrap.h"
#include "magnum/NonDestructible.h"
@ -76,6 +80,73 @@ void shaders(py::module& m) {
vertexColor(vertexColor2D);
vertexColor(vertexColor3D);
}
/* Phong shader */
{
NonDestructibleBase<Shaders::Phong, GL::AbstractShaderProgram> phong{m,
"Phong", "Phong shader"};
phong.attr("POSITION") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 0,
GL::DynamicAttribute::Components::Three,
GL::DynamicAttribute::DataType::Float};
phong.attr("TEXTURE_COORDINATES") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 1,
GL::DynamicAttribute::Components::Two,
GL::DynamicAttribute::DataType::Float};
phong.attr("NORMAL") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 2,
GL::DynamicAttribute::Components::Three,
GL::DynamicAttribute::DataType::Float};
py::enum_<Shaders::Phong::Flag> flags{phong, "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("ALPHA_MASK", Shaders::Phong::Flag::AlphaMask)
.value("NONE", Shaders::Phong::Flag{});
corrade::enumOperators(flags);
phong
.def(py::init<Shaders::Phong::Flag, UnsignedInt>(), "Constructor",
py::arg("flags") = Shaders::Phong::Flag{},
py::arg("light_count") = 1)
.def_property_readonly("flags", [](Shaders::Phong& self) {
return Shaders::Phong::Flag(UnsignedByte(self.flags()));
}, "Flags")
.def_property_readonly("light_count", &Shaders::Phong::lightCount,
"Light count")
// TODO: make write-only once https://github.com/pybind/pybind11/pull/1144
// is released
.def_property("ambient_color", [](Shaders::Phong&) { return Color4{}; },
&Shaders::Phong::setAmbientColor, "Ambient color")
.def_property("diffuse_color", [](Shaders::Phong&) { return Color4{}; },
&Shaders::Phong::setDiffuseColor, "Diffuse color")
.def_property("specular_color", [](Shaders::Phong&) { return Color4{}; },
&Shaders::Phong::setSpecularColor, "Specular color")
// TODO: textures, once exposed
.def_property("shininess", [](Shaders::Phong&) { return Float{}; },
&Shaders::Phong::setShininess, "Shininess")
.def_property("alpha_mask", [](Shaders::Phong&) { return Float{}; },
&Shaders::Phong::setAlphaMask, "Alpha mask")
.def_property("transformation_matrix", [](Shaders::Phong&) { return Matrix4{}; },
&Shaders::Phong::setTransformationMatrix, "Set transformation matrix")
.def_property("normal_matrix", [](Shaders::Phong&) { return Matrix3x3{}; },
&Shaders::Phong::setNormalMatrix, "Set normal matrix")
.def_property("projection_matrix", [](Shaders::Phong&) { return Matrix4{}; },
&Shaders::Phong::setProjectionMatrix, "Set projection matrix")
.def_property("light_positions", [](Shaders::Phong&) {
return std::vector<Vector3>{};
}, [](Shaders::Phong& self, const std::vector<Vector3>& positions) {
self.setLightPositions(positions);
}, "Light positions")
.def_property("light_colors", [](Shaders::Phong&) {
return std::vector<Color4>{};
}, [](Shaders::Phong& self, const std::vector<Color4>& colors) {
self.setLightColors(colors);
}, "Light colors");
}
}
}}

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

@ -40,3 +40,25 @@ class VertexColor(GLTestCase):
def test_uniforms(self):
a = shaders.VertexColor2D()
a.transformation_projection_matrix = Matrix3.translation(Vector2.x_axis())
class Phong(GLTestCase):
def test_init(self):
a = shaders.Phong()
self.assertEqual(a.flags, shaders.Phong.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)
self.assertEqual(b.light_count, 1)
c = shaders.Phong(shaders.Phong.Flags.NONE, 3)
self.assertEqual(c.flags, shaders.Phong.Flags.NONE)
self.assertEqual(c.light_count, 3)
def test_uniforms(self):
a = shaders.Phong()
a.diffuse_color = (0.5, 1.0, 0.9)
a.transformation_matrix = Matrix4.translation(Vector3.x_axis())
a.projection_matrix = Matrix4.zero_init()
a.light_positions = [(0.5, 1.0, 0.3)]
a.light_colors = [Color4()]

Loading…
Cancel
Save