0

鉴于以下代码段:

import os
import textwrap

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

from OpenGL.GL.ARB.multitexture import *
from OpenGL.extensions import alternate


def get_opengl_info():
    return textwrap.dedent("""\
        Vendor: {0}
        Renderer: {1}
        OpenGL Version: {2}
        Shader Version: {3}
        {4:*^80}
        Num Extensions: {5}
        {6}
    """).format(
        glGetString(GL_VENDOR).decode("utf-8"),
        glGetString(GL_RENDERER).decode("utf-8"),
        glGetString(GL_VERSION).decode("utf-8"),
        glGetString(GL_SHADING_LANGUAGE_VERSION).decode("utf-8"),
        "OPENGL EXTENSIONS",
        glGetIntegerv(GL_NUM_EXTENSIONS),
        "\n".join(glGetString(GL_EXTENSIONS).decode("utf-8").split())
    )


def create_gl_texture(use_active_texture, channel, width, height, pbits):
    id_texture = glGenTextures(1)
    if use_active_texture:
        glActiveTexture(GL_TEXTURE0 + channel)
    glBindTexture(GL_TEXTURE_2D, id_texture)
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1)
    glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, pbits)
    glTexParameter(
        GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameter(
        GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT)
    glTexParameter(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT)
    glGenerateMipmap(GL_TEXTURE_2D)

    return id_texture


def load_texture(use_active_texture, filename, i):
    image = Image.open(filename)
    ix = image.size[0]
    iy = image.size[1]
    pbits = image.convert("RGBA").tobytes("raw", "RGBA")

    id_texture = create_gl_texture(use_active_texture, i, ix, iy, pbits)
    print("Loaded", id_texture)
    return id_texture


X_AXIS = 0.0
Y_AXIS = 0.0
Z_AXIS = 0.0
DIRECTION = 1
id_textures = []


def init_gl(Width, Height):
    global glMultiTexCoord2f, glActiveTexture
    print(get_opengl_info())

    print("Choosing between: ", glMultiTexCoord2f.__name__,
          glMultiTexCoord2fARB.__name__)
    print("Choosing between: ", glActiveTexture.__name__,
          glActiveTextureARB.__name__)

    glMultiTexCoord2f = alternate(
        glMultiTexCoord2f,
        glMultiTexCoord2fARB
    )
    glActiveTexture = alternate(
        glActiveTexture,
        glActiveTextureARB,
    )

    print("Selected: ", glMultiTexCoord2f.__name__)
    print("Selected: ", glActiveTexture.__name__)

    if not glMultiTexCoord2f:
        print('Multitexture not supported!')
        sys.exit(1)

    glClearColor(0.0, 0.0, 0.0, 0.0)
    glClearDepth(1.0)
    glDepthFunc(GL_LESS)
    glEnable(GL_DEPTH_TEST)
    glShadeModel(GL_SMOOTH)
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(45.0, float(Width) / float(Height), 0.1, 100.0)
    glMatrixMode(GL_MODELVIEW)
    glEnable(GL_TEXTURE_2D)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)


def keyPressed(*args):
    if args[0] == "\033":
        sys.exit()

# Method0: Using glBindTexture + glTexCoord2f per face
def draw_method_0():
    global X_AXIS, Y_AXIS, Z_AXIS
    global DIRECTION
    global id_textures
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glLoadIdentity()
    glTranslatef(0.0, 0.0, -6.0)

    glRotatef(X_AXIS, 1.0, 0.0, 0.0)
    glRotatef(Y_AXIS, 0.0, 1.0, 0.0)
    glRotatef(Z_AXIS, 0.0, 0.0, 1.0)

    glBindTexture(GL_TEXTURE_2D, id_textures[0])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glEnd()

    glBindTexture(GL_TEXTURE_2D, id_textures[1])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glEnd()

    glBindTexture(GL_TEXTURE_2D, id_textures[2])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glEnd()

    glBindTexture(GL_TEXTURE_2D, id_textures[3])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0, -1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glEnd()

    glBindTexture(GL_TEXTURE_2D, id_textures[4])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glEnd()

    glBindTexture(GL_TEXTURE_2D, id_textures[5])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glEnd()

    X_AXIS = X_AXIS - 0.030
    Z_AXIS = Z_AXIS - 0.030

    glutSwapBuffers()


# Method1: Using glActiveTexture+glBindTexture+glMultiTexCoord2f per face
def draw_method_1():
    global X_AXIS, Y_AXIS, Z_AXIS
    global DIRECTION
    global id_textures
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glLoadIdentity()
    glTranslatef(0.0, 0.0, -6.0)

    glRotatef(X_AXIS, 1.0, 0.0, 0.0)
    glRotatef(Y_AXIS, 0.0, 1.0, 0.0)
    glRotatef(Z_AXIS, 0.0, 0.0, 1.0)

    glActiveTexture(GL_TEXTURE0 + 0)
    glBindTexture(GL_TEXTURE_2D, id_textures[0])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 0, 0.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 0, 1.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 0, 1.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 0, 0.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 1)
    glBindTexture(GL_TEXTURE_2D, id_textures[1])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 1, 1.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 1, 1.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 1, 0.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 1, 0.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 2)
    glBindTexture(GL_TEXTURE_2D, id_textures[2])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 2, 0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 2, 0.0, 0.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 2, 1.0, 0.0)
    glVertex3f(1.0,  1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 2, 1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 3)
    glBindTexture(GL_TEXTURE_2D, id_textures[3])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 3, 1.0, 1.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 3, 0.0, 1.0)
    glVertex3f(1.0, -1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 3, 0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 3, 1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 4)
    glBindTexture(GL_TEXTURE_2D, id_textures[4])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 4, 1.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 4, 1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 4, 0.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 4, 0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 5)
    glBindTexture(GL_TEXTURE_2D, id_textures[5])
    glBegin(GL_QUADS)
    glMultiTexCoord2f(GL_TEXTURE0 + 5, 0.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 5, 1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 5, 1.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glMultiTexCoord2f(GL_TEXTURE0 + 5, 0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glEnd()

    X_AXIS = X_AXIS - 0.030
    Z_AXIS = Z_AXIS - 0.030

    glutSwapBuffers()

# Method2: Using glActiveTexture+glBindTexture+glTexCoord2f per face
def draw_method_2():
    global X_AXIS, Y_AXIS, Z_AXIS
    global DIRECTION
    global id_textures
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

    glLoadIdentity()
    glTranslatef(0.0, 0.0, -6.0)

    glRotatef(X_AXIS, 1.0, 0.0, 0.0)
    glRotatef(Y_AXIS, 0.0, 1.0, 0.0)
    glRotatef(Z_AXIS, 0.0, 0.0, 1.0)

    glActiveTexture(GL_TEXTURE0 + 0)
    glBindTexture(GL_TEXTURE_2D, id_textures[0])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 1)
    glBindTexture(GL_TEXTURE_2D, id_textures[1])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 2)
    glBindTexture(GL_TEXTURE_2D, id_textures[2])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 3)
    glBindTexture(GL_TEXTURE_2D, id_textures[3])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0, -1.0, -1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 4)
    glBindTexture(GL_TEXTURE_2D, id_textures[4])
    glBegin(GL_QUADS)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(1.0,  1.0, -1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(1.0, -1.0,  1.0)
    glEnd()

    glActiveTexture(GL_TEXTURE0 + 5)
    glBindTexture(GL_TEXTURE_2D, id_textures[5])
    glBegin(GL_QUADS)
    glTexCoord2f(0.0, 0.0)
    glVertex3f(-1.0, -1.0, -1.0)
    glTexCoord2f(1.0, 0.0)
    glVertex3f(-1.0, -1.0,  1.0)
    glTexCoord2f(1.0, 1.0)
    glVertex3f(-1.0,  1.0,  1.0)
    glTexCoord2f(0.0, 1.0)
    glVertex3f(-1.0,  1.0, -1.0)
    glEnd()

    X_AXIS = X_AXIS - 0.030
    Z_AXIS = Z_AXIS - 0.030

    glutSwapBuffers()

def load_textures(use_active_texture):
    global id_textures

    id_textures.append(load_texture(use_active_texture, "tex00.jpg", 0))
    id_textures.append(load_texture(use_active_texture, "tex01.jpg", 1))
    id_textures.append(load_texture(use_active_texture, "tex02.jpg", 2))
    id_textures.append(load_texture(use_active_texture, "tex03.jpg", 3))
    id_textures.append(load_texture(use_active_texture, "tex04.jpg", 4))
    id_textures.append(load_texture(use_active_texture, "tex05.jpg", 5))


def main():
    glutInit(sys.argv)
    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
    glutInitWindowSize(640, 480)
    glutInitWindowPosition(200, 200)

    glutCreateWindow(b'OpenGL Python Textured Cube')

    init_gl(640, 480)

    current_method = 2
    draw_methods = {
        0: {"f": draw_method_0, "use_active_texture": False},
        1: {"f": draw_method_1, "use_active_texture": True},
        2: {"f": draw_method_2, "use_active_texture": True}
    }
    draw = draw_methods[current_method]["f"]
    load_textures(draw_methods[current_method]["use_active_texture"])
    glutDisplayFunc(draw)
    glutIdleFunc(draw)
    glutKeyboardFunc(keyPressed)

    glutMainLoop()

if __name__ == "__main__":
    main()

如果我设置current_method=0我会得到我期望的输出,所有立方体面都将使用不同的纹理(如预期的那样):

在此处输入图像描述

如果我current_method=1只设置一个面将被正确纹理,那是一个错误的输出:

在此处输入图像描述

如果我设置current_method=2所有面都将使用相同的纹理[0],这也是一个错误的输出:

在此处输入图像描述

我想了解为什么方法 1 和 2 没有提供与方法 0 相同的输出。我知道当使用 glActiveTexture 正确使用着色器时变得微不足道,但我想了解在旧的固定管道上使用这些方法时出了什么问题。

4

2 回答 2

2

我认为这里的核心问题是对多重纹理如何工作的误解。

多重纹理同时将多个纹理应用于相同的图元。您想将不同的纹理(一次一个纹理)应用于不同的图元。从中应该清楚的是,多重纹理不会做你想要的,所以使用它没有意义。

多重纹理的工作原理

使用多重纹理,多个纹理单元使用混合混合功能组合它们的结果。

texture unit 0 --> +---------+
                   |  blend  | --\
texture unit 1 --> +---------+    \--> +---------+
                                       |  blend  | --> etc.
texture unit 2 ----------------------> +---------+

每个阶段的混合函数由 设置glTexEnv,可以是GL_MODULATEGL_DECALGL_ADD。这就是当时光照贴图与纹理的组合方式:一个纹理将具有漫反射纹理,而一个纹理将具有光照贴图纹理。

同样,这与您想要实现的效果完全不同,因此多纹理在您的应用程序中没有任何意义。

glActiveTexture 的工作原理

glActiveTexture不会更改将哪个纹理单元绘制到屏幕上。默认情况下,只有纹理单元#0 会绘制到屏幕上。

glActiveTexture只允许您将纹理绑定到其他纹理单元。但是,由于没有使用纹理单元#1,因此将什么纹理绑定到单元#1 或坐标是什么并不重要。所以你应该一直使用glActiveTexture(GL_TEXTURE0),因为你总是想改变纹理单元#0。

解决方案

所以,你有不使用多重纹理的工作代码。伟大的!你完成了。使用更多 OpenGL 功能不会获得奖励积分。

或者,如果您使用 2D 数组纹理,您可以在一次绘制调用中绘制整个立方体。您只需将现有纹理加载为 2D 阵列纹理中的平面,并使用 3D 纹理坐标而不是 2D 纹理坐标。同样,不需要多重纹理。

于 2017-04-10T15:29:24.020 回答
1

如果我current_method=1只设置一张脸将被正确纹理化,那是一个错误的输出。

不,这不是错误的输出。这是完全正确的输出。您永远不会设置固定功能管道状态来执行任何多纹理处理(有关详细信息,请参阅@Dietrich Epp 的答案)。您的渲染管道配置为:

  • 取每个片段的插值顶点颜色的颜色值(您从未设置过,iirc 默认为白色)
  • 使用纹理坐标集 0 的每片段插值纹理坐标对当前绑定到纹理单元 0 的纹理进行采样
  • 使用纹理的 Alpha 通道(GL_DECAL模式)将结果混合在一起
  • 使用它作为最终的片段颜色。

所以很明显,从绑定到纹理单元 > 0 的纹理中,您将永远看不到任何东西。那么您看到了什么?

当 时current_method==0,你会看到你所期望的。

如果current_method==1,您会看到从第一个纹理中获取的单个样本- 它始终绑定在单元 0 上。由于您没有为单元 0 设置任何 tex 坐标,因此 GL 将仅重复最近设置的值,即(0,1)您的第一个面的最后一个顶点。这当然会用于新面的所有顶点,因此插值将产生一个恒定值,从而产生恒定的纹理采样结果,从而产生恒定的颜色。

现在,current_method=2所有面的第一个纹理仍然绑定在纹理 0 处。但是由于您现在再次为纹理单元 0 提供适当的每个顶点 texcoords,因此对其进行采样实际上会再次对不同区域的纹理进行采样,因此您将获得该纹理到基元的合理映射。

于 2017-04-10T19:00:14.233 回答