From aec29e7da8ebfcfd78bd64404d7e4274fc8d87c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 2 May 2019 17:31:15 +0200 Subject: [PATCH] python: initial bits of the GL library. --- package/ci/travis-desktop.sh | 2 +- src/python/magnum/CMakeLists.txt | 16 +++++ src/python/magnum/gl.cpp | 97 +++++++++++++++++++++++++++++++ src/python/magnum/test/test_gl.py | 36 ++++++++++++ src/python/setup.py.cmake | 1 + 5 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 src/python/magnum/gl.cpp create mode 100644 src/python/magnum/test/test_gl.py diff --git a/package/ci/travis-desktop.sh b/package/ci/travis-desktop.sh index f5b6d34..b71064d 100755 --- a/package/ci/travis-desktop.sh +++ b/package/ci/travis-desktop.sh @@ -27,7 +27,7 @@ cmake .. \ -DCMAKE_BUILD_TYPE=Release \ -DWITH_AUDIO=OFF \ -DWITH_DEBUGTOOLS=OFF \ - -DWITH_GL=OFF \ + -DWITH_GL=ON \ -DWITH_MESHTOOLS=OFF \ -DWITH_PRIMITIVES=OFF \ -DWITH_SCENEGRAPH=OFF \ diff --git a/src/python/magnum/CMakeLists.txt b/src/python/magnum/CMakeLists.txt index df8bcc1..9d51b62 100644 --- a/src/python/magnum/CMakeLists.txt +++ b/src/python/magnum/CMakeLists.txt @@ -23,6 +23,9 @@ # DEALINGS IN THE SOFTWARE. # +# *Not* REQUIRED +find_package(Magnum COMPONENTS GL) + set(magnum_SRCS magnum.cpp math.cpp @@ -39,6 +42,19 @@ set_target_properties(magnum PROPERTIES OUTPUT_NAME "_magnum" LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) +if(Magnum_GL_FOUND) + set(magnum_gl_SRCS + gl.cpp) + + pybind11_add_module(magnum_gl ${magnum_gl_SRCS}) + target_include_directories(magnum_gl PRIVATE ${PROJECT_SOURCE_DIR}/src/python) + target_link_libraries(magnum_gl PRIVATE Magnum::GL) + set_target_properties(magnum_gl PROPERTIES + FOLDER "python" + OUTPUT_NAME "gl" + LIBRARY_OUTPUT_DIRECTORY ${output_dir}/magnum) +endif() + file(GENERATE OUTPUT ${output_dir}/magnum/__init__.py INPUT ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py) diff --git a/src/python/magnum/gl.cpp b/src/python/magnum/gl.cpp new file mode 100644 index 0000000..d1cef81 --- /dev/null +++ b/src/python/magnum/gl.cpp @@ -0,0 +1,97 @@ +/* + This file is part of Magnum. + + Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 + Vladimír Vondruš + + 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 +#include + +#include "magnum/bootstrap.h" + +namespace magnum { namespace { + +void gl(py::module& m) { + /* (Dynamic) attribute */ + py::class_ attribute{m, "Attribute", "Vertex attribute location and type"}; + + py::enum_{attribute, "Kind", "Attribute kind"} + .value("GENERIC", GL::DynamicAttribute::Kind::Generic) + .value("GENERIC_NORMALIZED", GL::DynamicAttribute::Kind::GenericNormalized) + #ifndef MAGNUM_TARGET_GLES2 + .value("INTEGRAL", GL::DynamicAttribute::Kind::Integral) + #ifndef MAGNUM_TARGET_GLES + .value("LONG", GL::DynamicAttribute::Kind::Long) + #endif + #endif + ; + + py::enum_{attribute, "Components", "Component count"} + .value("ONE", GL::DynamicAttribute::Components::One) + .value("TWO", GL::DynamicAttribute::Components::Two) + .value("THREE", GL::DynamicAttribute::Components::Three) + .value("FOUR", GL::DynamicAttribute::Components::Four) + #ifndef MAGNUM_TARGET_GLES + .value("BGRA", GL::DynamicAttribute::Components::BGRA) + #endif + ; + + py::enum_{attribute, "DataType", "Data type"} + .value("UNSIGNED_BYTE", GL::DynamicAttribute::DataType::UnsignedByte) + .value("BYTE", GL::DynamicAttribute::DataType::Byte) + .value("UNSIGNED_SHORT", GL::DynamicAttribute::DataType::UnsignedShort) + .value("SHORT", GL::DynamicAttribute::DataType::Short) + .value("UNSIGNED_INT", GL::DynamicAttribute::DataType::UnsignedInt) + .value("INT", GL::DynamicAttribute::DataType::Int) + #if !(defined(MAGNUM_TARGET_WEBGL) && defined(MAGNUM_TARGET_GLES2)) + .value("HALF_FLOAT", GL::DynamicAttribute::DataType::HalfFloat) + #endif + .value("FLOAT", GL::DynamicAttribute::DataType::Float) + #ifndef MAGNUM_TARGET_GLES + .value("DOUBLE", GL::DynamicAttribute::DataType::Double) + .value("UNSIGNED_INT_10F_11F_11F_REV", GL::DynamicAttribute::DataType::UnsignedInt10f11f11fRev) + #endif + #ifndef MAGNUM_TARGET_GLES2 + .value("UNSIGNED_INT_2_10_10_10_REV", GL::DynamicAttribute::DataType::UnsignedInt2101010Rev) + .value("INT_2_10_10_10_REV", GL::DynamicAttribute::DataType::Int2101010Rev) + #endif + ; + + attribute + .def(py::init(), "Constructor", py::arg("kind"), py::arg("location"), py::arg("components"), py::arg("data_type")) + .def_property_readonly("kind", &GL::DynamicAttribute::kind, + "Attribute kind") + .def_property_readonly("location", &GL::DynamicAttribute::location, + "Attribute location") + .def_property_readonly("components", &GL::DynamicAttribute::components, + "Component count") + .def_property_readonly("data_type", &GL::DynamicAttribute::dataType, + "Type of passed data"); +} + +}} + +PYBIND11_MODULE(gl, m) { + m.doc() = "OpenGL wrapping layer"; + + magnum::gl(m); +} diff --git a/src/python/magnum/test/test_gl.py b/src/python/magnum/test/test_gl.py new file mode 100644 index 0000000..719ca6a --- /dev/null +++ b/src/python/magnum/test/test_gl.py @@ -0,0 +1,36 @@ +# +# This file is part of Magnum. +# +# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019 +# Vladimír Vondruš +# +# 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 + +from magnum import gl + +class Attribute(unittest.TestCase): + def test_init(self): + a = gl.Attribute(gl.Attribute.Kind.GENERIC, 2, gl.Attribute.Components.TWO, gl.Attribute.DataType.FLOAT) + self.assertEqual(a.kind, gl.Attribute.Kind.GENERIC) + self.assertEqual(a.location, 2) + self.assertEqual(a.components, gl.Attribute.Components.TWO) + self.assertEqual(a.data_type, gl.Attribute.DataType.FLOAT) diff --git a/src/python/setup.py.cmake b/src/python/setup.py.cmake index e538ff6..0cdfb13 100644 --- a/src/python/setup.py.cmake +++ b/src/python/setup.py.cmake @@ -33,6 +33,7 @@ extension_paths = { # Filled in by cmake 'corrade.containers': '$', 'magnum._magnum': '$', + 'magnum.gl': '$<$:$>', 'magnum.platform.egl': '$<$:$>', 'magnum.platform.glx': '$<$:$>', }