簡體   English   中英

opengl 用頂點顏色設置紋理顏色

[英]opengl set texture color with vertex color

因為我需要顯示大量獨立移動的標簽,所以我需要將pyglet 中的標簽渲染到紋理(否則更新每個字形的頂點列表太慢)。

我有一個解決方案來做到這一點,但我的問題是包含字形的紋理是黑色的,但我希望它是紅色的。 請參閱下面的示例:

from pyglet.gl import *

def label2texture(label):
    vertex_list = label._vertex_lists[0].vertices[:]
    xpos = map(int, vertex_list[::8])
    ypos = map(int, vertex_list[1::8])
    glyphs = label._get_glyphs()

    xstart = xpos[0]
    xend = xpos[-1] + glyphs[-1].width
    width = xend - xstart

    ystart = min(ypos)
    yend = max(ystart+glyph.height for glyph in glyphs)
    height = yend - ystart

    texture = pyglet.image.Texture.create(width, height, pyglet.gl.GL_RGBA)

    for glyph, x, y in zip(glyphs, xpos, ypos):
        data = glyph.get_image_data()
        x = x - xstart
        y =  height - glyph.height - y + ystart
        texture.blit_into(data, x, y, 0)

    return texture.get_transform(flip_y=True)

window = pyglet.window.Window()
label = pyglet.text.Label('Hello World!', font_size = 36)
texture = label2texture(label)

@window.event
def on_draw():
    hoff = (window.width / 2) - (texture.width / 2)
    voff = (window.height / 2) - (texture.height / 2)

    glClear(GL_COLOR_BUFFER_BIT)
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glClearColor(0.0, 1.0, 0.0, 1.0)
    window.clear()
    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, texture.id)
    glColor4f(1.0, 0.0, 0.0, 1.0) #I'd like the font to be red
    glBegin(GL_QUADS);
    glTexCoord2d(0.0,1.0); glVertex2d(hoff,voff);
    glTexCoord2d(1.0,1.0); glVertex2d(hoff+texture.width,voff);
    glTexCoord2d(1.0,0.0); glVertex2d(hoff+texture.width,voff+texture.height);
    glTexCoord2d(0.0,0.0); glVertex2d(hoff, voff+texture.height);
    glEnd();

pyglet.app.run()

知道我怎么給這個上色嗎?

你想設置glEnable(GL_COLOR_MATERIAL) 這使得紋理顏色與當前的 OpenGL 顏色混合。 您還可以使用glColorMaterial函數來指定是否應該影響每個多邊形的正面/背面/兩者。 文檔在這里

當你使用glTexEnv()時,不是通過glTexEnv()嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM