상세 컨텐츠

본문 제목

[Python OpenGL] Python을 활용하여 OpenGL로 주전자 만들기 2편

Python

by donggyu1998 2021. 9. 11. 15:32

본문

반응형

💡 글 작성자는 vscode를 활용하여 Python을 작성하고있습니다.

이전 게시글 : https://donggyu.tistory.com/84

💡 [실행 화면]

(실행하면 360도 돌아가는 주전자이다.)

💡 [코드]

from OpenGL.GLUT import *
from OpenGL.GL import *

angle = 0.0

def draw():
    glClear(GL_COLOR_BUFFER_BIT)
    glClear(GL_DEPTH_BUFFER_BIT)

    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LEQUAL)

    glEnable(GL_CULL_FACE)
    glCullFace(GL_BACK)

    glLoadIdentity()

    glPushMatrix()
    glRotatef(-45.0, 1.0, 0.0, 0.0)
    glTranslatef(0.5, 0.0, 0.0)
    glRotatef(angle, 0.0, 1.0, 0.0)
    glColor3f(1.0, 0.0, 0.0) 
    glutSolidTeapot(0.3)
    glPopMatrix()

    glPushMatrix()
    glRotatef(0.0, 1.0, 0.0, 0.0)
    glTranslatef(-0.5, 0.0, 0.0)
    glRotatef(-angle, 0.0, 1.0, 0.0)
    glColor3f(0.0, 0.0, 1.0) 
    glutSolidTeapot(0.3)
    glPopMatrix()

    glutSwapBuffers()

def mtimer(self):
    global angle
    angle += 1.0
    if angle >= 360.0:
        angle = 0.0
        print(angle)
    glutPostRedisplay()
    glutTimerFunc(20, mtimer, 1)

def init_gl():
    glShadeModel(GL_SMOOTH)
    
    ambient = [0.25, 0.25, 0.0, 1.0]
    diffuse = [0.9, 0.9, 0.0, 1.0]
    position = [0.0, 7.0, 0.0, 1.0]

    glEnable(GL_LIGHTING)
    glLightfv(GL_LIGHT0, GL_AMBIENT, ambient)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse)
    glLightfv(GL_LIGHT0, GL_POSITION, position)
    glEnable(GL_LIGHT0)

#print(angle)
glutInit()
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)
glutCreateWindow("OpenGL")
init_gl()
glutDisplayFunc(draw)
glutTimerFunc(20, mtimer, 1)
glutMainLoop()
반응형

관련글 더보기