{"id":9404,"date":"2016-12-12T18:52:40","date_gmt":"2016-12-12T16:52:40","guid":{"rendered":"http:\/\/www.geeks3d.com\/?p=9404"},"modified":"2018-06-14T09:26:39","modified_gmt":"2018-06-14T07:26:39","slug":"rendering-real-time-3d-graphics-on-a-rgb-led-matrix-panel-with-a-raspberry-pi-and-geexlab","status":"publish","type":"post","link":"https:\/\/www.geeks3d.com\/20161212\/rendering-real-time-3d-graphics-on-a-rgb-led-matrix-panel-with-a-raspberry-pi-and-geexlab\/","title":{"rendered":"Rendering Real Time 3D Graphics on a 32×32 RGB LED Matrix Panel with a Raspberry Pi and GeeXLab (**Updated**)"},"content":{"rendered":"
In the previous article<\/a>, I explained how to control the color of a particular LED of the RGB LED matrix panel<\/a>. Thanks to that knowledge, you can draw simple graphics: points, circles, lines and so on. But how can we easily draw a rotating triangle? Or a mesh like a torus? Or any 3D scene? Answer in this article!<\/p>\n <\/p>\n GeeXLab<\/a> comes with a powerful feature used in almost any 3D game: render targets<\/b>. In short, a render target allows to draw a 3D scene into a texture and then apply any kind of effects on that texture before displaying it. <\/p>\n That’s cool, but how will a render target help us to draw on our 32×32 RGB LED panel?<\/p>\n Simply by drawing into a 32×32 pixels render target. Once the scene has been rendered in the render target, we can read each pixel of the render target texture and then use the gh_rpi.rgbmatrix_set_pixel_u8()<\/tt> function (see the previous article<\/a>) to update the LED panel.<\/p>\n All following code samples and demos are available in two places: These demos require GeeXLab for Raspberry Pi with OpenGL 2.1<\/b> support. This version of GeeXLab can be downloaded from this page<\/a>.<\/p>\n In GeeXLab\u2019s folder you will find a file called demos.sh. Edit this file and uncomment\/comment the lines that start with sudo. If necessary, update the \/demofile parameter with correct path + file name of the demo.<\/p>\n You can now run the demos.sh file. The demos.sh file has the execution bit set, so you can launch it by double-clicking on it. Or you can run it in command line with:<\/p>\n RGB LED matrix demos requires root level because they use the GPIO<\/a> to control the RGB Matrix HAT<\/a>.<\/p>\n
\n– in the gl-21\/rpi\/rpi_rgb_matrix\/<\/tt> folder of the code sample pack<\/a>.
\n– in the demos\/rpi_rgb_matrix\/<\/tt> folder included with GeeXLab for Raspberry Pi.<\/p>\n
\n
\n<\/center><\/p>\n
\nHow to run a demo?<\/p>\n\r\n#---------------------------------------------------------\r\n# RGB LED Matrix demos (HUB75) - require root rights\r\n#---------------------------------------------------------\r\nsudo .\/GeeXLab \/demofile=\\\".\/demos\/rpi_rgb_matrix\/demo02_rpi_gl21.xml.xml\\\"\r\n#sudo .\/GeeXLab \/demofile=\\\".\/demos\/rpi_rgb_matrix\/demo05_rpi_gl21.xml.xml\\\"\r\n<\/pre>\n
\r\n{GeeXLab_RPi_folder} $ sh .\/demos.sh\r\n<\/pre>\n
\nHere is a code snippet in Lua (you can do the same thing in Python) that shows how to render a rotating triangle on the RGB LED matrix display. rt01<\/tt> is the render target, vertex_color_prog<\/tt> is a simple vertex color GLSL program, camera<\/tt> is a perspective camera and triangle<\/tt> is a simple mesh made up of 3 vertices.<\/p>\n\r\n-- We are rendering to the render target rt01\r\n--\r\ngh_render_target.bind(rt01)\r\n \r\n-- Binds the perspective camera\r\n--\r\ngh_camera.bind(camera)\r\n\r\n-- Clears the color and depth buffers\r\n--\r\ngh_renderer.clear_color_depth_buffers(0.0, 0.0, 0.0, 1.0, 1.0)\r\n\r\n-- Bind vertex_color_prog to make it the active GPU program.\r\n--\r\ngh_gpu_program.bind(vertex_color_prog)\r\n\r\n-- Render the triangle\r\n--\r\npitch = 0\r\nyaw = 0\r\nroll = elapsed_time * 40.0\r\ngh_object.set_euler_angles(triangle, pitch, yaw, roll)\r\ngh_object.render(triangle)\r\n \r\n \r\n-- Back to regular framebuffer.\r\n-- \r\ngh_render_target.unbind(rt01)\r\n\r\n\r\n\r\n-- rt_tex is the render target texture. rt_tex is initialized in \r\n-- the INIT script with:\r\n-- rt_tex = gh_texture.create_2d_from_rt(rt01)\r\n--\r\ngh_texture.gpu_mem_to_cpu_mem(rt_tex)\r\n\r\n-- Update the 32x32 RGB LED matrix panel\r\n--\r\nfor y=0, 31 do\r\n for x=0, 31 do\r\n r, g, b, a = gh_texture.get_texel_2d(rt_tex, x, 31-y)\r\n gh_rpi.rgbmatrix_set_pixel_u8(x, y, r*255.0, g*255.0, b*255.0)\r\n end\r\nend \r\n<\/pre>\n
\n