gpt4 book ai didi

c - 在 GLSL 中使用多个纹理+滤镜

转载 作者:行者123 更新时间:2023-11-30 15:44:14 26 4
gpt4 key购买 nike

我正在尝试沿着特定路径在 Opengl 中渲染点 Sprite 。我将 Sprite 定义为 2D 纹理,并将其设置为使用 GL_NEAREST 作为 mag/min 过滤器。我还定义了一个包含一些 2D 坐标的 1D 纹理(这样每个 Sprite 只需要 1 个 float 来确定它们的屏幕空间位置),并将其设置为 GL_LINEAR。

但是,当我在笔记本电脑上运行程序时,它始终使用 Sprite 纹理的磁过滤方法,并忽略路径纹理上的设置。我的笔记本电脑在 intel Iris Pro 5200 上运行 Linux+mesa 9.2.1+Opengl 3.0。在运行 Linux 并使用带有 opengl 4.2 的专有 Nvidia 驱动程序的台式机上,一切都按预期运行。

相关代码为

//A path "t -> (x,y)" along which we will place our sprites
v2f path[] = {{-0.5,0.5},
{-0.5,-0.5},
{0.5,-0.5},
{0.5,0.5}};
glActiveTexture(GL_TEXTURE0);
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Couldn't activate texture!");

GLuint pathTex;
glGenTextures(1, &pathTex); //create a new texture
glBindTexture(GL_TEXTURE_1D, pathTex); //start using it as a 1D texture
//store the coordinates of path in the texture so that we can interpolate our path in the shader.
glTexImage1D(GL_TEXTURE_1D,0,GL_RG32F, sizeof(path)/sizeof(v2f),0,GL_RG,GL_FLOAT,path);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);//We want the path to be linearly filtered
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);//Set the min filter just to be safe.
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Something went wrong setting up pathTex");
GLint uniformLoc = glGetUniformLocation(shaderProgram, "pathTexture"); //locate the uniform
uniformLoc >= 0?glUniform1i(uniformLoc, 0):report(FAIL, "found uniform spriteTexture at %d", uniformLoc); //and set it to "0" because we're using GL_TEXTURE0
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Couldn't set uniform!");
report(PASS, "Setup texture for path");

//create and set second texture
rgba sprite[] = {{1.0,0,0,1},{0,1.0,0,1},{0,0,1.0,1},{1,1,0,1}}; //our sprite, a simple "red,green,blue,yellow" square.
glActiveTexture(GL_TEXTURE0+1); //use texUnit 1 now.
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Couldn't activate texture!");
GLuint spriteTex;
glGenTextures(1, &spriteTex); //create a new texture
glBindTexture(GL_TEXTURE_2D, spriteTex); //start using it as a 2D texture
glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, 2,2,0,GL_RGBA,GL_FLOAT,sprite); //load the data again. 2x2 sprite.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);//We need these two lines
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);//because we don't have mipmaps.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);//We want our sprites to be filtered to
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);//the nearest pixel, not interpolated.
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Something went wrong setting up pathTex");
uniformLoc = glGetUniformLocation(shaderProgram, "spriteTexture"); //locates and set the second sampler.
uniformLoc >= 0?glUniform1i(uniformLoc, 1):report(FAIL, "found uniform spriteTexture at %d", uniformLoc);
if(glGetError() != GL_NO_ERROR)
report(FAIL, "Couldn't set uniform!");
report(PASS, "Setup texture for sprite");

我的顶点着色器是

#version 130
uniform sampler1D pathTexture;
in float t;
void main(){
gl_Position = texture(pathTexture, t);
}

我的片段着色器是

#version 130
uniform sampler2D spriteTexture;
out vec4 outputColor;
void main()
{
outputColor = texture(spriteTexture, gl_PointCoord);
}

我已经粘贴了整个代码(单个归档)here ,和here分别是我在 Nvidia 和 Intel 上看到的屏幕截图。

编辑:我在 Nvidia 3.0 环境下得到了相同的结果。

什么原因会导致这种情况?

最佳答案

确保在任何其他 GLFW 函数之前调用 glfwInit()

glewInit()相同。

关于c - 在 GLSL 中使用多个纹理+滤镜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19588184/

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