在 OpenGL 片段着色器中绘制三角形 [英] Drawing a triangle in OpenGL fragment shader
问题描述
我正在尝试使用 OpenGL 片段着色器绘制一个三角形.
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 a triangle.
draw_triangle(vec2 v1 , vec2 v2, vec2 v3)
这是片段着色器:
#version 330 core
out vec4 frag_color;
void draw_circle(vec2 shift_val, int radius,int color)
{
vec2 res = vec2(1280,720);
vec2 norm_cord = gl_FragCoord.xy;
float dist = length(norm_cord - (res*shift_val));
if( dist < radius )
{
if( color ==1 )
frag_color = vec4(1.0, 1.0, 1.0, 1.0);
else
frag_color = vec4(0.0, 0.0, 0.0, 1.0);
}
}
void draw_triangle(vec2 v1 , vec2 v2, vec2 v3)
{
vec2 res = vec2(1280,720)*vec2(0.58,0.4);
vec2 v = vec2(gl_FragCoord.x,gl_FragCoord.y);
float slope1 = abs((v1.y-v2.y)/(v1.x-v2.x)); //y2-y1/x2-x1
float slope2 = abs((v2.y-v3.y)/(v2.x-v3.x)); //y2-y1/x2-x1
float slope3 = abs((v1.y-v3.y)/(v1.x-v3.x)); //y2-y1/x2-x1
float slope_ref1 = abs((v.y-v1.y)/(v.x-v1.x)); //y2-y1/x2-x1
float slope_ref2 = abs((v.y-v2.y)/(v.x-v2.x)); //y2-y1/x2-x1
float slope_ref3 = abs((v.y-v3.y)/(v.x-v3.x)); //y2-y1/x2-x1
float slope_RES1 = abs((res.y-v1.y)/(res.x-v1.x)); //y2-y1/x2-x1
float slope_RES2 = abs((res.y-v2.y)/(res.x-v2.x)); //y2-y1/x2-x1
float slope_RES3 = abs((res.y-v3.y)/(res.x-v3.x)); //y2-y1/x2-x1
if (slope_RES1 < slope1 )
{
if(slope_ref1 < slope1)// && slope_ref3 < slope2 )//slope_ref1 < slope1 &&
frag_color = vec4(1.0, 0.0, 1.0, 1.0);
}
if (slope_RES2 > slope2)
{
if(slope_ref2 > slope2)
frag_color = vec4(1.0, 0.5, 1.0, 1.0);
}
/*if (slope_RES3 < slope3)
{
if(slope_ref3 > slope3)
frag_color = vec4(1.0, 0.0, 1.0, 1.0);
}*/
}
// This is entry point of the fragment shader and it will be called for every fragment covered by the rasterized geometry
void main() {
// Here we just output a constant color which is red (R=1, G=0, B=0, A=1)
//frag_color = vec4(0.0, 0.0, 0.0, 1.0);
draw_circle(vec2(0.5,0.5),100,1); //draws face of circle
draw_circle(vec2(0.5,0.58),16,0); //draws eye (1 for white and anynumber for black)
draw_triangle(vec2(0.5f,0.5f),vec2(-0.5,0.0f),vec2(0.5f,-0.5f));
}
推荐答案
希望你正在渲染覆盖视图/屏幕的 QUAD...
Hope you are rendering QUAD covering the view/screen...
片段着色器友好的渲染三角形的方式是:
The fragment shader friendly way of rendering triangle is to:
计算片段的重心
s,t
坐标强>
当你在 GLSL 中得到 mat3,vec3
时,去使用矩阵方法......
go for the matrix approach as you got mat3,vec3
in GLSL ...
决定是在里面还是在外面
简单地通过测试s+t<=1.0
然后设置输出颜色或discard;
然而丢弃不是你的选择,因为你有更多的形状......
however discard is not an option for you as you got more shapes...
所以计算:
--------------------------------------------------------
| 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
您还可以使用 s,t
进行纹理化(甚至是程序化的).
You can also use the s,t
for texturing (even procedural one).
这篇关于在 OpenGL 片段着色器中绘制三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!