Drawing a triangle in OpenGL fragment shader

后端 未结 2 1649
梦毁少年i
梦毁少年i 2021-01-26 17:37

I\'m trying to draw a triangle using an OpenGL fragment shader.

I succeeded in drawing a circle but I have a problem with handling the equation/logic or the code to draw

2条回答
  •  面向向阳花
    2021-01-26 18:13

    Hope you are rendering QUAD covering the view/screen...

    The fragment shader friendly way of rendering triangle is to:

    1. compute barycentric s,t coordinates of fragment

      go for the matrix approach as you got mat3,vec3 in GLSL ...

    2. decide if it is inside or outside

      simply by testing s+t<=1.0

    3. then set output color or discard;

      however discard is not an option for you as you got more shapes...

    So compute:

    --------------------------------------------------------
    | s |           | (p1.a - p0.a) , (p2.a - p0.a) , p0.a |   | p.a |
    | t | = inverse | (p1.b - p0.b) , (p2.b - p0.b) , p0.b | * | p.b |
    | 1 |           |       0       ,       0       ,   1  |   |  1  |
    ------------------------------------------------------------------
    if (s+t<=1.0) set output color
    

    You can also use the s,t for texturing (even procedural one).

提交回复
热议问题