PDA

View Full Version : Problem with Vertex Displacement Mapping (GLSL)



anuwat_boy
11-29-2012, 05:59 AM
hi

I am new to GLSL programming. Right now, I am trying to make simple "Displacement Mapping" by following this tutorial http://www.ozone3d.net/tutorials/vertex_displacement_mapping.php

I can load images of colormap and displacementmap into glTexImage2D() without any problem. (displacement texture filter is nearest already)

The problem is I did not see any bump/displace on my plane.... I could see it be mapped with correct colorMap.
I felt that the whole plane is move along z-axis (normal) according to vertex shader when I changed value that multiply df at line "newVertexPos = vec4(gl_Normal * df * 100.0, 0.0) + gl_Vertex;"

VS
uniform sampler2D displacementMap;

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

gl_TexCoord[0].xy = gl_MultiTexCoord0.xy;

dv = texture2D( displacementMap, gl_MultiTexCoord0.xy );

df = 0.30*dv.x + 0.59*dv.y + 0.11*dv.z;

newVertexPos = vec4(gl_Normal * df * 100.0, 0.0) + gl_Vertex;

gl_Position = gl_ModelViewProjectionMatrix * newVertexPos;
}


PS
uniform sampler2D colorMap;

void main(void)
{
gl_FragColor = texture2D(colorMap, gl_TexCoord[0].xy);
}

Render function
GLfloat position[] = { 0, 0,30.f, 0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, position);
glUseProgram(m_program);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, m_imageTexture);
int colorMap_location = glGetUniformLocation(m_program, "colorMap");
glUniform1i(colorMap_location,0);

glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, m_displacementTexture);
int displacementMap_location = glGetUniformLocation(m_program, "displacementMap");
glUniform1i(displacementMap_location,1);

glBegin(GL_TRIANGLE_FAN);

glNormal3d(0,0,1.0);
glMultiTexCoord2d(GL_TEXTURE0, 0.0,0.0);
glVertex3d(-1.0, -1.0 ,0);

glNormal3d(0,0,1.0);
glMultiTexCoord2d(GL_TEXTURE0, 1.0,0.0);
glVertex3d(1.0, -1.0,0);


glNormal3d(0,0,1.0);
glMultiTexCoord2d(GL_TEXTURE0, 1.0,1.0);
glVertex3d(1.0, 1.0,0);

glNormal3d(0,0,1.0);
glMultiTexCoord2d(GL_TEXTURE0, 0.0,1.0);
glVertex3d(-1.0, 1.0,0);

glEnd();


Any idea what wrong with my coding ?

Thank in advance!

tonyo_au
11-29-2012, 10:06 PM
There is nothing wrong that's what you asked to code to do - all your vertices have the same normal which point along the z-axis. In you shader you multiply this normal by a values and add it to
the vertex so you will get a displacement along the z axis. Try putting different values in each normal like (0,1,0), (0.5,0.5,0) , (0,0.5,0.5), (0,0,0) and see what happens