1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PythonでOpenGL

Posted at

マウス座標を取得して、その場所になにか描画するサンプルをpythonでしたくて、openGLを使った話です。

環境

Mac
PyOpenGL
glfl

ライブラリのインストール

pip install PyOpenGL PyOpenGL_accelerate
pip show PyOpenGL

Version: 3.1.7

pip install glfw
pip show glfw

Version: 2.7.0

python
import glfw
from OpenGL.GL import *

def main():
    glfw.init()
    window = glfw.create_window(640, 640, "title", None, None)

    glfw.make_context_current(window)
    glfw.set_cursor_pos_callback(window, mouseFunc)
    glfw.set_key_callback(window, keyFunc)

    while not glfw.window_should_close(window):
        # pyOpenGL Command
        glfw.swap_buffers(window)
        glfw.poll_events()

    glfw.terminate()

def keyFunc(window, key, scancode, action, mode):
    if key == glfw.KEY_ESCAPE:
        glfw.set_window_should_close(window, True)

def mouseFunc(window, mouseX, mouseY):
    print(str(mouseX)+","+str(mouseY))

    glViewport(0, 0, 1280, 1280)
    glClear(GL_COLOR_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(-1, 1, 1, -1, 1, -1)
    glMatrixMode(GL_MODELVIEW)

    glLoadIdentity()
    glTranslatef(mouseX/320.0-1, mouseY/320.0-1, 0)

    glBegin(GL_TRIANGLES)
    glColor3f(1, 1, 1)
    glVertex3f(0, 0, 0)
    glColor3f(1, 0, 0)
    glVertex3f(0.1, 0, 0)
    glColor3f(0, 1, 0)
    glVertex3f(0, 0.1, 0)
    glEnd()

if __name__ == "__main__":
    main()

image.png

その他

20年以上前のCの知識から、飛んできたので、ちょっとぐちゃぐちゃです。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?