how to pass variables among vertex, geometry and fragment shaders

Started by
4 comments, last by sobeit 10 years, 10 months ago

Hi,

I just started to learn using geometry shaders to handle more complex things. but I get stuck in passing variables through different shaders. What I want to do is simply pass pixelNormal computed in vertex shader to fragment shader. Following is my vertex, geometry and fragment shaders

code. Could anyone help point out what's with my code? Thanks very much.


#version 420

layout(location = 0) in vec3 position;
layout(location = 1) in vec2 texCoord;
layout(location = 2) in vec3 normal;

out vec3 v2gPixelNormal; // v2g means vertex shader to geometry shader

uniform mat4 vNormalMatrix;

void main()
{
	gl_Position = vec4(position, 1.0);	
	v2gPixelNormal = (vec4(normal, 1.0f) * vNormalMatrix).xyz;
}

#version 420

layout (triangles) in;
layout (triangle_strip, max_vertices = 3) out;

uniform mat4 gPVM; //perspective view model matrix

in vec3 v2gPixelNormal[];

out vec3 g2fPixelNormal;

void main()
{
	for(int i = 0; i < gl_in.length(); ++i)
	{
		gl_Position = gPVM * gl_in[i].gl_Position;
		g2fPixelNormal = v2gPixelNormal[i];
		EmitVertex();
	}
	EndPrimitive();
}


#version 420

smooth in vec3 g2fPixelNormal; // interplated normal of pixel

layout(location = 0)out vec4 FragClr;

void main()
{	
	FragClr = phongShading(normalize(g2fPixelNormal));
}
Advertisement

I don't see anything wrong in the way you are passing your variables around. Do you get any compile/link error or is it just looking wrong?

I don't see anything wrong in the way you are passing your variables around. Do you get any compile/link error or is it just looking wrong?

no errors, it looks flat, like no lighting applied. if I skip geomety shader, it works just what I expected. I think it's the problem of how I pass the pixelNormal.

Isn't your normal multiplication wrong? Shouldn't it be matrix * normal? And you're using 1.0f for the normal's last component, it should be zero. Not sure if this is your problem though, if it really somehow worked without the geometry shader.


v2gPixelNormal = (vNormalMatrix * vec4(normal, 0.0f)).xyz;

Derp

Sponji is right, vec4(normal, 1.0f) actually translates your normales too, you could also pass the normal matrix as a 3x3 matrix into the shader

thank you guys, it turns out that I foolishly mixed up uniforms in vertex shader with those in geometry shader.

and I have an another question now. Actually, I use geometry shader to do ray cast picking in which I need to do line-triangle intersection for every triangle of an object.

what I want to do is to set a bool variable "ifSelected" that will be set to true once line-triangle intersection succeed, so that I don't need to do line-triangle intersection detection for rest of triangles.

This topic is closed to new replies.

Advertisement