omz:software Forums

Pythonista

GLKView and OpenGLES

Category: Questions Comment Feed

Cethric

I have attempted to load the GLKit GLKView class as a subview and then change the background colour however it does not seem to work. What am i doing wrong?

from objc_util import *
import ctypes

class EAGLRenderingAPI(object):
    EAGLRenderingAPIOpenGLES1 = 1,
    EAGLRenderingAPIOpenGLES2 = 2,
    EAGLRenderingAPIOpenGLES3 = 3

class GLKViewDrawableColorFormat(object):
    GLKViewDrawableColorFormatRGBA8888 = 0,
    GLKViewDrawableColorFormatRGB565 = 1,
    GLKViewDrawableColorFormatSRGBA8888 = 2

class GLKViewDrawableDepthFormat(object):
    GLKViewDrawableDepthFormatNone = 0,
    GLKViewDrawableDepthFormat16 = 1,
    GLKViewDrawableDepthFormat24 = 2

class GLKViewDrawableStencilFormat(object):
    GLKViewDrawableStencilFormatNone = 0,
    GLKViewDrawableStencilFormat8 = 1

class GLKViewDrawableMultisample(object):
    GLKViewDrawableMultisampleNone = 0,
    GLKViewDrawableMultisample4X = 1

gl_dict = {}
gl_dict["GLKView"] = ObjCClass('GLKView')
gl_dict["EAGLContext"] = ObjCClass("EAGLContext")

class GLKitView(ui.View):
    @on_main_thread
    def __init__(self, *args, **kwargs):
        ui.View.__init__(self, *args, **kwargs)
        GLKView = gl_dict["GLKView"]
        frame = CGRect(CGPoint(0, 0), CGSize(self.width, self.height))
        self.gl_kit_view = GLKView.alloc().initWithFrame_(frame)
        EAGLContext = gl_dict["EAGLContext"]
        self.context = EAGLContext.alloc().initWithAPI_(EAGLRenderingAPI.EAGLRenderingAPIOpenGLES3)
        self.gl_kit_view.context = self.context
        flex_width, flex_height = (1<<1), (1<<4)
        self.gl_kit_view.setAutoresizingMask_(flex_width|flex_height)
        self.gl_kit_view.drawableColorFormat = GLKViewDrawableColorFormat.GLKViewDrawableColorFormatRGBA8888
        self.gl_kit_view.drawableDepthFormat = GLKViewDrawableDepthFormat.GLKViewDrawableDepthFormat24
        self.gl_kit_view.drawableStencilFormat = GLKViewDrawableStencilFormat.GLKViewDrawableStencilFormat8
        self.gl_kit_view.drawableMultisample = GLKViewDrawableMultisample.GLKViewDrawableMultisample4X
        self_objc = ObjCInstance(self)
        self_objc.addSubview_(self.gl_kit_view)
        self.gl_kit_view.release()

    @on_main_thread  
    def draw(self):
        if gl_dict["EAGLContext"].setCurrentContext_(self.context):
            print "Rendering"
            glClearColor = c.glClearColor
            glClearColor(ctypes.c_float(0.0), ctypes.c_float(0.0), ctypes.c_float(1.0), ctypes.c_float(1.0))
            glClear = c.glClear

if __name__ == "__main__":
    v = GLKitView()
    v.present()


JonB

i have noticed that self.context is empty. On my ipad3, initWithAPI_(2) returns an object, but 3 does not.

also, see here http://www.raywenderlich.com/3664/opengl-tutorial-for-ios-opengl-es-2-0 Dont you need to set up a framebuffer, and also call presentRenderbuffer? Also note that you didnt actually call glClear in your code.


Cethric

OpenGLES3 is supported on iOS8 and greater, so there is a context for me i think it has something to do with needing to call self.gl_kit_view.setContext_ instead of just changing the self.gl_kit_view.context variable. Will look into it thanks.


Cethric

With ctypes how do you get a pointer value?
ie
python def glGenRenderbuffers(amount): target = ctypes.c_int() print c.glGenRenderbuffers(amount, ctypes.pointer(target)) return target
How do i get the value of target from the function. It only returns 0 and the print function returns 1


JonB

have you set the argtypes for c.glGenrenderbuffers?


Cethric

This is the full glGenRenderbuffer code

def glGenRenderbuffer(amount):
    target = GLuint(0)
    func = c.glGenRenderbuffer
    func.argtypes = [GLsizei, ctypes.POINTER(GLuint)]
    func.restype = None
    func(amount, ctypes.byref(target))
    return target.value

with GLsizei being a ctypes.cint32 and GLuint a ctypes.cuint32


You can comment on this topic after logging in.