- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序使用 OpenGL 着色器代码全屏显示图像,如下所示。我在这里使用的顶点着色器是从某处复制的。谁能解释为什么这里使用 vTexCoord = (a_position.xy+1)/2;
?当我尝试使用 vTexCoord = a_position.xy
时,我的 OpenGL 输出被分成四个矩形,只有右上角的部分显示图像。其他三个边看起来一样模糊。我应该做哪些更改才能使用 vTexCoord = a_position.xy
?
项目中用到的一些重要函数如下所示。请检查并帮助更正。
float[] vertices = {
// Left bottom triangle
-1f, -1f, 0f,
1f, -1f, 0f,
1f, 1f, 0f,
// Right top triangle
1f, 1f, 0f,
-1f, 1f, 0f,
-1f, -1f, 0f
};
private void CreateShaders()
{
/***********Vert Shader********************/
vertShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource(vertShader, @"attribute vec3 a_position;
varying vec2 vTexCoord;
void main() {
vTexCoord = (a_position.xy+1)/2;
gl_Position = vec4(a_position, 1);
}");
GL.CompileShader(vertShader);
/***********Frag Shader ****************/
fragShader = GL.CreateShader(ShaderType.FragmentShader);
GL.ShaderSource(fragShader, @"precision highp float;
uniform sampler2D sTexture;varying vec2 vTexCoord;
void main ()
{
vec4 color= texture2D (sTexture, vTexCoord);
gl_FragColor =color;
}");
GL.CompileShader(fragShader);
}
private void InitBuffers()
{
buffer = GL.GenBuffer();
positionLocation = GL.GetAttribLocation(program, "a_position");
positionLocation1 = GL.GetUniformLocation(program, "sTexture");
GL.EnableVertexAttribArray(positionLocation);
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, 0, 0);
}
public void DrawImage(int image)
{
GL.Viewport(new Rectangle(0, 0, ScreenWidth, ScreenHeight));
GL.MatrixMode(MatrixMode.Projection);
GL.PushMatrix();
GL.LoadIdentity();
//GL.Ortho(0, 1920, 0, 1080, 0, 1);
GL.MatrixMode(MatrixMode.Modelview);
GL.PushMatrix();
GL.LoadIdentity();
GL.Disable(EnableCap.Lighting);
GL.Enable(EnableCap.Texture2D);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.Texture2D, image);
GL.Uniform1(positionLocation1, 0);
GL.Begin(PrimitiveType.Quads);
GL.TexCoord2(0, 1);
GL.Vertex3(0, 0, 0);
GL.TexCoord2(0, 0);
GL.Vertex3(1920, 0, 0);
GL.TexCoord2(1, 1);
GL.Vertex3(1920, 1080, 0);
GL.TexCoord2(1, 0);
GL.Vertex3(0, 1080, 0);
GL.End();
RunShaders();
GL.Disable(EnableCap.Texture2D);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Projection);
GL.PopMatrix();
GL.MatrixMode(MatrixMode.Modelview);
glControl1.SwapBuffers();
}
private void RunShaders()
{
GL.UseProgram(program);
GL.DrawArrays(PrimitiveType.Triangles, 0, vertices.Length / 3);
}
最佳答案
Can anyone explain why here used
vTexCoord = (a_position.xy+1)/2;
?
对于 x 和分量,您示例中的顶点坐标在 [-1, 1] 范围内。这与规范化的设备空间相匹配。规范化设备空间是一个从左下前 (-1, -1, -1) 到右上后 (1, 1, 1) 的立方体,这是“可见”的区域。此区域映射到视口(viewport)。
在您的示例中,这会导致顶点坐标形成一个覆盖整个视口(viewport)的矩形。
如果纹理坐标应将整个纹理包裹到四边形,则纹理坐标 (u, v) 必须在 [0, 1] 范围内。 (0, 0) 是纹理的左下角,(1, 1) 是右上角。
另见 How do OpenGL texture coordinates work?
所以 a_position
的 x 和 y 分量必须从范围 [-1, 1] 映射到范围 [0, 1],用作纹理的 uv 坐标查找:
u = (a_position.x + 1) / 2
v = (a_position.y + 1) / 2
What change should I do to work it with
vTexCoord = a_position.xy
?
这是不可能的,但你可以生成一个单独的纹理坐标属性,这很常见:
attribute vec3 a_position;
attribute vec2 a_texture;
varying vec2 vTexCoord;
void main() {
vTexCoord = a_texture;
gl_Position = vec4(a_position, 1);
}
float[] vertices = {
// x y z u v
// Left bottom triangle
-1f, -1f, 0f, 0f, 0f
1f, -1f, 0f, 1f, 0f
1f, 1f, 0f, 1f, 1f
// Right top triangle
1f, 1f, 0f, 1f, 1f
-1f, 1f, 0f, 0f, 1f
-1f, -1f, 0f 0f, 0f
};
buffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer, buffer);
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);
positionLocation = GL.GetAttribLocation(program, "a_position");
tetureLocation = GL.GetAttribLocation(program, "a_texture");
GL.EnableVertexAttribArray(positionLocation);
GL.EnableVertexAttribArray(tetureLocation);
int stride = sizeof(float) * 5; // 5 because of (x, y, z, u, v)
int offsetUV = sizeof(float) * 3; // 3 because the u and v coordinates are the 4th and 5th coordinate
GL.VertexAttribPointer(positionLocation, 3, VertexAttribPointerType.Float, false, stride, 0);
GL.VertexAttribPointer(tetureLocation, 2, VertexAttribPointerType.Float, false, stride, (IntPtr)(offsetUV));
关于c# - vTextCoord 值不起作用 - c# OPENTK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54414094/
这是顶点着色器: uniform mat4 projection; uniform mat4 view; uniform mat4 model; void main(void) { gl_Po
我有一个问题,我是需要二次幂 (POT) 纹理并且似乎无法使用 NPOT 的幸运者之一。 通常,这不会让我太烦恼,因为我只会确保我需要使用的任何纹理都是 POT。但是,我在尝试将光标 X 和 Y 位置
我最近在我的游戏中实现了视锥体剔除,并且为了从渲染周期中挤出最后一滴,我还决定实现遮挡剔除。它工作得很好,但是,我很痛苦地发现,如果我不看游戏中的任何东西(如果我向虚空看,远离我的游戏对象),我的显卡
我正在寻找一种用颜色“填充”三维几何体的方法,并且很可能在稍后的某个时间填充纹理。 假设你可以将头撞到混凝土墙上,从逻辑上讲你只会看到黑暗。然而,在 OpenGL 中,当您执行此操作时,由于剔除以及几
警告:总的 OpenGL/TK 新手,请善待。我可能咬得太多了。 规范: 2017 年 Visual Studio 社区 C# .Net 4.6.1(使用 WPF、WindowsFormsHost 控
我正在寻找一种跨平台(Win 和 MacOS)方法来检测 C# 中 OpenGL 应用程序的按键。 以下有效,但仅适用于字母数字字符。 protected override void OnKeyPre
我在 http://viewport3d.com/trackball.htm 上使用教程编程轨迹球。我计算了轴和角度,但因为我只是 OpenTK 和 OpenGL 的初学者,所以我不知道如何将旋转应用
我有两张图片。我必须找到第一张图像中强度大于 0.8 的点。同时,我必须在相同点上找到第二张图像的强度,并且需要使用阈值/ slider 值(范围从 0 到 1)调整相同点上第二张图像上的光线。我做了
有没有人知道如何使用 OpenTK 框架滚动(动画化)2D 文本,或者在哪里可以找到一些好的示例。我需要新闻电视 channel 底部(顶部)滚动代码之类的东西。 谢谢。 最佳答案 有许多可能的文本渲
尝试制作游戏,但纹理从未出现在屏幕上。 只为四边形使用颜色是可行的,但是当我尝试为四边形添加纹理时,它只是显示为黑色方 block 。 基本片段着色器: #version 300 es precisi
有没有办法在另一个线程中使用 OpenTK/OpenGL 绘制到屏幕上?我尝试使用的代码是 GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColo
它是 tao 框架中已弃用的 OpenTK.Graphics.Glu 的替代品吗?尤其是对几何渲染的需求。 最佳答案 OpenTK 是 Tao 的演变。而且,老实说,我没有听说过 glut 被贬低了。
我是 opengl 的新手,我正在努力让 stencilbuffer 为一个简单的案例工作;我有两种纹理,一种是位图,另一种是“ mask ”,我试图用它来隐藏位图中的某些部分。 我似乎无法让它工作,
我正在尝试在我的 OpenTK 项目中旋转 2D 对象。 发生的事情是旋转对象之后的对象正在旋转,我不希望它们这样做。 我已经旋转了一个对象后如何重置旋转? protected overr
我正在为 OpenGL 和 C# 使用 OpenTK。 我需要使用模板缓冲区,目前想知道如何设置 StencilBuffer 深度。 我之前知道的是 TaoFramework,它的控件具有可以设置此深
我在进行视锥体剔除的方式上遇到了一些问题。当前的方法确实解决了剔除问题,但效果确实很奇怪。当我离我的主要父对象太近时,我正在使用场景图来渲染所有内容,对象开始非常快速地闪烁。当我离开时,它就消失了。我
我正在使用 OpenTK 和 C#,我在 3D 空间中定义了一个平面,如下所示: GL.Begin(BeginMode.Quads); GL.Color3(Color.Magenta); GL.Ver
我不确定为什么这段代码不是简单地在屏幕上绘制一个三角形(正交)。我正在使用与 OpenGL 1.1 相同的 OpenTK 1.1。 List simpleVertices = new List();
我正在尝试找到一种方法来使窗口始终位于顶部。 (必须是窗口模式)我目前正在使用 OpenTk.NetCore 库在 .net 核心中制作一个窗口。是否可以使用 OpenTk 将此窗口保持在顶部或是否有
我目前正在开发一款使用 OpenTK 进行渲染的游戏; C#、.Net 4.0、OpenGL 2。 在 1280x1024 分辨率下最大化运行(不是全屏,而是占用所有可用的屏幕空间),我看到平均帧率为
我是一名优秀的程序员,十分优秀!