This post has been de-listed (Author was flagged for spam)
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
2
[opentk] messed up triangles
Author Summary
minsin56 is
in
OpenTk
Post Body
im making a voxel game and currently indices aren't being added properly or the buffers or messed up im not sure what's wrong
its mostly fixed if i use primitvetype.quads and im not sure what else to do
https://i.imgur.com/AyJQpJ0.png
the voxel data class
public static readonly Vector3[] vertices =
{
new Vector3(-0.5f, 0.5f, -0.5f), new Vector3(-0.5f, 0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, -0.5f), new Vector3(-0.5f, -0.5f, 0.5f),
//right
new Vector3( 0.5f, 0.5f, 0.5f), new Vector3( 0.5f, 0.5f, -0.5f),
new Vector3( 0.5f, -0.5f, 0.5f), new Vector3( 0.5f, -0.5f, -0.5f),
//bottom
new Vector3(-0.5f, -0.5f, 0.5f), new Vector3( 0.5f, -0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, -0.5f), new Vector3( 0.5f, -0.5f, -0.5f),
//top
new Vector3(-0.5f, 0.5f, -0.5f), new Vector3( 0.5f, 0.5f, -0.5f),
new Vector3(-0.5f, 0.5f, 0.5f), new Vector3( 0.5f, 0.5f, 0.5f),
//back
new Vector3( 0.5f, 0.5f, -0.5f), new Vector3(-0.5f, 0.5f, -0.5f),
new Vector3( 0.5f, -0.5f, -0.5f), new Vector3(-0.5f, -0.5f, -0.5f),
//front
new Vector3(-0.5f, 0.5f, 0.5f), new Vector3( 0.5f, 0.5f, 0.5f),
new Vector3(-0.5f, -0.5f, 0.5f), new Vector3( 0.5f, -0.5f, 0.5f)
};
public static Vector2[] uvs =
{
new Vector2(0, 0), new Vector2(1, 0),
new Vector2(0, 1), new Vector2(1, 1),
};
public static int[] indices =
{
2, 1, 0, 2, 3, 1
};
public static void AddVoxelToMesh(int x,int y,int z,int id,Chunk chunk)
{
if(id == 0)
{
return;
}
foreach(VoxelFace face in VoxelFaceHelper.Faces)
{
var facenormal = face.GetFaceNorm();
if(chunk.GetVoxel(x facenormal.x,y facenormal.y,z facenormal.z) == 0)
AddFaceToMesh(x, y, z, face, chunk);
}
}
public static void AddFaceToMesh(int x,int y,int z,VoxelFace face,Chunk chunk)
{
int faceID = (int)face;
int vertoffset = chunk.mesh.Vertices.Length;
for(int i = 0; i < 4; i )
{
chunk.vertices.Add(vertices[faceID * 4 i] new Vector3(x,y,z));
chunk.UV.Add(uvs[i]);
}
for(int i = 0; i < indices.Length; i )
{
chunk.indices.Add((uint)indices[i] (uint)vertoffset);
}
}
}
mesh uploadtogl
public void UploadToGL()
{
VAO = GL.GenVertexArray();
VBO = GL.GenBuffer();
IBO = GL.GenBuffer();
CBO = GL.GenBuffer();
TBO = GL.GenBuffer();
GL.BindVertexArray(VAO);
GL.BindBuffer(BufferTarget.ArrayBuffer, VBO);
GL.NamedBufferStorage(VBO, Vector3.SizeInBytes * Vertices.Length, Vertices, BufferStorageFlags.MapWriteBit);
GL.VertexArrayAttribBinding(VAO, 0, 0);
GL.EnableVertexArrayAttrib(VAO, 0);
GL.VertexArrayAttribFormat(VAO, 0, 3, VertexAttribType.Float, false, 0);
GL.VertexArrayVertexBuffer(VAO, 0, VBO, IntPtr.Zero, Vector3.SizeInBytes);
GL.BindBuffer(BufferTarget.ElementArrayBuffer, IBO);
GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Length * sizeof(uint), indices, BufferUsageHint.StaticDraw);
GL.BindBuffer(BufferTarget.ArrayBuffer, CBO);
GL.BufferData(BufferTarget.ArrayBuffer, Colors.Length * Vector4.SizeInBytes, Colors, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 0, 0);
GL.BindBuffer(BufferTarget.ArrayBuffer, TBO);
GL.BufferData(BufferTarget.ArrayBuffer, UV.Length * Vector2.SizeInBytes, UV, BufferUsageHint.StaticDraw);
GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0);
}
mesh draw
GL.EnableVertexAttribArray(1);
GL.EnableVertexAttribArray(2);
GL.DrawArrays(PrimitiveType.Quads, 0, vertexCount);
GL.DisableVertexAttribArray(1);
GL.DisableVertexAttribArray(2);
Author
Account Strength
0%
Account Age
7 years
Verified Email
Yes
Verified Flair
No
Total Karma
34,548
Link Karma
9,973
Comment Karma
23,263
Profile updated: 7 months ago
Posts updated: 8 months ago
Subreddit
Post Details
Location
We try to extract some basic information from the post title. This is not
always successful or accurate, please use your best judgement and compare
these values to the post title and body for confirmation.
- Posted
- 5 years ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/programming...