jonwright / pyopengltk

OpenGL frame for Python/Tkinter via ctypes and pyopengl
MIT License
52 stars 14 forks source link

Strange issue with glvertex3fv #15

Closed atv2016 closed 3 years ago

atv2016 commented 3 years ago

Hi, I wanted to play around with this but running into some strange issue, was wondering if this has to do with pyopengltk. Or i might just be a noob.

For example: glBegin(GL_LINES) glVertex2f(100,100) glVertex2f(-100,100) glVertex2f(-100,-100) glVertex2f(200,200)

This works.

But this doesn't for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd()

Below is the full code i use. Any comments most appreciated. I might just be overlooking something.

!/usr/bin/python3

Allows TKInter menu with OpenGL running on the canvas.

Installation requirements:

sudo apt-get install python3-tk

pip3 install pyopengl

pip3 install pyopengltk

import time import tkinter from OpenGL.GL import from OpenGL.GLU import from pyopengltk import OpenGLFrame

vertices = ( (1,-1,-1), (1,1,-1), (-1,1,-1), (-1,-1,-1), (1,-1,1), (1,1,1), (-1,-1,1), (-1,1,1) )

edges = ( (0,1), (0,3), (0,4), (2,1), (2,3), (2,7), (6,3), (6,4), (6,7), (5,1), (5,4), (5,7) )

class AppOgl(OpenGLFrame):

def initgl(self): glViewport(0,0,self.width,self.height) glClearColor(0.0,1.0,0.0,0.0)

setup projection matrix

glMatrixMode(GL_PROJECTION) glLoadIdentity() glOrtho(0,self.width,self.height,0,-1,1)

setup identity model view matrix

glMatrixMode(GL_MODELVIEW) glLoadIdentity()

self.start=time.time() self.nframes=0

def redraw(self): display=(800,600) gluPerspective(45,(display[0]/display[1]),0.1,50.0) glTranslatef(0.0,0.0,-5) glRotatef(1,3,1,1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glLoadIdentity()

glBegin(GL_LINES) #Below works glColor3f(1.0,0.0,3.0) glVertex2f(200,100) glVertex2f(100,100) glVertex2f(-100,100) glVertex2f(-100,-100) glVertex2f(200,200)

But this doesn't

for edge in edges: for vertex in edge: glVertex3fv(vertices[vertex]) glEnd() glFlush()

tm=time.time()-self.start self.nframes+=1 print("fps",self.nframes/tm,end="\r")

if name == 'main': root=tkinter.Tk() app=AppOgl(root,width=320,height=200) app.pack(fill=tkinter.BOTH,expand=tkinter.YES) app.animate=1 app.after(100,app.printContext) app.mainloop()

atv2016 commented 3 years ago

Also, interested in the performance on this. And how well does it work with menus on tkinter?

jonwright commented 3 years ago

I guess it is an opengl geometry problem. I can see something by moving your points into the window like this:

    for edge in edges:
     for vertex in edge:
       vx, vy, vz = vertices[vertex]
       glVertex3fv( (25*vx+50, 25*vy+50, vz) )

This widget should work like any other Tkinter frame (for menus, geometry, etc).

Performance is subjective, but feel free to send a pull request if you can fix a slowdown that is coming from the code in this repository.

atv2016 commented 3 years ago

Thanks for your reply John. In any case it's an awesome addition to tkinter.

Well the code works fine when i use it in pygame, hence my confusion. Yes i can see simple points to but i should see a rotating cube.

Works fine with this:

#!/usr/bin/python3

import pygame
from pygame.locals import *

from OpenGL.GL import *
from OpenGL.GLU import *

verticies = (
    (1, -1, -1),
    (1, 1, -1),
    (-1, 1, -1),
    (-1, -1, -1),
    (1, -1, 1),
    (1, 1, 1),
    (-1, -1, 1),
    (-1, 1, 1)
    )

edges = (
    (0,1),
    (0,3),
    (0,4),
    (2,1),
    (2,3),
    (2,7),
    (6,3),
    (6,4),
    (6,7),
    (5,1),
    (5,4),
    (5,7)
    )

def Cube():
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(verticies[vertex])
    glEnd()

def main():
    pygame.init()
    display = (800,600)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)

    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)

    glTranslatef(0.0,0.0, -5)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()

        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        Cube()
        pygame.display.flip()
        pygame.time.wait(10)

main()
jonwright commented 3 years ago

If I get some time I will see if I can translate it and add it as another example here. Perhaps the defaults for viewport and geometry are different. What is the glLoadIdentity line doing after the rotate in your tk version ?

atv2016 commented 3 years ago

That would be great. I just noticed if i add a glMatrixMode(GL_PROJECTION) after my glClear in the tkcode example, the 3d cube shows up (but still not rotating).

i.e.

def redraw(self): display=(800,600) gluPerspective(45,(display[0]/display[1]),0.1,50.0) glTranslatef(0.0,0.0,-5) glRotatef(1,3,1,1) glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT) glMatrixMode(GL_PROJECTION) glLoadIdentity()

jonwright commented 3 years ago

Can you let me know if this works for you please? If it looks OK can we add it as an example?

#!/usr/bin/python3

import tkinter
from pyopengltk import OpenGLFrame
from OpenGL import GL
from OpenGL import GLU

verticies = (    (1, -1, -1),    (1, 1, -1),    (-1, 1, -1),    (-1, -1, -1),
                 (1, -1, 1),    (1, 1, 1),    (-1, -1, 1),    (-1, 1, 1)    )

edges = (    (0,1),    (0,3),    (0,4),    (2,1),    (2,3),    (2,7),
    (6,3),    (6,4),    (6,7),    (5,1),    (5,4),    (5,7)    )

def Cube():
    GL.glBegin(GL.GL_LINES)
    for edge in edges:
        for vertex in edge:
            GL.glVertex3fv(verticies[vertex])
    GL.glEnd()

class CubeSpinner( OpenGLFrame ):
    def initgl(self):
        GLU.gluPerspective(45, (self.width/self.height), 0.1, 50.0)
        GL.glTranslatef(0.0,0.0, -5)
    def redraw(self):
        GL.glRotatef(1, 3, 1, 1)
        GL.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT)
        Cube()

def main():
    frm = CubeSpinner( height = 600, width = 800 )
    frm.animate = 10
    frm.pack()
    return frm.mainloop()

if __name__=="__main__":
    main()
atv2016 commented 3 years ago

Yesterday i ran both demo.py , ran fine, and this one today, also works. Now i'm trying to wrap my head around what the difference is and why this works.

BTW - resizing this messes up the cube in the window.

Thanks Jon.

jonwright commented 3 years ago

This is added in the examples folder now with a fix on resize (forgot to load identity)