C 使用多个纹理+;GLSL中的滤波器

C 使用多个纹理+;GLSL中的滤波器,c,opengl,glsl,mesa,C,Opengl,Glsl,Mesa,我试图在Opengl中沿特定路径渲染点精灵。我将精灵定义为2D纹理,并将其设置为使用GL_NEAREST作为mag/min过滤器。我还定义了一个1D纹理,其中包含一些2D坐标(因此每个精灵只需要1个浮点来确定它们的屏幕空间位置),我将其设置为GL_LINEAR 但是,当我在笔记本电脑上运行程序时,它总是使用精灵纹理的mag filter方法,并忽略路径纹理的设置。我的笔记本电脑在intel Iris Pro 5200上运行Linux+mesa 9.2.1+Opengl 3.0。我的桌面运行Lin

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

但是,当我在笔记本电脑上运行程序时,它总是使用精灵纹理的mag filter方法,并忽略路径纹理的设置。我的笔记本电脑在intel Iris Pro 5200上运行Linux+mesa 9.2.1+Opengl 3.0。我的桌面运行Linux,使用专有的Nvidia驱动程序和opengl 4.2,一切正常

有关守则如下:

//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);
}
我的片段着色器是

#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);
}
我粘贴的全部代码(单个字段),是我分别在Nvidia和Intel上看到的屏幕截图

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


什么会导致这种情况?

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


<> >与<代码> gLeWiTe](< /代码>

),如果使用OpenGL 4 .x,则可以考虑使用采样器对象来存储纹理筛选器/FLASH状态。可以将采样器绑定到使用最近过滤的纹理单元0,将采样器绑定到使用双线性的纹理单元1。然后,您可以将想要的任何纹理绑定到一个单元,而不是使用每个纹理对象的过滤器,取而代之的是取样器的过滤器。这确实是一个更加优雅的解决方案,因为它基本上将纹理对象转化为图像存储,并分离出一些真正不属于它们的状态;D3D已经做了10多年了,这是一个非常好的主意:)谢谢你的建议:),但是我也用采样器对象尝试过,但是我得到了同样的结果。现在我明白了你在这里实际要做的事情,我觉得有义务指出顶点纹理的获取与片段相比是非常有限的。某些硬件在顶点阶段仅支持选定的几种图像格式,线性过滤并不总是硬件加速的,各向异性过滤在顶点着色器中永远不起作用(它需要梯度/偏导数的知识,这在逐顶点提取的上下文中真的没有意义),mipmapping也不支持。我有一种感觉,这可能是Mesa和Iris硬件本身的局限性,也可能是两者的局限性。这是一种耻辱:(。我计划最终制作一款风格类似于Bloons tower Defense的塔防游戏。我认为这将是一种减少cpu端工作的好方法。谢谢。我修复了它,但不幸的是,我仍然得到了相同的结果。