C# 使用着色器绘制纹理

C# 使用着色器绘制纹理,c#,opengl,glsl,shader,opentk,C#,Opengl,Glsl,Shader,Opentk,我想在着色器中绘制纹理,但得到一个异常(请参见下文) 我有以下代码: int vertexArray; //Pointer to Buffers int vertexBuffer; int colorBuffer; int coordBuffer; int texUniform; //Pointer to Uniform int texture; //Pointer to Texture 初始化 画 片段着色器 #version 330 core in vec4 vColor; in v

我想在着色器中绘制纹理,但得到一个异常(请参见下文)

我有以下代码:

int vertexArray;

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

int texUniform; //Pointer to Uniform

int texture; //Pointer to Texture
初始化

片段着色器

#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不会返回任何错误

我的代码有什么问题?

不启用客户端状态顶点数组。 替换以下内容: 与: 目前,您正在告诉GL从
glVertexPointer(…)
GLCOLORPOITER(…)
glTexCoordPointer(…)
中生成顶点属性,但实际上您没有设置这些属性

您可能可以不启用客户机状态:
ArrayCap.VertexArray
,因为许多驱动程序将其别名为属性0,但其他驱动程序会导致灾难。然而,在删除
EnableClientState(…)
调用之前,您将继续崩溃


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

您还需要替换此行: 为此:
编译/链接程序时有错误吗?请参阅编译后和编译后linking@ratchetfreak当我注释GL.drawArray时,我得到“0{11}:错误C1035:不兼容类型的赋值”(当我将
vec4 texCoords[]
(片段和顶点)中的
vec4
更改为
vec2
时,没有着色器文件夹和程序信息日志错误)。我在问题中将它编辑为
vec2
。它现在可以工作:)我应该在
VertexAttribPointer
之前还是之后调用
EnableVertexAttribArray
?如果我不需要启用客户机状态,那么这个客户机状态的用途是什么?您根本不应该调用它。这些状态适用于传统固定函数顶点管道。
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);
#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.EnableClientState(ArrayCap.VertexArray);
...
GL.EnableClientState(ArrayCap.ColorArray);
...
GL.EnableClientState(ArrayCap.TextureCoordArray);
GL.EnableVertexAttribArray(0);
...
GL.EnableVertexAttribArray(1);
...
GL.EnableVertexAttribArray(2);
GL.TexCoordPointer(2, TexCoordPointerType.Float, Vector2.SizeInBytes, 0);
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0);