Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I'm not an Android programmer but I assume it uses OpenGL-ES in which these functions are deprecated (and by the way missing).</p> <p>Tutorials explaining the good solution are drawn amongst a bunch of others that show how to draw triangles with glVertex3f functions (because it gives easy and fast results but totally pointless). I find it tragic since NOBODY should use those things.</p> <p>glBegin/glEnd, glVertex3f, glTexcoords2f, and such functions are now deprecated for performance sake (they are "slow" because we have to limit the number of calls to the graphic library). I won't expand much on that since you can search for it if interested. Instead, make use of Vertex and Indices buffers. I'm sorry because I have no "perfect" link to recommend, but you should easily get what you need on google :)</p> <hr> <p>However, I dug up some come from an ancient C# project:</p> <ul> <li>Note: OpenTK binding change functions name but they remain very close to the OGL ones, for example glVertex3f becomes GL.Vertex3.</li> </ul> <h1>The Vertex definition</h1> <p>A simple struct to store your custom vertex's informations (position, normal (if needed), color...)</p> <pre><code>[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential, Pack = 1)] public struct Vertex { public Core.Math.Vector3 Position; public Core.Math.Vector3 Normal; public Core.Math.Vector2 UV; public uint Coloring; public Vertex(float x, float y, float z) { this.Position = new Core.Math.Vector3(x, y, z); this.Normal = new Core.Math.Vector3(0, 0, 0); this.UV = new Core.Math.Vector2(0, 0); System.Drawing.Color color = System.Drawing.Color.Gray; this.Coloring = (uint)color.A &lt;&lt; 24 | (uint)color.B &lt;&lt; 16 | (uint)color.G &lt;&lt; 8 | (uint)color.R; } } </code></pre> <h1>The Vertex Buffer class</h1> <p>It's a wrapper class around an OpenGL buffer object to handle our vertex format.</p> <pre><code>public class VertexBuffer { public uint Id; public int Stride; public int Count; public VertexBuffer(Graphics.Objects.Vertex[] vertices) { int size; // We create an OpenGL buffer object GL.GenBuffers(1, out this.Id); //note: out is like passing an object by reference in C# this.Stride = OpenTK.BlittableValueType.StrideOf(vertices); //size in bytes of the VertexType (Vector3 size*2 + Vector2 size + uint size) this.Count = vertices.Length; // Fill the buffer with our vertices data GL.BindBuffer(BufferTarget.ArrayBuffer, this.Id); GL.BufferData(BufferTarget.ArrayBuffer, (System.IntPtr)(vertices.Length * this.Stride), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * this.Stride != size) throw new System.ApplicationException("Vertex data not uploaded correctly"); } } </code></pre> <h1>The Indices Buffer class</h1> <p>Very similar to the vertex buffer, it stores vertex indices of each face of your model.</p> <pre><code>public class IndexBuffer { public uint Id; public int Count; public IndexBuffer(uint[] indices) { int size; this.Count = indices.Length; GL.GenBuffers(1, out this.Id); GL.BindBuffer(BufferTarget.ElementArrayBuffer, this.Id); GL.BufferData(BufferTarget.ElementArrayBuffer, (System.IntPtr)(indices.Length * sizeof(uint)), indices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (indices.Length * sizeof(uint) != size) throw new System.ApplicationException("Indices data not uploaded correctly"); } } </code></pre> <h1>Drawing buffers</h1> <p>Then, to render a triangle, you have to create one Vertex Buffer to store vertices' positions. One Indice buffer containing the indices of the vertices [0, 1, 2] (pay attention to the counter-clockwise rule, but it's the same with glVertex3f method) When done, just call this function with specified buffers. Note you can use multiple sets of indices whith only one vertex buffer to render only some faces each time.</p> <pre><code>void DrawBuffer(VertexBuffer vBuffer, IndexBuffer iBuffer) { // 1) Ensure that the VertexArray client state is enabled. GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); // 2) Bind the vertex and element (=indices) buffer handles. GL.BindBuffer(BufferTarget.ArrayBuffer, vBuffer.Id); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iBuffer.Id); // 3) Set up the data pointers (vertex, normal, color) according to your vertex format. GL.VertexPointer(3, VertexPointerType.Float, vBuffer.Stride, new System.IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, vBuffer.Stride, new System.IntPtr(Vector3.SizeInBytes)); GL.TexCoordPointer(2, TexCoordPointerType.Float, vBuffer.Stride, new System.IntPtr(Vector3.SizeInBytes * 2)); GL.ColorPointer(4, ColorPointerType.UnsignedByte, vBuffer.Stride, new System.IntPtr(Vector3.SizeInBytes * 3 + Vector2.SizeInBytes)); // 4) Call DrawElements. (Note: the last parameter is an offset into the element buffer and will usually be IntPtr.Zero). GL.DrawElements(BeginMode.Triangles, iBuffer.Count, DrawElementsType.UnsignedInt, System.IntPtr.Zero); //Disable client state GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.TextureCoordArray); } </code></pre> <p>I hope this can help ;)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload