【发布时间】:2011-10-11 11:07:20
【问题描述】:
我正在使用 OpenTK 编写自己的引擎(基本上只是 C# 的 OpenGL 绑定,gl* 变为 GL.*),并且我将存储大量顶点缓冲区,每个缓冲区中有数千个顶点。因此我需要我自己的自定义顶点格式,因为带有浮点数的 Vec3 只会占用太多空间。 (我这里说的是数百万个顶点)
我想做的是用这个布局创建我自己的顶点格式:
Byte 0: Position X
Byte 1: Position Y
Byte 2: Position Z
Byte 3: Texture Coordinate X
Byte 4: Color R
Byte 5: Color G
Byte 6: Color B
Byte 7: Texture Coordinate Y
这是顶点的 C# 代码:
public struct SmallBlockVertex
{
public byte PositionX;
public byte PositionY;
public byte PositionZ;
public byte TextureX;
public byte ColorR;
public byte ColorG;
public byte ColorB;
public byte TextureY;
}
一个字节作为每个轴的位置就足够了,因为我只需要 32^3 个唯一位置。
我已经编写了我自己的顶点着色器,它以两个 vec4 作为输入,为每组字节打开。 我的顶点着色器是这样的:
attribute vec4 pos_data;
attribute vec4 col_data;
uniform mat4 projection_mat;
uniform mat4 view_mat;
uniform mat4 world_mat;
void main()
{
vec4 position = pos_data * vec4(1.0, 1.0, 1.0, 0.0);
gl_Position = projection_mat * view_mat * world_mat * position;
}
为了尝试找出问题所在,我让顶点着色器尽可能简单。 编译着色器的代码是用立即模式绘图测试的,它可以工作,所以不能这样。
这是我的函数,它生成、设置并用数据填充顶点缓冲区,并建立指向属性的指针。
public void SetData<VertexType>(VertexType[] vertices, int vertexSize) where VertexType : struct
{
GL.GenVertexArrays(1, out ArrayID);
GL.BindVertexArray(ArrayID);
GL.GenBuffers(1, out ID);
GL.BindBuffer(BufferTarget.ArrayBuffer, ID);
GL.BufferData<VertexType>(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * vertexSize), vertices, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(Shaders.PositionDataID, 4, VertexAttribPointerType.UnsignedByte, false, 4, 0);
GL.VertexAttribPointer(Shaders.ColorDataID, 4, VertexAttribPointerType.UnsignedByte, false, 4, 4);
}
据我了解,这是正确的程序: 生成一个顶点数组对象并绑定它 生成一个顶点缓冲区并绑定它 用数据填充顶点缓冲区 设置属性指针
Shaders.*DataID 在编译和使用着色器后使用此代码设置。
PositionDataID = GL.GetAttribLocation(shaderProgram, "pos_data");
ColorDataID = GL.GetAttribLocation(shaderProgram, "col_data");
这是我的渲染函数:
void Render()
{
GL.UseProgram(Shaders.ChunkShaderProgram);
Matrix4 view = Constants.Engine_Physics.Player.ViewMatrix;
GL.UniformMatrix4(Shaders.ViewMatrixID, false, ref view);
//GL.Enable(EnableCap.DepthTest);
//GL.Enable(EnableCap.CullFace);
GL.EnableClientState(ArrayCap.VertexArray);
{
Matrix4 world = Matrix4.CreateTranslation(offset.Position);
GL.UniformMatrix4(Shaders.WorldMatrixID, false, ref world);
GL.BindVertexArray(ArrayID);
GL.BindBuffer(OpenTK.Graphics.OpenGL.BufferTarget.ArrayBuffer, ID);
GL.DrawArrays(OpenTK.Graphics.OpenGL.BeginMode.Quads, 0, Count / 4);
}
//GL.Disable(EnableCap.DepthTest);
//GL.Disable(EnableCap.CullFace);
GL.DisableClientState(ArrayCap.VertexArray);
GL.Flush();
}
谁能给我一些指点(不是双关语)?我是按错误的顺序执行此操作还是需要调用某些函数?
我在整个网络上进行了搜索,但找不到一个很好的教程或指南来解释如何实现自定义顶点。 如果您需要更多信息,请说出来。
【问题讨论】:
-
那么......当你运行它时会发生什么?无论如何,您应该做的是让系统处于可以工作的状态。如果您知道如何使其与浮点数据一起使用,请使用它。让它工作。一旦你正确渲染了东西,你就可以慢慢地优化数据。在每个步骤中,验证它是否有效。然后,当您到达断点时,您就知道出了什么问题。
-
如果我运行它,什么都不会发生。也就是说,它运行,但没有显示任何内容。我还将尝试仅使用浮点数据并从那里进行开发,但问题是我不确切知道我应该做什么才能让我自己的顶点格式正常工作(因此是标题)。这是一个重要的重要步骤(可能不能分成更小的步骤),很多事情都可能出错。所以它基本上只是反复试验。如果你知道怎么做,你能在我的代码中发现任何错误吗?
标签: c# opengl glsl opentk vertices