Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 4 of 4

Thread: Draw a sphere in Gtk.GlArea using pyopengl

  1. #1
    Newbie Newbie
    Join Date
    Jan 2018
    Posts
    2

    Draw a sphere in Gtk.GlArea using pyopengl

    Hi,
    I am very new to 3d-graphics. I do some work in Gtk3+ and python3. Now, I want to develop a code to show atoms inside Gtk.GlArea. But I cant find any way of doing that. Since I am using Gtk, I dont need GLUT, for which, there are few example.

    So, here is my code so far. I will be really grateful if somebody shows me the way.

    Code :
    import gi
    import sys
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk, Gio, Gdk
    import OpenGL
    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL.GLUT import *
     
    class GridWindow(Gtk.ApplicationWindow):
      def __init__(self, app):
        Gtk.Window.__init__(self, application = app,
                            default_width=1000,
                            default_height=200,
                            border_width=2,
                            name = "MyWindow")
     
        # Main drawing area
        self.area = DrawArea()
        self.area.set_size_request(1000,800)
        # To attach everything
        grid = Gtk.Grid()
     
        grid.attach(self.area,1,0,1,1)
        self.add(grid)
     
    class DrawArea(Gtk.GLArea):
        def __init__(self):
            Gtk.GLArea.__init__(self)
            self.connect("realize", self.on_realize)
            self.connect("render", self.render)
     
        def on_realize(self, area):
            ctx = self.get_context()
            ctx.make_current()
            err = self.get_error()
            if err:
                print("The error is {}".format(err))
     
        def render(self, area, ctx):
            glClearColor(1.0,.0,.0,1)
            glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
     
            return True
     
    class draw(Gtk.Application):
      def __init__(self):
        Gtk.Application.__init__(self)
      def do_activate(self):
        win = GridWindow(self)
        win.show_all()
     
     
    app = draw()
    exit_status = app.run(sys.argv)
    sys.exit(exit_status)

    Thanks.

  2. #2
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    460
    Please tell which exact version of Gtk you are using and which graphic card you are using.
    Depending on that, you might or not be able to use glu (which you seem to include).

    But the main idea is to create a VAO/VBO for your sphere in your init. Then to call to any appropriate gl draw calls in render.

  3. #3
    Newbie Newbie
    Join Date
    Jan 2018
    Posts
    2
    Hi,

    Thanks for your reply.

    Quote Originally Posted by Silence View Post
    Please tell which exact version of Gtk you are using and which graphic card you are using.
    Depending on that, you might or not be able to use glu (which you seem to include).
    Here are my VGA card and Gtk3 version.
    Code :
    lspci|grep VGA
    VGA compatible controller: Intel Corporation HD Graphics 620 (rev 02)
     
    rpm -q gtk3
    gtk3-3.22.26-2.fc27.x86_64

    But, I probably have a massive misunderstanding. I thought opengl is hardware independent. Isnt that correct? Do I need to change/alter my code according to graphics card?

  4. #4
    Member Regular Contributor
    Join Date
    Jul 2012
    Posts
    460
    Quote Originally Posted by brudra View Post
    Hi,

    Thanks for your reply.



    Here are my VGA card and Gtk3 version.
    Code :
    lspci|grep VGA
    VGA compatible controller: Intel Corporation HD Graphics 620 (rev 02)
     
    rpm -q gtk3
    gtk3-3.22.26-2.fc27.x86_64

    But, I probably have a massive misunderstanding. I thought opengl is hardware independent. Isnt that correct? Do I need to change/alter my code according to graphics card?
    Gtk 3, depending on its minor version will behave differently. With your version, a core profile will be tried to be created. If it cannot be created then Gtk will fallback to a compatibility profile context. With your graphic card you can request a context up to 4.5 version. For more information see this documentation for example (not sure if it is fully complete).

    With that in mind, don't use glu (most functions are not compatible with 'modern' OpenGL). Since you use Gtk, don't use glut neither (plus glutSolidSphere function will not be compatible anymore).
    So, as stated in my previous post, you'll have to create a VAO/VBO and set all vertices, normals, texture coordinates and so on appropriately so that you can render a sphere. For that, this might help (not tested so unsure).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •