Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在平面上发光的聚光灯_Python_Opengl - Fatal编程技术网

Python 在平面上发光的聚光灯

Python 在平面上发光的聚光灯,python,opengl,Python,Opengl,我想把一个漂亮的聚光灯照在一个平坦的表面上。我知道光照是逐顶点进行的,因此可以在曲面上创建顶点,请参见。然而,我得到了这些——使用GL_四边形和GL_线条带只是为了检查我做的事情是否正确 这些显然相当糟糕。所以 我需要什么机会使聚光灯看起来更像表面上的一个圆 我怎样才能画得更快 注:我意识到,在这种情况下,正常计算并非绝对必要,但在一般情况下,它将是必要的。此外,我还可以为曲面使用显示列表,因此它只绘制一次 #!/usr/bin/env python # -*- coding: utf-8

我想把一个漂亮的聚光灯照在一个平坦的表面上。我知道光照是逐顶点进行的,因此可以在曲面上创建顶点,请参见。然而,我得到了这些——使用GL_四边形和GL_线条带只是为了检查我做的事情是否正确

这些显然相当糟糕。所以

  • 我需要什么机会使聚光灯看起来更像表面上的一个圆
  • 我怎样才能画得更快
注:我意识到,在这种情况下,正常计算并非绝对必要,但在一般情况下,它将是必要的。此外,我还可以为曲面使用显示列表,因此它只绘制一次

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from OpenGL.GLUT import *
from OpenGL.GL import *
from OpenGL.GLU import *
import numpy as np
from Numeric import *

lightPosition = np.array([10, 30, 20, 1])
view_rotation = np.array([0, 0, 0])

def init():
    globAmb = [0.3, 0.3, 0.3, 1.0]
    lightAmb = [0.0, 0.0, 0.0, 1.0]
    lightDifAndSpec = [0.7, 0.7, 0.7, 1.0]

    glutInit()

    glClearColor(0.0, 0.0, 0.0, 0.0)

    glEnable(GL_DEPTH_TEST)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)

    glShadeModel(GL_SMOOTH)

    glEnable(GL_LIGHTING)
    glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDifAndSpec)
    glLightfv(GL_LIGHT0, GL_SPECULAR, lightDifAndSpec)
    glEnable(GL_LIGHT0)
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, globAmb)
    glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE)

    glEnable(GL_CULL_FACE)
    glCullFace(GL_BACK)


def display():
    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glLoadIdentity()
    gluLookAt(0.0, 40.0, 40.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

    glRotatef(view_rotation[0], 1.0, 0.0, 0.0)
    glRotatef(view_rotation[1], 0.0, 1.0, 0.0)
    glRotatef(view_rotation[2], 0.0, 0.0, 1.0)

    glPushMatrix()
    pos = [0, 20, 0, 1]
    direction = [0.0, -1.0, 0.0]
    spotAngle = 20
    glLightfv(GL_LIGHT0, GL_POSITION, pos)
    glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, spotAngle)
    glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, direction)
    glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 2)

    glPushMatrix();
    glDisable(GL_LIGHTING)
    glTranslate(pos[0], 0.5* pos[1], pos[2])
    glRotatef(-90.0, 1.0, 0.0, 0.0)
    glColor3f(1.0, 1.0, 1.0)
    PI = 3.141592
    glutWireCone(3.0 * np.tan( spotAngle/180.0 * PI ), pos[1], 10, 6)
    glEnable(GL_LIGHTING)
    glPopMatrix();

    draw_cube()

    glPopMatrix()
    glFlush ()


def reshape(w, h):
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(w) / float(h), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)


def keyboard(key, x, y):
    if key == chr(27):
        sys.exit(0)
    elif key == 'w':
        view_rotation[0] += 10
        display()
    elif key == 's':
        view_rotation[0] -= 10
        display()
    elif key == 'a':
        view_rotation[1] -= 10
        display()
    elif key == 'd':
        view_rotation[1] += 10
        display()
    else:
        print "Unknown %s key" %(key)

def draw_cube ():
    glPushMatrix()
    glRotatef(45, 0, 1, 0)
    glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, [183/256.0, 65/256.0, 14/256.0, 1.0]);
    glMaterialfv(GL_FRONT, GL_SPECULAR, [1, 1, 1, 1]);
    glMaterialfv(GL_FRONT, GL_SHININESS, [100.0]);
    sz = 10
    step = 1
    for x in arange(-sz, sz, step):
        for z in arange(-sz, sz, step):

            v0 = np.array([x,  sz, z])
            v1 = np.array([x,  sz, z+step])
            v2 = np.array([x+step,  sz, z+step])
            v3 = np.array([x+step,  sz, z])

            #glBegin(GL_QUADS) # Uncomment to get the surface instead of lines.
            glBegin(GL_LINE_STRIP)

            n = get_normal_vector(v0, v1, v3)
            glNormal(n[0], n[1], n[2])
            glVertex3f(v0[0], v0[1], v0[2])

            n = get_normal_vector(v1, v2, v0)
            glNormal(n[0], n[1], n[2])
            glVertex3f(v1[0], v1[1], v1[2])

            n = get_normal_vector(v2, v3, v1)
            glNormal(n[0], n[1], n[2])
            glVertex3f(v2[0], v2[1], v2[2])

            n = get_normal_vector(v3, v0, v2)
            glNormal(n[0], n[1], n[2])
            glVertex3f(v3[0], v3[1], v3[2])
            glEnd()

    glPopMatrix()




def get_normal_vector (v1, v2, v3):
    v = np.cross(v2-v1, v3-v1)
    n = np.sqrt(np.dot(v, v.conj()))
    if n:
        return v/n 
    else:
        print v1
        print v2
        print v3
        print v/n
        sys.exit(-1)


glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)
glutInitWindowSize(800, 800)
glutInitWindowPosition(300, 0)
glutCreateWindow('Lines')
init()
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutKeyboardFunc(keyboard)
glutMainLoop()
PS:我将在答案正常工作时使用着色器用源代码更新答案…

使用。这也是渲染场景的最快方法,因为您不会增加细分,但会获得最高质量的照明

希望这有帮助

使用。这也是渲染场景的最快方法,因为您不会增加细分,但会获得最高质量的照明


希望这有帮助

有Python中的代码示例吗?我找到了一些可能包含我需要的东西。。。你知道有更好的源代码吗?抱歉,我现在还不知道任何python教程:(。但是移植C/C++版本应该相当容易。如果遇到任何问题,请尝试并告诉我!尝试使用其他版本来编译着色器,如“#version 120”或“#version 150”.你有两种不同光源的可用着色器对的例子吗?.有Python代码的例子吗?我找到了一些可能包含我需要的…你知道的更好的源吗?抱歉,我现在还不知道任何Python教程:(。但是移植C/C++版本应该相当容易。尝试一下,如果遇到任何问题,请告诉我!尝试使用不同的版本来编译着色器,如“#version 120”或“#version 150”。您是否有针对两个不同光源的可用着色器对的示例。