M//Hax
12-22-2010, 11:17 AM
I'm working on transforming a set of point sprites to sphere using a geometry shader (i found that it could be a good idea).
RIght now, the vertex and fragment shader are working, they make the point sprites look like sphere but in 2D (the texture of the sphere is always facing you). When i'm trying to use the geometry shader below, nothing is rendered. HELP!!! lol!
Here the code :
// vertex shader
uniform float pointRadius; // point size in world space
uniform float pointScale; // scale to calculate size in pixels
uniform vec4 eyePos;
void main()
{
vec4 wpos = vec4(gl_Vertex.xyz, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * wpos;
// calculate window-space point size
vec4 eyeSpacePos = gl_ModelViewMatrix * wpos;
float dist = length(eyeSpacePos.xyz);
gl_PointSize = pointRadius * (pointScale / dist);
gl_TexCoord[0] = gl_MultiTexCoord0; // sprite texcoord
gl_TexCoord[1] = eyeSpacePos;
gl_FrontColor = gl_Color;
}
// pixel shader for rendering points as shaded spheres
uniform float pointRadius;
uniform vec3 lightDir = vec3(0.577, 0.577, 0.577);
void main()
{
// calculate eye-space sphere normal from texture coordinates
vec3 N;
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
float r2 = dot(N.xy, N.xy);
if (r2 > 1.0) discard; // kill pixels outside circle
N.z = sqrt(1.0-r2);
// calculate depth
vec4 eyeSpacePos = vec4(gl_TexCoord[1].xyz + N*pointRadius, 1.0); // position of this pixel on sphere in eye space
vec4 clipSpacePos = gl_ProjectionMatrix * eyeSpacePos;
gl_FragDepth = (clipSpacePos.z / clipSpacePos.w)*0.5+0.5;
float diffuse = max(0.0, dot(N, lightDir));
gl_FragColor = diffuse*gl_Color;
}
// geometry shader
//#version 120
//#extension GL_EXT_geometry_shader4 : enable
const float radius = 0.5;
varying out vec2 coord;
void main()
{
for (int i = 0 ; i < gl_VerticesIn ; ++i )
{
gl_FrontColor = gl_FrontColorIn[i];
gl_Position = gl_PositionIn[i];
EmitVertex ( );
}
gl_FrontColor = gl_FrontColorIn[0];
coord = vec2( -1,-1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,-radius,0,0) );
EmitVertex();
coord = vec2( -1,1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,radius, 0,0) );
EmitVertex();
coord = vec2( 1,-1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,-radius, 0,0) );
EmitVertex();
coord = vec2( 1,1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,radius, 0,0) );
EmitVertex();
EndPrimitive();
}
RIght now, the vertex and fragment shader are working, they make the point sprites look like sphere but in 2D (the texture of the sphere is always facing you). When i'm trying to use the geometry shader below, nothing is rendered. HELP!!! lol!
Here the code :
// vertex shader
uniform float pointRadius; // point size in world space
uniform float pointScale; // scale to calculate size in pixels
uniform vec4 eyePos;
void main()
{
vec4 wpos = vec4(gl_Vertex.xyz, 1.0);
gl_Position = gl_ModelViewProjectionMatrix * wpos;
// calculate window-space point size
vec4 eyeSpacePos = gl_ModelViewMatrix * wpos;
float dist = length(eyeSpacePos.xyz);
gl_PointSize = pointRadius * (pointScale / dist);
gl_TexCoord[0] = gl_MultiTexCoord0; // sprite texcoord
gl_TexCoord[1] = eyeSpacePos;
gl_FrontColor = gl_Color;
}
// pixel shader for rendering points as shaded spheres
uniform float pointRadius;
uniform vec3 lightDir = vec3(0.577, 0.577, 0.577);
void main()
{
// calculate eye-space sphere normal from texture coordinates
vec3 N;
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
float r2 = dot(N.xy, N.xy);
if (r2 > 1.0) discard; // kill pixels outside circle
N.z = sqrt(1.0-r2);
// calculate depth
vec4 eyeSpacePos = vec4(gl_TexCoord[1].xyz + N*pointRadius, 1.0); // position of this pixel on sphere in eye space
vec4 clipSpacePos = gl_ProjectionMatrix * eyeSpacePos;
gl_FragDepth = (clipSpacePos.z / clipSpacePos.w)*0.5+0.5;
float diffuse = max(0.0, dot(N, lightDir));
gl_FragColor = diffuse*gl_Color;
}
// geometry shader
//#version 120
//#extension GL_EXT_geometry_shader4 : enable
const float radius = 0.5;
varying out vec2 coord;
void main()
{
for (int i = 0 ; i < gl_VerticesIn ; ++i )
{
gl_FrontColor = gl_FrontColorIn[i];
gl_Position = gl_PositionIn[i];
EmitVertex ( );
}
gl_FrontColor = gl_FrontColorIn[0];
coord = vec2( -1,-1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,-radius,0,0) );
EmitVertex();
coord = vec2( -1,1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4(-radius,radius, 0,0) );
EmitVertex();
coord = vec2( 1,-1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,-radius, 0,0) );
EmitVertex();
coord = vec2( 1,1 );
gl_Position = (gl_PositionIn[0] + gl_ProjectionMatrix * vec4( radius,radius, 0,0) );
EmitVertex();
EndPrimitive();
}