public void Bind(OpenGL gl) { // Bind the vertex, normal and index buffers. if (_transformationsBufferId != null) { var transStride = 16; //Bind gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value); gl.VertexAttribPointer(VertexAttributes.Position, transStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(VertexAttributes.Position); } if (_colorsBufferId != null) { var colStride = 3; //Bind gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value); gl.VertexAttribPointer(VertexAttributes.Normal, colStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(VertexAttributes.Normal); } if (_indicesBufferId != null) { gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, _indicesBufferId.Value); } }
/// <summary> /// Calls VertexBuffer.Bind(gl), IndexBuffer.Bind(gl) and Material.Bind(gl). /// </summary> /// <param name="gl">The OpenGL</param> public void Bind(OpenGL gl) { //if (gl == null) //{ // throw new ArgumentNullException("OpenGL parameter cannot be null. Call 'GenerateGeometry(...)' before attempting to bind."); //} // Bind the vertex, normal and index buffers. if (VertexBuffer != null) { //Bind //VertexBuffer.BindBuffer(gl); // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VertexBuffer.BufferId.Value); gl.VertexAttribPointer(VertexAttributes.Position, BufferStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(VertexAttributes.Position); } if (NormalBuffer != null) { //Bind //NormalBuffer.BindBuffer(gl); // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, NormalBuffer.BufferId.Value); gl.VertexAttribPointer(VertexAttributes.Normal, BufferStride, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(VertexAttributes.Normal); } if (IndexBuffer != null) { //IndexBuffer.BindBuffer(gl); // gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, IndexBuffer.BufferId.Value); } }
//private void InitVertexes(OpenGL gl, float [] vertexes, float[] colorArray) //{ // uint ATTRIB_INDEX_POSITION = 0; // uint ATTRIB_INDEX_COLOUR = 1; // uint[] vao = new uint[1]; // gl.GenVertexArrays(vao.Length, vao); // gl.BindVertexArray(vao[0]); // uint[] vboVertex = new uint[1]; // gl.GenBuffers(vboVertex.Length, vboVertex); // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]); // gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes, OpenGL.GL_STATIC_DRAW); // gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); // gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION); // uint[] vboColor = new uint[1]; // gl.GenBuffers(vboColor.Length, vboColor); // gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]); // gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, OpenGL.GL_DYNAMIC_DRAW); // gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); // gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR); // gl.BindVertexArray(0); // gl.InvalidateBufferData(vboVertex[0]); // gl.InvalidateBufferData(vboColor[0]); // gl.DeleteBuffers(1, vboVertex); // gl.DeleteBuffers(1, vboColor); // gl.DeleteVertexArrays(1, vao); //} private void InitVertexes(OpenGL gl, UnmanagedArray <Vertex> vertexes, UnmanagedArray <ColorF> colorArray) { uint ATTRIB_INDEX_POSITION = 0; uint ATTRIB_INDEX_COLOUR = 1; uint[] vao = new uint[1]; gl.GenVertexArrays(vao.Length, vao); gl.BindVertexArray(vao[0]); uint[] vboVertex = new uint[1]; gl.GenBuffers(vboVertex.Length, vboVertex); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes.ByteLength, vertexes.Header, OpenGL.GL_STATIC_DRAW); gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION); uint[] vboColor = new uint[1]; gl.GenBuffers(vboColor.Length, vboColor); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray.ByteLength, colorArray.Header, OpenGL.GL_DYNAMIC_DRAW); gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR); gl.BindVertexArray(0); //gl.InvalidateBufferData(vboVertex[0]); //gl.InvalidateBufferData(vboColor[0]); gl.DeleteBuffers(1, vboVertex); gl.DeleteBuffers(1, vboColor); gl.DeleteVertexArrays(1, vao); }
/// <summary> /// Function to draw the model /// </summary> private void drawModel(OpenGL gl) { if (l_vboId != null) { gl.EnableClientState(OpenGL.GL_VERTEX_ARRAY); gl.EnableClientState(OpenGL.GL_COLOR_ARRAY); // itering over each list of points for (int k = 0; k < l_vboId.Count; k++) { gl.PushMatrix(); //transformations gl.Scale(1.0f / f_scale, 1.0f / f_scale, 1.0f / f_scale); gl.Translate(-v_center.X, -v_center.Y, -v_center.Z); //vertexes gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[k][0]); gl.VertexPointer(3, OpenGL.GL_FLOAT, 0, BUFFER_OFFSET_ZERO); //color gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[k][1]); gl.ColorPointer(3, OpenGL.GL_FLOAT, 0, BUFFER_OFFSET_ZERO); //draw l_sizes[k] points gl.DrawArrays(OpenGL.GL_POINTS, 0, l_sizes[k]); gl.PopMatrix(); } gl.DisableClientState(OpenGL.GL_VERTEX_ARRAY); gl.DisableClientState(OpenGL.GL_COLOR_ARRAY); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); } }
/// <summary> /// Creation of vertex and color buffer from a List<Vertex>. This function reset all values once a new file is loaded /// </summary> private void createInitialBuffers() { // Get the OpenGL object. OpenGL gl = openGLControl.OpenGL; //create if (l_sizes == null) { l_sizes = new List <int>(); //create list to store points of each buffer's list } l_sizes.Add(l_vertex.Count); uint [] ids = new uint[2]; l_vboId.Add(ids); //create buffers gl.GenBuffers(2, l_vboId[0]); //vertex buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[0][0]); IntPtr ptr1 = GCHandle.Alloc(l_vertex.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject(); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * l_vertex.Count * 3, ptr1, OpenGL.GL_STATIC_DRAW); //color buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[0][1]); IntPtr ptr2 = GCHandle.Alloc(l_color.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject(); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * l_color.Count * 3, ptr2, OpenGL.GL_DYNAMIC_DRAW); //unbind buffers gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); }
private void CreateVertexArrayObject(OpenGL gl, RenderMode renderMode) { if (this.positionBuffer == null || this.colorBuffer == null) { return; } this.vertexArrayObject = new uint[1]; gl.GenVertexArrays(1, this.vertexArrayObject); gl.BindVertexArray(this.vertexArrayObject[0]); // prepare positions { int location = shaderProgram.GetAttributeLocation(gl, in_Position); ATTRIB_INDEX_POSITION = (uint)location; gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, positionBuffer[0]); gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION); } // prepare colors { int location = shaderProgram.GetAttributeLocation(gl, in_uv); ATTRIB_INDEX_UV = (uint)location; gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, colorBuffer[0]); gl.VertexAttribPointer(ATTRIB_INDEX_UV, 1, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_UV); } gl.BindVertexArray(0); }
private void InitVertexes(OpenGL gl, UnmanagedArray <Vertex> vertexes, UnmanagedArray <ColorF> colorArray, UnmanagedArray <float> visibles) { uint[] vao = new uint[1]; gl.GenVertexArrays(vao.Length, vao); gl.BindVertexArray(vao[0]); this.vertexArrayObject = vao[0]; uint[] vboVertex = new uint[1]; gl.GenBuffers(vboVertex.Length, vboVertex); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVertex[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertexes.ByteLength, vertexes.Header, OpenGL.GL_STATIC_DRAW); gl.VertexAttribPointer(ATTRIB_INDEX_POSITION, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_POSITION); this.vertexsBufferObject = vboVertex[0]; uint[] vboColor = new uint[1]; gl.GenBuffers(vboColor.Length, vboColor); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboColor[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray.ByteLength, colorArray.Header, OpenGL.GL_DYNAMIC_DRAW); gl.VertexAttribPointer(ATTRIB_INDEX_COLOUR, 4, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_COLOUR); this.colorsBufferObject = vboColor[0]; uint[] vboVisual = new uint[1]; gl.GenBuffers(vboVisual.Length, vboVisual); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vboVisual[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, visibles.ByteLength, visibles.Header, OpenGL.GL_DYNAMIC_READ); gl.VertexAttribPointer(ATTRIB_INDEX_VISIBLE, 1, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(ATTRIB_INDEX_VISIBLE); this.visiblesBufferObject = vboVisual[0]; gl.BindVertexArray(0); }
public void Create(OpenGL gl, Viewport viewport) { _viewport = viewport; GC = new GraphicsContext(); GC.GL = gl; shader = new Shader(GC, "vertex.vert", "frag.frag"); float[] vertices = { -1f, -1f, 0f, -1f, 1f, 0f, 1f, -1f, 0f, 1f, 1f, 0.0f, }; gl.GenBuffers(1, VBO); gl.GenVertexArrays(1, VAO); gl.BindVertexArray(VAO[0]); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[0]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices, OpenGL.GL_STATIC_DRAW); gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(0); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); gl.BindVertexArray(0); }
public void Begin() { if (IsDisposed) { throw new ObjectDisposedException(nameof(SpriteBatch)); } gl.Disable(GL_CULL_FACE); gl.Enable(GL_DEPTH_TEST); gl.Enable(GL_BLEND); gl.BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); gl.BlendEquation(GL_FUNC_ADD_EXT); _shaderProgram.Bind(gl); gl.BindBuffer(GL_ARRAY_BUFFER, vertexBufferIds[0]); gl.BindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferIds[0]); gl.BindVertexArray(vaos[0]); currTexture = white1x1Tex; }
public void SetBufferData(OpenGL gl, IEnumerable <TransformationMatrix> transformations, OGLModelUsage usage = OGLModelUsage.StaticRead) { var transCount = transformations.Count(); // Validation. if (transCount > Math.Pow(2, 24)) { throw new OverflowException( "This shader can't handle more than 2^24 transformations while " + "using 24 bit colors."); } #region get indices var indices = new ushort[transCount]; for (ushort i = 0; i < indices.Length; i++) { indices[i] = i; // Do all transformations once. } #endregion get indices #region get transformations array var stride = 16; // Transformation matrix is a 4x4 = 16. var transformationsArray = new float[transCount * stride]; for (int i = 0; i < transCount; i++) { float[] transAsFloats = transformations.ElementAt(i).ResultMatrix.to_array(); for (int j = 0; j < stride; j++) { transformationsArray[i * stride + j] = transAsFloats[j]; } } #endregion get transformations array #region get color array int colorStride = 3; var colorArray = new float[transCount * colorStride]; for (int i = 0; i < transCount; i++) { ulong id = transformations.ElementAt(i).UniqueId; var color = new ColorF((uint)id); colorArray[i * stride] = color.R; colorArray[i * stride + 1] = color.G; colorArray[i * stride + 2] = color.B; } #endregion get color array gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _indicesBufferId.Value); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, indices, (uint)usage); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _transformationsBufferId.Value); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, transformationsArray, (uint)usage); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _colorsBufferId.Value); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, colorArray, (uint)usage); }
public void Create(int bufferSize) { uint[] temp = new uint[1]; Device.GenBuffers(1, temp); Device.BindBuffer(OpenGL.GL_SHADER_STORAGE_BUFFER, temp[0]); Device.BufferData(OpenGL.GL_SHADER_STORAGE_BUFFER, bufferSize, IntPtr.Zero, OpenGL.GL_DYNAMIC_DRAW); Device.BindBuffer(OpenGL.GL_SHADER_STORAGE_BUFFER, 0); Buffer = temp[0]; }
protected uint CreateVertexBufferObject(uint mode, VertexBuffer bufferData, uint usage) { uint[] ids = new uint[1]; gl.GenBuffers(1, ids); gl.BindBuffer(mode, ids[0]); gl.BufferData(mode, bufferData.SizeInBytes, bufferData.Data, usage); return(ids[0]); }
public void BufferData(OpenGL gl, uint[] indices, float[] vertices, float[] normals, float[] ambs, float[] diffs, float[] specs, float[] shinis) { gl.BindBuffer(_iboTarget, Ibo); gl.BufferData(_iboTarget, indices, _usage); gl.BindBuffer(_vboTarget, Position); gl.BufferData(_vboTarget, vertices, _usage); gl.BindBuffer(_vboTarget, Normal); gl.BufferData(_vboTarget, normals, _usage); gl.BindBuffer(_vboTarget, AmbientMaterial); gl.BufferData(_vboTarget, ambs, _usage); gl.BindBuffer(_vboTarget, DiffuseMaterial); gl.BufferData(_vboTarget, diffs, _usage); gl.BindBuffer(_vboTarget, SpecularMaterial); gl.BufferData(_vboTarget, specs, _usage); gl.BindBuffer(_vboTarget, ShininessValue); gl.BufferData(_vboTarget, shinis, _usage); gl.BindBuffer(_vboTarget, 0); gl.BindBuffer(_iboTarget, 0); }
protected void InitializeVAO(OpenGL gl, out uint[] vao, out BeginMode primitiveMode, out int vertexCount) { primitiveMode = BeginMode.Quads; vertexCount = positions.Length; vao = new uint[1]; gl.GenVertexArrays(1, vao); gl.BindVertexArray(vao[0]); // Create a vertex buffer for the vertex data. { uint[] ids = new uint[1]; gl.GenBuffers(1, ids); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]); var positionArray = new UnmanagedArray <vec3>(positions.Length); for (int i = 0; i < positions.Length; i++) { positionArray[i] = positions[i]; } uint positionLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_Position); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, positionArray.ByteLength, positionArray.Header, OpenGL.GL_STATIC_DRAW); gl.VertexAttribPointer(positionLocation, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(positionLocation); positionArray.Dispose(); } // Create a vertex buffer for the uv data. { uint[] ids = new uint[1]; gl.GenBuffers(1, ids); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ids[0]); var uvArray = new UnmanagedArray <vec2>(uvs.Length); for (int i = 0; i < uvs.Length; i++) { uvArray[i] = uvs[i]; } uint uvLocation = (uint)shaderProgram.GetAttributeLocation(gl, strin_uv); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, uvArray.ByteLength, uvArray.Header, OpenGL.GL_STATIC_DRAW); gl.VertexAttribPointer(uvLocation, 2, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(uvLocation); uvArray.Dispose(); } // Unbind the vertex array, we've finished specifying data for it. gl.BindVertexArray(0); }
private void LoadSceneData(OpenGL gl) { var modelLoader = Model.Load("male_head"); modelLoader.Wait(); Shape = modelLoader.Result; UI = new Legend(); gl.Enable(OpenGL.GL_DEPTH_TEST); gl.Enable(OpenGL.GL_CULL_FACE); gl.ClearColor(0.0f, 0.0f, 0.0f, 1.0f); gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); gl.GenVertexArrays(ArrayIds.Length, ArrayIds); // Model data gl.BindVertexArray(ArrayIds[0]); gl.GenBuffers(ModelBufferIds.Length, ModelBufferIds); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, ModelBufferIds[0]); gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, Shape.Indices, OpenGL.GL_STATIC_DRAW); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, ModelBufferIds[1]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, Shape.Data, OpenGL.GL_STATIC_DRAW); gl.EnableVertexAttribArray(0); gl.EnableVertexAttribArray(1); gl.EnableVertexAttribArray(2); gl.VertexAttribPointer(0, Shape.GetAttribSize(0), Shape.GetAttribType(0), Shape.ShouldAttribNormalize(0), Shape.GetAttribStride(0), Shape.GetAttribOffset(0)); gl.VertexAttribPointer(1, Shape.GetAttribSize(1), Shape.GetAttribType(1), Shape.ShouldAttribNormalize(1), Shape.GetAttribStride(1), Shape.GetAttribOffset(1)); gl.VertexAttribPointer(2, Shape.GetAttribSize(2), Shape.GetAttribType(2), Shape.ShouldAttribNormalize(2), Shape.GetAttribStride(2), Shape.GetAttribOffset(2)); // UI data gl.BindVertexArray(ArrayIds[1]); gl.GenBuffers(UIBufferIds.Length, UIBufferIds); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, UIBufferIds[0]); gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, UI.Indices, OpenGL.GL_STATIC_DRAW); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, UIBufferIds[1]); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, UI.Data, OpenGL.GL_STATIC_DRAW); gl.EnableVertexAttribArray(0); gl.EnableVertexAttribArray(1); gl.EnableVertexAttribArray(2); gl.VertexAttribPointer(0, UI.GetAttribSize(0), UI.GetAttribType(0), UI.ShouldAttribNormalize(0), UI.GetAttribStride(0), UI.GetAttribOffset(0)); gl.VertexAttribPointer(1, UI.GetAttribSize(1), UI.GetAttribType(1), UI.ShouldAttribNormalize(1), UI.GetAttribStride(1), UI.GetAttribOffset(1)); gl.VertexAttribPointer(2, UI.GetAttribSize(2), UI.GetAttribType(2), UI.ShouldAttribNormalize(2), UI.GetAttribStride(2), UI.GetAttribOffset(2)); }
protected void Initialise(OpenGL gl, string textureName) { sp = new ShaderProg(gl); // Load the shader's & link them to the program shaderProgram = sp.Loader("Shader.vert", "Shader.frag"); VBA = new uint[1]; gl.GenVertexArrays(1, VBA); gl.BindVertexArray(VBA[0]); // Allocate 2 buffers (verts & UV's) VBO = new uint[2]; // Generate the 2 buffers gl.GenBuffers(2, VBO); // Bind the first buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[0]); // Copy the data from the Vertex array into the array buffer gl.BufferData(OpenGL.GL_ARRAY_BUFFER, vertices, OpenGL.GL_STATIC_DRAW); // Align the Buffer to the Vertex shader layout location value (0) gl.VertexAttribPointer(0, 3, OpenGL.GL_FLOAT, false, 3 * sizeof(float), IntPtr.Zero); // Unbind the buffer gl.EnableVertexAttribArray(0); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); // Bind the UV's buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, VBO[1]); // Copy the data from the Colour array into the array buffer gl.BufferData(OpenGL.GL_ARRAY_BUFFER, texCoords, OpenGL.GL_STATIC_DRAW); // Align the Buffer to the Vertex shader layout location value (1) gl.VertexAttribPointer(1, 2, OpenGL.GL_FLOAT, false, 2 * sizeof(float), IntPtr.Zero); // Unbind the buffer gl.EnableVertexAttribArray(1); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); // Unbind VBA gl.BindVertexArray(0); texture.Create(gl, textureName); textureValue = texture.TextureName; // Specify linear filtering. gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR); gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR); // Create a model matrix to make the model a little bigger. modelMatrix = glm.scale(new mat4(1.0f), new vec3(1.5f));//2.5 }
public void BufferData(OpenGL gl, uint[] indices, float[] vertices, float[] colors) { if (indices != null) { gl.BindBuffer(_iboTarget, Ibo); gl.BufferData(_iboTarget, indices, _usage); } gl.BindBuffer(_vboTarget, Position); gl.BufferData(_vboTarget, vertices, _usage); gl.BindBuffer(_vboTarget, ColorValue); gl.BufferData(_vboTarget, colors, _usage); }
public void Init(OpenGL gl, uint blendMode, int particleCount, bool isContinous, bool autoRotateColors, Random random = null) { // Init particles List <Particle> particles = new List <Particle>(); for (int x = 0; x < particleCount; x++) { Particle particle = new Particle(x); particle.Init(_random); if (AfterParticleInit != null) { AfterParticleInit(particle, 1.0f); } particles.Add(particle); } // Set member variables if (random != null) { _random = random; } _blendMode = blendMode; _isContinuous = isContinous; _isActive = true; _autoRotateColors = autoRotateColors; _particles = particles; _pointData = new float[_particles.Count * _point.DataStride]; if (_autoRotateColors) { _colorRotateStopwatch.Start(); } pickColor(); // Load tex _texture = gl.LoadTexture("Images/GlowParticle.png"); // OpenGL init gl.GenVertexArrays(1, _vertexArrayObject); gl.GenBuffers(1, _vertexBufferObject); // Bind gl.BindVertexArray(_vertexArrayObject[0]); { // Vertex buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]); GlBuffer.SetArrayData(gl, _pointData, _pointData.Length * PrimitiveSizes.FloatBytes, OpenGL.GL_STREAM_DRAW); // Vertex attribute gl.VertexAttribPointer(0, _point.VertexDataStride, OpenGL.GL_FLOAT, false, _point.DataStride * PrimitiveSizes.FloatBytes, IntPtr.Zero); gl.EnableVertexAttribArray(0); // Color attribute gl.VertexAttribPointer(1, _point.ColorDataStride, OpenGL.GL_FLOAT, false, _point.DataStride * PrimitiveSizes.FloatBytes, new IntPtr(_point.VertexDataStride * PrimitiveSizes.FloatBytes)); gl.EnableVertexAttribArray(1); } gl.BindVertexArray(0); // Unbind }
protected override void DoInitialize() { { var computeProgram = new ShaderProgram(); var shaderCode = new ShaderCode(File.ReadAllText(@"05ParticleSimulator\particleSimulator.comp"), ShaderType.ComputeShader); var shader = shaderCode.CreateShader(); computeProgram.Create(shader); shader.Delete(); this.computeProgram = computeProgram; } { OpenGL.GenTextures(1, textureBufferPosition); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferPosition[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, this.positionBufferPtrId); OpenGL.GenTextures(1, textureBufferVelocity); OpenGL.BindTexture(OpenGL.GL_TEXTURE_BUFFER, textureBufferVelocity[0]); OpenGL.GetDelegateFor <OpenGL.glTexBuffer>()(OpenGL.GL_TEXTURE_BUFFER, OpenGL.GL_RGBA32F, this.velocityBufferPtrId); } { OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, attractor_buffer); OpenGL.BindBuffer(BufferTarget.UniformBuffer, attractor_buffer[0]); OpenGL.GetDelegateFor <OpenGL.glBufferData>()(OpenGL.GL_UNIFORM_BUFFER, 64 * Marshal.SizeOf(typeof(vec4)), IntPtr.Zero, OpenGL.GL_DYNAMIC_COPY); OpenGL.GetDelegateFor <OpenGL.glBindBufferBase>()(OpenGL.GL_UNIFORM_BUFFER, 0, attractor_buffer[0]); } }
public void Init(OpenGL gl) { // Load tex _texture = gl.LoadTexture("Images/LaserFlare.png"); // OpenGL init gl.GenVertexArrays(1, _vertexArrayObject); gl.GenBuffers(1, _vertexBufferObject); // Bind gl.BindVertexArray(_vertexArrayObject[0]); { // Vertex buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]); GlBuffer.SetArrayData(gl, _quad.VertexData, _quad.SizeOfVertexDataBytes, OpenGL.GL_STATIC_DRAW); // Vertex attribute gl.VertexAttribPointer(0, _quad.VertexDataStride, OpenGL.GL_FLOAT, false, _quad.DataStride * PrimitiveSizes.FloatBytes, IntPtr.Zero); gl.EnableVertexAttribArray(0); // Color attribute gl.VertexAttribPointer(1, _quad.ColorDataStride, OpenGL.GL_FLOAT, false, _quad.DataStride * PrimitiveSizes.FloatBytes, new IntPtr(_quad.VertexDataStride * PrimitiveSizes.FloatBytes)); gl.EnableVertexAttribArray(1); // Texture attribute gl.VertexAttribPointer(2, _quad.TexCoordDataStride, OpenGL.GL_FLOAT, false, _quad.DataStride * PrimitiveSizes.FloatBytes, new IntPtr((_quad.VertexDataStride + _quad.ColorDataStride) * PrimitiveSizes.FloatBytes)); gl.EnableVertexAttribArray(2); } gl.BindVertexArray(0); // Unbind }
public void BindHTVBOs(OpenGL gl, HitTestProgram program) { var attribPos = program.Attribs["Position"]; gl.BindBuffer(_vboTarget, Position); gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(attribPos); attribPos = program.Attribs["HTColorId"]; gl.BindBuffer(_vboTarget, HTColorId); gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.VertexAttribDivisor(attribPos, 1); gl.EnableVertexAttribArray(attribPos); gl.BindBuffer(_iboTarget, Ibo); }
protected override void DoInitialize() { base.DoInitialize(); { // velocity var velocity_buffer = new uint[1]; OpenGL.GetDelegateFor <OpenGL.glGenBuffers>()(1, velocity_buffer); OpenGL.BindBuffer(BufferTarget.ArrayBuffer, velocity_buffer[0]); var velocities = new UnmanagedArray <vec4>(SunshineParticleModel.particleCount); unsafe { var random = new Random(); var array = (vec4 *)velocities.Header.ToPointer(); for (int i = 0; i < SunshineParticleModel.particleCount; i++) { array[i] = new vec4( (float)(random.NextDouble() - 0.5) * 0.2f, (float)(random.NextDouble() - 0.5) * 0.2f, (float)(random.NextDouble() - 0.5) * 0.2f, 0); } } OpenGL.BufferData(BufferTarget.ArrayBuffer, velocities, BufferUsage.DynamicCopy); velocities.Dispose(); //GL.GetDelegateFor<GL.glVertexAttribPointer>()(0, 4, GL.GL_FLOAT, false, 0, IntPtr.Zero); //GL.GetDelegateFor<GL.glEnableVertexAttribArray>()(0); // OpenGL.BindBuffer(BufferTarget.ArrayBuffer, 0); this.VelocityBufferPtrId = velocity_buffer[0]; } this.PositionBufferPtr = this.bufferable.GetProperty(SunshineParticleModel.strPosition, null); this.VertexArrayObject = this.vertexArrayObject; }
public void Init(OpenGL gl) { // Data init _pointData = new float[_pointSphere.VertexCount * _point.DataStride]; _colorIndex = _random.Next(Constants.Colors.Length / 3); // Load tex _texture = gl.LoadTexture("Images/GlowParticle.png"); // OpenGL init gl.GenVertexArrays(1, _vertexArrayObject); gl.GenBuffers(1, _vertexBufferObject); // Bind gl.BindVertexArray(_vertexArrayObject[0]); { // Vertex buffer gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, _vertexBufferObject[0]); GlBuffer.SetArrayData(gl, _pointData, _pointData.Length * PrimitiveSizes.FloatBytes, OpenGL.GL_STREAM_DRAW); // Vertex attribute gl.VertexAttribPointer(0, _point.VertexDataStride, OpenGL.GL_FLOAT, false, _point.DataStride * PrimitiveSizes.FloatBytes, IntPtr.Zero); gl.EnableVertexAttribArray(0); // Color attribute gl.VertexAttribPointer(1, _point.ColorDataStride, OpenGL.GL_FLOAT, false, _point.DataStride * PrimitiveSizes.FloatBytes, new IntPtr(_point.VertexDataStride * PrimitiveSizes.FloatBytes)); gl.EnableVertexAttribArray(1); } gl.BindVertexArray(0); // Unbind }
// create the OpenGL Element Buffer Object (EBO) private uint loadElementBufferObject() { uint elementBuffer = 0; OpenGL.GenBuffers(1, ref elementBuffer); OpenGL.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, elementBuffer); return(elementBuffer); }
// create the OpenGL Vertex Buffer Object (VBO) private static uint loadVertexBufferObject() { uint vertexBuffer = 0; OpenGL.GenBuffers(1, ref vertexBuffer); OpenGL.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBuffer); return(vertexBuffer); }
private void DrawTrefoilBuffers(OpenGL gl) { // Bind the vertex and index buffer. gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBuffer); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, indexBuffer); gl.EnableVertexAttribArray(attrPosition); gl.EnableVertexAttribArray(attrNormal); // Draw the geometry, straight from the vertex buffer. gl.VertexAttribPointer(attrPosition, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Zero); int normalOffset = Marshal.SizeOf(typeof(Vertex)); gl.VertexAttribPointer(attrNormal, 3, OpenGL.GL_FLOAT, false, Marshal.SizeOf(typeof(Vertex)), IntPtr.Add(new IntPtr(0), normalOffset)); gl.DrawElements(OpenGL.GL_LINES, (int)trefoilKnot.IndexCount, OpenGL.GL_UNSIGNED_SHORT, IntPtr.Zero); }
private void InitTrianglesBuffer(OpenGL gl, UnmanagedArray <uint> TriangleStrip) { uint[] triangleBuffer = new uint[1]; gl.GenBuffers(triangleBuffer.Length, triangleBuffer); gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, triangleBuffer[0]); gl.BufferData(OpenGL.GL_ELEMENT_ARRAY_BUFFER, TriangleStrip.ByteLength, TriangleStrip.Header, OpenGL.GL_STATIC_DRAW); this.triangleIndexCount = TriangleStrip.Length; this.triangleBufferObject = triangleBuffer[0]; }
/// <summary> /// Function invoked when points and colors must be added to the render. /// The size of lvertex and lcolor MUST BE THE SAME. Otherwise, there will be black points (no visible) /// </summary> /// <param name="lvertex"> List of vertexes to be added</param> /// <param name="lcolor"> List of colors to be added</param> private void addPointsAndColor(List <SharpGL.SceneGraph.Vertex> lvertex, List <SharpGL.SceneGraph.Vertex> lcolor) { OpenGL gl = openGLControl.OpenGL; //append new points to the existing list (if exists) if (l_vertex == null) { l_vertex = new List <SharpGL.SceneGraph.Vertex>(); } l_vertex.AddRange(lvertex); //append new colors to the existing list (if exists) if (l_color == null) { l_color = new List <SharpGL.SceneGraph.Vertex>(); } l_color.AddRange(lcolor); //set new size of the new list (just to render, if it exists) if (l_sizes == null) { l_sizes = new List <int>(); } l_sizes.Add(lvertex.Count); uint[] ids = new uint[2]; int pos = l_vboId.Count; //add ids to the existing list if (l_vboId == null) { l_vboId = new List <uint[]>(); } l_vboId.Add(ids); gl.GenBuffers(2, l_vboId[pos]); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[pos][0]); IntPtr ptr1 = GCHandle.Alloc(lvertex.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject(); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * lvertex.Count * 3, ptr1, OpenGL.GL_STATIC_DRAW); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, l_vboId[pos][1]); IntPtr ptr2 = GCHandle.Alloc(lcolor.ToArray().ToArray(), GCHandleType.Pinned).AddrOfPinnedObject(); gl.BufferData(OpenGL.GL_ARRAY_BUFFER, sizeof(float) * lcolor.Count * 3, ptr2, OpenGL.GL_DYNAMIC_DRAW); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, 0); }
public void UpdateColorBuffer(OpenGL gl, UnmanagedArray <ColorF> colors, UnmanagedArray <float> visibles) { if (this.visiblesBufferObject == 0 || this.colorsBufferObject == 0) { return; } gl.MakeCurrent(); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, this.visiblesBufferObject); IntPtr destVisibles = gl.MapBuffer(OpenGL.GL_ARRAY_BUFFER, OpenGL.GL_READ_WRITE); MemoryHelper.CopyMemory(destVisibles, visibles.Header, (uint)visibles.ByteLength); gl.UnmapBuffer(OpenGL.GL_ARRAY_BUFFER); gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, this.colorsBufferObject); IntPtr destColors = gl.MapBuffer(OpenGL.GL_ARRAY_BUFFER, OpenGL.GL_READ_WRITE); MemoryHelper.CopyMemory(destColors, colors.Header, (uint)colors.ByteLength); gl.UnmapBuffer(OpenGL.GL_ARRAY_BUFFER); }
public void BindVBOs(OpenGL gl, LinesProgram program) { var attribPos = program.Attribs["Position"]; gl.BindBuffer(_vboTarget, Position); gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.EnableVertexAttribArray(attribPos); attribPos = program.Attribs["ColorValue"]; gl.BindBuffer(_vboTarget, ColorValue); gl.VertexAttribPointer(attribPos, 3, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); gl.VertexAttribDivisor(attribPos, 1); gl.EnableVertexAttribArray(attribPos); if (IndicesCount > 0) { gl.BindBuffer(_iboTarget, Ibo); } }
public void Bind(OpenGL gl) { gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, vertexBufferObject); }
public void Unbind(OpenGL gl) { gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, 0); }
public void Bind(OpenGL gl) { gl.BindBuffer(OpenGL.GL_ELEMENT_ARRAY_BUFFER, bufferObject); }