3d model and projection support?

665 views
Skip to first unread message

Olivier

unread,
Mar 29, 2011, 4:58:59 AM3/29/11
to Kivy users support
Hi all,

I was wondering what would be the best option to include custom openGL
objects (so, 3D, for instance, with perspective projections...) in
Kivy widgets.

I can see that OpenGL is exposed, but I don't seem to find a way
include the rendering code : the context and vertex instructions seem
to live in Cython world, and I can't quite figure out whether I can
write my own instructions... For instance, it seems that the
instructions implement the "apply" cdef, but I'm not sure, not being
very familiar with Cython), whether it means that one can't create
one's own instruction in the python world.

Otherwise, kivy is a very nice piece of software, very cunningly
designed, I'm looking forward to using it :)

Thanks for your help!

Mathieu Virbel

unread,
Mar 29, 2011, 5:12:45 AM3/29/11
to kivy-...@googlegroups.com, Olivier
Hi Olivier,

The graphics part of Kivy live in Cython, however, the apply() is from
us. We don't expose a way to write custom instruction inside Python,
mostly for a performance issue. If you still want to try your own
instructions, you have no choice to create a .pyx in the
kivy/graphics/ directory, and put your instructions in it.

Care, pure Kivy 3D integration is not yet possible, cause of our fixed
vertex format and shader pipeline. You'll hit severals issue if you
try to add z coordinate :)

However, for another project, we've managed to use Panda3D inside
Kivy. Maybe you'll be interested on that point. Next week, we'll work
on the extension system of Kivy, as soon as the system work, we can
provide custom external extension (Panda3D, berkelium, poppler, ...)

Mathieu

2011/3/29 Olivier <olivier...@gmail.com>:

Olivier

unread,
Mar 29, 2011, 5:25:59 AM3/29/11
to Kivy users support
Hi Mathieu,

Thanks for the lightning fast answer.
Is the 3D integration permanently locked because of the aforementioned
structural issues, or is that just that you didn't have time to hook
it in properly yet?
External plugin support sounds good, but probably overkill for the use
I was thinking of (mostly 2D that the current instructions could
cover, with the occasional 3D widget sub-element), although I'm
guessing it will allow more flexibility in building widgets.

Cheers!

Olivier



On Mar 29, 10:12 am, Mathieu Virbel <m...@kivy.org> wrote:
> Hi Olivier,
>
> The graphics part of Kivy live in Cython, however, the apply() is from
> us. We don't expose a way to write custom instruction inside Python,
> mostly for a performance issue. If you still want to try your own
> instructions, you have no choice to create a .pyx in the
> kivy/graphics/ directory, and put your instructions in it.
>
> Care, pure Kivy 3D integration is not yet possible, cause of our fixed
> vertex format and shader pipeline. You'll hit severals issue if you
> try to add z coordinate :)
>
> However, for another project, we've managed to use Panda3D inside
> Kivy. Maybe you'll be interested on that point. Next week, we'll work
> on the extension system of Kivy, as soon as the system work, we can
> provide custom external extension (Panda3D, berkelium, poppler, ...)
>
> Mathieu
>
> 2011/3/29 Olivier <olivier.carr...@gmail.com>:

Mathieu Virbel

unread,
Mar 29, 2011, 5:34:19 AM3/29/11
to kivy-...@googlegroups.com, Olivier
3D integration with panda: need extension system
3D integration with kivy: need to extend our graphics base to support 3D

If you really want to do it yourself, i think you can use from
kivy.graphics.opengl import *
And use RenderContext() + Callback() class : rendercontext will allow
you to setup a custom vertex and fragment shader, and Callback will
call a python method when graphics engine need to update your element.
So yes, i think you might be able to use that approach if you want to
do 3D. Never tested though. :)

2011/3/29 Olivier <olivier...@gmail.com>:

Olivier

unread,
Mar 29, 2011, 5:38:29 AM3/29/11
to Kivy users support
That sounds interesting, I'll let you know when I finally get round to
test it.
I suppose, by using the "by hand" solution, I may end up facing the
same problems as if I was trying to implement 3D integration in kivy
(renderstate issues and z issues?)

On Mar 29, 10:34 am, Mathieu Virbel <m...@kivy.org> wrote:
> 3D integration with panda: need extension system
> 3D integration with kivy: need to extend our graphics base to support 3D
>
> If you really want to do it yourself, i think you can use from
> kivy.graphics.opengl import *
> And use RenderContext() + Callback() class : rendercontext will allow
> you to setup a custom vertex and fragment shader, and Callback will
> call a python method when graphics engine need to update your element.
> So yes, i think you might be able to use that approach if you want to
> do 3D. Never tested though. :)
>
> 2011/3/29 Olivier <olivier.carr...@gmail.com>:

Olivier

unread,
Mar 29, 2011, 9:48:40 AM3/29/11
to Kivy users support
Hi Mathieu,

I found the RenderContext class.
I think I found the Callback one as well, but it's (currently) only on
the cython side of kivy.graphics.instructions.
Looking at the sources, it looks like it would just do the work, if
only it was exported :)

I was also thinking, maybe a good option to allow the "3d stuff" to be
nicely contained would be to render it to FBO? that way it could play
nicely with the rest of kivy...

On Mar 29, 10:34 am, Mathieu Virbel <m...@kivy.org> wrote:
> 3D integration with panda: need extension system
> 3D integration with kivy: need to extend our graphics base to support 3D
>
> If you really want to do it yourself, i think you can use from
> kivy.graphics.opengl import *
> And use RenderContext() + Callback() class : rendercontext will allow
> you to setup a custom vertex and fragment shader, and Callback will
> call a python method when graphics engine need to update your element.
> So yes, i think you might be able to use that approach if you want to
> do 3D. Never tested though. :)
>
> 2011/3/29 Olivier <olivier.carr...@gmail.com>:

Olivier

unread,
Apr 1, 2011, 3:34:15 AM4/1/11
to Kivy users support
Hi all,

I tried to use Callback() + RenderContext(), but unfortunately, a few
more things are missing :

- A way to setup a matrix from values, or at least a way to get the
equivalent of gluLookAt into a Matrix.
- Sounds like glUniformMatrix4fv has not been implemented yet, looking
at the Cython sources.

I may start to play with the git sources if I find the time, to add
those... (Is the git repo the latest from 1.04beta, BTW?)

Cheers!

Mathieu Virbel

unread,
Apr 1, 2011, 4:10:39 AM4/1/11
to kivy-...@googlegroups.com, Olivier
Hi Olivier,

The gluLooAt is implented though though kivy.graphics.transformation package.
Take a look at kivy/core/__init__.py in update_viewport() :

382 from kivy.graphics.transformation import Matrix
390 projection_mat = Matrix()
391 projection_mat.view_clip(0.0, width, 0.0, height, -1.0, 1.0, 0)

And then, the projection matrix is set in the render context :

392 self.render_context['projection_mat'] = projection_mat


They are no glUniformMatrix4fv, cause we are using shader for
rendering. Once you use shader, gl model matrix and projection matrix
are not used. All the transformation work is done inside vertex
shader.
If you check the file kivy/data/glsl/default.fs, you'll see a line :

21 gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy,
0.0, 1.0) ;

Here you are, the projection_mat is the one you set in the
render_context['projection_mat'].


Now, i think you've understand the process, i can warn you about one
thing: the implementation of perspective matrix (in clip_matrix)
function is not yet done. :) Check how the orthogonal clip matrix is
done, and you can take a perspective matrix function from internet
(lgpl code or free domain), and push it in the clip_matrix if
perspective=True. Patch welcome :)

Mathieu
2011/4/1 Olivier <olivier...@gmail.com>:

Olivier

unread,
Apr 1, 2011, 4:19:13 AM4/1/11
to Kivy users support
Hi Mathieu,

I see! This is indeed a lot easier to use than I expected!

I'll see what I can do about the patch ;)

On Apr 1, 9:10 am, Mathieu Virbel <m...@kivy.org> wrote:
> Hi Olivier,
>
> The gluLooAt is implented though though kivy.graphics.transformation package.
> Take a look at kivy/core/__init__.py in update_viewport() :
>
> 382         from kivy.graphics.transformation import Matrix
> 390         projection_mat = Matrix()
> 391         projection_mat.view_clip(0.0, width, 0.0, height, -1.0, 1.0, 0)
>
> And then, the projection matrix is set in the render context :
>
> 392         self.render_context['projection_mat'] = projection_mat
>
> They are no glUniformMatrix4fv, cause we are using shader for
> rendering. Once you use shader, gl model matrix and projection matrix
> are not used. All the transformation work is done inside vertex
> shader.
> If you check the file kivy/data/glsl/default.fs, you'll see a line :
>
> 21   gl_Position = projection_mat * modelview_mat * vec4(vPosition.xy,
> 0.0, 1.0)   ;
>
> Here you are, the projection_mat is the one you set in the
> render_context['projection_mat'].
>
> Now, i think you've understand the process, i can warn you about one
> thing: the implementation of perspective matrix (in clip_matrix)
> function is not yet done. :) Check how the orthogonal clip matrix is
> done, and you can take a perspective matrix function from internet
> (lgpl code or free domain), and push it in the clip_matrix if
> perspective=True. Patch welcome :)
>
> Mathieu
> 2011/4/1 Olivier <olivier.carr...@gmail.com>:

ADNAAN BADR

unread,
Sep 19, 2011, 7:43:26 AM9/19/11
to kivy-...@googlegroups.com
Hi Mathieu,

                    Would you be able to share the panda3d support for kivy yet?

Thanks

Mathieu Virbel

unread,
Sep 21, 2011, 6:17:09 AM9/21/11
to kivy-...@googlegroups.com
Hi,

The current patch is already available here:
http://txzone.net/files/contrib/panda3d/
I didn't retest since June on their latest svn, but i'm planning to
restart the panda port for Kivy + Android during october.

Regards,

Mathieu

2011/9/19 ADNAAN BADR <adn...@notionink.com>:

ADNAAN BADR

unread,
Sep 21, 2011, 6:25:46 AM9/21/11
to kivy-...@googlegroups.com
thanks

Martin Burger

unread,
Oct 18, 2012, 6:56:40 AM10/18/12
to kivy-...@googlegroups.com
Hi Mathieu,

Did you allready restart the port?
Have you got hints how to port panda for kivy + Windows?

thanks in advance

Martin
Reply all
Reply to author
Forward
0 new messages