Drawing a triangle in OpenGL fragment shader

后端 未结 2 1682
梦毁少年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:14

    To compute if a point is in a triangle using the same side technique, you need to test the candidate point against three lines to see which side of each line it is on. If it meets the sidedness test for all three lines, then it is inside the triangle.

    The condition test will be C(0) && C(1) && C(2).

    Where C(n) means: "Is the point on the correct side of edge n"

    The condition "which side of the line AB is the point X" is typically checked by checking the sign of the cross product of AB × AX. You could, by convention, assign a winding order to your triangle, and always check that the sign of this cross product is positive. This, of course, depends on the winding order of the vertices of your triangle. (For example, clockwise vertices require a negative cross product, and counterclockwise vertices require a positive cross product. Choose whichever convention you like or is most convenient given the definition of your polygon.)

    You can, alternatively, test using the barycentric technique.

    See: this site for more details.

提交回复
热议问题