PDA

View Full Version : Draw a sphere in Gtk.GlArea using pyopengl



brudra
01-14-2018, 05:20 AM
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.


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.

Silence
01-15-2018, 12:51 AM
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.

brudra
01-16-2018, 10:37 PM
Hi,

Thanks for your reply.


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.

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?

Silence
01-17-2018, 12:15 AM
Hi,

Thanks for your reply.



Here are my VGA card and Gtk3 version.

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 (https://developer.gnome.org/gtk3/stable/GtkGLArea.html) 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 (https://gist.github.com/zwzmzd/0195733fa1210346b00d) might help (not tested so unsure).