gpt4 book ai didi

python - PyOpenGL 片段着色器纹理采样

转载 作者:行者123 更新时间:2023-11-30 22:03:21 26 4
gpt4 key购买 nike

好吧,感谢你们,我对OpenGL在(几乎)现代工作中的了解有了更深入的了解。但有件事我想不通。

即使我错过了正确设置的片段着色器如何对我的纹理进行采样?我认为采样器会以某种方式自动获取纹理。但如何呢?

在我注释掉 glBindTexture(GL_TEXTURE_2D, 1) 后,纹理仍然显示。

*(由于我的 Raspberry Pi,我使用 GLSL 1.2。)

顶点着色器:

#version 120

attribute vec2 vPosition;
attribute vec2 vTexcoords;

varying vec2 fTexcoords;

void main()
{
gl_Position = vec4(vPosition.x/1.33, vPosition.y, 0.0, 1.0);
fTexcoords = vTexcoords;
}

片段着色器:

#version 120

varying vec2 fTexcoords;
uniform sampler2D textureObj;

void main()
{
gl_FragColor = texture2D(textureObj, fTexcoords);
}

代码:

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

import pygame
from pygame.locals import *
import numpy, time, sys

def getFileContent(file):
content = open(file, 'r').read()
return content

def init():
pygame.init()
pygame.display.set_mode((640, 480), HWSURFACE | OPENGL | DOUBLEBUF)
glViewport(0, 0, 640, 480)

img = pygame.image.load("brick.jpg")
textureData = pygame.image.tostring(img, "RGB", 1)
width = img.get_width()
height = img.get_height()
#glGenTextures(1)
#glBindTexture(GL_TEXTURE_2D, 1)
glTexImage2D(GL_TEXTURE_2D, 0, 3, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)

vertices = [-0.5, -0.5,
-0.5, 0.5,
0.5, 0.5,
0.5, -0.5]

texcoords = [0.0, 0.0,
0.0, 1.0,
1.0, 1.0,
1.0, 0.0]

vertices = numpy.array(vertices, dtype=numpy.float32)
texcoords = numpy.array(texcoords, dtype=numpy.float32)

vertexShader = compileShader(getFileContent("helloTriangle.vert"), GL_VERTEX_SHADER)
fragmentShader = compileShader(getFileContent("helloTriangle.frag"), GL_FRAGMENT_SHADER)

global shaderProgram
shaderProgram = glCreateProgram()
glAttachShader(shaderProgram, vertexShader)
glAttachShader(shaderProgram, fragmentShader)
glLinkProgram(shaderProgram)

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices)
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoords)
glEnableVertexAttribArray(0)
glEnableVertexAttribArray(1)

global texLocation
texLocation = glGetUniformLocation(shaderProgram, "textureObj")

def main():
init()

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

glClearColor(0.25, 0.25, 0.25, 1)
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

glUseProgram(shaderProgram)
#glUniform1i(texLocation, 0)
glDrawArrays(GL_QUADS, 0, 4)

pygame.display.flip()

main()

最佳答案

在 glsl 中,统一变量默认分别初始化为 0 和 0.0。因此纹理采样器uniform textureObj 的值也由(纹理单元)0 初始化。

此外还有默认纹理对象 (0)。如果不创建并绑定(bind)命名纹理对象,则仍然存在默认纹理对象 (0) 和 glTexImage2D指定默认纹理对象 (0) 的纹理图像。

参见OpenGL 4.6 API Compatibility Profile Specification; 8.1 Texture Objects; page 179

Textures in GL are represented by named objects. The name space for texture objects is the unsigned integers, with zero reserved by the GL to represent the default texture object. The default texture object is bound to each of the TEXTURE_1D, TEXTURE_2D, TEXTURE_3D, [...] targets during context initialization.

GLSL - The OpenGL Shading Language 4.6; 4.3.5. Uniform Variables; page 50

The uniform qualifier is used to declare global variables whose values are the same across the entire primitive being processed. All uniform variables are read-only and are initialized externally either at link time or through the API. The link-time initial value is either the value of the variable’s initializer, if present, or 0 if no initializer is present.

<小时/>

顺便说一句,glGenTextures返回一个命名纹理对象,可以通过glBindTexture绑定(bind)该对象。没有指定由 glGenTextures 生成的第一个命名纹理对象必须为 1。它可以是任何 id(数字)。

所以它必须是:

texID = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texID)

而不是

glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, 1)

关于python - PyOpenGL 片段着色器纹理采样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53585040/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com