/// <summary> /// /// </summary> /// <param name="vbo"></param> /// <param name="nbo"></param> /// <param name="tcbo"></param> /// <param name="ibo"></param> /// <param name="cbo"></param> /// <param name="count"></param> public static void RenderBuffers(int vbo, int nbo, int tcbo, int ibo, int cbo, int count) { GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.EnableClientState(ArrayCap.ColorArray); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.VertexPointer(3, VertexPointerType.Float, Marshal.SizeOf(default(Vector3)), new IntPtr(0)); GL.BindBuffer(BufferTarget.ArrayBuffer, nbo); GL.NormalPointer(NormalPointerType.Float, Marshal.SizeOf(default(Vector3)), new IntPtr(0)); GL.BindBuffer(BufferTarget.ArrayBuffer, tcbo); GL.TexCoordPointer(2, TexCoordPointerType.Float, Marshal.SizeOf(default(Vector3)), new IntPtr(0)); GL.BindBuffer(BufferTarget.ArrayBuffer, cbo); GL.ColorPointer(4, ColorPointerType.Float, 0, new IntPtr(0)); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo); GL.DrawElements(PrimitiveType.Triangles, count, DrawElementsType.UnsignedShort, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.TextureCoordArray); GL.DisableClientState(ArrayCap.ColorArray); }
public VertexArray( VertexBuffer <TVertex> vertexBuffer, ShaderProgram program, params VertexAttribute[] attributes) { // create new vertex array object GL.GenVertexArrays(1, out this._handle); // bind the object so we can modify it this.Bind(); // bind the vertex buffer object vertexBuffer.Bind(); // set all attributes foreach (var attribute in attributes) { attribute.Set(program); } // unbind objects to reset state GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
protected override void OnRenderFrame(FrameEventArgs e) { Title = $"(Vsync: {VSync}) FPS: {1f / e.Time:0}"; var backColor = new Color4(0f, 0, 0, 0); GL.ClearColor(backColor); GL.Clear(ClearBufferMask.ColorBufferBit); GL.UseProgram(theProgram); GL.BindBuffer(BufferTarget.ArrayBuffer, positionBufferObject); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 4, VertexAttribPointerType.Float, false, 0, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 3); GL.DisableVertexAttribArray(0); GL.UseProgram(0); SwapBuffers(); }
private void CreateBuffers() { GL.GenVertexArrays(1, out vao); GL.BindVertexArray(vao); GL.GenBuffers(1, out vbo); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(m_VertexSize * MaxVerts), IntPtr.Zero, BufferUsageHint.StreamDraw); // Allocate // Vertex positions GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, m_VertexSize, 0); // Tex coords GL.EnableVertexAttribArray(1); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, m_VertexSize, 2 * sizeof(float)); // Colors GL.EnableVertexAttribArray(2); GL.VertexAttribPointer(2, 4, VertexAttribPointerType.Float, false, m_VertexSize, 2 * (sizeof(float) + sizeof(float))); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindVertexArray(0); }
protected override void OnLoad() { Gpu.Controller.Init(); GL.ClearColor(0f, 0f, 0f, 1.0f); VertexBufferObject = GL.GenBuffer(); VertexBufferObject = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Length * sizeof(float), vertices, BufferUsageHint.StaticDraw); ElementBufferObject = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject); GL.BufferData(BufferTarget.ElementArrayBuffer, _indices.Length * sizeof(uint), _indices, BufferUsageHint.StaticDraw); _openGlTexture = new OpenGlTexture(Gpu.FbWidth, Gpu.FbHeight, Gpu.GetFrameBufferAddr()); _openGlShader = new OpenGlShader("OpenGL/shader.vert", "OpenGL/shader.frag"); _openGlShader.Use(); VertexArrayObject = GL.GenVertexArray(); GL.BindVertexArray(VertexArrayObject); GL.BindBuffer(BufferTarget.ArrayBuffer, VertexBufferObject); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ElementBufferObject); var vertexLocation = _openGlShader.GetAttribLocation("aPosition"); GL.EnableVertexAttribArray(vertexLocation); GL.VertexAttribPointer(vertexLocation, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0); var texCoordLocation = _openGlShader.GetAttribLocation("aTexCoord"); GL.EnableVertexAttribArray(texCoordLocation); GL.VertexAttribPointer(texCoordLocation, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float)); base.OnLoad(); }
/// <summary> /// Generates the four corner vertices of the encapsulated image. /// </summary> /// <returns>The native OpenGL ID of the buffer.</returns> protected int GenerateVertices() { // Generate vertex positions uint halfWidth = GetResolution().X / 2; uint halfHeight = GetResolution().Y / 2; List <float> vertexPositions = new List <float> { -halfWidth, halfHeight, halfWidth, halfHeight, -halfWidth, -halfHeight, halfWidth, -halfHeight }; // Buffer the generated vertices in the GPU int bufferID; GL.GenBuffers(1, out bufferID); GL.BindBuffer(BufferTarget.ArrayBuffer, bufferID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertexPositions.Count * sizeof(float)), vertexPositions.ToArray(), BufferUsageHint.StaticDraw); return(bufferID); }
private void CreateVBOs() { GL.GenBuffers(2, this.buffers); this.positionVboHandle = this.buffers[0]; this.elementsHandle = this.buffers[1]; GL.BindBuffer(GL.GL_ARRAY_BUFFER, this.positionVboHandle); GL.BindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, this.elementsHandle); GL.GenVertexArrays(1, this.vertexArray); this.vaoHandle = this.vertexArray[0]; GL.BindVertexArray(this.vaoHandle); GL.BindBuffer(GL.GL_ARRAY_BUFFER, this.positionVboHandle); GL.EnableVertexAttribArray(this.attributePositon); GL.EnableVertexAttribArray(this.attributeUV); GL.EnableVertexAttribArray(this.attributeColor); GL.VertexAttribPointer(this.attributePositon, 2, GL.GL_FLOAT, false, Marshal.SizeOf <DrawVertex>(), Marshal.OffsetOf <DrawVertex>("pos")); GL.VertexAttribPointer(this.attributeUV, 2, GL.GL_FLOAT, false, Marshal.SizeOf <DrawVertex>(), Marshal.OffsetOf <DrawVertex>("uv")); GL.VertexAttribPointer(this.attributeColor, 4, GL.GL_FLOAT, true, Marshal.SizeOf <DrawVertex>(), Marshal.OffsetOf <DrawVertex>("color")); Utility.CheckGLESError(); }
public void GenQuad() { qva = GL.GenVertexArray(); GL.BindVertexArray(qva); float[] qd = new float[18]; qd[0] = -1.0f; qd[1] = -1.0f; qd[2] = 0.0f; qd[3] = 1.0f; qd[4] = -1.0f; qd[5] = 0.0f; qd[6] = -1.0f; qd[7] = 1.0f; qd[8] = 0.0f; qd[9] = -1.0f; qd[10] = 1.0f; qd[11] = 0.0f; qd[12] = 1.0f; qd[13] = -1.0f; qd[14] = 0.0f; qd[15] = 1.0f; qd[16] = 1.0f; qd[17] = 0.0f; qvb = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, qvb); GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(18 * 4), qd, BufferUsageHint.StaticDraw); // GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
public void Update() { int n = triangles.Length; int count = 2 * n; float[] newTriangles = new float[count]; for (int i = 0; i < n; i++) { Vector2 vertex = vertices[triangles[i]]; newTriangles[2 * i] = vertex.X; newTriangles[2 * i + 1] = vertex.Y; } GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, count * sizeof(float), newTriangles, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(attribPtrIndex, 2, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(attribPtrIndex); GL.BindVertexArray(0); }
private unsafe void PlatformGetData <T>(int byteOffset, Span <T> destination) where T : unmanaged { if (GL.IsES) { // Buffers are write-only on OpenGL ES 1.1 and 2.0. See the GL_OES_mapbuffer extension for more information. // http://www.khronos.org/registry/gles/extensions/OES/OES_mapbuffer.txt throw new PlatformNotSupportedException("Index buffers are write-only on OpenGL ES platforms"); } GL.BindBuffer(BufferTarget.ElementArrayBuffer, _glBuffer); GL.CheckError(); IntPtr mapPtr = GL.MapBuffer(BufferTarget.ElementArrayBuffer, BufferAccess.ReadOnly); int srcBytes = Capacity * ElementType.TypeSize(); var byteSrc = new ReadOnlySpan <byte>((byte *)mapPtr + byteOffset, srcBytes); var byteDst = MemoryMarshal.AsBytes(destination); byteSrc.Slice(0, byteDst.Length).CopyTo(byteDst); GL.UnmapBuffer(BufferTarget.ElementArrayBuffer); GL.CheckError(); }
private void LoadBuffer() { for (uint i = 0; i < SizeY; i++) { for (uint j = 0; j < SizeX; j++) { Cell cell = cells[i, j]; if (cell == null) { continue; } cell.y = i; cell.x = j; field[i * SizeX + j] = new CellPoint { x = j, y = i, color = cell.Color, textureLayer = cell.TextureLayer }; } } fixed(void *pointer = field) { GL.BindBuffer(GL.ARRAY_BUFFER, vbo); GL.BindTexture(GL.TEXTURE_2D_ARRAY, textureId); GL.TexImage3D(GL.TEXTURE_2D_ARRAY, 0, GL.RGBA8, 16, 16, CellTextureManager.Textures.Count, 0, GL.BGRA, GL.UNSIGNED_BYTE, IntPtr.Zero); for (int i = 0; i < CellTextureManager.Textures.Count; i++) { CellTexture texture = CellTextureManager.Textures[i]; GL.TexSubImage3D(GL.TEXTURE_2D_ARRAY, 0, 0, 0, i, texture.Width, texture.Height, 1, GL.BGRA, GL.UNSIGNED_BYTE, texture.DataPointer); } GL.GenerateMipmap(GL.TEXTURE_2D_ARRAY); GL.BindTexture(GL.TEXTURE_2D_ARRAY, 0); GL.BufferData(GL.ARRAY_BUFFER, sizeof(CellPoint) * field.Length, pointer, GL.DYNAMIC_DRAW); GL.BindBuffer(GL.ARRAY_BUFFER, 0); } }
private void CreateVAOs() { // GL3 allows us to store the vertex layout in a "vertex array object" (VAO). // This means we do not have to re-issue VertexAttribPointer calls // every time we try to use a different vertex layout - these calls are // stored in the VAO so we simply need to bind the correct VAO. GL.GenVertexArrays(1, out vaoHandle); GL.BindVertexArray(vaoHandle); GL.EnableVertexAttribArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, positionVboHandle); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); GL.EnableVertexAttribArray(1); GL.BindBuffer(BufferTarget.ArrayBuffer, normalVboHandle); GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, Vector3.SizeInBytes, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, eboHandle); GL.BindVertexArray(0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); }
/// <summary> /// データを変更しないバッファーを作成 /// </summary> /// <typeparam name="T">描画するデータの型</typeparam> /// <param name="viewport">描画対象</param> /// <param name="type">データの種類</param> /// <param name="objects">バッファーに格納するデータ</param> /// <param name="indices">インデックスの配列</param> public static Buffer CreateStatic <T>(Viewport viewport, BeginMode type, T[] objects, uint[] indices) where T : struct { // バッファーを作成 var buffer = new Buffer(viewport, indices.Length, System.Runtime.InteropServices.Marshal.SizeOf(default(T)), type); // 描画対象を有効化 buffer.target.MakeCurrent(); // VAOを有効化(バインド) GL.BindVertexArray(buffer.vertexArray); // VBOを有効化(バインド) GL.BindBuffer(BufferTarget.ArrayBuffer, buffer.vertexBuffer); // VBOを確保してデータを割り当て GL.BufferData <T>(BufferTarget.ArrayBuffer, new IntPtr(buffer.sizeInByte * objects.Length), objects, BufferUsageHint.StaticDraw); // EBOを有効化(バインド) GL.BindBuffer(BufferTarget.ElementArrayBuffer, buffer.elementBuffer); // EBOを確保してデータ割り当て GL.BufferData <uint>( BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(uint) * indices.Length), indices, BufferUsageHint.StaticDraw); // 作成したバッファーを返す return(buffer); }
/// <summary> /// 頂点属性のプログラム中の位置を設定する /// </summary> /// <param name="attribution">頂点属性</param> /// <param name="location">位置</param> public void SetLocation(VertexAttribution attribution, int location) { // 描画対象を有効化 this.target.MakeCurrent(); // VAOを有効化 GL.BindVertexArray(this.vertexArray); // VBOを有効化(バインド) GL.BindBuffer(BufferTarget.ArrayBuffer, this.vertexBuffer); // 属性値を有効化 GL.EnableVertexAttribArray(location); // パラメーターの位置を設定 GL.VertexAttribPointer( location, attribution.Size, attribution.Type, attribution.Normalized, this.sizeInByte, attribution.Offset); }
protected override void SetVAO() { GL.GenVertexArrays(1, out Buf.VaoHandle[0]); GL.BindVertexArray(Buf.VaoHandle[0]); GL.BindBuffer(BufferTarget.ArrayBuffer, Buf.VboID[0]); int vertexUVLocation = GL.GetAttribLocation(fProgramObj.ShaderProgramID(), "vertVertexUV"); GL.EnableVertexAttribArray(vertexUVLocation); GL.VertexAttribPointer(vertexUVLocation, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, Buf.VboID[1]); int instanceIdLocation = GL.GetAttribLocation(fProgramObj.ShaderProgramID(), "vertInstanceID"); GL.EnableVertexAttribArray(instanceIdLocation); GL.VertexAttribPointer(instanceIdLocation, 1, VertexAttribPointerType.Float, false, sizeof(float), 0); GL.VertexAttribDivisor(0, 0); GL.VertexAttribDivisor(1, 1); GL.BindVertexArray(0); }
public void Render(OpenTK.FrameEventArgs e) { GL.PushAttrib(AttribMask.EnableBit); GL.BindTexture(TextureTarget.Texture2D, texBuff); GL.PushMatrix(); GL.Translate(this.RenderMover(this)); GL.Enable(EnableCap.Texture2D); GL.PushClientAttrib(ClientAttribMask.ClientAllAttribBits); GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); GL.Color3(colorv); GL.BindBuffer(BufferTarget.ArrayBuffer, xntBuff); int stride = sizeof(float) << 0x03; GL.VertexPointer(0x03, VertexPointerType.Float, stride, 0x00); GL.NormalPointer(NormalPointerType.Float, stride, 0x03 * sizeof(float)); GL.TexCoordPointer(0x02, TexCoordPointerType.Float, stride, 0x06 * sizeof(float)); GL.DrawArrays(BeginMode.Triangles, 0x00, xntN); GL.PopMatrix(); GL.Color3(1.0f, 1.0f, 1.0f); GL.PopClientAttrib(); GL.PopAttrib(); }
/** -------------------------------------------- * @brief OBJロード * @param filename * @return int リソースハンドル * --------------------------------------------*/ public int loadOBJ(string filename) { int handle = _models_res.Count; int vbo = -1; int ibo = -1; var vertices = new List <Vertex>(); var indices = new List <uint>(); vertices.Add(new Vertex(new Vector3(0, 0, 0), new Vector3(0, 1, 0), new Vector2(0, 0), new Color4(1, 1, 1, 1))); vertices.Add(new Vertex(new Vector3(100, 0, 0), new Vector3(0, 1, 0), new Vector2(0, 0), new Color4(1, 1, 1, 1))); vertices.Add(new Vertex(new Vector3(100, 100, 0), new Vector3(0, 1, 0), new Vector2(0, 0), new Color4(1, 1, 1, 1))); indices.Add(0); indices.Add(1); indices.Add(2); // 頂点バッファ GL.GenBuffers(1, out vbo); // リソースバッファ生成 GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); // リソースハンドルをバッファとバインド(紐づけ) GL.BufferData <Vertex>(BufferTarget.ArrayBuffer, new IntPtr(vertices.Count * Vertex.Stride), vertices.ToArray(), BufferUsageHint.StaticDraw); // バッファセット // インデックスバッファ GL.GenBuffers(1, out ibo); GL.BufferData <uint>(BufferTarget.ElementArrayBuffer, new IntPtr(sizeof(uint) * indices.Count), indices.ToArray(), BufferUsageHint.StaticDraw); // 解除 GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); // resource handle add _models_res.Add(new Tuple <int, int, int>(vbo, ibo, indices.Count)); return(handle); }
public void EndRender() { GL.Disable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); GL.BindVertexArray(vao); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, vertices.Count * VertexTexture.SizeInBytes, vertices.ToArray(), BufferUsageHint.DynamicDraw); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, VertexTexture.SizeInBytes, 0); GL.EnableVertexAttribArray(0); GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, VertexTexture.SizeInBytes, Vector3.SizeInBytes); GL.EnableVertexAttribArray(1); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ebo); GL.BufferData(BufferTarget.ElementArrayBuffer, indices.Count * sizeof(ushort), indices.ToArray(), BufferUsageHint.DynamicDraw); font.Bind(); sdf.Use(); sdf.UploadMatrix("ortho", ref ortho); sdf.UploadMatrix("model", ref scale); sdf.UploadColor("color", Color4.White); GL.DrawElements(BeginMode.Triangles, indices.Count, DrawElementsType.UnsignedShort, 0); font.Unbind(); GL.BindVertexArray(0); GL.Disable(EnableCap.Blend); GL.Enable(EnableCap.DepthTest); vertices.Clear(); indices.Clear(); }
/// <summary> /// Uploads the buffer to the GPU if it has changed since last buffered. /// </summary> /// <param name="usageHint">The usage hint.</param> public void BufferData(BufferUsageHint usageHint = BufferUsageHint.DynamicDraw) { ValidateDispose(); if (m_dirty) { GL.BindBuffer(m_target, this); // If the allocated buffer on the GPU is large enough, don't reallocate int requiredSize = m_elementSize * m_count; if (m_capacity >= requiredSize) { GL.BufferSubData(m_target, (IntPtr)0, requiredSize, m_buffer); } else { GL.BufferData(m_target, requiredSize, m_buffer, usageHint); m_capacity = requiredSize; } m_dirty = false; GL.BindBuffer(m_target, 0); } }
internal void VBOGenerateTextureCoords1(Mesh mesh, Scene scene, int isKWCube = 0) { if (mesh.HasTextureCoords(0)) { float[] values = new float[mesh.TextureCoordinateChannels[0].Count * 2]; for (int i = 0, arrayIndex = 0; i < mesh.TextureCoordinateChannels[0].Count; i++, arrayIndex += 2) { if (isKWCube == 2) { //values[arrayIndex] = 1 - mesh.TextureCoordinateChannels[0][i].X; //values[arrayIndex + 1] = 1 - mesh.TextureCoordinateChannels[0][i].Y; values[arrayIndex] = mesh.TextureCoordinateChannels[0][i].X; values[arrayIndex + 1] = mesh.TextureCoordinateChannels[0][i].Y; } else if (isKWCube == 6) { values[arrayIndex] = mesh.TextureCoordinateChannels[0][i].X; values[arrayIndex + 1] = 1 - mesh.TextureCoordinateChannels[0][i].Y; } else { values[arrayIndex] = mesh.TextureCoordinateChannels[0][i].X; values[arrayIndex + 1] = mesh.TextureCoordinateChannels[0][i].Y; } } VBOTexture1 = GL.GenBuffer(); GL.BindBuffer(BufferTarget.ArrayBuffer, VBOTexture1); GL.BufferData(BufferTarget.ArrayBuffer, values.Length * 4, values, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Float, false, 0, 0); GL.EnableVertexAttribArray(2); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); } }
public static void UploadQuadMesh() { var mesh = new Mesh <BitmapTag>(); mesh.Verticies = new VertexFormat[] { new VertexFormat(new Vector3(-1, -1, 1), new Vector2(0, 1), Vector3.Zero), new VertexFormat(new Vector3(-1, 1, 1), new Vector2(0, 0), Vector3.Zero), new VertexFormat(new Vector3(1, 1, 1), new Vector2(1, 0), Vector3.Zero), new VertexFormat(new Vector3(1, -1, 1), new Vector2(1, 1), Vector3.Zero), }; mesh.Indicies = new int[] { 2, 1, 0, 3, 2, 0 }; mesh.ElementType = MeshElementType.TriangleList; quadMesh = mesh; var verticies = mesh.Verticies; var indicies = mesh.Indicies; uint vao, vbo, ibo; GL.GenVertexArrays(1, out vao); GL.BindVertexArray(vao); GL.GenBuffers(1, out vbo); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(verticies.Length * VertexFormat.Size), verticies, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out ibo); GL.BindBuffer(BufferTarget.ElementArrayBuffer, ibo); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indicies.Length * sizeof(uint)), indicies, BufferUsageHint.StaticDraw); SetupVertexFormatAttributes(); QuadMeshId = vao; }
/// <summary>Load resources here.</summary> /// <param name="e">Not used.</param> protected override void OnLoad(EventArgs e) { base.OnLoad(e); Version version = new Version(GL.GetString(StringName.Version).Substring(0, 3)); Version target = new Version(1, 5); if (version < target) { throw new NotSupportedException(String.Format( "OpenGL {0} is required (you only have {1}).", target, version)); } GL.ClearColor(.1f, 0f, .1f, 0f); GL.Enable(EnableCap.DepthTest); // Setup parameters for Points GL.PointSize(PointSize); GL.Enable(EnableCap.PointSmooth); GL.Hint(HintTarget.PointSmoothHint, HintMode.Nicest); // Setup VBO state GL.EnableClientState(EnableCap.ColorArray); GL.EnableClientState(EnableCap.VertexArray); GL.GenBuffers(1, out VBOHandle); // Since there's only 1 VBO in the app, might aswell setup here. GL.BindBuffer(BufferTarget.ArrayBuffer, VBOHandle); GL.ColorPointer(4, ColorPointerType.UnsignedByte, VertexC4ubV3f.SizeInBytes, (IntPtr)0); GL.VertexPointer(3, VertexPointerType.Float, VertexC4ubV3f.SizeInBytes, (IntPtr)(4 * sizeof(byte))); PointCount = 0; LineCount = 12; VBO = new VertexC4ubV3f[PointCount + 2 * LineCount]; }
public void Render(Shader shader) { if (!IsLoaded) { return; } GL.UseProgram(shader.Program); GL.BindBuffer(BufferTarget.ArrayBuffer, mVertexBufferObjectID); GL.EnableVertexAttribArray(shader.PositionLocation); GL.EnableVertexAttribArray(shader.ColorLocation); GL.VertexAttribPointer(shader.PositionLocation, 3, VertexAttribPointerType.Float, false, Stride, 0); GL.VertexAttribPointer(shader.ColorLocation, 4, VertexAttribPointerType.Float, false, Stride, 12); GL.BindBuffer(BufferTarget.ElementArrayBuffer, mElementBufferObjectID); GL.DrawElements(DrawMode, mIndexPosition, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); GL.DisableVertexAttribArray(2); GL.DisableVertexAttribArray(3); GL.UseProgram(0); }
void FlushQuadBuffer(bool check = true) { if (quadBufferPos < quadBufferLength && check) { return; } if (quadBufferPos == 0) { return; } GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferID); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(quadBufferPos * 2 * 8 * 4), quadVertexbuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Double, false, 16, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, colorBufferID); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(quadBufferPos * 4 * 4 * 4), quadColorbuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 16, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, attribBufferID); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(quadBufferPos * 2 * 8 * 4), quadAttribbuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(2, 2, VertexAttribPointerType.Double, false, 16, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, indexBufferId); GL.IndexPointer(IndexPointerType.Int, 1, 0); GL.DrawElements(PrimitiveType.Triangles, quadBufferPos * 6, DrawElementsType.UnsignedInt, IntPtr.Zero); quadBufferPos = 0; }
public void Draw(IndexBuffer ibo, PrimitiveType primitiveType) { ibo.Draw(); bool bWireframe = (SceneManager.Current.RenderMode == SceneManager.RenderMeshMode.Wireframe); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.EnableClientState(ArrayCap.VertexArray); GL.EnableClientState(ArrayCap.NormalArray); GL.EnableClientState(ArrayCap.TextureCoordArray); if (!bWireframe) { GL.EnableClientState(ArrayCap.ColorArray); } GL.VertexPointer(3, VertexPointerType.Float, Vertex.Stride, new IntPtr(0)); GL.NormalPointer(NormalPointerType.Float, Vertex.Stride, new IntPtr(Vector3.SizeInBytes)); GL.TexCoordPointer(2, TexCoordPointerType.Float, Vertex.Stride, new IntPtr(2 * Vector3.SizeInBytes)); if (!bWireframe) { GL.ColorPointer(4, ColorPointerType.Float, Vertex.Stride, new IntPtr((2 * Vector3.SizeInBytes) + Vector4.SizeInBytes)); } GL.DrawElements(primitiveType, ibo.Length, DrawElementsType.UnsignedInt, IntPtr.Zero); GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.TextureCoordArray); if (!bWireframe) { GL.DisableClientState(ArrayCap.ColorArray); } GL.BindBuffer(BufferTarget.ArrayBuffer, 0); }
void FlushNoteBuffer(bool check = true) { if (noteBuffPos < noteBuffLen && check) { return; } if (noteBuffPos == 0) { return; } GL.BindBuffer(BufferTarget.ArrayBuffer, noteVert); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(noteBuffPos * 3 * 8 * 8), noteVertBuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Double, false, 24, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, noteCol); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(noteBuffPos * 4 * 8 * 4), noteColBuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(1, 4, VertexAttribPointerType.Float, false, 16, 0); GL.BindBuffer(BufferTarget.ArrayBuffer, noteShade); GL.BufferData( BufferTarget.ArrayBuffer, (IntPtr)(noteBuffPos * 1 * 8 * 4), noteShadeBuff, BufferUsageHint.StaticDraw); GL.VertexAttribPointer(2, 1, VertexAttribPointerType.Float, false, 4, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, noteIndx); GL.IndexPointer(IndexPointerType.Int, 1, 0); GL.DrawElements(PrimitiveType.Triangles, noteBuffPos * 12 * 3, DrawElementsType.UnsignedInt, IntPtr.Zero); noteBuffPos = 0; }
/// <summary> /// Create and load our VBO. This will dispose previous VBO handles and data, if it existed. /// </summary> public void Create <TVertex>(TVertex[] vertices, short[] elements) where TVertex : struct { Dispose(); m_handle = new VboHandle(); 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. m_handle.m_vertexStride = BlittableValueType.StrideOf(vertices); GL.GenBuffers(1, out m_handle.m_vboId); GL.BindBuffer(BufferTarget.ArrayBuffer, m_handle.m_vboId); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * m_handle.m_vertexStride), vertices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (vertices.Length * m_handle.m_vertexStride != size) { throw new ApplicationException("Vertex data not uploaded correctly"); } GL.GenBuffers(1, out m_handle.m_eboId); GL.BindBuffer(BufferTarget.ElementArrayBuffer, m_handle.m_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"); } m_handle.m_numElements = elements.Length; m_handle.m_colorVertex = typeof(TVertex) == typeof(VertexPositionColor); }
private void SetIndexBuffer(IndexBuffer indexBuffer) { _indexBuffer = indexBuffer; GL.BindBuffer(All.ElementArrayBuffer, indexBuffer._bufferStore); }
private void ApplyAttribs(Shader shader, int baseVertex) { Debug.Assert(_bufferBindingInfos != null); Debug.Assert(_newEnabledVertexAttributes != null); int programHash = ShaderProgramHash; bool bindingsChanged = false; int vertexBufferCount = _vertexBuffers.Count; for (int slot = 0; slot < vertexBufferCount; slot++) { var vertexBufferBinding = _vertexBuffers.Get(slot); var vertexDeclaration = vertexBufferBinding.VertexBuffer.VertexDeclaration; var attrInfo = vertexDeclaration.GetAttributeInfo(shader, programHash); int vertexStride = vertexDeclaration.VertexStride; var offset = (IntPtr)(vertexDeclaration.VertexStride * (baseVertex + vertexBufferBinding.VertexOffset)); if (!_attribsDirty) { var info = _bufferBindingInfos[slot]; if (info.VertexOffset == offset && info.InstanceFrequency == vertexBufferBinding.InstanceFrequency && info.Vbo == vertexBufferBinding.VertexBuffer._glBuffer && ReferenceEquals(info.AttributeInfo, attrInfo)) { continue; } } bindingsChanged = true; GL.BindBuffer(BufferTarget.ArrayBuffer, vertexBufferBinding.VertexBuffer._glBuffer); GL.CheckError(); // If InstanceFrequency of the buffer is not zero // and instancing is not supported, throw an exception. if (vertexBufferBinding.InstanceFrequency > 0) { AssertSupportsInstancing(); } foreach (var element in attrInfo.Elements.Span) { GL.VertexAttribPointer( element.AttributeLocation, element.NumberOfElements, element.VertexAttribPointerType, element.Normalized, vertexStride, offset + element.Offset); // only set the divisor if instancing is supported if (Capabilities.SupportsInstancing) { GL.VertexAttribDivisor(element.AttributeLocation, vertexBufferBinding.InstanceFrequency); } GL.CheckError(); } _bufferBindingInfos[slot].VertexOffset = offset; _bufferBindingInfos[slot].AttributeInfo = attrInfo; _bufferBindingInfos[slot].InstanceFrequency = vertexBufferBinding.InstanceFrequency; _bufferBindingInfos[slot].Vbo = vertexBufferBinding.VertexBuffer._glBuffer; } _attribsDirty = false; if (bindingsChanged) { for (int i = 0; i < _newEnabledVertexAttributes.Length; i++) { _newEnabledVertexAttributes[i] = false; } for (int slot = 0; slot < vertexBufferCount; slot++) { foreach (var element in _bufferBindingInfos[slot].AttributeInfo.Elements.Span) { _newEnabledVertexAttributes[element.AttributeLocation] = true; } } } SetVertexAttributeArray(_newEnabledVertexAttributes); }
public void SetVertexBuffer(VertexBuffer vertexBuffer) { _vertexBuffer = vertexBuffer; GL.BindBuffer(All.ArrayBuffer, vertexBuffer._bufferStore); }
void IMustInit.Init(GL gl) { if(B == 0) { fixed(uint* pB = &B) gl.GenBuffers(1,pB); if(B == 0) throw new Exception("GLObject Gen fails"); } gl.BindBuffer((uint)BType,B); switch(T) { case DataType.SByte : fixed(sbyte* pD = Data as sbyte[]) gl.BufferData((uint)BType,sizeof(sbyte) * Data.Length,pD,(uint)U); break; case DataType.Byte: fixed(byte* pD = Data as byte[]) gl.BufferData((uint)BType,sizeof(byte) * Data.Length,pD,(uint)U); break; case DataType.Short: fixed(short* pD = Data as short[]) gl.BufferData((uint)BType,sizeof(short) * Data.Length,pD,(uint)U); break; case DataType.UShort: fixed(ushort* pD = Data as ushort[]) gl.BufferData((uint)BType,sizeof(ushort) * Data.Length,pD,(uint)U); break; case DataType.Int: fixed(int* pD = Data as int[]) gl.BufferData((uint)BType,sizeof(int) * Data.Length,pD,(uint)U); break; case DataType.UInt: fixed(uint* pD = Data as uint[]) gl.BufferData((uint)BType,sizeof(uint) * Data.Length,pD,(uint)U); break; case DataType.Float: fixed(float* pD = Data as float[]) gl.BufferData((uint)BType,sizeof(float) * Data.Length,pD,(uint)U); break; case DataType.Double: fixed(double* pD = Data as double[]) gl.BufferData((uint)BType,sizeof(double) * Data.Length,pD,(uint)U); break; } // Data = null; }