pyopengl - 运行 没有纹理

pyopengl - run with no texture

我的最小代码:

from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
from PIL import Image
from PIL import ImageDraw
import numpy

rotate=[33,40,20]

block_VAO=0
draw=False
block_EBO_buffer_len=0
texture_VBO=0

def create_block_texture():
    block=Image.new("RGB",(100,100),"white")
    draw=ImageDraw.Draw(block)
    draw.line([5,5,5,95,95,95,95,5,5,5],(0,255,0),10)
    pixels=block.load()
    all_pixels=[]
    for x in range(100):
        for y in range(100):all_pixels+=list(pixels[x,y])
    return bytes(all_pixels)

def create_blocks(x: int,y: int,z: int):
    global draw,block_VAO,block_EBO_buffer_len,texture_VBO
    if draw:
        return
    draw=True
    block_point_buffer=[]
    block_color_buffer=[]
    block_EBO_buffer=[]
    block_point_buffer+=[x-0.5,y+0.5,z-0.5,  #V0
                         x+0.5,y+0.5,z-0.5,  #V1
                         x+0.5,y-0.5,z-0.5,  #V2
                         x-0.5,y-0.5,z-0.5,  #V3
                         x-0.5,y+0.5,z+0.5,  #V4
                         x+0.5,y+0.5,z+0.5,  #V5
                         x+0.5,y-0.5,z+0.5,  #V6
                         x-0.5,y-0.5,z+0.5]  #V7
    block_EBO_buffer+=[0,1,5,4,
                       3,2,6,7,
                       0,3,7,4,
                       1,2,6,5,
                       0,1,2,3,
                       4,5,6,7]

    block_color_buffer+=[1.0,0.0,1.0,1.0]*8

    texture_VBO=glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D,texture_VBO)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,100,100,0,GL_RGB,GL_UNSIGNED_BYTE,create_block_texture())
    glGenerateMipmap(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D,0)

    texture_EBO=glGenBuffers(1)
    texture_coord=[
        0.0,1.0,
        1.0,1.0,
        1.0,0.0,
        0.0,0.0,
        0.0,0.0
    ]
    glBindBuffer(GL_ARRAY_BUFFER,texture_EBO)
    a=numpy.array(texture_coord,dtype='float32')
    glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)

    block_VBO=glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
    a=numpy.array(block_point_buffer,dtype='float32')
    glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)

    color_VBO=glGenBuffers(1)
    glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
    a=numpy.array(block_color_buffer,dtype='float32')
    glBufferData(GL_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)

    block_VAO=glGenVertexArrays(1)
    glBindVertexArray(block_VAO)

    block_EBO=glGenBuffers(1)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,block_EBO)
    a=numpy.array(block_EBO_buffer,dtype='uint32')
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,sys.getsizeof(a),a,GL_STATIC_DRAW)
    block_EBO_buffer_len=len(a)

    glBindBuffer(GL_ARRAY_BUFFER,block_VBO)
    glVertexPointer(3,GL_FLOAT,0,None)
    glEnableClientState(GL_VERTEX_ARRAY)

    glBindBuffer(GL_ARRAY_BUFFER,color_VBO)
    glColorPointer(4,GL_FLOAT,0,None)
    glEnableClientState(GL_COLOR_ARRAY)

    glBindBuffer(GL_ARRAY_BUFFER,texture_EBO)
    glTexCoordPointer(2,GL_FLOAT,0,None)
    glEnableClientState(GL_TEXTURE_COORD_ARRAY)

    glBindVertexArray(0)


def display():
    glMatrixMode(GL_MODELVIEW)
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glTranslatef(0,0,-4.5)
    glRotatef(rotate[0],1,0.0,0)
    glRotatef(rotate[1],0,1,0)
    glRotatef(rotate[2],0,0,1)
    glScalef(1,1,1)

    glBindVertexArray(block_VAO)
    glBindTexture(GL_TEXTURE_2D,texture_VBO)
    glDrawElements(GL_QUADS,block_EBO_buffer_len,GL_UNSIGNED_INT,None)
    glBindVertexArray(0)
    rotate[1]+=0.1

    glutSwapBuffers()
    glutPostRedisplay()


def reshape(width,height):
    glViewport(0,0,width,height)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(40.0,width/height,0.5,20.0)
    glMatrixMode(GL_MODELVIEW)


glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB)
glutInitWindowSize(400,350)
glutCreateWindow(b"OpenGL Window")
create_blocks(0,0,0)
glClearColor(0.0,0.0,0.0,0.0)
glutDisplayFunc(display)
glutReshapeFunc(reshape)
glutMainLoop()

它应该画一个绿色边框的立方体,但它只是画了一个没有绿色的粉红色立方体(glColorPointer 做到了)border.I尝试了很多方法,但它总是不能跟随我的mind.Please根据我的原始代码用一个最小的例子回答!⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀

使用旧版 OpenGL 时,two-dimensional 必须在绘制几何之前启用纹理:

glEnable(GL_TEXTURE_2D)

这会设置一个全局状态。所以如果想绘制后续的没有纹理的几何体,需要再次关闭:

glDisable(GL_TEXTURE_2D)

应该提到的是,默认情况下纹理颜色是用当前颜色调制的。所以在绘制带有纹理的对象之前使用白色是有意义的。同样,您可以将环境模式更改为 GL_REPLACE,而不是:

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);