在纹理OpenGL上绘制矩形 [英] Draw rectangle over texture OpenGL

查看:119
本文介绍了在纹理OpenGL上绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 pygame 和 OpenGL 的 python 3 来尝试完成我认为很简单的任务:绘制一个矩形.这个想法是在透明纹理上(或下面)有一个白色矩形,但是每当我将纹理添加到屏幕时,矩形就会消失,无论是在纹理之前还是之后渲染.

Bellow 是显示问题的示例代码(您可以添加您选择的任何 Player1.png 图像,问题将保持不变 - 至少在我的电脑中)

导入pygame导入系统从 OpenGL.GL 导入 *从 pygame.locals 导入 *# 设置pygame屏幕pygame.display.set_mode((500, 500), OPENGL | DOUBLEBUF)信息 = pygame.display.Info()# 基本的opengl配置glViewport(0, 0, info.current_w, info.current_h)glDepthRange(0, 1)glMatrixMode(GL_PROJECTION)glMatrixMode(GL_MODELVIEW)glLoadIdentity()glShadeModel(GL_SMOOTH)glClearColor(0.0, 0.0, 0.0, 0.0)glClearDepth(1.0)glDisable(GL_DEPTH_TEST)glDisable(GL_LIGHTING)glDepthFunc(GL_LEQUAL)glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST)启用(GL_BLEND)# 加载纹理surf = pygame.image.load('Player1.png')s = pygame.image.tostring(surf, 'RGBA')texID = glGenTextures(1)glBindTexture(GL_TEXTURE_2D,texID)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 142, 65, 0, GL_RGBA, GL_UNSIGNED_BYTE, s)glGenerateMipmap(GL_TEXTURE_2D)glBindTexture(GL_TEXTURE_2D,0)# 创建pygame时钟主锁 = pygame.time.Clock()# 初始化屏幕pygame.display.init()为真:# 获取退出事件对于 pygame.event.get() 中的事件:如果 event.type == 退出:pygame.quit()系统退出()# 准备渲染屏幕glClear(GL_COLOR_BUFFER_BIT)glLoadIdentity()glDisable(GL_LIGHTING)glEnable(GL_TEXTURE_2D)glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)glClearColor(0, 0, 0, 1.0)# 绘制纹理glBindTexture(GL_TEXTURE_2D,texID)glBegin(GL_QUADS)glTexCoord2f(0, 0);glVertex2f(-1, -1)glTexCoord2f(0, 1);glVertex2f(-1, 1)glTexCoord2f(1, 1);glVertex2f(1, 1)glTexCoord2f(1, 0);glVertex2f(1, -1)glEnd()# 绘制矩形glColor3fv((1, 1, 1))glRectf(-1, 1, 0, 0.5)pygame.display.flip()主锁.tick(60)

它很可能必须对 OpenGL 如何处理纹理与矩形做一些事情,但我不确定是什么.

顺便说一句:我知道图像是颠倒的

提前致谢

解决方案

在绘制纹理之前,您必须启用二维纹理 (

另外请注意,如果您要更改矩形的颜色,则整个纹理都会被这种颜色着色.如果启用了纹理,则默认情况下纹理像素的颜色乘以当前颜色,因为默认情况下纹理环境模式 (GL_TEXTURE_ENV_MODE) 是 GL_MODULATE.请参阅

在绘制纹理之前设置白色"颜色:

glEnable(GL_TEXTURE_2D)glColor3fv((1, 1, 1))

I'm using python 3 with pygame and OpenGL to try to accomplish what I thought it would be a simple task: Drawing a rectangle. The idea is to have a white rectangle over (or bellow) a transparent texture, but whenever I add the texture to the screen the rectangle vanishes, whether I render it before or after the texture.

Bellow is a sample code displaying the problem (you can add any Player1.png image of your choice, the problem will remain the same - at least in my computer)

import pygame
import sys
from OpenGL.GL import *
from pygame.locals import *

# set pygame screen
pygame.display.set_mode((500, 500), OPENGL | DOUBLEBUF)
info = pygame.display.Info()

# basic opengl configuration
glViewport(0, 0, info.current_w, info.current_h)
glDepthRange(0, 1)
glMatrixMode(GL_PROJECTION)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDisable(GL_DEPTH_TEST)
glDisable(GL_LIGHTING)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
glEnable(GL_BLEND)

# load texture
surf = pygame.image.load('Player1.png')
s = pygame.image.tostring(surf, 'RGBA')
texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texID)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 142, 65, 0, GL_RGBA, GL_UNSIGNED_BYTE, s)
glGenerateMipmap(GL_TEXTURE_2D)
glBindTexture(GL_TEXTURE_2D, 0)

# create pygame clock
MAINCLOCK = pygame.time.Clock()

# init screen
pygame.display.init()

while True:
    # get quit event
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

    # prepare to render screen
    glClear(GL_COLOR_BUFFER_BIT)
    glLoadIdentity()
    glDisable(GL_LIGHTING)
    glEnable(GL_TEXTURE_2D)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glClearColor(0, 0, 0, 1.0)

    # draw texture
    glBindTexture(GL_TEXTURE_2D, texID)
    glBegin(GL_QUADS)
    glTexCoord2f(0, 0); glVertex2f(-1, -1)
    glTexCoord2f(0, 1); glVertex2f(-1, 1)
    glTexCoord2f(1, 1); glVertex2f(1, 1)
    glTexCoord2f(1, 0); glVertex2f(1, -1)
    glEnd()

    # draw rectangle
    glColor3fv((1, 1, 1))
    glRectf(-1, 1, 0, 0.5)

    pygame.display.flip()
    MAINCLOCK.tick(60)

It most likely has to do something on how OpenGL treats Textures vs Rects, but I'm not sure what.

BTW: I know the image is upside down

Thanks in advance

解决方案

You have to enable two-dimensional texturing before you draw the texture, as you do it (glEnable(GL_TEXTURE_2D)).

But you have to disable two-dimensional texturing again, before you draw the rectangle:

# draw rectangle
glDisable(GL_TEXTURE_2D)
glColor3fv((1, 1, 1))
glRectf(-1, 1, 0, 0.5)

Note, the texture is still bound, when you draw the rectangle. Since you do not provide texture coordinates, when you draw the rectangle. This causes that the current texture coordinate is applied to the rectangle and a single texel is drawn all over the rectangle.

e.g. The last texture coordinate set was glTexCoord2f(1, 0):

Further note, if you would change the color for the rectangle, then the entire texture get tint by this color. If texturing is enabled, then by default the color of the texel is multiplied by the current color, because by default the texture environment mode (GL_TEXTURE_ENV_MODE) is GL_MODULATE. See glTexEnv.

glDisable(GL_TEXTURE_2D)
glColor3fv((1, 0, 0))    # red
glRectf(-1, 1, 0, 0.5)

Set the "white" color before you draw the texture:

glEnable(GL_TEXTURE_2D)
glColor3fv((1, 1, 1))

这篇关于在纹理OpenGL上绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆