简体   繁体   English

py2exe和PyOpenGL

[英]py2exe and PyOpenGL

I have tried using py2exe to make an executable of my PyOpenGL project, and I did... The problem is, it is not running... The exe is not running and i am getting this log: 我尝试使用py2exe来制作我的PyOpenGL项目的可执行文件,但我确实...问题是,它没有运行... exe没有运行,并且我收到此日志:

Traceback (most recent call last):
File "gl.py", line 1, in <module>
ImportError: No module named OpenGL.GL

setup.py looks like this: setup.py看起来像这样:

from distutils.core import setup
import py2exe

setup(windows=['gl.py'],
      options={
          "py2exe": {
              "includes": ["ctypes", "logging"],
              "excludes": ["OpenGL"],
              }
          }
      )

My project looks like this: 我的项目如下所示:

from OpenGL.GL import *
from OpenGL.GLU import *
import random
from math import * 

import pygame

import Image
import sys
import time


sys.path += ['.']


pygame.init() 
pygame.display.set_mode((800,600), pygame.OPENGL|pygame.DOUBLEBUF)

def jpg_file_write(name, number, data):
    im = Image.frombuffer("RGBA", (800,600), data, "raw", "RGBA", 0, 0)
    fnumber = "%05d" % number
    im.save(name + fnumber + ".jpg")


glEnable(GL_DEPTH_TEST)


def createAndCompileShader(type,source):
    shader=glCreateShader(type)
    glShaderSource(shader,source)
    glCompileShader(shader)



    result=glGetShaderiv(shader,GL_COMPILE_STATUS)

    if (result!=1): 
        raise Exception("Greska u kompajliranju... \nLog:\n"+glGetShaderInfoLog(shader))
    return shader


vertex_shader=createAndCompileShader(GL_VERTEX_SHADER,"""
varying vec3 v;
varying vec3 N;

void main(void)
{

   v = gl_ModelViewMatrix * gl_Vertex;
   N = gl_NormalMatrix * gl_Normal;

   gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;

}
""");

fragment_shader=createAndCompileShader(GL_FRAGMENT_SHADER,"""
varying vec3 N;
varying vec3 v;

void main(void)
{
   vec3 L = gl_LightSource[0].position.xyz-v;

   // "Lambert's law"? (see notes)
   // Rather: faces will appear dimmer when struck in an acute angle
   // distance attenuation

   float Idiff = max(dot(normalize(L),N),0.0)*pow(length(L),-2.0); 

   gl_FragColor = vec4(0.5,0,0.5,1.0)+ // purple
                  vec4(1.0,1.0,1.0,1.0)*Idiff; // diffuse reflection
}
""");



program=glCreateProgram()
glAttachShader(program,vertex_shader)
glAttachShader(program,fragment_shader)
glLinkProgram(program)


try:
    glUseProgram(program)   
except OpenGL.error.GLError:
    print glGetProgramInfoLog(program)
    raise

done = False

t=0 



glNewList(1,GL_COMPILE)

glBegin(GL_QUADS)

glColor3f(1,1,1)

glNormal3f(0,0,-1)
glVertex3f( -1, -1, -1)
glVertex3f(  1, -1, -1)
glVertex3f(  1,  1, -1)
glVertex3f( -1,  1, -1)

glNormal3f(0,0,1)
glVertex3f( -1, -1,  1)
glVertex3f(  1, -1,  1)
glVertex3f(  1,  1,  1)
glVertex3f( -1,  1,  1)

glNormal3f(0,-1,0) 
glVertex3f( -1, -1, -1)
glVertex3f(  1, -1, -1)
glVertex3f(  1, -1,  1)
glVertex3f( -1, -1,  1)

glNormal3f(0,1,0) 
glVertex3f( -1,  1, -1)
glVertex3f(  1,  1, -1)
glVertex3f(  1,  1,  1)
glVertex3f( -1,  1,  1)

glNormal3f(-1,0,0)     
glVertex3f( -1, -1, -1)
glVertex3f( -1,  1, -1)
glVertex3f( -1,  1,  1)
glVertex3f( -1, -1,  1)                      

glNormal3f(1,0,0)        
glVertex3f(  1, -1, -1)
glVertex3f(  1,  1, -1)
glVertex3f(  1,  1,  1)
glVertex3f(  1, -1,  1)

glEnd()
glEndList()

while not done:

    t=t+1

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90,1,0.01,1000)
    gluLookAt(sin(t/260.0)*4,cos(t/260.0)*4,cos(t/687.0)*3,0,0,0,0,1,0)

    glClearColor(0.0, 0.0, 0.0, 1.0)
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

    glMatrixMode(GL_MODELVIEW)



    ld=[sin(t/16.0)*4.0,sin(t/20.0)*4.0,cos(t/16.0)*4.0]



    glLightfv(GL_LIGHT0,GL_POSITION,[ld[0],ld[1],ld[2]]);



    glColor3f(1,1,1)

    glLoadIdentity()


    for i in range(-5,5):
        for j in range(-5,5):
            for k in range(-5,5):
                glPushMatrix()

                glTranslate(i,j,k)
                glScale(0.1,0.1,0.1)
                glCallList(1)
                glPopMatrix()

    pygame.display.flip()

Does anyone know what might be causing this problem? 有谁知道可能是什么原因导致此问题?

When you exclude the package in py2exe you need to then manually add it to the resulting "dist" directory. 当您在py2exe中排除该软件包时,您需要手动将其添加到生成的“ dist”目录中。 That is, you would copy the whole of the OpenGL package from your site-packages directory into the py2exe dist directory. 也就是说,您将整个OpenGL软件包从您的site-packages目录复制到py2exe dist目录中。

I've done an experiment with PyOpenGL 3.1.0b2 and py2exe using explicit includes such that only those parts of PyOpenGL you use are included. 我已经使用显式包含对PyOpenGL 3.1.0b2和py2exe进行了实验,以便包含您使用的PyOpenGL的那些部分。 The results are written up in a blog post . 结果写在博客文章中 For your purposes (using Pygame) you shouldn't need to worry about the lack of GLUT/GLE DLLS, and on a real Win32 machine it may simply work as expected (the tests failed on my VM). 出于您的目的(使用Pygame),您不必担心缺少GLUT / GLE DLLS,并且在一台真正的Win32计算机上,它可以按预期工作(在我的VM上测试失败)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM