Search code examples
pythonopenglpyopengl

pyopengl - run with no texture


my minimal code:

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()

It should draw a cube with green border,but it just drew a pink cube(glColorPointer did it) with no green border.I tried many ways to do it,but it always can't follow my mind.Please answer with a minimal example based on my original code!⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀


Solution

  • When using legacy OpenGL, two-dimensional texturing must be enabled befor drawing the geometry:

    glEnable(GL_TEXTURE_2D)
    

    This sets a global state. So if you want to draw subsequent geometry without texture, you need to turn it off again:

    glDisable(GL_TEXTURE_2D)
    

    It should be mentioned that by default the texture color is modulated with the current color. So it makes sense to use a white color before drawing an object with a texture. Likewise you can change the environment mode to GL_REPLACE, instead:

    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);