public GlVertexArray(GlVertexBuffer <TVertex> vertices, GlIndexBuffer <TIndex> indices, Layout layout) { GlVbo = vertices; GlIbo = indices; Layout = layout; Handle = GL.GenVertexArray(); Bind(); Vbo.Bind(); Ibo.Bind(); int stride = 0; for (int i = 0; i < layout.Count; i++) { stride += Layout.GetSizeOf(layout[i].Type) * layout[i].Count; } int offset = 0; for (int i = 0; i < layout.Count; i++) { int location = layout.Shader.GetAttributeLocation(layout[i].AttributeName); GL.EnableVertexAttribArray(location); GL.VertexAttribPointer(location, layout[i].Count, GetAttribType(layout[i].Type), false, stride, offset); offset += Layout.GetSizeOf(layout[i].Type) * layout[i].Count; } }
private void LoadFromRandomNoise(int Size, bool isVoronoi) { DiamondSquare diamondSquare = new DiamondSquare(Size); diamondSquare.Execute(); Voronoi vornoi = new Voronoi(Size, 20); if (isVoronoi) { vornoi.Execute(); } Width = Size; Height = Size; heights = new float[Size, Size]; Vertices = new Vbo[Width * Height]; for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { if (isVoronoi) { heights[i, j] = (int)(((diamondSquare.Array[i, j] + 1) * 255) + vornoi.Array[i, j]) / 2; } else { heights[i, j] = (int)((diamondSquare.Array[i, j] + 1) * 255); } } } }
private void InitVBO(ref Common.Vbo handle, string name) { handle = new Vbo() { vboName = name }; }
Vbo LoadVBO(Vector3[] vertices, uint[] indices) { Vbo handle = new Vbo(); int size; GL.GenBuffers(1, out handle.VboID); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * Vector3.SizeInBytes != size) { throw new ApplicationException("Vertex array not uploaded correctly"); } //GL.BindBuffer(Version15.ArrayBuffer, 0); GL.GenBuffers(1, out handle.EboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(uint)), indices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (indices.Length * sizeof(int) != size) { throw new ApplicationException("Element array not uploaded correctly"); } //GL.BindBuffer(Version15.ElementArrayBuffer, 0); handle.NumElements = indices.Length; return(handle); }
/// <summary> /// Currently only called during construction, this method uploads the input mesh ( /// the RenderMesh instance is bound to) to a VBO. /// </summary> /// <param name="vboToFill"></param> private void Upload(out Vbo vboToFill) { vboToFill = new Vbo(); UploadVertices(out vboToFill.VertexBufferId); if (_mesh.HasNormals) { UploadNormals(out vboToFill.NormalBufferId); } if (_mesh.HasVertexColors(0)) { UploadColors(out vboToFill.ColorBufferId); } if (_mesh.HasTextureCoords(0)) { UploadTextureCoords(out vboToFill.TexCoordBufferId); } if (_mesh.HasTangentBasis) { UploadTangentsAndBitangents(out vboToFill.TangentBufferId, out vboToFill.BitangentBufferId); } UploadPrimitives(out vboToFill.ElementBufferId, out vboToFill.NumIndices, out vboToFill.Is32BitIndices); // TODO: upload bone weights }
public void SetUp() { var fakeVertex = new FakeVertex(); _quad = new Quad <FakeVertex>(fakeVertex, fakeVertex, fakeVertex, fakeVertex); _expectedVertices = new[] { _quad.FirstVertex, _quad.SecondVertex, _quad.ThirdVertex, _quad.FourthVertex, _quad.FirstVertex, _quad.SecondVertex, _quad.ThirdVertex, _quad.FourthVertex }; _expectedIndices = new uint[] { 0, 2, 3, 0, 1, 2, 4, 6, 7, 4, 5, 6 }; _fakeVao = new Vao <FakeVertex>(); _fakeVbo = new Vbo <FakeVertex>(3); _fakeEbo = new Ebo(); _vaoFactoryMock = new Mock <Factory <Vao <FakeVertex>, VaoArgs <FakeVertex> > >(); _vaoFactoryMock.Setup(m => m.Create(It.IsAny <VaoArgs <FakeVertex> >())).Returns(_fakeVao); _vboFactoryMock = new Mock <Factory <Vbo <FakeVertex>, VboArgs <FakeVertex> > >(); _vboFactoryMock.Setup(m => m.Create(It.IsAny <VboArgs <FakeVertex> >())).Returns(_fakeVbo); _eboFactoryMock = new Mock <Factory <Ebo, EboArgs> >(); _eboFactoryMock.Setup(m => m.Create(It.IsAny <EboArgs>())).Returns(_fakeEbo); _vboRendererMock = new Mock <VboRenderer <FakeVertex> >(); _quadRenderer = new QuadRenderer <FakeVertex>(new[] { _quad, _quad }, _vaoFactoryMock.Object, _vboFactoryMock.Object, _eboFactoryMock.Object, _vboRendererMock.Object); }
public Plane(bool invertAllFaces, Vbo p1, Vbo p2, Vbo p3, Vbo p4) { Vertices = new Vbo[4]; if (invertAllFaces) { Vertices[0] = p2; Vertices[1] = p1; Vertices[2] = p4; Vertices[3] = p3; } else { Vertices[0] = p1; Vertices[1] = p2; Vertices[2] = p3; Vertices[3] = p4; } Points = new Vector3[4]; Points[0] = p1.Position; Points[1] = p2.Position; Points[2] = p3.Position; Points[3] = p4.Position; Indices = new List <int>(); Indices.AddRange(new int[] { 0, 1, 2, 2, 3, 0 }); Height = p1.Position.Y - p2.Position.Y; Width = p3.Position.X - p2.Position.X; Depth = p2.Position.Z - p1.Position.Z; }
Vbo LoadVBO <TVertex>(TVertex[] vertices, short[] elements) where TVertex : struct { Vbo handle = new Vbo(); int size; // To create a VBO: // 1) Generate the buffer handles for the vertex and element buffers. // 2) Bind the vertex buffer handle and upload your vertex data. Check that the buffer was uploaded correctly. // 3) Bind the element buffer handle and upload your element data. Check that the buffer was uploaded correctly. GL.GenBuffers(1, out handle.VboID); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * BlittableValueType.StrideOf(vertices) != size) { throw new ApplicationException("Vertex data not uploaded correctly"); } GL.GenBuffers(1, out handle.EboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(elements.Length * sizeof(short)), elements, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (elements.Length * sizeof(short) != size) { throw new ApplicationException("Element data not uploaded correctly"); } handle.NumElements = elements.Length; return(handle); }
private void SetColor(ref Vbo handle) { Color c = Color.Bisque; handle.color = new Vector3( (float)c.R / 255f, (float)c.G / 255f, (float)c.B / 255f); }
public Plane(bool invertAllFaces, Vbo p1, Vbo p2, Vbo p3, Vbo p4, Image textureImage, TextureWrapMode textureWrapMode = TextureWrapMode.ClampToEdge) : this(invertAllFaces, p1, p2, p3, p4) { if (textureImage != null) { Texture1 = TextureFactory.Create(textureImage, textureWrapMode); } else { Texture1 = TextureFactory.Create(TextureWrapMode.ClampToEdge, 50, 50); } }
public unsafe override void Dispose() { Vbo.Dispose(); Ebo.Dispose(); Vao.Dispose(); shader.Dispose(); foreach (var item in textures) { item.Dispose(); } }
private void initModels() { vectorLineVbo = createVectorLineVbo(); vectorArrowVbo = createVectorArrowVbo(VectorArrowQuality, VectorArrowRadius, VectorArrowLength); pointVbo = createSphereVbo(PointQuality, PointQuality, false); sphereVbo = createSphereVbo(SphereSegments, SphereRings, true); planeVbo = createPlaneVbo(PlaneInfinity, PlaneLineDensity); lineVbo = createLineVbo(LineInfinity); circleVbo = createCircleVbo(CircleLines); }
private void rebuild() { if (_vbo != null) { _vbo.Dispose(); _vbo = null; } float v = Constants.Kernel.Get<RenderConfig>().FarClippingPlane / 2; Vertex[] vertexes = new Vertex[] { //_pos_z new Vertex(-v, -v, v, Color.White, _pos_z.TextureLocation.X, _pos_z.TextureLocation.Y + _pos_z.TextureLocation.Height), new Vertex(-v, v, v, Color.White, _pos_z.TextureLocation.X, _pos_z.TextureLocation.Y), new Vertex(v, v, v, Color.White, _pos_z.TextureLocation.X + _pos_z.TextureLocation.Width, _pos_z.TextureLocation.Y), new Vertex(v, -v, v, Color.White, _pos_z.TextureLocation.X + _pos_z.TextureLocation.Width, _pos_z.TextureLocation.Y + _pos_z.TextureLocation.Height), //_neg_z new Vertex(-v, -v, -v, Color.White, _neg_z.TextureLocation.X + _neg_z.TextureLocation.Width, _neg_z.TextureLocation.Y + _neg_z.TextureLocation.Height), new Vertex(-v, v, -v, Color.White, _neg_z.TextureLocation.X + _neg_z.TextureLocation.Width, _neg_z.TextureLocation.Y), new Vertex(v, v, -v, Color.White, _neg_z.TextureLocation.X, _neg_z.TextureLocation.Y), new Vertex(v, -v, -v, Color.White, _neg_z.TextureLocation.X, _neg_z.TextureLocation.Y + _neg_z.TextureLocation.Height), //_neg_x new Vertex(-v, -v, -v, Color.White, _neg_x.TextureLocation.X, _neg_x.TextureLocation.Y + _neg_x.TextureLocation.Height), new Vertex(-v, v, -v, Color.White, _neg_x.TextureLocation.X, _neg_x.TextureLocation.Y), new Vertex(-v, v, v, Color.White, _neg_x.TextureLocation.X + _neg_x.TextureLocation.Width, _neg_x.TextureLocation.Y), new Vertex(-v, -v, v, Color.White, _neg_x.TextureLocation.X + _neg_x.TextureLocation.Width, _neg_x.TextureLocation.Y + _neg_x.TextureLocation.Height), //_pos_x new Vertex(v, -v, -v, Color.White, _pos_x.TextureLocation.X + _pos_x.TextureLocation.Width, _pos_x.TextureLocation.Y + _pos_x.TextureLocation.Height), new Vertex(v, v, -v, Color.White, _pos_x.TextureLocation.X + _pos_x.TextureLocation.Width, _pos_x.TextureLocation.Y), new Vertex(v, v, v, Color.White, _pos_x.TextureLocation.X, _pos_x.TextureLocation.Y), new Vertex(v, -v, v, Color.White, _pos_x.TextureLocation.X, _pos_x.TextureLocation.Y + _pos_x.TextureLocation.Height), //_pos_y new Vertex(-v, v, v, Color.White, _pos_y.TextureLocation.X + _pos_y.TextureLocation.Width, _pos_y.TextureLocation.Y + _pos_y.TextureLocation.Height), new Vertex(-v, v, -v, Color.White, _pos_y.TextureLocation.X, _pos_y.TextureLocation.Y + _pos_y.TextureLocation.Height), new Vertex(v, v, -v, Color.White, _pos_y.TextureLocation.X, _pos_y.TextureLocation.Y), new Vertex(v, v, v, Color.White, _pos_y.TextureLocation.X + _pos_y.TextureLocation.Width, _pos_y.TextureLocation.Y), //_neg_y new Vertex(-v, -v, v, Color.White, _neg_y.TextureLocation.X + _neg_y.TextureLocation.Width, _neg_y.TextureLocation.Y + _neg_y.TextureLocation.Height), new Vertex(-v, -v, -v, Color.White, _neg_y.TextureLocation.X, _neg_y.TextureLocation.Y + _neg_y.TextureLocation.Height), new Vertex(v, -v, -v, Color.White, _neg_y.TextureLocation.X, _neg_y.TextureLocation.Y), new Vertex(v, -v, v, Color.White, _neg_y.TextureLocation.X + _neg_y.TextureLocation.Width, _neg_y.TextureLocation.Y) }; short[] indices = new short[] { 0,1,2,3, 7,6,5,4, 8,9,10,11, 15,14,13,12, 16,17,18,19, 23,22,21,20 }; _vbo = new Vbo<Vertex>(vertexes, indices); _vbo.PrimitiveMode = OpenTK.Graphics.OpenGL.BeginMode.Quads; }
private void drawModel(Vbo vbo) { GL.EnableClientState(ArrayCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VboId); GL.BindBuffer(BufferTarget.ElementArrayBuffer, (int)vbo.EboId); GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(new Vector3()), 0); GL.DrawElements(vbo.Mode, vbo.NumElements, DrawElementsType.UnsignedShort, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); }
public Renderer2D() { lastEntityOffset = 0; vao = new Vao(); vbo = new Vbo(BufferUsage.DynamicDraw, 100000); ibo = new Ibo(100000 * 6); // Set layouts for vbo vbo.PushLayout(new BufferLayout(2, VertexAttribType.Float, false)); // x, y vbo.PushLayout(new BufferLayout(4, VertexAttribType.Float, false)); // r, g, b, a vbo.PushLayout(new BufferLayout(2, VertexAttribType.Float, false)); // tx, ty vbo.PushLayout(new BufferLayout(1, VertexAttribType.Float, false)); // texture id }
public override void Dispose() { if (Name != "") { if (Material != null) { Material.Dispose(); } if (Vbo != null) { Vbo.Dispose(); } Log.WriteLine("Disposed: " + Name + " (mesh)", false); Name = ""; } }
public Terrain(Image heightmapImage, Image blendMap, params Image[] image) { Center = new Vector3(Width / 2, 0, Height / 2); Texture1 = TextureFactory.Create(image[0], TextureWrapMode.Repeat); Texture2 = TextureFactory.Create(image[1], TextureWrapMode.Repeat); Texture3 = TextureFactory.Create(image[2], TextureWrapMode.Repeat); Texture4 = TextureFactory.Create(blendMap, TextureWrapMode.Repeat); heightMap = new Bitmap(heightmapImage); Width = heightMap.Width; Height = heightMap.Height; heights = new float[Width, Height]; Vertices = new Vbo[Width * Height]; Indices = new List <int>(); }
private void GenerateVertices() { for (int i = 0; i < Width; i++) { for (int j = 0; j < Height; j++) { Vertices[i + j * Width] = new Vbo() { Position = new Vector3((float)-Width + i, (float)heights[i, j], (float)-Height + j), TexCoord = new Vector2((float)i / 10, (float)j / 10), TexCoord2 = new Vector2((float)i / Width, (float)j / Height) } } } ; }
public void Load() { Foam = TextureFactory.Create(Utils.Utils.GetImageResource <ITerrain>("Landscape.Terrains.foam.jpg"), TextureWrapMode.Repeat); LoadWaveNumbersModerate(); DataTexture.LoadData(SizedInternalFormat.Rgba16f, WaveNumbers); Texture3 = Foam; Texture4 = DataTexture; Texture5 = TextureFactory.Create(Utils.Utils.GetImageResource <ITerrain>("Landscape.Terrains.IKT4l.jpg"), TextureWrapMode.Repeat); for (int i = 0; i < Width; i += 1) { for (int j = 0; j < Height; j += 1) { Vertices[i + j * Width] = new Vbo { Position = new Vector3((float)i, (float)1, (float)j), TexCoord = new Vector2((float)i / Width, (float)j / Height), Normal = new Vector3(0, 1, 0), }; } } for (int i = 0; i < Width - 1; i++) { for (int j = 0; j < Height - 1; j++) { Indices.Add(j * Width + i); Indices.Add(j * Width + i + 1); Indices.Add((j + 1) * Width + i); Indices.Add((j + 1) * Width + i + 1); Indices.Add((j + 1) * Width + i); Indices.Add(j * Width + i + 1); } } indice = Indices.ToArray(); Refraction = FramBufferOBjectFactory.Create(Width, Height); Refraction.Load(); Reflection = FramBufferOBjectFactory.Create(Width, Height); Reflection.Load(); InitWaterShader(); }
/// <summary> /// Generate a VertexBuffer for each of Color, Normal, TextureCoordinate, Vertex, and Indices /// </summary> public static Vbo LoadVBO(Shape shape) { Vbo vbo = new Vbo(); if (shape.VertexData == null) { return(vbo); } if (shape.Indices == null) { return(vbo); } { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.VertexBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VertexBufferID); // Send data to buffer GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.VertexData.Length * sizeof(float)), shape.VertexData, BufferUsageHint.StaticDraw); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } // Element Array Buffer { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.ElementBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo.ElementBufferID); // Send data to buffer GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(shape.Indices.Length * sizeof(int)), shape.Indices, BufferUsageHint.StaticDraw); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } // Store the number of elements for the DrawElements call vbo.NumElements = shape.Indices.Length; return(vbo); }
public void RenderMesh() { if (Vbo == null) { return; } if (DoubleSided) { GL.Disable(EnableCap.CullFace); } if (VBO.FastRenderPass) { if (CastShadow) { Vbo.Render(); } } else { Material.SetMaterial(); if (WorldMatrix != null) { GLExt.MatrixMode(MatrixMode.Texture); GLExt.PushMatrix(); GLExt.MultMatrix(ref WorldMatrix); GLExt.MatrixMode(MatrixMode.Modelview); } Vbo.Render(); if (WorldMatrix != null) { GLExt.MatrixMode(MatrixMode.Texture); GLExt.PopMatrix(); GLExt.MatrixMode(MatrixMode.Modelview); } } if (DoubleSided) { GL.Enable(EnableCap.CullFace); } }
/// <summary> /// Registers the handles VBO of this mesh with OpenGL and initializes the data. /// </summary> private void Init() { // To create a VBO: // 1) Generate the buffer handles for the vertex and element buffers. // 2) Bind the vertex buffer handle and upload your vertex data. Check that the buffer was uploaded correctly. // 3) Bind the element buffer handle and upload your element data. Check that the buffer was uploaded correctly. this.handle = new Vbo(); int size; GL.GenBuffers(1, out handle.vertexId); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.vertexId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * BlittableValueType.StrideOf(vertices) != size) { throw new ApplicationException("Vertex data not uploaded correctly"); } if (this.colors != null) { GL.GenBuffers(1, out handle.colorId); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.colorId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(colors.Length * BlittableValueType.StrideOf(colors)), colors, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (colors.Length * BlittableValueType.StrideOf(colors) != size) { throw new ApplicationException("Color data not uploaded correctly"); } } GL.GenBuffers(1, out handle.faceId); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.faceId); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(triangleIndices.Length * sizeof(int)), triangleIndices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (triangleIndices.Length * sizeof(int) != size) { throw new ApplicationException("Element data not uploaded correctly"); } handle.numElements = triangleIndices.Length; }
public Water(int width, int height) { Width = width; Height = height; Center = new Vector3(Width / 2, Height / 2, 0); Vertices = new Vbo[Width * Height]; Indices = new List <int>(); DataTexture = TextureFactory.Create(); WaveNumbers = new float[24, 4]; stopwatch = new Stopwatch(); stopwatch.Start(); _randomAngle = new Random(); _randomWaveNumber = new Random(); ReflectionClipPlane = new float[] { 0, -1, 0, 0 }; RefractionClipPlane = new float[] { 0, 1, 0, 0 }; }
void Draw(Vbo handle) { // To draw a VBO: // 1) Ensure that the VertexArray client state is enabled. // 2) Bind the vertex and element buffer handles. // 3) Set up the data pointers (vertex, normal, color) according to your vertex format. // 4) Call DrawElements. (Note: the last parameter is an offset into the element buffer // and will usually be IntPtr.Zero). GL.EnableClientState(EnableCap.ColorArray); GL.EnableClientState(EnableCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.VertexPointer(3, VertexPointerType.Float, BlittableValueType.StrideOf(CubeVertices), new IntPtr(0)); GL.ColorPointer(4, ColorPointerType.UnsignedByte, BlittableValueType.StrideOf(CubeVertices), new IntPtr(12)); GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedShort, IntPtr.Zero); }
void Draw(Vbo handle) { // To draw a VBO: // 1) Ensure that the VertexArray client state is enabled. // 2) Bind the vertex and element buffer handles. // 3) Set up the data pointers (vertex, normal, color) according to your vertex format. // 4) Call DrawElements. (Note: the last parameter is an offset into the element buffer // and will usually be IntPtr.Zero). GL.EnableClientState(ArrayCap.TextureCoordArray); GL.EnableClientState(ArrayCap.VertexArray); //GL.EnableClientState(ArrayCap.NormalArray); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.TexCoordPointer(2, TexCoordPointerType.Float, 8 * sizeof(float), (IntPtr)(0)); //not using yet: //GL.NormalPointer(NormalPointerType.Float, 8 * sizeof(float), (IntPtr)(2 * sizeof(float))); GL.VertexPointer(3, VertexPointerType.Float, 8 * sizeof(float), (IntPtr)(5 * sizeof(float))); GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero); }
private Vbo createVbo(BeginMode mode, Vector3[] vertices, ushort[] elements = null) { Vbo result = new Vbo(); result.Mode = mode; result.VboId = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, result.VboId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); if (elements == null) { elements = new ushort[vertices.Length]; for (ushort i = 0; i < vertices.Length; i++) { elements[i] = i; } } result.NumElements = elements.Length; result.EboId = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, result.EboId); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(elements.Length * sizeof(short)), elements, BufferUsageHint.StaticDraw); return(result); }
void Draw(Vbo handle) { //GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); //GL.EnableClientState(EnableCap.TextureCoordArray); GL.EnableClientState(EnableCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); //GL.TexCoordPointer(2, TexCoordPointerType.Float, vector2_size, (IntPtr)vector2_size); GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero); GL.DrawElements(BeginMode.Triangles, handle.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero); //GL.DrawArrays(BeginMode.LineLoop, 0, vbo.element_count); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); GL.DisableClientState(EnableCap.VertexArray); //GL.DisableClientState(EnableCap.TextureCoordArray); //GL.PopClientAttrib(); }
private Vbo FillVertexAndElementsArray() { Vbo handle = new Vbo(); int NumVertPerRow = (int)(size.Width / RenderSteps); int NumVertPerCol = (int)(size.Height / RenderSteps); int NumVertices = (int)(NumVertPerRow * NumVertPerCol); vertices = new Vertex[NumVertices]; // 3 vertices per triangle, 2 triangles per vertex created (except last row and col) int NumTriangles = (NumVertPerRow - 1) * (NumVertPerCol - 1) * 2 * 3; handle.NumElements = NumTriangles; indices = new int[NumTriangles]; int i = 0; int j = 0; for (int bx = 0; bx < (size.Width - RenderSteps); bx += RenderSteps) { for (int bz = 0; bz < (size.Height - RenderSteps); bz += RenderSteps, i++) { // Position vertices[i].Position = new Vector3(bx, heightTable[bx, bz], bz); // Texture coordinates (fraction of picture, whole thing will be [0..1] float s = (float)(bx) / (float)size.Width; float t = (float)(bz) / (float)size.Height; vertices[i].TexCoord = new Vector2(s, t); // Normal (average of 4 faces' normals that share this vertex, 2 for sides and itself for corners) // TODO //Vector3.Cross( //vertices[i].Normal = new Vector3(bx, heightTable[bx, bz], bz); // Create 2 triangles for each vertex // Skip if it's last in col, or is the last col if ((i + 1) % NumVertPerCol == 0 || i >= (NumVertices - NumVertPerCol)) { continue; } indices[j++] = i; indices[j++] = i + NumVertPerRow; indices[j++] = i + NumVertPerRow + 1; indices[j++] = i; indices[j++] = i + NumVertPerRow + 1; indices[j++] = i; } } int _size; GL.GenBuffers(1, out handle.VboID); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out _size); if (vertices.Length * BlittableValueType.StrideOf(vertices) != _size) { throw new ApplicationException("Vertex data not uploaded correctly"); } GL.GenBuffers(1, out handle.EboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out _size); if (indices.Length * sizeof(int) != _size) { throw new ApplicationException("Element data not uploaded correctly"); } return(handle); }
private Vbo FillVertexAndElementsArray() { Vbo handle = new Vbo(); int NumVertPerRow = (int)(size.Width / RenderSteps); int NumVertPerCol = (int)(size.Height / RenderSteps); int NumVertices = (int)(NumVertPerRow * NumVertPerCol); vertices = new Vertex[NumVertices]; // 3 vertices per triangle, 2 triangles per vertex created (except last row and col) int NumTriangles = (NumVertPerRow - 1) * (NumVertPerCol - 1) * 2 * 3; handle.NumElements = NumTriangles; indices = new int[NumTriangles]; int i = 0; int j = 0; for (int bx = 0; bx < (size.Width - RenderSteps); bx += RenderSteps) { for (int bz = 0; bz < (size.Height - RenderSteps); bz += RenderSteps, i++) { // Position vertices[i].Position = new Vector3(bx, heightTable[bx, bz], bz); // Texture coordinates (fraction of picture, whole thing will be [0..1] float s = (float)(bx) / (float)size.Width; float t = (float)(bz) / (float)size.Height; vertices[i].TexCoord = new Vector2(s, t); // Normal (average of 4 faces' normals that share this vertex, 2 for sides and itself for corners) // TODO //Vector3.Cross( //vertices[i].Normal = new Vector3(bx, heightTable[bx, bz], bz); // Create 2 triangles for each vertex // Skip if it's last in col, or is the last col if ((i + 1) % NumVertPerCol == 0 || i >= (NumVertices - NumVertPerCol)) continue; indices[j++] = i; indices[j++] = i + NumVertPerRow; indices[j++] = i + NumVertPerRow + 1; indices[j++] = i; indices[j++] = i + NumVertPerRow + 1; indices[j++] = i; } } int _size; GL.GenBuffers(1, out handle.VboID); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out _size); if (vertices.Length * BlittableValueType.StrideOf(vertices) != _size) throw new ApplicationException("Vertex data not uploaded correctly"); GL.GenBuffers(1, out handle.EboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out _size); if (indices.Length * sizeof(int) != _size) throw new ApplicationException("Element data not uploaded correctly"); return handle; }
/// <summary> /// Currently only called during construction, this method uploads the input mesh ( /// the RenderMesh instance is bound to) to a VBO. /// </summary> /// <param name="vboToFill"></param> private void Upload(out Vbo vboToFill) { vboToFill = new Vbo(); vboToFill.VerticeCount = _mesh.VertexCount; FullVertex[] tempVertexBuffer = new FullVertex[vboToFill.VerticeCount]; FillBufferV3D(ref tempVertexBuffer, VertexVector.Position, _mesh.Vertices); if (_mesh.HasNormals) { Debug.Assert(_mesh.HasNormals); FillBufferV3D(ref tempVertexBuffer, VertexVector.Normal, _mesh.Normals); } if (_mesh.HasVertexColors(0)) { Debug.Assert(_mesh.HasVertexColors(0)); var colors = _mesh.VertexColorChannels[0]; FillBufferC4D(ref tempVertexBuffer, colors); } if (_mesh.HasTextureCoords(0)) { Debug.Assert(_mesh.HasTextureCoords(0)); var uvs = _mesh.TextureCoordinateChannels[0]; FillBufferV2D(ref tempVertexBuffer, VertexVector.TextureCoordinate, uvs); } if (_mesh.HasTangentBasis) { Debug.Assert(_mesh.HasTangentBasis); var tangents = _mesh.Tangents; FillBufferV3D(ref tempVertexBuffer, VertexVector.Tangent, tangents); var bitangents = _mesh.BiTangents; Debug.Assert(bitangents.Count == tangents.Count); FillBufferV3D(ref tempVertexBuffer, VertexVector.Bitangent, bitangents); } UploadPrimitives(out vboToFill.ElementBufferId, out vboToFill.NumIndices, out vboToFill.Is32BitIndices); // TODO: upload bone weights vboToFill.VertexArray = GL.GenVertexArray(); GL.BindVertexArray(vboToFill.VertexArray); GL.GenBuffers(1, out vboToFill.VertexBufferId); int byteCount = vboToFill.VerticeCount * FullVertex.Size; GL.BindBuffer(BufferTarget.ArrayBuffer, vboToFill.VertexBufferId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(byteCount), tempVertexBuffer, BufferUsageHint.StaticDraw); VerifyBufferSize(byteCount); GL.BindBuffer(BufferTarget.ArrayBuffer, vboToFill.VertexBufferId); GL.VertexArrayAttribBinding(vboToFill.VertexArray, 0, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 0); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 0, // attribute index, from the shader location = 0 : Vector4 _position; 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 0); // relative offsetm first item GL.VertexArrayAttribBinding(vboToFill.VertexArray, 1, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 1); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 1, // attribute index, from the shader location = 1 : Vector4 _normal 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 16); // relative offset after a vec4 GL.VertexArrayAttribBinding(vboToFill.VertexArray, 2, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 2); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 2, // attribute index, from the shader location = 2 : Color4D _color; 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 32); // relative offset after a vec4 GL.VertexArrayAttribBinding(vboToFill.VertexArray, 3, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 3); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 3, // attribute index, from the shader location = 3 : Vector4 _textureCoordinate; 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 48); // relative offset after a vec4 GL.VertexArrayAttribBinding(vboToFill.VertexArray, 4, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 4); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 4, // attribute index, from the shader location = 4 : Vector4 _tangent; 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 64); // relative offset after a vec4 GL.VertexArrayAttribBinding(vboToFill.VertexArray, 5, 0); GL.EnableVertexArrayAttrib(vboToFill.VertexArray, 5); GL.VertexArrayAttribFormat( vboToFill.VertexArray, 5, // attribute index, from the shader location = 4 : Vector4 _bitangent; 4, // size of attribute, vec4 VertexAttribType.Float, // contains floats false, // does not need to be normalized as it is already, floats ignore this flag anyway 80); // relative offset after a vec4 GL.VertexArrayVertexBuffer(vboToFill.VertexArray, 0, vboToFill.VertexBufferId, IntPtr.Zero, FullVertex.Size); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); }
public StaticMesh(Logging.Logger log) { vbo = new Vbo(ref log); }
private void rebuild() { IList<VertexPositionColorTexture> vertexes = new List<VertexPositionColorTexture>(); IList<short> indices = new List<short>(); ContentManager contentManager = Constants.Kernel.Get<ContentManager>(); for (byte x = 0; x < Constants.CHUNK_SIZE_X; x++) for (byte y = 0; y < Constants.CHUNK_SIZE_Y; y++) for (byte z = 0; z < Constants.CHUNK_SIZE_Z; z++) { IBlock block = Chunk[x, y, z]; if (block == null) continue; if (z == 0 || Chunk[x, y, (byte)(z - 1)] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Front, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Front)))); if (z == Constants.CHUNK_SIZE_Z - 1 || Chunk[x, y, (byte)(z + 1)] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Back, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Back)))); if (x == 0 || Chunk[(byte)(x - 1), y, z] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Left, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Left)))); if (x == Constants.CHUNK_SIZE_X - 1 || Chunk[(byte)(x + 1), y, z] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Right, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Right)))); if (y == 0 || Chunk[x, (byte)(y - 1), z] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Bottom, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Bottom)))); if (y == Constants.CHUNK_SIZE_Y - 1 || Chunk[x, (byte)(y + 1), z] == null) createCubeSide(ref vertexes, ref indices, (Vector3)Chunk.Location + new Vector3(x, y, z), BlockSides.Top, _textureAtlas.AddSubImage(contentManager.LoadBitmap(block.GetTextureForSide(BlockSides.Top)))); } if (_vbo != null) _vbo.Dispose(); _vbo = new Vbo<VertexPositionColorTexture>(vertexes.ToArray(), indices.ToArray()); }
private void deleteVbo(Vbo vbo) { GL.DeleteBuffer(vbo.VboId); GL.DeleteBuffer(vbo.EboId); }
/// <summary> /// Generate a VertexBuffer for each of Color, Normal, TextureCoordinate, Vertex, and Indices /// </summary> Vbo LoadVBO(Shape shape) { Vbo vbo = new Vbo(); if (shape.Vertices == null) return vbo; if (shape.Indices == null) return vbo; int bufferSize; // Color Array Buffer if (shape.Colors != null) { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.ColorBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.ColorBufferID); // Send data to buffer GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Colors.Length * sizeof(int)), shape.Colors, BufferUsageHint.StaticDraw); // Validate that the buffer is the correct size GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out bufferSize); if (shape.Colors.Length * sizeof(int) != bufferSize) throw new ApplicationException("Vertex array not uploaded correctly"); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } // Normal Array Buffer if (shape.Normals != null) { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.NormalBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.NormalBufferID); // Send data to buffer GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Normals.Length * Vector3.SizeInBytes), shape.Normals, BufferUsageHint.StaticDraw); // Validate that the buffer is the correct size GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out bufferSize); if (shape.Normals.Length * Vector3.SizeInBytes != bufferSize) throw new ApplicationException("Normal array not uploaded correctly"); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } // TexCoord Array Buffer if (shape.Texcoords != null) { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.TexCoordBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.TexCoordBufferID); // Send data to buffer GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Texcoords.Length * 8), shape.Texcoords, BufferUsageHint.StaticDraw); // Validate that the buffer is the correct size GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out bufferSize); if (shape.Texcoords.Length * 8 != bufferSize) throw new ApplicationException("TexCoord array not uploaded correctly"); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } // Vertex Array Buffer { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.VertexBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VertexBufferID); // Send data to buffer GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(shape.Vertices.Length * Vector3.SizeInBytes), shape.Vertices, BufferUsageHint.DynamicDraw); // Validate that the buffer is the correct size GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out bufferSize); if (shape.Vertices.Length * Vector3.SizeInBytes != bufferSize) throw new ApplicationException("Vertex array not uploaded correctly"); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } // Element Array Buffer { // Generate Array Buffer Id GL.GenBuffers(1, out vbo.ElementBufferID); // Bind current context to Array Buffer ID GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo.ElementBufferID); // Send data to buffer GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(shape.Indices.Length * sizeof(int)), shape.Indices, BufferUsageHint.StaticDraw); // Validate that the buffer is the correct size GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out bufferSize); if (shape.Indices.Length * sizeof(int) != bufferSize) throw new ApplicationException("Element array not uploaded correctly"); // Clear the buffer Binding GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } // Store the number of elements for the DrawElements call vbo.NumElements = shape.Indices.Length; return vbo; }
void Draw(Vbo vbo) { // Push current Array Buffer state so we can restore it later GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); if (vbo.VertexBufferID == 0) return; if (vbo.ElementBufferID == 0) return; if (GL.IsEnabled(EnableCap.Lighting)) { // Normal Array Buffer if (vbo.NormalBufferID != 0) { // Bind to the Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.NormalBufferID); // Set the Pointer to the current bound array describing how the data ia stored GL.NormalPointer(NormalPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero); // Enable the client state so it will use this array buffer pointer GL.EnableClientState(EnableCap.NormalArray); } } else { // Color Array Buffer (Colors not used when lighting is enabled) if (vbo.ColorBufferID != 0) { // Bind to the Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.ColorBufferID); // Set the Pointer to the current bound array describing how the data ia stored GL.ColorPointer(4, ColorPointerType.UnsignedByte, sizeof(int), IntPtr.Zero); // Enable the client state so it will use this array buffer pointer GL.EnableClientState(EnableCap.ColorArray); } } // Texture Array Buffer if (GL.IsEnabled(EnableCap.Texture2D)) { if (vbo.TexCoordBufferID != 0) { // Bind to the Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.TexCoordBufferID); // Set the Pointer to the current bound array describing how the data ia stored GL.TexCoordPointer(2, TexCoordPointerType.Float, 8, IntPtr.Zero); // Enable the client state so it will use this array buffer pointer GL.EnableClientState(EnableCap.TextureCoordArray); } } // Vertex Array Buffer { // Bind to the Array Buffer ID GL.BindBuffer(BufferTarget.ArrayBuffer, vbo.VertexBufferID); // Set the Pointer to the current bound array describing how the data ia stored GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero); // Enable the client state so it will use this array buffer pointer GL.EnableClientState(EnableCap.VertexArray); } // Element Array Buffer { // Bind to the Array Buffer ID GL.BindBuffer(BufferTarget.ElementArrayBuffer, vbo.ElementBufferID); // Draw the elements in the element array buffer // Draws up items in the Color, Vertex, TexCoordinate, and Normal Buffers using indices in the ElementArrayBuffer GL.DrawElements(BeginMode.Triangles, vbo.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero); // Could also call GL.DrawArrays which would ignore the ElementArrayBuffer and just use primitives // Of course we would have to reorder our data to be in the correct primitive order } // Restore the state GL.PopClientAttrib(); }
private void Draw(Vbo handle) { //GL.PushClientAttrib(ClientAttribMask.ClientVertexArrayBit); //GL.EnableClientState(EnableCap.TextureCoordArray); GL.EnableClientState(ArrayCap.VertexArray); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); //GL.TexCoordPointer(2, TexCoordPointerType.Float, vector2_size, (IntPtr)vector2_size); GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, IntPtr.Zero); GL.DrawElements(PrimitiveType.Triangles, handle.NumElements, DrawElementsType.UnsignedInt, IntPtr.Zero); //GL.DrawArrays(BeginMode.LineLoop, 0, vbo.element_count); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); GL.DisableClientState(ArrayCap.VertexArray); //GL.DisableClientState(EnableCap.TextureCoordArray); //GL.PopClientAttrib(); }
private Vbo LoadVBO(Vector3[] vertices, uint[] indices) { var handle = new Vbo(); int size; GL.GenBuffers(1, out handle.VboID); GL.BindBuffer(BufferTarget.ArrayBuffer, handle.VboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * Vector3.SizeInBytes != size) { throw new ApplicationException("Vertex array not uploaded correctly"); } //GL.BindBuffer(Version15.ArrayBuffer, 0); GL.GenBuffers(1, out handle.EboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, handle.EboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(uint)), indices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (indices.Length * sizeof(int) != size) { throw new ApplicationException("Element array not uploaded correctly"); } //GL.BindBuffer(Version15.ElementArrayBuffer, 0); handle.NumElements = indices.Length; return handle; }
public Shape(Logging.Logger log) { vbo = new Vbo(ref log); }
public void ReturnsVbo() { Vbo <FakeVertex> vbo = _factory.Create(new VboArgs <FakeVertex>(_vertices)); Assert.AreEqual(2, vbo.VboId); }
protected override void OnLoad(EventArgs e) { base.OnLoad(e); GL.ClearColor(0.1f, 0.1f, 0.5f, 0.0f); GL.Enable(EnableCap.DepthTest); // Vertex Buffers vbo = LoadVBO(shape); // Lighting GL.Enable(EnableCap.Light0); GL.Enable(EnableCap.Lighting); // Texture GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.GenTextures(1, out textureID); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear); Bitmap bitmap = new Bitmap("..\\..\\Data\\logo.jpg"); BitmapData data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); { GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, data.Scan0); } bitmap.UnlockBits(data); GL.Enable(EnableCap.Texture2D); }
private Vbo createVbo(PrimitiveType mode, Vector3[] vertices, ushort[] elements = null) { Vbo result = new Vbo(); result.Mode = mode; result.VboId = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, result.VboId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * BlittableValueType.StrideOf(vertices)), vertices, BufferUsageHint.StaticDraw); if (elements == null) { elements = new ushort[vertices.Length]; for (ushort i = 0; i < vertices.Length; i++) { elements[i] = i; } } result.NumElements = elements.Length; result.EboId = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, result.EboId); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(elements.Length * sizeof(short)), elements, BufferUsageHint.StaticDraw); return result; }