Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: 1px line using triangles

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    11

    1px line using triangles

    Hi,
    I have geometry (or fallback vertex only) shader, that generates lines with custom width and pass float uniform into shader.
    For lines that are more than 1px thick, it works great. But if I want to draw nonantialliased line with 1px width, it looks ugly, especialy when line is not perpendicular to screen.
    Any options ?
    Problem is that I have only one definition of some geometry and wants to draw it with different line widths many times
    Thank you

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    it looks ugly
    Define ugly. Screen shot please.

  3. #3
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    11
    Click image for larger version. 

Name:	ugly.png 
Views:	105 
Size:	1.2 KB 
ID:	876
    in 45 degrees rotation the line (created by two triangles) is 2px wide instead of 1px

  4. #4
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    Hmm, shaders please.

  5. #5
    Junior Member Newbie
    Join Date
    Jul 2012
    Posts
    11
    Code :
    geometry shader line draw function: 
    // make thick line
    // from : transformed point (by modelviewprojectionmatrix)
    // to : trasformed point
    // colorfrom : first point color
    // colorto : second point color
    void makeLine(in vec4 from, in vec4 to, in vec4 colorfrom, in vec4 colorto)
    {
    // triangle positions
        vec4 position1 = from;
        vec4 position2 = from;
        vec4 position3 = to;
        vec4 position4 = to;
     
        // direction of line
        vec2 line2d = position3.xy - position1.xy;    
     
        // normalized direction
        vec2 direction = normalize(line2d);
     
        // shift vector
        vec2 vector;
     
        // make vector perpendicular to line
        vector.x = direction.y / screen.x;
        vector.y = -direction.x / screen.y;    
     
     
        // normalizedwidthdiv2 : global variable width normalized from px width to normalized coords
        // vec2 normalizedwidthdiv2 = width / screen;
        // uniform vec2 screen; // screen size in px
        // uniform vec2 width; // width of line in px
     
        vec2 shift = normalizedwidthdiv2 * vector;
     
        // shift positions
        position1.xy -= shift;
        position2.xy += shift;
        position3.xy -= shift;
        position4.xy += shift;
     
        // emit triangles
        gl_Position = position1;
        gl_FrontColor = colorfrom;
        EmitVertex();
     
       gl_Position = position3;
        gl_FrontColor = colorto;
        EmitVertex(); 
     
        gl_Position = position4;
        gl_FrontColor = colorto;
        EmitVertex();
     
        EndPrimitive();
     
        gl_Position = position1;
        gl_FrontColor = colorfrom;
        EmitVertex();
     
        gl_Position = position2;
        gl_FrontColor = colorfrom;
        EmitVertex(); 
     
        gl_Position = position4;
        gl_FrontColor = colorto;
        EmitVertex();
     
        EndPrimitive();
    }
    Last edited by Dark Photon; 09-21-2012 at 06:04 PM.

  6. #6
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    3,213
    Please use [ code ] ... [ /code ] blocks. Preserves the indentation. I inserted them for you.

  7. #7
    Member Regular Contributor
    Join Date
    Jan 2005
    Location
    USA
    Posts
    411
    Is it guaranteed that a consumer card can do this with pixel perfect precision?

    Quote Originally Posted by Dark Photon View Post
    Please use [ code ] ... [ /code ] blocks. Preserves the indentation. I inserted them for you.
    I guess that makes Dark Photon a mod-and-or-admin then.

    So many interesting threads.
    God have mercy on the soul that wanted hard decimal points and pure ctor conversion in GLSL.

Tags for this Thread

Posting Permissions

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