Cross-platform OpenGL + Python with Kivy

Background: being a naive Chukchi programmer, I thought: "python is so cross-platform, I will write a toy for my son, run it on a tablet, let it play." As a result, it took two weeks to try to stumble upon a solution for moving from PyOpenGL + pygame to kivy, since I did not find a clear example of using OpenGL with kivy. Perhaps my experience will help someone save time.





Sample process diagram
Sample process diagram

Disclaimer:





  1. I do not urge anyone to develop applications in python with 3D graphics for android. This is not the smartest option. But if you really want to, then you can read on.





  2. Kivy has a built-in-its-native Mesh solution that can do 3D graphics. Examples are also not so simple. I preferred bare OpenGL.





  3. Perhaps I discovered America trying to find India. I hope more experienced comrades will correct and add.





Before you start working with 3D graphics in kivy, you should read about the library in general and about its installation (for example, the manuals recommend using a virtual environment).





What is the problem? The kivy developers cunningly hide the description of the OpenGL functions on their official website . The only question is how to make it work.





, PyOpenGL+pygame kivy OpenGL :





from OpenGL.GL import *
from OpenGL.GL.shaders import *
      
      



:





from kivy.graphics.opengl import *
      
      



: PyOpenGL numpy , kivy tobytes(). kivy glPixelStorei glTexImage2D glTexStorage2D glTexSubImage2D. 2 ( in, out ) :





#ifdef GL_ES
    precision highp float;
#endif
      
      



- - . PyOpenGL+pygame :





#:
import pygame
from pygame.locals import *

def init():
    pygame.init()
    pygame.display.set_mode((Width, Height), HWSURFACE | OPENGL | DOUBLEBUF)
    '''   ,    PyOpenGL '''
    
def main():
    init()
    while True:
        '''      '''
        pygame.display.flip()
      
      



kivy App, , Widget. , Window, kivy . Window - WindowBase.





:





#:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.base import EventLoop
from kivy.clock import Clock

def init():
    '''    ,    OpenGL '''
init()

class CustomWidget(Widget):
    def __init__(self, **kwargs):
        super(CustomWidget, self).__init__(**kwargs)
    def update_glsl(self, nap):
        '''       '''
        Window.flip()

class MainApp(App):
    def build(self):
        root = CustomWidget()
        EventLoop.ensure_window()
        return root

    def on_start(self):
        Clock.schedule_interval(self.root.update_glsl, 40 ** -1) #   FPS

if __name__ == '__main__':
    MainApp().run()
      
      



( OpenGL), . , Window flip(), . WindowBase. , , MainApp:





def passFunc(W):
    pass

class MainApp(App):
    def build(self):
        root = CustomWidget()
        EventLoop.ensure_window()
        
        #    :
        Window.on_flip = lambda W = Window: passFunc(W)
        
        return root
      
      



? , apk- . , (. , , ):





spec- .





Example . ( ), /.





- ? . - . FPS 51 15 25. :





My son, meanwhile, refused to play my part, well, at least he looks with interest. Flat drawn games for a two-year-old turn out to be better suited. This is what it all started for:





Chopped Snake Style - A couple of screenshots from the debug step as a tribute to Jackie Chan:
Vanguard 1
Vanguard 1
Vanguard 2
Vanguard 2
When debugging textures, there is a desire to use the tuning table from the old TV broadcast
When debugging textures, there is a desire to use the tuning table from the old TV broadcast



All Articles