Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: scaling mesh along the normal

  1. #1
    Junior Member Regular Contributor
    Join Date
    Sep 2006
    Location
    thailand
    Posts
    130

    scaling mesh along the normal

    I tried to implement outlining of cell shade model by rendering Frontface-culled of the model using just black color.

    I do this by scale the mesh along the normal using following vertex shader

    Code :
    #version 150 core
     
    in vec3 vertex;
    in vec3 normal;
     
    uniform mat4 cameraMatrix;
    uniform mat4 modelMatrix;
    uniform mat4 projectionMatrix;
     
    void main()
    {	
            float scaleFactor = 0.005;
    	vec3 scaleVertex = vertex + normal*scaleFactor;
     
    	gl_Position = projectionMatrix*cameraMatrix*modelMatrix*vec4(scaleVertex.xyz,1.0);				
     
    }

    but with constant-predefined scaleFactor the outline will only look good at certain distance.

    When zoom in the outline will be very thick when zoom out the outline will be too fainted.

    What should I do to be able to calculate scale factor on the fly so that I can control the thickness of outline across different distance ?

    Thank

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,575

    Re: scaling mesh along the normal

    What about this :
    Code :
    {	
            float scaleFactor = 0.005;
    	gl_Position = projectionMatrix*cameraMatrix*modelMatrix*vec4(vertex.xyz,1.0)+ normal*scaleFactor;				
     
    }

  3. #3
    Intern Contributor
    Join Date
    Jun 2008
    Posts
    63

    Re: scaling mesh along the normal

    Assuming normal is transformed here, right?

    - Chris

  4. #4
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,575

    Re: scaling mesh along the normal

    Indeed, my bad

  5. #5
    Junior Member Regular Contributor
    Join Date
    Sep 2006
    Location
    thailand
    Posts
    130

    Re: scaling mesh along the normal

    Sorry to bother you again.

    I modified the shader as suggest by ZBuffer (with normal transformed) but the result are still the same (different size when zooming).

    Code :
    #version 150 core
     
    in vec3 vertex;
    in vec3 normal;
     
    uniform mat4 cameraMatrix;
    uniform mat4 modelMatrix;
    uniform mat4 projectionMatrix;
     
    void main()
    {	
    	float scaleFactor = 0.005;
     
    	mat4 modelviewMatrix = cameraMatrix*modelMatrix;
     
    	mat3 normalMatrix = mat3(modelviewMatrix[0].xyz,
    	modelviewMatrix[1].xyz,
    	modelviewMatrix[2].xyz);
     
    	vec3 viewSpaceNormal = normalMatrix*normal;
    	vec4 replacementPosition = projectionMatrix*vec4(viewSpaceNormal,0.0)*scaleFactor;	
     
    	gl_Position = projectionMatrix*cameraMatrix*modelMatrix*vec4(vertex.xyz,1.0)+replacementPosition;				
     
    }

    what I really want to do is making the scaled pixel (the width of the outline) fixed at some value , like 2 pixel across different zooming distance.

    Actually I could just use glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);
    and draw the line with out modifing the mesh. but my mesh has many unclosed surface (like back of character sleeve,hair).

  6. #6
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,575

    Re: scaling mesh along the normal

    What about a small normalize() here :

    vec4 replacementPosition = normalize(projectionMatrix*vec4(viewSpaceNormal,0. 0))*scaleFactor;

    gl_Position = projectionMatrix*cameraMatrix*modelMatrix*vec4(ver tex.xyz,1.0)+replacementPosition;

  7. #7
    Junior Member Regular Contributor
    Join Date
    Sep 2006
    Location
    thailand
    Posts
    130

    Re: scaling mesh along the normal

    Well it still look the same (this time with picture).

    zoom out (click to enlarge)


    zoom in (click to enlarge)


    May be I have to re-calculate scaleFactor based on camera position or something but I dont really care now since the result will look ugly anyway(The thin line look too jaggy).

    Can you guy suggest me some line rendering technique that work well with anime style cell shading technique ?

    I also try using edge detection on normal g-buffer but cannot manage to make line wide more than 1~2 pixel. and the same edge detection kernel produced and ugly result when use with anti-aliased (resolved to NOAA texture) normal g-buffer (resolved to NOAA texture) , the detected line still look jaggy which destroy the purpose of anti-aliasing.

    Edge detection line (NOAA , blend with source image)


    Edge detection line (2XAA , blend with source image)


    noticed that the line still look very jaggy and there also color leak

    Thank.

  8. #8
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,575

    Re: scaling mesh along the normal

    I am *very* surprised that adding a normalize still give the same result.
    Some thoughts :

    vec4 projNormal = projectionMatrix*vec4(normalize(viewSpaceNormal),0 .0)
    vec4 replacementPosition = (normalize(projNormal)/projNormal)*scaleFactor;

  9. #9
    Senior Member OpenGL Pro Ilian Dinev's Avatar
    Join Date
    Jan 2008
    Location
    Watford, UK
    Posts
    1,294

    Re: scaling mesh along the normal

    Don't you have to multiply that rotated normal by W or (Z*0.5+ 0.5) at some point?

    The higher the Z, the larger the added displacement should be.

    Btw, how about rendering wide lines, in wireframe, and cull=front?

  10. #10
    Junior Member Regular Contributor
    Join Date
    Sep 2006
    Location
    thailand
    Posts
    130

    Re: scaling mesh along the normal

    That would be ideal solution (and should handle AA fine) but my mesh had many unclosed part (like sleeve , skirt and hair) which will result in visible wire frame and glLineWidth is also disable in OpenGL 3.2 core profile.

    I am now try to improve my edge detection shader to better handle AA image (managed to fix color bleeding but still look jagged).

    Thank

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •