首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在GLSL中使用多个textures+filters

在GLSL中使用多个textures+filters
EN

Stack Overflow用户
提问于 2013-10-25 19:09:59
回答 1查看 634关注 0票数 0

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

然而,当我在我的笔记本电脑上运行我的程序时,它总是使用sprite-texture的mag filter方法,而忽略了path-texture上的设置。我的笔记本电脑在英特尔Iris Pro5200上运行Linux+mesa 9.2.1+Opengl 3.0。我的桌面运行Linux,使用opengl 4.2专有的Nvidia驱动程序,一切工作正常。

相关代码为

代码语言:javascript
复制
//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");

我的顶点着色器是

代码语言:javascript
复制
#version 130
uniform sampler1D pathTexture;
in float t;
void main(){
  gl_Position = texture(pathTexture, t);
}

我的片段着色器是

代码语言:javascript
复制
#version 130
uniform sampler2D spriteTexture;
out vec4 outputColor;
void main()
{
      outputColor = texture(spriteTexture, gl_PointCoord);
}

我粘贴的herehere的全部代码(单个文件)分别是我在Nvidia和英特尔上看到的截图。

编辑:我在Nvidia上得到了与3.0上下文相同的结果。

是什么导致了这种情况?

EN

回答 1

Stack Overflow用户

发布于 2013-10-25 22:37:29

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

glewInit()也是如此。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19588184

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档