[英]GLSL Geometry Shader problems
I have been pulling my hair out for 2 days over this. 为此,我已经把头发拉了两天了。
First.. the code.. All of it. 首先..代码..所有。
//Leaf Vertex Shader
#version 330 compatibility
out vec4 VertexColor;
void main(void)
{
gl_Position = gl_ModelViewMatrix *gl_Vertex;
gl_TexCoord[0].st = gl_MultiTexCoord0.st;
gl_TexCoord[1].st = gl_MultiTexCoord1.st;
gl_TexCoord[2].st = gl_MultiTexCoord2.st;
gl_TexCoord[3].st = gl_MultiTexCoord3.st;
gl_TexCoord[4].st = gl_MultiTexCoord4.st; // this is out size in x.y
VertexColor = gl_Color;
}
Geo shader 地理着色器
#version 330 compatibility
#extension GL_EXT_geometry_shader4 : enable
//uniform float uNormalsLength;
out vec2 texCoord;
out vec4 color;
layout(points) in;
layout(triangle_strip, max_vertices = 4) out;
void main(void)
{
vec4 vVertex = gl_in[0].gl_Position;
vec2 uvs[4];
uvs[0] = gl_TexCoord[0].st;
uvs[1] = gl_TexCoord[1].st;
uvs[2] = gl_TexCoord[2].st;
uvs[3] = gl_TexCoord[3].st;
vec2 scale = gl_TexCoord[4].st;
float vx[4];
vx[0] = -1.0;
vx[1] = -1.0;
vx[2] = 1.0;
vx[3] = 1.0;
float vy[4];
vy[0]= -1.0;
vy[1]= 1.0;
vy[2]= 1.0;
vy[3]= -1.0;
int indi[4];
indi[0] = 0;
indi[1] = 3;
indi[2] = 1;
indi[3] = 2;
texCoord = uvs[0];
vec4 oVertex = gl_PositionIn[0];
color = vec4(0.5, 0.5, 0.5, 1.0);
for(int i = 0; i < 4; ++i)
{
oVertex.xyz = vVertex.xyz;
color = vec4(0.0, 0.0, 0.5, 1.0);
oVertex.x += (vx[ indi[i] ] * scale.x);
oVertex.y += (vx[ indi[i] ] * scale.y);
texCoord.xy = uvs[ indi[i] ].xy;
gl_Position = gl_ModelViewProjectionMatrix * oVertex;
EmitVertex();
}
EndPrimitive();
}
Frag shader 片段着色器
// leaf fragment shader
#version 330 compatibility
uniform sampler2D colorMap;
in vec2 texCoord;
in vec4 color;
out vec4 FragColor;
void main (void)
{
vec2 uv = texCoord.xy;
uv.y *= -1.0;
vec4 t = texture2D(colorMap,uv);
FragColor = t;
FragColor.rgb += color.rgb;
}
When I send this a point, I get nothing out but a point. 当我提出一个要点时,我只会得到一个要点。 I have tried sending triangle_stips.. It makes no difference.
我试过发送triangle_stips ..没关系。
When I check the int32 returned gl_getuniformlocation for colorMap, its -1. 当我检查int32返回的colorMap的gl_getuniformlocation时,其为-1。
It is almost like this program does NOT running when I call gl_useprogram. 当我调用gl_useprogram时,几乎就像该程序未运行。 It has not effect.. I can't even effect the color and.. getting back -1 for colorMap makes me think the fragment shader never gets any input.
它没有效果。我什至不能影响颜色,并且为colorMap返回-1使我认为片段着色器永远不会得到任何输入。 I have tried hard coding a vertex in the vertex shader and it makes no difference.
我尝试在顶点着色器中对顶点进行硬编码,这没有什么区别。
I'm not sure about the transform but I can't test this as I cant see ANYTHING but a point. 我不确定转换,但是我无法测试,因为我看不到任何东西。 Like I said.. Its like its never being loaded.
就像我说的那样。 If I send the vertices as a triangle_strip, I see a triangle_strip on the screen.
如果将顶点作为triangle_strip发送,则在屏幕上会看到triangle_strip。 Thanks for any one that takes the time to read this.
感谢您抽出宝贵的时间阅读本文。
Sorry about all the code but maybe someone can spot something I'm missing. 对不起所有代码,但也许有人可以发现我所缺少的东西。 Also.. There is a lot of deprecated things in there but it has never been a problem in my other shaders.
另外,那里有很多不推荐使用的东西,但是在我的其他着色器中从来没有问题。
OK, I'll try to point out all of the wrong things you do. 好的,我将尝试指出您所做的所有错误操作。 But since there's so much that's wrong with this code, I probably won't get them all.
但是由于这段代码有很多错误,因此我可能不会全部了解。
#extension GL_EXT_geometry_shader4 : enable
The interactions between core Geometry Shaders in GL 3.2 and the EXT_geometry_shader4 functionality is not well-defined. GL 3.2中的核心“ 几何着色器”与EXT_geometry_shader4功能之间的交互作用尚不明确。 Therefore, exactly what this line will do is unknown.
因此,确切知道此行将要执行的操作。
This line, and any code that relates to it, should be removed. 此行以及与之相关的任何代码均应删除。 For example, the input variable
gl_PositionIn
is defined by EXT_geometry_shader4, but not core GS. 例如,输入变量
gl_PositionIn
由EXT_geometry_shader4定义,但不由核心GS定义。 Oddly, you do use that value, but you immediately overwrite it in the first step of the loop, for some reason. 奇怪的是,您确实使用了该值,但是由于某种原因,您立即在循环的第一步中将其覆盖。
uvs[0] = gl_TexCoord[0].st;
uvs[1] = gl_TexCoord[1].st;
uvs[2] = gl_TexCoord[2].st;
uvs[3] = gl_TexCoord[3].st;
vec2 scale = gl_TexCoord[4].st;
No matter what shader stage you put it in, gl_TexCoord
always refers to the output of that stage. 无论您将其放置在哪个着色器阶段,
gl_TexCoord
始终引用该阶段的输出 。 Never the input. 永远不要输入。 So you're copying the value of a shader stage output variable that you never write to.
因此,您正在复制从未写入的着色器阶段输出变量的值。
The correct input variable name would be gl_in[0].gl_TexCoord[x]
. 正确的输入变量名称应为
gl_in[0].gl_TexCoord[x]
。
The easiest way to check to see if you're reading an input variable in a GS is this: do you have to prefix/suffix it with an array index? 检查您是否正在读取GS中的输入变量的最简单方法是:您是否必须在数组索引前添加前缀/后缀? If you don't, then it's not an input variable.
如果不这样做,则它不是输入变量。 All Geometry Shader input variables are arrayed.
所有“几何着色器”输入变量均已排列。
gl_Position = gl_ModelViewProjectionMatrix * oVertex;
In your vertex shader, you transformed the output position by gl_ModelViewMatrix
. 在您的顶点着色器中,通过
gl_ModelViewMatrix
转换了输出位置。 Which transforms the positions from model space to camera/eye/view space (whatever you want to call it). 它将位置从模型空间转换为相机/眼睛/视图空间(无论您要调用什么)。 So those positions are now camera-relative.
因此,这些位置现在相对于相机而言。
The gl_ModelViewProjectionMatrix
transforms positions from model space to projection space. gl_ModelViewProjectionMatrix
将位置从模型空间转换为投影空间。 If your positions are in camera space (which, as previously established, they are), then this transformation is nonsensical. 如果您的位置在相机空间中(如先前所确定的那样),那么这种转换是没有意义的。
The correct matrix to use here would be gl_ProjectionMatrix
, which transforms from camera space to projection space. 此处使用的正确矩阵将是
gl_ProjectionMatrix
,它将从摄影机空间转换为投影空间。
This is what functioning code would look like. 这就是功能代码的样子。 Code that uses the OpenGL 3.3 core profile.
使用OpenGL 3.3核心配置文件的代码。
Vertex Shader: 顶点着色器:
#version 330 core
in vec4 position;
in vec4 color;
in vec2 texCoords[4]; //Yes, that's perfectly legal.
in vec2 scaleFactor;
out VS
{
out vec4 color;
out vec2 texCoords[4]; //Yes, this is legal too.
out vec2 scaleFactor;
} dest;
//The transformation from model space to camera space.
uniform mat4 modelToCamera;
void main(void)
{
gl_Position = modelToCamera * position;
dest.color = color;
for(int i = 0; i < texCoord.length(); ++i)
dest.texCoord[i] = texCoord[i];
dest.scaleFactor = scaleFactor;
}
Geometry Shader: 几何着色器:
#version 330 core
layout(points) in;
layout(triangle_strip, points) out;
in VS
{
vec4 color;
vec2 texCoords[4]; //Still legal.
vec2 scaleFactor;
} source[]; //One for each input vertex, but since we're using points, it's just one.
out FS
{
vec4 color;
vec2 texCoord;
} dest;
const vec2 offsets[4] = vec2[4](
vec2(-1.0, -1.0),
vec2(-1.0, 1.0),
vec2( 1.0, 1.0),
vec2( 1.0, -1.0));
//The transformation from camera to projection.
uniform mat4 ProjectionTM;
void main(void)
{
for(int i = 0; i < offsets.length(); ++i)
{
dest.color = source[0].color;
dest.texCoord = source[0].texCoord[i];
vec2 scale = offsets[i] * source[0].scaleFactor;
vec4 pos = gl_in[0].gl_Position;
pos.xy += scale;
gl_Position = ProjectionTM * pos;
EmitVertex();
}
EndPrimitive();
}
Fragment Shader: 片段着色器:
#version 330 core
in FS
{
vec4 color;
vec2 texCoord;
};
layout(location = 0) out vec4 FragColor;
uniform sampler2D colorMap;
void main (void)
{
vec4 t = texture2D(colorMap, texCoord.xy * vec2(1.0, -1.0));
FragColor = t;
FragColor.rgb += color.rgb;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.