如何使用Pyglet在OpenGL中制作阴影?

时间:2019-05-20 22:49:51

标签: python python-3.x opengl pyglet opengl-compat

这更多是出于好奇,我在大学里做计算机图形学,在那里学习了OpenGL,并且我使用Python来实现(更具体地讲是Pyglet库),在这个过程中,我对该主题产生了兴趣,并经过了几次测试现在,我试图了解阴影的映射方式,无法理解互联网的教程,或多或少地了解了理论部分,但实际上我仍然没有得到这样的代码:

import pyglet
from pyglet.gl import *

t = pyglet.window.Window(height=500, width=500)
glEnable(GL_DEPTH_TEST)
glEnable(GL_LIGHTING)
glEnable(GL_LIGHT0)
rodar = 0

@t.event
def on_draw():
    global rodar
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45, 1, 0.1, 2000)
    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
    gluLookAt(0,0,25,0,0,-1,0,1,0)
    glPushMatrix()
    glLightfv(GL_LIGHT0, GL_AMBIENT, (GLfloat*4)(.4,.4,.4,1))
    glLightfv(GL_LIGHT0, GL_DIFFUSE, (GLfloat*4)(1,0,0,1))
    glLightfv(GL_LIGHT0, GL_SPECULAR, (GLfloat*4)(0,0,0,1))
    glLightfv(GL_LIGHT0, GL_POSITION, (GLfloat*4)(50,0,0,1))
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, (GLfloat*3)(-40,0,0))
    glPopMatrix()
    glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, (GLfloat*4)(.6,.1,.1,1))
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, (GLfloat*4)(.7,.7,.7,1))
    glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, (GLfloat*1)(1))
    # cubo
    rodar+=1
    glPushMatrix()
    glRotatef(rodar, .8, .5, 0)
    glBegin(GL_POLYGON)
    glNormal3f(0,1,0)
    glVertex3f(2,2,2)
    glVertex3f(2,2,-2)
    glVertex3f(-2,2,-2)
    glVertex3f(-2,2,2)
    glEnd()
    glBegin(GL_POLYGON)
    glNormal3f(0,-1,0)
    glVertex3f(2,-2,2)
    glVertex3f(2,-2,-2)
    glVertex3f(-2,-2,-2)
    glVertex3f(-2,-2,2)
    glEnd()
    glBegin(GL_POLYGON)
    glNormal3f(1,0,0)
    glVertex3f(2,-2,2)
    glVertex3f(2,-2,-2)
    glVertex3f(2,2,-2)
    glVertex3f(2,2,2)
    glEnd()
    glBegin(GL_POLYGON)
    glNormal3f(-1,0,0)
    glVertex3f(-2,-2,2)
    glVertex3f(-2,-2,-2)
    glVertex3f(-2,2,-2)
    glVertex3f(-2,2,2)
    glEnd()
    glBegin(GL_POLYGON)
    glNormal3f(0,0,1)
    glVertex3f(2,-2,2)
    glVertex3f(-2,-2,2)
    glVertex3f(-2,2,2)
    glVertex3f(2,2,2)
    glEnd()
    glBegin(GL_POLYGON)
    glNormal3f(0,0,-1)
    glVertex3f(2,-2,-2)
    glVertex3f(-2,-2,-2)
    glVertex3f(-2,2,-2)
    glVertex3f(2,2,-2)
    glEnd()
    glPopMatrix()
    # local onde a sombra vai ficar
    glBegin(GL_POLYGON)
    glNormal3f(1,0,0)
    glVertex3f(-5,-6,6)
    glVertex3f(-5,-6,-6)
    glVertex3f(-5,6,-6)
    glVertex3f(-5,6,6)
    glEnd()

def SRO(dt):
    on_draw()

pyglet.clock.schedule_interval(SRO, 1/60)

pyglet.app.run()

我的疑问是如何在他这一边的多边形中创建立方体的阴影?我想以我理解得更好的代码形式得到答案,另一个问题是您是否可以使用Buffer Stencil来做到这一点?好吧,我在某些网站上看到了这个,我很好奇,但我不明白如何只隐藏阴影会保留的部分

0 个答案:

没有答案