如何解决OpenGL 绘制的纹理非常奇怪OpenTK
但是 OpenGL 是这样绘制的:
这太奇怪了。这里发生了什么?角落的像素到了中心?
此代码加载纹理(我使用 ImageSharp 加载图像)
Handle = GL.GenTexture();
GL.BindTexture(TextureTarget.Texture2D,Handle);
Image<Rgba32> image = Image.Load(File.ReadAllBytes(path));
image.Mutate(x => x.Flip(FlipMode.Vertical));
List<byte> pixels = new List<byte>(4 * image.Width * image.Height);
for (int y = 0; y < image.Height; y++)
{
var row = image.GetPixelRowSpan(y);
for (int x = 0; x < image.Width; x++)
{
pixels.Add(row[x].R);
pixels.Add(row[x].G);
pixels.Add(row[x].B);
pixels.Add(row[x].A);
}
}
byte count = 1;
foreach (byte pixel in pixels)
{
Console.Write(pixel + " ");
if (count == 4)
{
Console.WriteLine();
count = 0;
}
count++;
}
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapS,(int)TextureWrapMode.Repeat);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureWrapT,TextureParameterName.TextureMinFilter,(int)TextureMinFilter.Nearest);
GL.TexParameter(TextureTarget.Texture2D,TextureParameterName.TextureMagFilter,(int)TextureMagFilter.Nearest);
GL.TexImage2D(TextureTarget.Texture2D,PixelInternalFormat.Rgba,image.Width,image.Height,PixelFormat.Rgba,PixelType.UnsignedByte,pixels.ToArray());
GL.GenerateMipmap(GenerateMipmapTarget.Texture2D);
这绘制(Shader
和 Texture
类正在工作,Texture
构造函数如上所示):
private int VAO;
private int VBO;
private int EBO;
private Shader shader;
private Texture texture;
float[] vertices =
{
//Position Texture coordinates
0.5f,0.5f,0.0f,1.0f,// top right
0.5f,-0.5f,// bottom right
-0.5f,// bottom left
-0.5f,1.0f // top left
};
uint[] indices =
{
0,1,3,2,};
VAO = GL.GenVertexArray();
GL.BindVertexArray(VAO);
shader.Use();
texture.Use();
EBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ElementArrayBuffer,EBO);
GL.BufferData(BufferTarget.ElementArrayBuffer,indices.Length * sizeof(uint),indices,BufferUsageHint.StaticDraw);
VBO = GL.GenBuffer();
GL.BindBuffer(BufferTarget.ArrayBuffer,VBO);
GL.BufferData(BufferTarget.ArrayBuffer,vertices.Length * sizeof(float),vertices,BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(0,VertexAttribPointerType.Float,false,5 * sizeof(float),0);
GL.EnabLevertexAttribArray(0);
GL.VertexAttribPointer(1,0);
GL.EnabLevertexAttribArray(1);
Matrix4 model = Matrix4.CreateRotationX(0);
shader.SetMatrix4("projection",Transform.projection);
shader.SetMatrix4("model",model);
shader.SetMatrix4("view",Camera.viewMatrix);
GL.DrawElements(PrimitiveType.Triangles,indices.Length,DrawElementsType.UnsignedInt,0);
片段着色器
#version 330
out vec4 outputColor;
in vec2 texCoord;
uniform sampler2D texture0;
void main()
{
outputColor = texture(texture0,texCoord);
}
顶点着色器
#version 330 core
layout(location = 0) in vec3 aPosition;
layout(location = 1) in vec2 aTexCoord;
out vec2 texCoord;
uniform mat4 model;
uniform mat4 projection;
uniform mat4 view;
void main(void)
{
gl_Position = vec4(aPosition,1.0) * model * view * projection;
texCoord = aTexCoord;
}
解决方法
您的纹理包裹不正确,因为您将顶点坐标的 x 和 y 分量用于纹理坐标。
当命名缓冲区对象绑定到 ArrayBuffer
,target 时,VertexAttribPointer
的最后一个参数被视为缓冲区对象数据存储中的字节偏移量。
您需要指定纹理坐标的偏移量。偏移量为3 * sizeof (float)
,因为属性缓冲区以顶点坐标的三个分量开始,然后是纹理坐标:
GL.VertexAttribPointer(1,2,VertexAttribPointerType.Float,false,5 * sizeof(float),0);
GL.VertexAttribPointer(1,3*sizeof(float));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。