pyopengl设置多通道纹理混合模式

时间:2012-02-24 00:20:50

标签: python opengl texture-mapping pyopengl color-blending

我试图让多纹理工作,并且到目前为止已经使用此函数加载了多个纹理

def loadTexture(name):

    img = PIL.Image.open(name) # .jpg, .bmp, etc. also work
    img_data = numpy.array(list(img.getdata()), numpy.int8)

    id = glGenTextures(1)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glBindTexture(GL_TEXTURE_2D, id)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img.size[0], img.size[1], 0, GL_RGB, GL_UNSIGNED_BYTE, img_data)
    return id

我可以设置要使用此代码的纹理

glBindTexture(GL_TEXTURE_2D, 1)
glEnable(GL_TEXTURE_2D)

我的第一次尝试是:

glBindTexture(GL_TEXTURE_2D, 1)
glEnable(GL_TEXTURE_2D)
glBegin(GL_TRIANGLES)
....
glEnd()

glBindTexture(GL_TEXTURE_2D, 3)
glEnable(GL_TEXTURE_2D)
glBegin(GL_TRIANGLES)
....
glEnd()

所以我渲染聚合物两次并每次选择一个不同的纹理,这看起来像调用glBindTexture(GL_TEXTURE_2D,n)将选择relivant纹理一样多,它将渲染但是本身没有混合,我只是看到渲染中最后选择的纹理。我尝试过添加glEnable(GL_BLEND),但似乎没有做任何事情。

我想做的是将两个通道的像素加在一起

我该怎么做?

1 个答案:

答案 0 :(得分:2)

您确定需要多次通行证吗?这是一个普通的旧OpenGL多纹理的例子:

import pygame
from pygame.locals import *

import numpy
import numpy.linalg

from OpenGL.GL import *
from OpenGL.GL.shaders import *

RESOLUTION = (800,600)

POSITIONS = numpy.array([[-1.0, -1.0], [+1.0, -1.0], [-1.0, +1.0], [+1.0, +1.0]], dtype=numpy.float32)
TEXCOORDS = numpy.array([[0.0, 1.0], [1.0, 1.0], [0.0, 0.0], [1.0, 0.0]], dtype=numpy.float32)

from PIL import Image

tex0 = 0
tex1 = 0

def loadTexture(path):
    img = Image.open(path)
    img = img.convert("RGBA") 
    img_data = numpy.array(list(img.getdata()), numpy.int8)

    texture = glGenTextures(1)
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)
    glBindTexture(GL_TEXTURE_2D, texture)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.size[0], img.size[1], 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data)

    return texture

def init():
    global tex0
    global tex1

    tex0 = loadTexture("texture0.png")
    tex1 = loadTexture("texture1.png")

    glViewport(0, 0, *RESOLUTION)

    aspect = RESOLUTION[0]/float(RESOLUTION[1])

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-aspect, +aspect, -1.0, +1.0, -1.0, +1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0.0, 0.0, 0.0, 1.0);

    glVertexPointer(2, GL_FLOAT, 0, POSITIONS);
    glEnableClientState(GL_VERTEX_ARRAY);

    glClientActiveTexture(GL_TEXTURE0);
    glTexCoordPointer(2, GL_FLOAT, 0, TEXCOORDS); 
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glClientActiveTexture(GL_TEXTURE1);
    glTexCoordPointer(2, GL_FLOAT, 0, TEXCOORDS);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

def draw():
    glClear(GL_COLOR_BUFFER_BIT);

    glActiveTexture(GL_TEXTURE0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, tex0);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glActiveTexture(GL_TEXTURE1);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, tex1);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

def main():
    pygame.init()

    screen = pygame.display.set_mode(RESOLUTION, OPENGL | DOUBLEBUF)

    init()

    while True:
        for event in pygame.event.get():
            if event.type == QUIT: return

        draw()

        pygame.display.flip()

if __name__ == "__main__":
    main()

看看Texture Combiners,它们可能会启用您正在寻找的效果类型。当然,现在这种事物的最先进技术是着色器。