Browse Source

python: initial bits of the Shaders library.

pull/1/head
Vladimír Vondruš 7 years ago
parent
commit
4de30ab058
  1. 2
      package/ci/travis-desktop.sh
  2. 15
      src/python/magnum/CMakeLists.txt
  3. 87
      src/python/magnum/shaders.cpp
  4. 42
      src/python/magnum/test/test_shaders_gl.py
  5. 1
      src/python/setup.py.cmake

2
package/ci/travis-desktop.sh

@ -31,7 +31,7 @@ cmake .. \
-DWITH_MESHTOOLS=OFF \
-DWITH_PRIMITIVES=OFF \
-DWITH_SCENEGRAPH=OFF \
-DWITH_SHADERS=OFF \
-DWITH_SHADERS=ON \
-DWITH_TEXT=OFF \
-DWITH_TEXTURETOOLS=OFF \
-DWITH_TRADE=OFF \

15
src/python/magnum/CMakeLists.txt

@ -24,7 +24,7 @@
#
# *Not* REQUIRED
find_package(Magnum COMPONENTS GL)
find_package(Magnum COMPONENTS GL Shaders)
set(magnum_SRCS
magnum.cpp
@ -55,6 +55,19 @@ if(Magnum_GL_FOUND)
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum)
endif()
if(Magnum_Shaders_FOUND)
set(magnum_shaders_SRCS
shaders.cpp)
pybind11_add_module(magnum_shaders ${magnum_shaders_SRCS})
target_include_directories(magnum_shaders PRIVATE ${PROJECT_SOURCE_DIR}/src/python)
target_link_libraries(magnum_shaders PRIVATE Magnum::Shaders)
set_target_properties(magnum_shaders PROPERTIES
FOLDER "python"
OUTPUT_NAME "shaders"
LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum)
endif()
file(GENERATE OUTPUT ${output_dir}/magnum/__init__.py
INPUT ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py)

87
src/python/magnum/shaders.cpp

@ -0,0 +1,87 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <pybind11/pybind11.h>
#include <Magnum/Shaders/VertexColor.h>
#include "magnum/bootstrap.h"
#include "magnum/NonDestructible.h"
namespace magnum { namespace {
template<UnsignedInt dimensions> void vertexColor(NonDestructibleBase<Shaders::VertexColor<dimensions>, GL::AbstractShaderProgram>& c) {
/* Attributes */
c.attr("COLOR3") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 3,
GL::DynamicAttribute::Components::Three,
GL::DynamicAttribute::DataType::Float};
c.attr("COLOR4") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 3,
GL::DynamicAttribute::Components::Four,
GL::DynamicAttribute::DataType::Float};
/* Methods */
c
.def(py::init(), "Constructor")
/* Using lambdas to avoid method chaining getting into signatures */
// TODO: make writeonly once https://github.com/pybind/pybind11/pull/1144
// is released
.def_property("transformation_projection_matrix", [](Shaders::VertexColor<dimensions>&) {
return MatrixTypeFor<dimensions, Float>{};
}, &Shaders::VertexColor<dimensions>::setTransformationProjectionMatrix,
"Transformation and projection matrix");
}
void shaders(py::module& m) {
m.import("magnum.gl");
/* 2D/3D vertex color shader */
{
NonDestructibleBase<Shaders::VertexColor2D, GL::AbstractShaderProgram> vertexColor2D{m,
"VertexColor2D", "2D vertex color shader"};
NonDestructibleBase<Shaders::VertexColor3D, GL::AbstractShaderProgram> vertexColor3D{m,
"VertexColor3D", "3D vertex color shader"};
vertexColor2D.attr("POSITION") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 0,
GL::DynamicAttribute::Components::Two,
GL::DynamicAttribute::DataType::Float};
vertexColor3D.attr("POSITION") = GL::DynamicAttribute{
GL::DynamicAttribute::Kind::Generic, 0,
GL::DynamicAttribute::Components::Three,
GL::DynamicAttribute::DataType::Float};
vertexColor(vertexColor2D);
vertexColor(vertexColor3D);
}
}
}}
PYBIND11_MODULE(shaders, m) {
m.doc() = "Builtin shaders";
magnum::shaders(m);
}

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

@ -0,0 +1,42 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019
# Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import unittest
# setUpModule gets called before everything else, skipping if GL tests can't
# be run
from . import GLTestCase, setUpModule
from magnum import *
from magnum import shaders
class VertexColor(GLTestCase):
def test_init(self):
a = shaders.VertexColor2D()
b = shaders.VertexColor3D()
def test_uniforms(self):
a = shaders.VertexColor2D()
a.transformation_projection_matrix = Matrix3.translation(Vector2.x_axis())

1
src/python/setup.py.cmake

@ -34,6 +34,7 @@ extension_paths = {
'corrade.containers': '$<TARGET_FILE:corrade_containers>',
'magnum._magnum': '$<TARGET_FILE:magnum>',
'magnum.gl': '$<$<TARGET_EXISTS:magnum_gl>:$<TARGET_FILE:magnum_gl>>',
'magnum.shaders': '$<$<TARGET_EXISTS:magnum_shaders>:$<TARGET_FILE:magnum_shaders>>',
'magnum.platform.egl': '$<$<TARGET_EXISTS:magnum_platform_egl>:$<TARGET_FILE:magnum_platform_egl>>',
'magnum.platform.glx': '$<$<TARGET_EXISTS:magnum_platform_glx>:$<TARGET_FILE:magnum_platform_glx>>',
}

Loading…
Cancel
Save