我正在尝试将多个纹理绑定到片段着色器中的采样器。加载代码似乎运行良好。ATI的CodeXL显示正确加载的纹理。
但是,当我将模型的纹理绑定到活动纹理0和1时,我无法让它将值发送到我的着色器。当我将着色器统一标记为usampler2D并使用uvec4存储颜色时,就像我应该做的那样,因为我的纹理是以无符号字节提供的,我得到了一个全白色的模型。当我将着色器统一更改为sampler2D并使用vec4存储颜色时,我的glUniform1i调用无法再获取着色器变量的位置,因此不会为活动纹理设置任何内容。这导致可以使用漫反射纹理,但我无法获得正常纹理。从好的方面来看,漫反射纹理是以这种方式在模型上绘制的。
我不确定是什么问题。我在网上查了好几个地方想弄明白,我已经翻阅了红皮书。我知道我漏掉了什么,或者把状态设置错了,但我似乎找不到。提前感谢您为我解决此问题提供的任何帮助。
int[] testWidth;
testWidth = new int[1];
testWidth[0] = 1000;
// First bind the texture.
bind();
// Make sure that textures are enabled.
// I read that ATI cards need this before MipMapping.
glEnable(GL_TEXTURE_2D);
// Test to make sure we can create a texture like this.
glTexImage2D(GL_PROXY_TEXTURE_2D, 0, format, width, height,
0, format, GL_UNSIGNED_BYTE, null);
glGetTexLevelParameteriv(GL_PROXY_TEXTURE_2D, 0, GL_TEXTURE_WIDTH,
testWidth);
if (testWidth[0] == 0)
{
message("Could not load texture onto the graphics card.");
}
else
{
// Not so sure about this part....but it seems to work.
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Load the texture data.
glTexImage2D(texture_type, 0, format, width, height,
0, format, GL_UNSIGNED_BYTE, (GLvoid[]?)value);
// Smaller mipmaps need linear mipmap coords.
// Larger just uses linear of the main texture.
glTexParameterf(texture_type, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(texture_type, GL_TEXTURE_MAG_FILTER,
GL_LINEAR);
// Clamp the texture to the edges.
glTexParameterf(texture_type, GL_TEXTURE_WRAP_S,
GL_CLAMP_TO_EDGE);
glTexParameterf(texture_type, GL_TEXTURE_WRAP_T,
GL_CLAMP_TO_EDGE);
glTexParameterf(texture_type, GL_TEXTURE_WRAP_R,
GL_CLAMP_TO_EDGE);
// Generate the mipmaps. The tex parameter is there
// for ATI cards. Again, it's something I read online.
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
glGenerateMipmap(texture_type);
}
// Now unbind the texture.
unbind();
if (currentShader != null)
{
currentShader.set_uniform_matrix("model_matrix", ref model_matrix,
true);
if (material != null)
{
if (material.diffuse_texture != null)
{
glActiveTexture(GL_TEXTURE0);
material.diffuse_texture.bind();
currentShader.set_uniform_texture("diffuse_texture",
Constants.DIFFUSE_TEXTURE);
if (material.normal_testure != null)
{
glActiveTexture(GL_TEXTURE1);
material.normal_texture.bind();
currentShader.set_uniform_texture("normal_texture",
Constants.NORMAL_TEXTURE);
}
}
}
// If there is a renderable then render it.
if (renderable != null)
{
renderable.render(1.0);
}
if (material != null)
{
material.unbind();
}
#version 400 core
/**
* Smooth the inward vertex color. Smooth it so that the fragments
* which will be in between the vertices as well can get a value close
* to where they are positioned after being rasterized.
*/
smooth in vec4 vertex_color;
/**
* Smooth the inward texture coordinates. Smooth it so that the
* fragments which will be in between the vertices as well can get a
* value close to where they are positioned after being rasterized.
*/
smooth in vec2 out_texture_coordinate;
/**
* The color to make this fragment.
*/
out vec4 frag_color;
/**
* The models diffuse texture. This will be mapped to index 0.
*/
uniform usampler2D diffuse_texture;
/**
* The models normal texture. This will be mapped to index 1.
*/
uniform usampler2D normal_texture;
/**
* The starting function of the shader.
*/
void main(void)
{
uvec4 diffuseColor;
uvec4 normalModifier;
diffuseColor = texture(diffuse_texture, out_texture_coordinate);
normalModifier = texture(normal_texture, out_texture_coordinate);
// Discard any fragments that have an alpha color less than 0.05.
if (diffuseColor.a < 1.0)
{
// This works as part of depth testing to remove the fragments that
// are not useful.
discard;
}
frag_color = diffuseColor;
}
/**
* Sets the uniform value for a texture in the shader.
*
* @param name The name of the uniform to bind this texture to.
* This must have already been registered.
*
* @param textureUnit The id for the texture unit to bind to the uniform.
* This is not the texture's id/reference, but the OpenGL texture unit
* that the reference is bound to.
* This is set by calling glActiveTexture.
*/
public void set_uniform_texture(string name, int textureUnit)
{
// Check to make sure the uniform was given a location already.
if (register_uniform(name) == true)
{
// Set the data for this uniform then.
glUniform1i(uniform_mapping.get(name), textureUnit);
}
else
{
message("Texture was not set. %s", name);
}
}
/**
* Register a uniform for passing data to the shader program.
*
* @return true if the uniform was found with a valid location;
* otherwise, false.
*
* @param name The name for the parameter to get a uniform location for.
* Use this name for the variable in your shader.
*/
public bool register_uniform(string name)
{
int location;
// Make sure we didn't already get the location of the uniform value.
if (uniform_mapping.has_key(name) == false)
{
location = Constants.OPENGL_INVALID_INDEX;
// We have no information about this uniform, so try
// to get it's location.
location = glGetUniformLocation(reference, name);
// The location will 0 or higher if we found the uniform.
if (location != Constants.OPENGL_INVALID_INDEX)
{
uniform_mapping.set(name, location);
return true;
}
}
else
{
// The uniform was previously found and can be used.
return true;
}
debug("Uniform %s not found!!!!!", name);
return false;
}
由于您没有显示Con量
的定义,因此请确保您的代码具有以下断言:
if (material.diffuse_texture != null)
{
glActiveTexture(GL_TEXTURE0);
material.diffuse_texture.bind();
assert(Constants.DIFFUSE_TEXTURE + GL_TEXTURE0 == GL_TEXTURE0);
currentShader.set_uniform_texture("diffuse_texture",
Constants.DIFFUSE_TEXTURE);
if (material.normal_testure != null)
{
glActiveTexture(GL_TEXTURE1);
material.normal_texture.bind();
assert(Constants.NORMAL_TEXTURE + GL_TEXTURE0 == GL_TEXTURE1);
currentShader.set_uniform_texture("normal_texture",
Constants.NORMAL_TEXTURE);
}
传递给采样器统一的值是给定给glActiveTexture的枚举值,这是一种常见的误解。事实上,glActiveTexture将GL_TEXTURE0作为偏移基准值。
将内部格式设置为GL_RGB/A意味着您应该使用sampler2D,而不是usampler2D,即使原始图像数据最初是以无符号字节的形式给出的。编辑在调用glTexImage2D时将给定数据转换为内部格式(在本例中,GL_RGBA为每个通道8位,因此不需要太多操作)。然而,对于大多数图形应用程序,需要更高精度的数据,例如,当使用非“最近”插值对纹理进行采样时,这就是为什么它通常以浮点数的形式显示。
绑定多个纹理...
glActiveTexture(GL_TEXTURE0 + firstTextureIndex); //firstTextureIndex should be unique amongst the textures bound for a particular shader
glBindTexture(GL_TEXTURE_2D, myFirstTextureHandle);
glUniform1i(glGetUniformLocation(shaderProgramHandle, "firstSampler"), firstTextureIndex); //note the same index given in the glActiveTexture call. This is also always glUniform1i
对secondTextureIndex、mySecondTextureHandle、secondSampler等重复上述步骤。
如果glGetUniformLocation没有返回位置,请仔细检查您是否实际使用了它,它会影响着色器输出(或者完全优化)。还要检查常见的打字错误或缺少“uniform”关键字等。
我有一个用于OpenGL中模型渲染的纹理数组。此数组包含漫反射和镜面纹理。由于某些网格没有镜面纹理,因此我在每次渲染后简单地将NULL纹理绑定到我的镜面采样器,并且只有当网格具有镜面纹理时,我才绑定一个。但是,我仍然在没有镜面纹理的网格上看到镜面纹理。 这是我的渲染代码 材质索引保存数组中纹理的索引,如果网格没有镜面纹理 索引为-1 纹理单元0用于漫反射 纹理单元2用于镜面
我对如何将纹理绑定到GLSL中的插槽有点困惑。以下是我的着色器: 当我设置矩阵时,我需要查询统一位置,然后手动设置该位置的值,执行如下操作: 然而,据我所知,我不需要这样做来初始化我的纹理采样器。在设置时,我似乎只需要这样做: 所以,我的问题是,为什么我不需要对纹理进行与矩阵相同的处理?OpenGL运行时如何“知道”我绑定的纹理应该是我定义的采样器?如果我想拥有多种纹理,我该怎么做呢?
在我的程序中,我使用了2种纹理:t0和t1。t1是附加的,仅在某些情况下需要: 绘图着色器: 它的工作原理很好:第二个纹理在需要时首先重叠。假设使用glBindTexture(..,0)返回零纹理(0,0,0,0),但在将NVidia驱动程序更新到314.07后,我的代码会出现黑屏,比如glBindTexture(..,0)返回(0,0,0,1)纹理。 我做了一些测试:使用着色器 我在旧驱动程序(
我是OpenGL的新手,在整理如何将纹理和着色器绑定到VBOs时遇到了困难。 我正在使用Cinder的纹理和着色器类。以下是我绘制方法的一部分: 在上面的代码中,如果我注释掉对mShader的调用。bind(),我的球体VBO将显示纹理(myImage)。我的着色器适用于普通(无纹理)形状,但当我在绘制任何带有包裹纹理的形状之前绑定着色器时,它会阻止纹理显示。 这是我使用的着色器的问题,还是我不理
我正在研究使用无绑定纹理来快速显示一系列图像。我的参考是OpenGL 4.5红皮书。这本书说我可以使用这个片段着色器在着色器中对无绑定纹理进行采样: 我创建了一个顶点着色器,如下所示: 正如你可能注意到的,我对发生的事情的把握有点弱。 在我的OpenGL代码中,我创建了一组纹理,获取它们的句柄,并使它们常驻。我用来获取纹理句柄的函数是“glGetTextureHandleARB”。还有另一个可以替
在glsl 420中,添加了功能来指定着色器中的绑定,而不必调用。例如: 但是,如何才能为阵列做到这一点呢? 我希望绑定为0,1,2,。。。,15.如果没有任何调用,我是否无法在glsl中执行此操作?