Skip to content

Instantly share code, notes, and snippets.

@alanjfs
Last active October 24, 2019 07:35
Show Gist options
  • Save alanjfs/3f36f5a1ea7cf26d91b4787b2a303df5 to your computer and use it in GitHub Desktop.
Save alanjfs/3f36f5a1ea7cf26d91b4787b2a303df5 to your computer and use it in GitHub Desktop.
Magnum Python Quickstart

Magnum Python Quickstart - Windows Edition

Prerequisites

  • Windows
  • CMake 3.4+
  • git
  • Visual Studio 2017-2019
git clone https://github.com/mosra/magnum-bootstrap.git --branch base --single-branch
cd magnum-bootstrap
git clone https://github.com/mosra/corrade.git
git clone https://github.com/mosra/magnum.git
git clone https://github.com/mosra/magnum-bindings.git bindings
git clone https://github.com/pybind/pybind11.git
remove-item CMakeLists.txt
invoke-webrequest -outfile CMakeLists.txt https://gist.githubusercontent.com/alanjfs/3f36f5a1ea7cf26d91b4787b2a303df5/raw/dd0d7e3c77783da85d0f8931fe230c9d9ce9be9b/CMakeLists.txt
invoke-webrequest -outfile sdl.zip https://www.libsdl.org/release/SDL2-devel-2.0.10-VC.zip
expand-archive .\sdl.zip -destinationpath .
mkdir build
cd build
cmake .. -DWITH_PYTHON=ON
cmake --build . --config Release
.\Debug\bin\MyApplication.exe

Troubleshooting

'invoke-webrequest' is not recognized as an internal or external command

This is a PowerShell command, try running powershell.exe from your command-line, before running the above commands.


Congratulations!

image


Next Steps

Have a look at MyApplication.cpp from the bootstrap repository for a look at what you just built, and head on over to the Examples to learn more about what you can do with Magnum!

Good luck, and see you soon!

cmake_minimum_required(VERSION 3.4)
project(MyApplication)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/modules/" ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/SDL2-2.0.10" ${CMAKE_PREFIX_PATH})
# Add Corrade as a subproject
add_subdirectory(corrade EXCLUDE_FROM_ALL)
# Add Magnum as a subproject, enable Sdl2Application
set(WITH_SDL2APPLICATION ON CACHE BOOL "" FORCE)
add_subdirectory(magnum EXCLUDE_FROM_ALL)
# Add Python stuff
add_subdirectory(pybind11 EXCLUDE_FROM_ALL)
add_subdirectory(bindings)
add_subdirectory(src)

Python Example

The above includes bindings for Python, here's how you can use them.

See NOTE for minor bugs

image

import magnum
from magnum import gl, math, shaders, primitives, meshtools
from magnum.platform import sdl2 as platform


class PythonExample(platform.Application):
    def __init__(self, arguments):
        super(PythonExample, self).__init__(arguments)

        self._mesh = meshtools.compile(primitives.cube_solid())
        self._shader = shaders.Phong()

        self._transformation = (

            # NOTE: Missing Matrix * Matrix
            magnum.Matrix4.rotation_x(magnum.Deg(30.0)).__matmul__(
            magnum.Matrix4.rotation_y(magnum.Deg(40.0))
        ))

        # NOTE: Can't call self.window_size()
        # TypeError: '_magnum.Vector2i' object is not callable

        aspect_ratio = float(640) / 480
        self._projection = magnum.Matrix4.perspective_projection(
            magnum.Deg(35.0), aspect_ratio, 0.01, 100.0
        ).__matmul__(
            magnum.Matrix4.translation(magnum.Vector3.z_axis(-10.0))
        )

        self._color = magnum.Color3.from_hsv(magnum.Deg(35.0), 1.0, 1.0)

        gl.Renderer.enable(gl.Renderer.Feature.DEPTH_TEST)
        gl.Renderer.enable(gl.Renderer.Feature.FACE_CULLING)

    def draw_event(self):
        gl.default_framebuffer.clear(gl.FramebufferClear.COLOR | gl.FramebufferClear.DEPTH)

        self._shader.light_positions = [(7.0, 5.0, 2.5)]
        self._shader.light_colors = [magnum.Color3(1.0)]
        self._shader.diffuse_color = self._color
        self._shader.ambient_color = magnum.Color3.from_hsv(self._color.hue(), 1.0, 0.3)
        self._shader.transformation_matrix = self._transformation
        self._shader.normal_matrix = self._transformation.rotation_scaling()
        self._shader.projection_matrix = self._projection

        self._mesh.draw(self._shader)
        self.swap_buffers()


config = platform.Application.Configuration()
config.title = "Python Example"
config.size = magnum.Vector2i(640, 480)
app = PythonExample(config)

# NOTE: `exec` is a reserved keyword
# Try `exec_()`, like PyQt/PySide
getattr(app, "exec")()

Suggestions

  1. Move math classes to math, e.g. Vector, Matrix and Color
    • Lose that extra import, keep root package clean/empty
  2. Auto-detect sdl2 on import magnum.platform, if available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment