【问题标题】:Draw texture with shader使用着色器绘制纹理
【发布时间】:2014-05-13 00:09:46
【问题描述】:

我想在我的着色器中绘制纹理,但出现异常(见下文)。

我有以下代码:

int vertexArray;

//Pointer to Buffers
int vertexBuffer;
int colorBuffer;
int coordBuffer;

int texUniform; //Pointer to Uniform

int texture; //Pointer to Texture

初始化

GL.Enable(EnableCap.Texture2D);
texture = LoadPNG("Resources\\Test.png");
//...
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (float)All.Nearest);
GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (float)All.Nearest);
GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest);

vertexArray = GL.GenVertexArray();
GL.BindVertexArray(vertexArray);

float[] TexCoords = new float[] {
            0.0f, 0.0f,
            1.0f, 0.0f,
            0.0f, 1.0f,
}; //(Array.Length = 2*3)

//Arrays for Vertex (3*3) and Color (4*3)
//GenBuffer, BindBuffer and BufferData for Color and Vertex

coordBuffer = GL.GenBuffer();
GL.BindBuffer(BufferTarget.TextureBuffer, coordBuffer);
GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(sizeof(float) * TexCoords.Length), TexCoords, BufferUsageHint.StaticDraw);

//Load shader

texUniform = GL.GetUniformLocation(shaderProgram, "tex");
GL.Uniform1(texUniform, 0);
GL.ActiveTexture(TextureUnit.Texture0);

GL.UseProgram(shaderProgram);
GL.BindTexture(TextureTarget.Texture2D, texture);

GL.EnableVertexAttribArray(0);
GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBuffer);
GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableClientState(ArrayCap.VertexArray);

GL.EnableVertexAttribArray(1);
GL.BindBuffer(BufferTarget.ArrayBuffer, colorBuffer);
GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 0, 0);
GL.EnableClientState(ArrayCap.ColorArray);

GL.BindBuffer(BufferTarget.TextureBuffer, coordBuffer);
GL.TexCoordPointer(2, TexCoordPointerType.Float, Vector2.SizeInBytes, 0);
GL.EnableClientState(ArrayCap.TextureCoordArray);

GL.DrawArrays(PrimitiveType.Triangles, 0, 6); //<------ Exception

GL.DisableVertexAttribArray(0);
GL.DisableVertexAttribArray(1);

我在GL.DrawArrays(...); 收到 System.AccessViolationException。我怀疑我没有正确加载缓冲区或以不正确的方式使用指针。异常是由我所做的更改引起的,以便将带有纹理坐标的纹理放入着色器,这意味着顶点和颜色缓冲区正在工作。

我不确定我做错了什么。我用着色器尝试了不同的东西,但似乎我用着色器做什么并不重要......

在我最后一次尝试中: 顶点着色器

#version 330 core
layout(location = 0) in vec3 position;
layout(location = 1) in vec4 color;
layout(location = 2) in vec2 texCoord;

out vec4 vColor;
out vec2 texCoords[];

void main(){
  gl_Position = vec4(position, 1.0);
  texCoords[0] = texCoord;
  vColor = color;
}

片段着色器

#version 330 core
in vec4 vColor;
in vec2 texCoords[];

uniform sampler2D tex;

out vec4 fColor;

void main(void)
{
  //fColor = vColor;
  fColor = texture2D(Texture0, texCoords[0].st);
}

当我评论 GL.DrawArrays(...) 并运行应用程序时,GetShaderInfoLog 和 GetProgramInfoLog 不会返回任何错误。

我的代码有什么问题?

【问题讨论】:

  • 在编译/链接程序时有什么错误吗?编译后查看shader log,链接后查看program log
  • @ratchetfreak 当我评论 GL.DrawArrays 时,我得到“0{11}:错误 C1035:不兼容类型的分配”(当我在 vec4 texCoords[](片段和顶点)中更改 vec4 时) vec2 没有 ShaderInfoLog 和 ProgramInfoLog 错误)。我在我的问题中将其编辑为vec2

标签: c# opengl glsl shader opentk


【解决方案1】:

不要启用客户端状态顶点数组。

替换以下内容:

GL.EnableClientState(ArrayCap.VertexArray);
...
GL.EnableClientState(ArrayCap.ColorArray);
...
GL.EnableClientState(ArrayCap.TextureCoordArray);

与:

GL.EnableVertexAttribArray(0);
...
GL.EnableVertexAttribArray(1);
...
GL.EnableVertexAttribArray(2);

目前,您告诉 GL 从glVertexPointer (...)glColorPointer (...)glTexCoordPointer (...) 获取顶点属性,您实际上都没有设置。

可能能够逃脱启用客户端状态:ArrayCap.VertexArray,因为许多驱动程序将其别名为属性 0,但其他驱动程序是灾难的根源.不过,除非您删除 EnableClientState (...) 调用,否则您将继续崩溃。


更新:

我在你的纹理坐标设置中遗漏了一些东西...

你还需要替​​换这一行:

GL.TexCoordPointer(2, TexCoordPointerType.Float, Vector2.SizeInBytes, 0);

有了这个:

GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0);

【讨论】:

  • 它现在可以工作了 :) 我应该在VertexAttribPointer 之前或之后打电话给EnableVertexAttribArray 还是没关系?如果我不需要启用客户端状态,这个客户端状态是用来做什么的?
  • 你根本不应该调用它。这些状态适用于 legacy 固定功能顶点管道。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-12-19
  • 1970-01-01
  • 2013-09-17
  • 1970-01-01
  • 2015-07-04
  • 1970-01-01
  • 2019-01-12
相关资源
最近更新 更多