mieki256's diary



2022/09/15(木) [n年前の日記]

#1 [python] PyOpenGLで球を描画

Windows10 x64 21H2 + Python 3.9.13 64bit + PyOpenGL 3.1.6 で球を描画できるか実験。

結果画面は以下。

ソース。 :

ソースは以下。

_01_draw_sphere.py
import sys
import math
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *

# SCRW, SCRH = 1280, 720
SCRW, SCRH = 512, 288
FPS = 60

ang = 0.0
window = 0


def draw_func():
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    
    # set light
    glDisable(GL_LIGHTING)
    glEnable(GL_LIGHTING)
    glEnable(GL_NORMALIZE)
    glLightfv(GL_LIGHT0, GL_POSITION, [0.25, 1.0, 0.5, 0])
    glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 1, 1, 1])
    glLightfv(GL_LIGHT0, GL_SPECULAR, [1, 1, 1, 1])
    glEnable(GL_LIGHT0)

    glLoadIdentity()
    
    # rotate camera position
    r = 30
    x = r * math.cos(math.radians(ang))
    y = r * 0.5
    z = r * math.sin(math.radians(ang))
    tx, ty, tz = 0, 0, 0
    gluLookAt(x, y, z, tx, ty, tz, 0, 1, 0)

    glScalef(1.0, 1.0, 1.0)

    # set color
    green = [0.0, 1.0, 0.0, 1.0]
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, green)

    # draw sphere
    quadric = gluNewQuadric()
    # radius = 0.25 * math.sin(math.radians(ang * 8)) + 1.0
    radius = 4 * math.sin(math.radians(ang * 8)) + 5.0
    slices = 32
    stacks = 16
    gluSphere(quadric, radius, slices, stacks)
        
    glDisable(GL_LIGHT0)
    glDisable(GL_LIGHTING)
    
    # set color
    c = 0.5 * math.cos(math.radians(ang * 2)) + 0.5
    glColor3f(0.0, c, 1.0 - c)
    # glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, [0.0, 1.0, 1.0, 1.0])

    # draw cube
    glutWireCube(20)

    glutSwapBuffers()


def init_GL():
    global window

    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    # glutInitWindowPosition(0, 0)
    glutInitWindowSize(SCRW, SCRH)
    window = glutCreateWindow("Draw sphere")

    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)

    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)

    glShadeModel(GL_SMOOTH)
    # glShadeModel(GL_FLAT)

    glDisable(GL_LIGHTING)
    glEnable(GL_LIGHTING)
    glEnable(GL_NORMALIZE)

    # glEnable(GL_CULL_FACE)
    # glCullFace(GL_BACK)

    glViewport(0, 0, SCRW, SCRH)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, float(SCRW) / float(SCRH), 0.1, 100.0)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()


def on_timer(value):
    global ang
    ang += 0.5
    glutPostRedisplay()
    glutTimerFunc(int(1000 / FPS), on_timer, 0)


def key_func(key, x, y):
    global window

    ESCAPE = b"\x1b"
    print("Push key,", key, x, y)
    if key == ESCAPE or key == b"q":
        print("Exit")
        if glutLeaveMainLoop:
            glutLeaveMainLoop()
        else:
            sys.exit()


def main():
    # main
    init_GL()
    glutKeyboardFunc(key_func)
    glutDisplayFunc(draw_func)
    glutTimerFunc(int(1000 / FPS), on_timer, 0)
    glutMainLoop()


if __name__ == "__main__":
    main()

python 01_draw_sphere.py で実行。

球の描画は何を使うのかググってみたところ、gluSphere() が使えるっぽい。

以上です。

過去ログ表示

Prev - 2022/09 - Next
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project