{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "\n", "!pip install ogre-python\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "import Ogre\n", "import Ogre.Bites\n", "import Ogre.RTShader\n", "\n", "class Application(Ogre.Bites.ApplicationContextBase):\n", " def __init__(self):\n", " Ogre.Bites.ApplicationContextBase.__init__(self, \"OgreColab\")\n", "\n", " def oneTimeConfig(self):\n", " rs = self.getRoot().getRenderSystemByName(\"OpenGL 3+ Rendering Subsystem\")\n", " self.getRoot().setRenderSystem(rs)\n", " return True\n", "\n", " def pollEvents(self):\n", " pass\n", "\n", "\n", "ctx = Application()\n", "ctx.initApp()\n", "root = ctx.getRoot()\n", "\n", "scn_mgr = root.createSceneManager()\n", "\n", "shadergen = Ogre.RTShader.ShaderGenerator.getSingleton()\n", "shadergen.addSceneManager(scn_mgr)\n", "\n", "# no RenderWindows are available here, so create an offscreen rendertarget and use that\n", "w, h = 640, 480\n", "tex = Ogre.TextureManager.getSingleton().createManual(\n", " \"target\", Ogre.RGN_DEFAULT, Ogre.TEX_TYPE_2D, w, h, 0, Ogre.PF_BYTE_RGB,\n", " Ogre.TU_RENDERTARGET, None)\n", "win = tex.getBuffer().getRenderTarget()\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# this is the same code as in the numpy_sample.py example\n", "cam = scn_mgr.createCamera(\"myCam\")\n", "vp = win.addViewport(cam)\n", "\n", "# create a numpy image\n", "arr = np.zeros((256, 256, 3), dtype=np.uint8)\n", "arr[:, :, 1] = np.mgrid[0:256, 0:256][1]\n", "\n", "# convert to an Ogre image and create a texture out of it\n", "ogre_img = Ogre.Image()\n", "ogre_img.loadDynamicImage(arr, 256, 256, Ogre.PF_BYTE_RGB)\n", "\n", "Ogre.TextureManager.getSingleton().loadImage(\"gradient\", \"General\", ogre_img)\n", "\n", "## apply texture to a rectangle\n", "mat = Ogre.MaterialManager.getSingleton().create(\"gradient_mat\", \"General\")\n", "rpass = mat.getTechniques()[0].getPasses()[0]\n", "rpass.setLightingEnabled(False)\n", "rpass.createTextureUnitState(\"gradient\")\n", "\n", "rect = scn_mgr.createScreenSpaceRect(True)\n", "rect.setCorners(-0.5, 0.5, 0.5, -0.5) # in normalized screen space\n", "rect.setMaterial(mat)\n", "\n", "scn_mgr.getRootSceneNode().createChildSceneNode().attachObject(rect)\n", "\n", "vp.setBackgroundColour((0.3, 0.3, 0.3))\n", "\n", "win.update()\n", "\n", "## read back the pixels to a numpy array\n", "mem = np.empty((win.getHeight(), win.getWidth(), 3), dtype=np.uint8)\n", "pb = Ogre.PixelBox(win.getWidth(), win.getHeight(), 1, Ogre.PF_BYTE_RGB, mem)\n", "win.copyContentsToMemory(pb, pb)\n", "\n", "plt.imshow(mem)\n" ] } ], "metadata": { "interpreter": { "hash": "767d51c1340bd893661ea55ea3124f6de3c7a262a8b4abca0554b478b1e2ff90" }, "kernelspec": { "display_name": "Python 3.8.10 64-bit", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.10" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }