【发布时间】:2019-10-07 12:19:55
【问题描述】:
使用下面的着色器代码,我可以将来自三个摄像头的帧显示到单个 openGL 控件。这个 opengl 控件的起始位置应该从屏幕中心开始,并且应该从左端开始到全屏宽度。也就是说控件的宽度是屏幕宽度,高度是屏幕高度的一半。但问题是纹理以外的区域,它显示为 ClearColor(设置为蓝色)。
如果 (uv.y > 1.0) 丢弃;
我可以从 GLControl 中移除/删除这个额外的区域吗?
int y = Screen.PrimaryScreen.Bounds.Height - this.PreferredSize.Height;
glControl1.Location = new Point(0, y/2);
private void OpenGL_SizeChanged(object sender, EventArgs e)
{
glControl1.Width = this.Width;
glControl1.Height = this.Height/2;
}
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder);
private void CreateShaders()
{
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, @"attribute vec3 a_position;
varying vec2 vTexCoordIn;
//uniform float aspect;
void main() {
vTexCoordIn=( a_position.xy+1)/2;
gl_Position = vec4(a_position,1);
}");
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, @"
uniform sampler2D sTexture;
uniform sampler2D sTexture1;
uniform sampler2D sTexture2;
uniform vec2 sTexSize;
uniform vec2 sTexSize1;
uniform vec2 sTexSize2;
varying vec2 vTexCoordIn;
void main ()
{
vec2 vTexCoord=vec2(vTexCoordIn.x,vTexCoordIn.y);
if ( vTexCoord.x < 1.0/3.0 )
{
vec2 uv = vec2(vTexCoord.x * 3.0, vTexCoord.y);
uv.y *= sTexSize.x / sTexSize.y;
if (uv.y > 1.0)
discard;
else
gl_FragColor = texture2D(sTexture, uv);
}
else if ( vTexCoord.x >= 1.0/3.0 && vTexCoord.x < 2.0/3.0 )
{
vec2 uv = vec2(vTexCoord.x * 3.0 - 1.0, vTexCoord.y);
uv.y *= sTexSize1.x / sTexSize1.y;
if (uv.y > 1.0)
discard;
else
gl_FragColor = texture2D(sTexture1, uv);
}
else if ( vTexCoord.x >= 2.0/3.0 )
{
vec2 uv = vec2(vTexCoord.x * 3.0 - 2.0, vTexCoord.y);
uv.y *= sTexSize2.x / sTexSize2.y;
if (uv.y > 1.0)
discard;
else
gl_FragColor = texture2D(sTexture2, uv);
}
}");
GL.CompileShader(fragShader);
}
【问题讨论】:
-
懒得分析你的代码(尤其是当我不使用 C# 编写代码时),但你所描述的不是着色器的工作。对于单个帧中的多个摄像机视图,您应该使用
glViewport,请参阅How to show visible part of planar world rendered with 3D perspective on topside 2D minimap?。无论如何,您应该向我们展示一些显示您的问题的屏幕截图,这样我们就不必猜测您在处理什么。 -
@Spektre 请看添加的截图
-
因此,您已将 3 个图像重新缩放到共同高度,并且它们的宽度总和等于桌面宽度。空白区域只是您的窗口高度与计算的图像的共同高度之间的差异。所以要么调整你的窗口大小,要么将你的框架重新调整到它的高度(但后者会破坏纵横比)。由于帧已经是图像,因此不需要 glViewport ...对于具有不同相机而不是来自物理相机的帧的渲染帧就是这种情况:)
-
每个相机可能有不同的分辨率。现在所有纹理的分辨率都设置为相机 2 的分辨率。
标签: c# winforms opengl fragment-shader opentk