Загрузка...

Помогите с PyOpengl (для тех, кто сталкивался)

Тема в разделе Программирование создана пользователем Lenin_inactive44618 28 июн 2017. (поднята 28 июн 2017) 226 просмотров

  1. Lenin_inactive44618
    Lenin_inactive44618 Автор темы 28 июн 2017 #СЛАВАКПСС 289 28 июл 2016
    Суть такая, мне нужно сделать домик с помощью PyOpengl (Python). Есть код, не мой конечно, но точно рабочий.
    Код

    from OpenGL.GL import *
    from OpenGL.GLU import *
    from OpenGL.GLUT import *
    import sys
    from PIL import Image, ImageDraw


    global house
    global xrot
    global yrot
    global ambient
    global lightpos
    global xscaled
    global yscaled
    global zscaled

    texture=0
    texture2=0
    texture3=0
    ##############################################
    def init():
    global xrot
    global yrot
    global ambient
    global lightpos
    global wallcolor
    global roofcolor
    global xscaled
    global yscaled
    global zscaled


    xrot = 0.0
    yrot = 0.0
    xscaled = 1.0
    yscaled = 1.0
    zscaled = 1.0
    ambient = (1.0, 1.0, 1.0)
    lightpos = (3.0, 3.0, 3.0)

    glEnable(GL_TEXTURE_2D)
    glClearColor(1.0, 1.0, 1.0, 1.0)
    glClearDepth(1.0)
    glOrtho(-3., 3., -3., 3., -10., 10.)
    glRotatef(-10, 1.0, 1.0, 1.0)
    ##############################################
    def load_textures(fileName: object) -> object:
    image=Image.open(fileName)
    width = image.size[0]
    height = image.size[1]
    image = image.tostring("raw", "RGBX", 0, -1)
    texture = glGenTextures(1)

    # Create Texture
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1)) # 2d texture (x and y size)

    glBindTexture(GL_TEXTURE_2D, texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image)

    return texture

    def load_textures2(fileName1):
    image1=Image.open(fileName1)
    width1 = image1.size[0]
    height1 = image1.size[1]
    image1 = image1.tostring("raw", "RGBX", 0, -1)
    texture2 = glGenTextures(1)

    # Create Texture
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1)) # 2d texture (x and y size)

    glBindTexture(GL_TEXTURE_2D, texture2)
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width1, height1, GL_RGBA, GL_UNSIGNED_BYTE, image1)
    return texture2

    def load_textures3(fileName1):
    image=Image.open(fileName1)
    width = image.size[0]
    height = image.size[1]
    image = image.tostring("raw", "RGBX", 0, -1)
    texture3 = glGenTextures(1)

    # Create Texture
    glBindTexture(GL_TEXTURE_2D, glGenTextures(1)) # 2d texture (x and y size)
    glBindTexture(GL_TEXTURE_2D, texture3)
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image)
    return texture3

    ##############################
    def specialkeys(key, x, y):
    global xrot
    global yrot
    global xscaled
    global yscaled
    global zscaled
    if key == GLUT_KEY_UP:
    xrot -= 2.0
    if key == GLUT_KEY_DOWN:
    xrot += 2.0
    if key == GLUT_KEY_LEFT:
    yrot -= 2.0
    if key == GLUT_KEY_RIGHT:
    yrot += 2.0
    if key == GLUT_KEY_F1:
    xscaled += 0.05
    yscaled += 0.05
    zscaled += 0.05
    if key == GLUT_KEY_F2:
    xscaled -= 0.05
    yscaled -= 0.05
    zscaled -= 0.05

    glutPostRedisplay()
    ##############################
    def draw():
    global xrot
    global yrot
    global xscaled
    global yscaled
    global zscaled

    glEnable(GL_DEPTH_TEST)
    glDepthFunc(GL_LESS)
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
    glPushMatrix()
    glRotatef(xrot, 1.0, 0.0, 0.0)
    glRotatef(yrot, 0.0, 1.0, 0.0)
    glScaled(xscaled, yscaled, zscaled,)

    glBindTexture(GL_TEXTURE_2D, texture)
    glBegin(GL_POLYGON)
    glTexCoord2f(1.0, 1.0); glVertex3f(0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, -0.5)
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, 0.5, -0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, -0.5, -0.5)

    glEnd()


    glBegin(GL_POLYGON)

    glTexCoord2f(1.0, 1.0);glVertex3f(0.5, -0.5, 0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, 0.5)
    glTexCoord2f(0.0, 0.0);glVertex3f(-0.5, 0.5, 0.5)
    glTexCoord2f(0.0, 1.0);glVertex3f(-0.5, -0.5, 0.5)

    glEnd()


    glBegin(GL_POLYGON)
    glTexCoord2f(1.0, 1.0); glVertex3f(0.5, -0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(0.5, 0.5, -0.5)
    glTexCoord2f(0.0, 0.0); glVertex3f(0.5, 0.5, 0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(0.5, -0.5, 0.5)

    glEnd()

    glBindTexture(GL_TEXTURE_2D, texture2)
    glBegin(GL_POLYGON)
    glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.5)
    glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, 0.5)
    glTexCoord2f(1.0, 1.0); glVertex3f(-0.5, 0.5, -0.5)
    glTexCoord2f(1.0, 0.0); glVertex3f(-0.5, -0.5, -0.5)

    glEnd()


    glBegin(GL_POLYGON)
    glColor3f(0.55, 0.55, 0.55)
    glVertex3f(0.5, 0.5, 0.5)
    glVertex3f(0.5, 0.5, -0.5)
    glVertex3f(-0.5, 0.5, -0.5)
    glVertex3f(-0.5, 0.5, 0.5)
    glEnd()


    glBegin(GL_POLYGON)
    glColor3f(0.55, 0.55, 0.55)
    glVertex3f(0.5, -0.5, -0.5)
    glVertex3f(0.5, -0.5, 0.5)
    glVertex3f(-0.5, -0.5, 0.5)
    glVertex3f(-0.5, -0.5, -0.5)
    glEnd()

    #triangle roof(front)
    glBegin(GL_POLYGON)
    glColor3f(1.0, 0.55, 0.55)
    glVertex3f(0.6, 0.5, 0.5)
    glVertex3f(0.0, 1.5, 0.5)
    glVertex3f(-0.6, 0.5, 0.5)
    glEnd()

    #triangle roof(front)
    glBegin(GL_POLYGON)
    glColor3f(1.0, 0.55, 0.55)
    glVertex3f(0.6, 0.5, -0.5)
    glVertex3f(0.0, 1.5, -0.5)
    glVertex3f(-0.6, 0.5, -0.5)
    glEnd()

    #roof
    glBegin(GL_POLYGON)
    glColor3f(1.0, 0.55, 0.55)
    glVertex3f(0.6, 0.5, -0.7)
    glVertex3f(0.0, 1.5, -0.7)
    glVertex3f(0.0, 1.5, 0.7)
    glVertex3f(0.6, 0.5, 0.7)
    glEnd()

    #roof
    glBegin(GL_POLYGON)
    glColor3f(1.0, 0.55, 0.55)
    glVertex3f(-0.6, 0.5, -0.7)
    glVertex3f(0.0, 1.5, -0.7)
    glVertex3f(0.0, 1.5, 0.7)
    glVertex3f(-0.6, 0.5, 0.7)
    glEnd()

    #groung
    glBindTexture(GL_TEXTURE_2D, texture3)
    glBegin(GL_POLYGON)
    glTexCoord2f(6, 6); glVertex3f(3, -0.5, -3)
    glTexCoord2f(6, 0.0); glVertex3f(3, -0.5, 3)
    glTexCoord2f(0.0, 0.0); glVertex3f(-3, -0.5, 3)
    glTexCoord2f(0.0, 6); glVertex3f(-3, -0.5, -3)
    glEnd()


    glPopMatrix()
    glutSwapBuffers()


    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)

    glutInitWindowSize(1500, 1500)

    glutInitWindowPosition(50, 50)

    glutInit(sys.argv)

    glutCreateWindow(b"box")

    glutDisplayFunc(draw)

    glutSpecialFunc(specialkeys)

    init()

    texture = load_textures("wind.jpg")
    texture2 = load_textures2("wind2.jpg")
    texture3 = load_textures3("grass.jpg")
    glutMainLoop()

    Все библеотеки, указанные в import, установлены. В результате выходит это:
    [IMG]
    И в довесок следующая ошибка:
    Код
    Traceback (most recent call last):
    File "C:\Users\Roman\Desktop\дом\srefr.py", line 264, in <module>
    texture = load_textures("wind.jpg")
    File "C:\Users\Roman\Desktop\дом\srefr.py", line 51, in load_textures
    image = image.tostring("raw", "RGBX", 0, -1)
    File "C:\Programs\Python\Python36\lib\site-packages\PIL\Image.py", line 709, in tostring
    raise NotImplementedError("tostring() has been removed. "
    NotImplementedError: tostring() has been removed. Please call tobytes() instead.
    Как понял я, проблема в PIL, возможно разница версий, может и нет (пробовал разные). Собственно хочу узнать, у знающих людей, в чем моя проблема.
     
Загрузка...
Top