'전체 글'에 해당되는 글 514건

  1. 2023.09.11 Python PyOpenGL

Python PyOpenGL

Python 2023. 9. 11. 22:59 |
반응형

파이썬에서 OpenGL을 사용해 보자.

 

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
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
 
def Display():
    glClear(GL_COLOR_BUFFER_BIT)
 
    glBegin(GL_QUADS)
    glVertex2f(0.50.5)
    glVertex2f(-0.50.5)
    glVertex2f(-0.5-0.5)
    glVertex2f(0.5-0.5)
    glEnd()
    
    glFlush()
    
if __name__ == "__main__":
    glutInit()
 
    #glutInitDisplayMode(GLUT_RGB)
    #glutInitWindowSize(640, 480)
    #glutInitWindowPosition(400, 300)
    glutCreateWindow("OpenGL")
    glutDisplayFunc(Display)
    glutMainLoop()
 

 

 

윈도우에 사각형이 표시된다.

 

pip install PyOpenGL
pip install PyOpenGL_accelerate

위 명령으로 공식 PyOpenGL, PyOpenGL_accelerate를 설치하면 OpenGL.GLUT에 문제가 있는 경우가 있다. 예를 들어 glutinit()를 실행하면 아래와 같은 에러가 발생한다.

NullFunctionError: Attempt to call an undefined function glutInit, check for bool(glutInit) before calling.

만약 에러가 발생한다면 설치한 패키지를 삭제하고 아래 링크에서 받아 다시 설치한다.

Unofficial Windows Binaries for Python Extension Packages

 

PyOpenGL_accelerate-3.1.6-cp311-cp311-win_amd64.whl
0.34MB
PyOpenGL-3.1.6-cp311-cp311-win_amd64.whl
2.64MB

 

 

하지만 OpenGL.GLUT을 사용하지 않는다면 공식 패키지를 사용해도 상관 없다. Pygame을 사용해 보자.

 

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
31
32
33
34
35
36
37
38
39
import pygame
from pygame.locals import *
 
from OpenGL.GL import *
from OpenGL.GLU import *
 
def Display():
    glBegin(GL_QUADS)
    glVertex2f(0.50.5)
    glVertex2f(-0.50.5)
    glVertex2f(-0.5-0.5)
    glVertex2f(0.5-0.5)
    glEnd()
 
def main():
    pygame.init()
    display = (640,480)
    pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
    clock = pygame.time.Clock()
 
    gluPerspective(45, (display[0]/display[1]), 0.150.0)
 
    glTranslatef(0.00.0-5)
 
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
 
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
        glRotatef(1312)
        Display()
 
        pygame.display.flip()
        clock.tick(30)
 
if __name__ == '__main__':
   main()
 

 

 

 

※ 참고

OpenGL 강좌

 

반응형
Posted by J-sean
: