Jump to content
  • Advertisement
Sign in to follow this  

generating greebles in realtime

This topic is 3276 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

Hello, i have been trying to figure out a way to generate "greebles" on geometry using shaders. can anyone point me in the right direction ? i was trying with Vertex-Texture-Fetch, but it seems to depend alot on high-poly geometry meshes and the displacement doesnt really look correct as it is in the texture. just tried something simple like: /// ----------------------- vec3 pos = gl_Vertex.xyz; texcoord0 = gl_MultiTexCoord0.xy; normal = normalize(gl_NormalMatrix * gl_Normal); // texture-fetch dv = texture2DLod( dispMap, gl_MultiTexCoord0.xy, 0 ); df = 0.30*dv.x + 0.59*dv.y + 0.11*dv.z; // get brightness // get new position for this vertex based on the texture coord newVertexPos = vec4( pos, 1.0 ); newVertexPos.xyz += normal * df * maxHeight; // compute clip space vec4 outPos = gl_ModelViewProjectionMatrix * newVertexPos; gl_Position = outPos; thanks in advance,

Share this post

Link to post
Share on other sites
Why do this in geometry? Use parallax occlusion mapping or the like, and do it all in pixel shading.

Share this post

Link to post
Share on other sites
hmm i thought about that, but how can it generate something like this?




Share this post

Link to post
Share on other sites
Original post by murderv
hmm i thought about that, but how can it generate something like this? http://vimeo.com/2010615
Probably not, although variants of cone-step relief mapping may be able to handle offsets that deep.

If you need extrusions of that depth, you probably need to muck around with extruding faces in the geometry shader.

Share this post

Link to post
Share on other sites
here's my vertex shader code for generating greebles using vertex texture-fetch
for lighting i've implemented bump mapping for a static displacemente map, but, i would love to know how to generate realtime normals for realtime generated texture. i want to change my displacemente texture in realtime.


uniform sampler2D dispMap;

varying vec2 texcoord0;
varying vec3 lightVec;
varying vec3 eyeVec;

// sphere distortion
uniform float time;
uniform vec3 count;
uniform vec3 phase;
uniform vec3 amount;
uniform float radius;

uniform float texSize;
uniform vec3 cameraPos;
uniform vec4 lightPos;

// changes height of all greebles. would be cool to be able to change independently
uniform float maxHeight;

#define PI 3.14159265
#define TWOPI 6.28318531

// sphere formula
vec3 getSpherePoint( float u, float v )
// 0 <= u <= PI, 0 <= v <= TWOPI
float x = sin(u) * cos(v);
float y = sin(u) * sin(v);
float z = cos(u);

float r = radius;
r += sin(0.25*x * count.x + time + phase.x) * amount.x;
r += cos(u*count.y + phase.y) * amount.y;

return vec3( x*r, y*r, z*r );


vec4 texture2DLod_bilinear( sampler2D texSampler, vec2 uv )
float texelSize = 1.0 / texSize;

vec4 height00 = texture2DLod( texSampler, uv, 0.0 );
vec4 height10 = texture2DLod( texSampler, uv+vec2(texelSize, 0), 0.0 );
vec4 height01 = texture2DLod( texSampler, uv+vec2(0, texelSize), 0.0 );
vec4 height11 = texture2DLod( texSampler, uv+vec2(texelSize, texelSize), 0.0 );
vec2 f = fract( uv.xy * texSize );

vec4 tA = mix( height00, height10, f.x );
vec4 tB = mix( height01, height11, f.x );

return mix( tA, tB, f.y );

void main(void)
vec4 newVertexPos;
vec4 dv;
float df;

float u = (gl_Vertex.x) * PI;
float v = (gl_Vertex.y) * TWOPI;
vec3 pos = getSpherePoint( u, v );

// Compute tangents and normals
float step = (1.0/texSize);
vec3 neighbour1 = getSpherePoint( u+step, v );
vec3 neighbour2 = getSpherePoint( u, v+step );
vec3 tangent = neighbour1 - pos;
vec3 bitangent = neighbour2 - pos;
tangent = normalize( tangent );
bitangent = normalize( bitangent );
vec3 normal = normalize( cross( tangent, bitangent ) );

texcoord0 = gl_MultiTexCoord0.xy;

// get offset on current pixel..
dv = texture2DLod_bilinear( dispMap, texcoord0 );
// compute pixel brightness
df = 0.30*dv.x + 0.59*dv.y + 0.11*dv.z;

// compute new vertex position based on current pos + normal + offset
newVertexPos = vec4( pos, 1.0 );
newVertexPos.xyz += normal * df * maxHeight;

// TBN matrix
vec3 n = normalize( gl_NormalMatrix * normal );
vec3 t = normalize( gl_NormalMatrix * tangent);
vec3 b = normalize( cross( n, t ) );

vec3 vertexPosition = vec3(gl_ModelViewMatrix * newVertexPos);
vec3 lightDir = normalize(gl_LightSource[0].position.xyz - vertexPosition);

lightVec.x = dot( lightDir, t );
lightVec.y = dot( lightDir, b );
lightVec.z = dot( lightDir, n );

eyeVec.x = dot( -vertexPosition, t );
eyeVec.y = dot( -vertexPosition, b );
eyeVec.z = dot( -vertexPosition, n );

vec4 outPos = gl_ModelViewProjectionMatrix * newVertexPos;
gl_Position = outPos;

// logaritmic z buffer
// gl_TexCoord[6] = outPos;

what is going on:
pass a unit size subdivided plane of 256 cells, applied a deformer to generate a distorted sphere out of it and then, apply per pixel lighting (its not bad, but can be wrong).

on the first image, sphere is distorted by music frequencies, on the second just texcoord scaling for more greebles.

its the only way i got some good results.. using relief mapping of POM didn't work out, as expect. the offseting is just too big for such technique imo.

hope this helps anyone looking for information.

Share this post

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

  • Advertisement

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!