私はOpenTKを使用して独自のエンジンを作成しています(基本的にはC#のOpenGLバインディングのみで、gl *はGL。*になります)。それぞれに数千の頂点を持つ多数の頂点バッファーを格納します。したがって、floatを使用した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の一意の位置しか必要ないため、各軸の位置としてのバイトは十分です。
バイトのセットごとに、入力として2つの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();
}
誰かが私にいくつかのポインタを与えるほど親切にすることができますか(しゃれは意図されていません)?これを間違った順序で実行していますか、それとも呼び出す必要のある関数がいくつかありますか?
Web全体を検索しましたが、カスタム頂点の実装方法を説明する優れたチュートリアルやガイドが1つ見つかりません。さらに情報が必要な場合は、そのように言ってください。