0
OpenGLとPyqt5を使用して線を描くことです(MSペイントのように)。 問題はすべて黒いウィンドウです。 glRectf(20,20,-20,20)
のようなものがpaintGL()
に書かれていれば、正しく表示されます。 コード -QGLWidgetが更新されない(Pyqt5とOpenGL)
from OpenGL.GL import *
from PyQt5.QtOpenGL import *
from PyQt5.QtCore import QTimer
a = b = None
class Coordinate():
def __init__(self,x,y):
self.x = x/2 -200 # mapping x [0,800] to [-200,200]
self.y = -2*y/3 + 200 # mapping y [0,600] to [200,-200]
def getx(self):
return self.x
def gety(self):
return self.y
class GlWindow(QGLWidget):
def __init__(self):
print("Entering GL")
super(GlWindow,self).__init__()
self.setAutoBufferSwap(True)
# timer = QTimer()
# timer.timeout.connect(self.update)
# timer.start(10)
def mousePressEvent(self,e):
a = Coordinate(e.x(),e.y())
print(a.getx(),a.gety())
def mouseMoveEvent(self,e):
b = Coordinate(e.x(),e.y())
print(b.getx(),b.gety())
self.updateGL()
def mouseReleaseEvent(self,e):
b = Coordinate(e.x(),e.y())
print(b.getx(),b.gety())
self.updateGL()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glColor3f(0.0,0.0,1.0)
if a and b is not None:
print("Drawing line")
glBegin(GL_LINES)
glVertex2f(a.getx(),a.gety())
glVertex2f(b.getx(),b.gety())
glEnd()
def resizeGL(self, w, h):
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-200, 200, -200, 200, -200.0, 200.0)
glViewport(0, 0, w, h)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def initializeGL(self):
glClearColor(0.10, 0.10, 0.10, 1.0) # Grey
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glShadeModel(GL_SMOOTH)
標準出力 -
Entering GL
66.5 40.0
66.5 40.0
66.5 40.66666666666666
67.0 40.66666666666666
67.5 40.66666666666666
69.0 42.0
70.5 42.66666666666666
72.5 44.66666666666666
74.0 46.0
75.5 46.66666666666666
76.5 48.0
77.5 48.66666666666666
78.5 49.33333333333334
79.0 49.33333333333334
79.0 49.33333333333334
79.5 49.33333333333334
79.5 49.33333333333334
79.5 49.33333333333334
GlWindow()インスタンスが別のウィジェット(メインウィンドウ:QMainWindow、メニューバーなど)で作成され、中央ウィジェットとして設定します。私はQtimerとupdate()を試しましたが、うまくいきませんでした。
あなたは100%確信しています。 'resizeGL'は常に' paintGL'の前に少なくとも一度呼び出されますか? – genpfault
はい、私がしたことは、2つのprintステートメント( "In resizeGL"、 "In paintGL")を加えて両方を印刷したことでした。また、マウスイベントが発生するたびに、「In paintGL」文が印刷されます。ですから、updateGLは動作していますか?私が初心者であることを申し訳ありません。ここで書きましたPyQt4(5ではなく)のウィンドウに三角形を描画する必要があります。 –
[この回答](https://stackoverflow.com/a/46259752/2588654)奇妙に思えるのは、幅と高さを利用する代わりに、その奇妙な 'glOrtho'マトリックスにあなたの座標をマッピングするのがいかがですか?絵が描かれているような気がしますが、ウィンドウの視点で線が描かれていない可能性があります。 – CodeSurgeon