Jump to content
  • Advertisement
Sign in to follow this  
DG756

OpenGL Shader help

This topic is 3036 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to implement Cone Step Mapping (if you have seen my previous thread) and I have made progress in loading the shaders, with the validation checks on compiling and linking both passing, however all I am getting is a black screen on my Mac (via Parallels - with an NVIDIA 9400M). And on my desktop (ATI HD4870) the OpenGL window goes white and the PC freezes and then my monitor goes black for a few seconds and then comes back on with the OpenGL window still white and unresponsive.

Below I've pasted my fragment shader (copied and pasted from the CSM paper - so that bit should be fine), my vertex shader (written myself but mostly taken from a different relief mapping example) and the C++ code I use to run it all. Can someone give it a glance-over to see if I am doing anything wrong?

Fragment Shader from CSM paper

uniform vec4 ambient;
uniform vec4 diffuse;
uniform float depth;
uniform float texsize;
uniform int conesteps;

varying vec2 texCoord;
varying vec3 eyeSpaceVert;
varying vec3 eyeSpaceTangent;
varying vec3 eyeSpaceBinormal;
varying vec3 eyeSpaceNormal;
varying vec3 eyeSpaceLight;

uniform sampler2D stepmap;
uniform sampler2D texmap;

float intersect_cone_fixed(in vec2 dp,in vec3 ds);
float intersect_cone_loop(in vec2 dp,in vec3 ds);
float intersect_cone_exact(in vec2 dp,in vec3 ds);

void main(void)
{
vec4 t,c;
vec3 p,v,l,s;
vec2 uv;
float d,a;

// ray intersect in view direction
p = eyeSpaceVert;
v = normalize(p);
a = dot(eyeSpaceNormal,-v)/depth;
s = normalize(vec3(dot(v,eyeSpaceTangent),dot(v,eyeSpaceBinormal),a));

// pick _one_ of the following variations
d = intersect_cone_fixed(texCoord,s);
//d = intersect_cone_loop(texCoord,s);
//d = intersect_cone_exact(texCoord,s);

// get rm and color texture points
uv=texCoord+s.xy*d;
c=texture2D(texmap,uv);

// expand normal from normal map in local polygon space
// blue = df/dx
// alpha = df/dy
// note: I _need_ the texture size to scale the normal properly!
t=texture2D(stepmap,uv);
t.xy=t.ba-0.5;
t.x = -t.x * depth * texsize;
t.y = -t.y * depth * texsize;
t.z = 1.0;
t.w = 0.0;
t.xyz=normalize(t.x*eyeSpaceTangent+t.y*eyeSpaceBinormal+t.z*eyeSpaceNormal);

// compute light direction
p += v*d*a;
l=normalize(p-eyeSpaceLight.xyz);

gl_FragColor = vec4(
ambient.xyz*c.xyz+
c.xyz*diffuse.xyz*max(0.0,dot(-l,t.xyz)),
1.0);
}

// slowest, but best quality
float intersect_cone_exact(in vec2 dp, in vec3 ds)
{
// minimum feature size parameter
float w = 1.0 / texsize;
// the "not Z" component of the direction vector
// (requires that the vector ds was normalized!)
float iz = sqrt(1.0-ds.z*ds.z);
// my starting location (is at z=1,
// and moving down so I don't have
// to invert height maps)
// texture lookup
vec4 t;
// scaling distance along vector ds
float sc=0.0;

// the ds.z component is positive!
// (headed the wrong way, since
// I'm using heightmaps)

// find the starting location and height
t=texture2D(stepmap,dp);
while (1.0-ds.z*sc > t.r)
{
// right, I need to take one step.
// I use the current height above the texture,
// and the information about the cone-ratio
// to size a single step. So it is fast and
// precise! (like a coneified version of
// "space leaping", but adapted from voxels)
sc += w + (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));

// find the new location and height
t=texture2D(stepmap,dp+ds.xy*sc);
}
// back off one step
sc -= w;

// return the vector length needed to hit the height-map
return (sc);
}

// the super fast version
// (change number of iterations at run time)
float intersect_cone_fixed(in vec2 dp, in vec3 ds)
{
// the "not Z" component of the direction vector
// (requires that the vector ds was normalized!)
float iz = sqrt(1.0-ds.z*ds.z);
// my starting location (is at z=1,
// and moving down so I don't have
// to invert height maps)
// texture lookup (and initialized to starting location)
vec4 t;
// scaling distance along vector ds
float sc;

// the ds.z component is positive!
// (headed the wrong way, since
// I'm using heightmaps)

// find the initial location and height
t=texture2D(stepmap,dp);
// right, I need to take one step.
// I use the current height above the texture,
// and the information about the cone-ratio
// to size a single step. So it is fast and
// precise! (like a coneified version of
// "space leaping", but adapted from voxels)
sc = (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));

// and repeat a few (4x) times
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));

// and another five!
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
t=texture2D(stepmap,dp+ds.xy*sc);
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));

// return the vector length needed to hit the height-map
return (sc);
}

// pretty fast version
// (and you can do LOD by changing "conesteps" based on size/distance, etc.)
float intersect_cone_loop(in vec2 dp, in vec3 ds)
{
// the "not Z" component of the direction vector
// (requires that the vector ds was normalized!)
float iz = sqrt(1.0-ds.z*ds.z);
// my starting location (is at z=1,
// and moving down so I don't have
// to invert height maps)
// texture lookup (and initialized to starting location)
vec4 t;
// scaling distance along vector ds
float sc=0.0;

//t=texture2D(stepmap,dp);
//return (max(0.0,-(t.b-0.5)*ds.x-(t.a-0.5)*ds.y));

// the ds.z component is positive!
// (headed the wrong way, since
// I'm using heightmaps)

// adaptive (same speed as it averages the same # steps)
//for (int i = int(float(conesteps)*(0.5+iz)); i > 0; --i)
// fixed
for (int i = conesteps; i > 0; --i)
{
// find the new location and height
t=texture2D(stepmap,dp+ds.xy*sc);
// right, I need to take one step.
// I use the current height above the texture,
// and the information about the cone-ratio
// to size a single step. So it is fast and
// precise! (like a coneified version of
// "space leaping", but adapted from voxels)
sc += (1.0-ds.z*sc-t.r) / (ds.z + iz/(t.g*t.g));
}
// return the vector length needed to hit the height-map
return (sc);
}



Vertex Shader

varying vec2 texCoord;

varying vec3 eyeSpaceVert;
varying vec3 eyeSpaceTangent;
varying vec3 eyeSpaceBinormal;
varying vec3 eyeSpaceNormal;
varying vec3 eyeSpaceLight;

void main() {

eyeSpaceVert = (gl_ModelViewMatrix * gl_Vertex).xyz;
eyeSpaceTangent = normalize(gl_NormalMatrix * gl_MultiTexCoord0.xyz); //tangent;
eyeSpaceBinormal = normalize(gl_NormalMatrix * (-gl_MultiTexCoord0.xyz)); //binormal;
eyeSpaceNormal = normalize(gl_NormalMatrix * gl_Normal); //normal
eyeSpaceLight = gl_LightSource[0].position.xyz;
texCoord = gl_MultiTexCoord0.xy;

gl_Position = ftransform();
}



CPP OpenGL Code to run

offscreen.loadImage("depth0.png-step.png", offscreen.depth1);
shader.setUniform(1, "stepmap", offscreen.depth1);
offscreen.loadImage("texture0.png", offscreen.tex1);
shader.setUniform(0, "texmap", offscreen.tex1);
glBegin(GL_TRIANGLES);
// front
glTexCoord2f(0.5f, 1.0f); glVertex3f( 0.0f, 0.0f, -400.0f);
glTexCoord2f(0.08f, 0.0f); glVertex3f(-335.0f, 335.0f, 300.0f);
glTexCoord2f(0.92f, 0.0f); glVertex3f( 335.0f, 335.0f, 300.0f);
glEnd();



setUniform function from above:

void setUniform(int activeTexture, string uniformName, GLuint id){
int location;

location = glGetUniformLocation(p, uniformName.c_str());
glUniform1i(location, activeTexture);

glActiveTexture(GL_TEXTURE0+activeTexture);
glBindTexture(GL_TEXTURE_2D, id);
}



Any help would be much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!