omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Python OpenGLES

    Pythonista
    3
    20
    12818
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • omz
      omz last edited by

      @JonB It basically checks if nextResponder is a UIViewController. The ui module creates view controllers for presentation automatically, and if a view already has an associated view controller, it usually means that it's currently being presented...

      1 Reply Last reply Reply Quote 0
      • Cethric
        Cethric last edited by

        How do I properly setup a GLKViewController delegate?
        My current code does not seem to work at all

        def glkViewControllerUpdate_(_controller):
            print "Update", _controller
        
        GLKViewControllerDelegate_Class = create_objc_class('GLKViewControllerDelegate_Class', methods=[glkViewControllerUpdate_], protocols='GLKViewControllerDelegate')
            
        def GLKViewControllerDelegate():
            return GLKViewControllerDelegate_Class.alloc().init()
        

        which is called in the GLKView class

        self.vc = GKLViewController("Test GLES", self.glview)
        self.vcd = GLKViewControllerDelegate()
        self.vc.delegate = self.vcd
        

        GKLViewController is just a wrapper for the ctypes function

        1 Reply Last reply Reply Quote 0
        • omz
          omz last edited by

          @Cethric Your glkViewControllerUpdate_ method needs to have three parameters, e.g.

          def glkViewControllerUpdate_(_self, _cmd, _controller):
              ...
          

          (the actual names of the parameters don't really matter, I tend to use underscores to indicate that these are pointers and not actual objects...)

          To actually set the delegate, use the setDelegate_ method, like this:

          self.vc.setDelegate_(self.vcd)
          
          1 Reply Last reply Reply Quote 0
          • Cethric
            Cethric last edited by

            @omz Thank you so much it seems to work now
            Hopefully one last question, the 3 parameters only return pointers so what would be the 'best' solution to get the GLKViewController.framesPerSecond variable?

            1 Reply Last reply Reply Quote 0
            • omz
              omz last edited by

              Wrap the _controller parameter in an ObjCInstance:

              def glkViewControllerUpdate_(_self, _cmd, _controller):
                  controller = ObjCInstance(_controller)
                  fps = controller.framesPerSecond()
                  dt = controller.timeSinceLastUpdate()
                  # ...
              
              1 Reply Last reply Reply Quote 0
              • Cethric
                Cethric last edited by

                @omz thanks again it works now

                1 Reply Last reply Reply Quote 0
                • Cethric
                  Cethric last edited by

                  glCreateShader always fails but I don't know why

                  def loadShader(source, glsl_type):
                      shader = GLuint()
                      compiled = GLint()
                      # Create the shader object
                      shader = glCreateShader(glsl_type)
                      if(shader == 0):
                          print "Failed to create shader"
                          return 0;
                      # Load the shader source
                      glShaderSource(shader, 1, shaderSrc, 0)
                      # Compile the shader
                      glCompileShader(shader)
                      # Check the compile status
                      
                      glGetShaderiv(shader, GL_COMPILE_STATUS, ctypes.byref(compiled))
                      if(not compiled):
                          infoLen = GLint(0)
                          glGetShaderiv(shader, GL_INFO_LOG_LENGTH, ctypes.byref(infoLen))
                          if(infoLen > 1):
                              infoLog = (ctypes.c_char * infoLen)()
                              glGetShaderInfoLog(shader, infoLen, 0, infoLog);
                              print("Error compiling shader:\n%s\n" % infoLog);
                          glDeleteShader(shader);
                          return 0;
                      return shader;
                  

                  How glCreateShader is referenced

                  try:
                      glCreateShader = c.glCreateShader
                      glCreateShader.restype = GLuint
                      glCreateShader.argtypes = [GLenum]
                  except AttributeError as e:
                      if DEBUG:
                          print 'could not load the function'
                          print e
                  

                  Called in the setup process as

                  class Renderer(Util.RenderCycler):
                      def __init__(self):
                          Util.RenderCycler.__init__(self)
                          self.r, self.g, self.b = colorsys.hsv_to_rgb((time.time() * 0.1) % 1.0, 0.1, 1)
                      
                      def setup(self, context):
                          if (EAGL.setCurrentContext(context)):
                              glClearColor(1, 1, 1, 1.0)
                              s = Util.loadShader("void main() {}", GLenum(GL_FRAGMENT_SHADER))
                              print s
                          else:
                              print "Could not Setup OpenGLES"
                              
                      def update(self, dt):
                          self.r, self.g, self.b = colorsys.hsv_to_rgb((time.time() * 0.1) % 1.0, 1.0, 1)
                      
                      def render(self, context):
                          if (EAGL.setCurrentContext(context)):
                              glClearColor(self.r, self.g, self.b, 1.0)
                              glClear(GL_COLOR_BUFFER_BIT)
                  
                  1 Reply Last reply Reply Quote 0
                  • Cethric
                    Cethric last edited by Cethric

                    Anyone got an idea? @omz @JonB
                    Is it potentially because I am calling it from a custom setup event and not on a onDraw or onUpdate event?

                    EDIT: Never mind I answered my own question... It was because I was calling setup at the wrong time

                    1 Reply Last reply Reply Quote 0
                    • Cethric
                      Cethric last edited by

                      @omz @JonB is there a way to access the GLKit Math classes? (ie. GLKMatrix4)

                      1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB last edited by JonB

                        you will need to find the structure definitions, and create your own ctypes structs.

                        for instance http://developer.limneos.net/?framework=GLKit.framework&header=GLKit-Structs.h

                        You will need to create classes that extend Structure, and implement the _fields_ appropriately. (note some of these are unions... you can either onherit from Union, or just pick which way you'd rather refer to the fields, and just use a Structure). these mostly sppesr to be simple arrays of 16 floats for example, so you could slso probably do GLKMatrix4=c_float*16 for example.

                        All of the GLKMatrix4Makexxxxxxxx functions are inlined, so you have to make your own constructors. For the most basic stule, you just define the 16 coefficients directly. zfor the more complicated versions, https://searchcode.com/codesearch/view/70695332/ may have some ideas on what these should look like, so you can roll your own.

                        1 Reply Last reply Reply Quote 0
                        • Cethric
                          Cethric last edited by

                          Will look into it, thanks for the response

                          1 Reply Last reply Reply Quote 0
                          • Cethric
                            Cethric last edited by

                            Working on an example file main.py to test shaders and rendering. However I cannot get the view to show a cube I think the issue is with the MVP however I do not know at what point I am going wrong, can someone please look at it and let me know
                            For simplicity I am using the euclid.py math library.

                            Thanks in advanced.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors