C 如何在现代opengl中制作1D纹理示例?
我创建了这个简单的程序。它创建两个纹理,一个2D纹理和一个1D纹理将它们绑定,等等 我可以在我的着色器程序中从2D纹理采样和绘制,没有问题。尝试从1D纹理采样和绘制时,四边形变为黑色 如果我不在If..else..中使用下面片段着色器中的1D纹理示例。。语句,所有内容都呈现为黑白。尝试使用该示例时,它只渲染一个黑屏 如果我做这样的黑客行为: 顶点着色器C 如何在现代opengl中制作1D纹理示例?,c,opengl,opengl-es,glsl,C,Opengl,Opengl Es,Glsl,我创建了这个简单的程序。它创建两个纹理,一个2D纹理和一个1D纹理将它们绑定,等等 我可以在我的着色器程序中从2D纹理采样和绘制,没有问题。尝试从1D纹理采样和绘制时,四边形变为黑色 如果我不在If..else..中使用下面片段着色器中的1D纹理示例。。语句,所有内容都呈现为黑白。尝试使用该示例时,它只渲染一个黑屏 如果我做这样的黑客行为: 顶点着色器 #version 330 core layout (location = 0) in vec3 position; layout (locat
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 icolor;
layout (location = 2) in vec2 vTexCoord;
uniform mat4 modelMat;
uniform mat4 viewMat;
uniform mat4 projMat;
out vec4 fcolor;
out vec2 fTexCoord;
out vec2 fCoord;
void main()
{
gl_Position = projMat * viewMat * modelMat * vec4(position, 1.0);
fCoord = vec2(position);
fTexCoord = vTexCoord;
fcolor = vec4(icolor, 1.0f);
}
#version 330 core
in vec4 fcolor;
in vec2 fTexCoord;
in vec2 fCoord;
uniform int maxIterations;
uniform vec2 center;
uniform float scale;
uniform sampler2D ourTexture;
uniform sampler1D mandiTexture;
out vec4 color;
void main()
{
vec2 c, z;
c.x = 1.3333 * (fCoord.x - 0.5) - center.x;
c.y = (fCoord.y - 0.5) - center.y;
int i;
z = c;
for(i=0; i<maxIterations; i++) {
float x = (z.x * z.x - z.y * z.y) + c.x;
float y = (z.y * z.x + z.x * z.y) + c.y;
if((x * x + y * y) > 4.0) break;
z.x = x;
z.y = y;
}
vec4 tcolor;
if (i == maxIterations)
{
tcolor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
else
{
/* tcolor = vec4(1.0f, 1.0f, 1.0f, 1.0f); this works but only renders black and white */
tcolor = texture(mandiTexture, float(i)).rgba; /* only renders black screen */
}
/* color = texture(ourTexture, fTexCoord) * fcolor; //this also works */
color = tcolor;
}
片段着色器
#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 icolor;
layout (location = 2) in vec2 vTexCoord;
uniform mat4 modelMat;
uniform mat4 viewMat;
uniform mat4 projMat;
out vec4 fcolor;
out vec2 fTexCoord;
out vec2 fCoord;
void main()
{
gl_Position = projMat * viewMat * modelMat * vec4(position, 1.0);
fCoord = vec2(position);
fTexCoord = vTexCoord;
fcolor = vec4(icolor, 1.0f);
}
#version 330 core
in vec4 fcolor;
in vec2 fTexCoord;
in vec2 fCoord;
uniform int maxIterations;
uniform vec2 center;
uniform float scale;
uniform sampler2D ourTexture;
uniform sampler1D mandiTexture;
out vec4 color;
void main()
{
vec2 c, z;
c.x = 1.3333 * (fCoord.x - 0.5) - center.x;
c.y = (fCoord.y - 0.5) - center.y;
int i;
z = c;
for(i=0; i<maxIterations; i++) {
float x = (z.x * z.x - z.y * z.y) + c.x;
float y = (z.y * z.x + z.x * z.y) + c.y;
if((x * x + y * y) > 4.0) break;
z.x = x;
z.y = y;
}
vec4 tcolor;
if (i == maxIterations)
{
tcolor = vec4(0.0f, 0.0f, 0.0f, 1.0f);
}
else
{
/* tcolor = vec4(1.0f, 1.0f, 1.0f, 1.0f); this works but only renders black and white */
tcolor = texture(mandiTexture, float(i)).rgba; /* only renders black screen */
}
/* color = texture(ourTexture, fTexCoord) * fcolor; //this also works */
color = tcolor;
}
#版本330核心
在vec4彩色;
在vec2 fTexCoord中;
在vec2fcoord中;
一致整数最大迭代;
均匀vec2中心;
均匀浮标;
纹理均匀;
均匀的纹理;
输出vec4颜色;
void main()
{
vec2c,z;
c、 x=1.3333*(fCoord.x-0.5)-中心点x;
c、 y=(fCoord.y-0.5)-中心点y;
int i;
z=c;
对于(i=0;i 4.0)断裂;
z、 x=x;
z、 y=y;
}
vec4色;
如果(i==maxIterations)
{
t颜色=vec4(0.0f,0.0f,0.0f,1.0f);
}
其他的
{
/*tcolor=vec4(1.0f、1.0f、1.0f、1.0f);此选项可用,但仅渲染黑白*/
t颜色=纹理(纹理,浮动(i)).rgba;/*仅渲染黑屏*/
}
/*颜色=纹理(我们的纹理,fTexCoord)*F颜色//这同样有效*/
颜色=t颜色;
}
目前,您正在使用范围为0到maxIterations
(可能值为i
)的纹理坐标来查找1D纹理中的值。正如dari在评论中提到的,纹理坐标在0-1的范围内,忽略环绕和夹紧
因此,将i
除以maxIterations
,得到0-1范围内的坐标:
tcolor = texture(mandiTexture, float(i) / float(maxIterations)).rgba;
纹理坐标在[0,1]@Olaf顶点和碎片着色器的范围内。这并不能回答我的问题。@dari这是答案,但对于一个新的图形程序员来说,它可能非常神秘。@Olaf:这里有两种不同的语言:C和GLSL。在这个问题中唯一具有main函数的代码是GLSL
voidmain(void)
在GLSL中有效,它的许多语法看起来与C完全相同,因此您可能很容易将两者混淆。