public void Memory_CopyBenchmarkManaged() { Memory.UseSystemCopy = false; using (MemoryLock srcLock = new MemoryLock(_BenchmarkArraySrc)) { Memory.Copy(_BenchmarkArrayDst, srcLock.Address, (ulong)Memory_CopyBenchmarkParams); } }
/// <summary> /// Add a contour to the current polygon. /// </summary> /// <param name="contourVertices"> /// /// </param> public void AddContour(Vertex3d[] contourVertices, Vertex3d normal) { if (contourVertices == null) { throw new ArgumentNullException("contourVertices"); } MemoryLock countourLock = new MemoryLock(contourVertices); // Dispose later _CountourLocks.Add(countourLock); // Set to Vertex3d.Zero to compute automatically Glu.TessNormal(_Tess, normal.x, normal.x, normal.z); IntPtr vLockAddr = countourLock.Address; Glu.TessBeginContour(_Tess); foreach (Vertex3d v in contourVertices) { Glu.TessVertex(_Tess, vLockAddr, vLockAddr); vLockAddr = new IntPtr(vLockAddr.ToInt64() + 24); } Glu.TessEndContour(_Tess); }
public void Memory_TestCopy(bool systemCopy) { Memory.UseSystemCopy = systemCopy; if (systemCopy != Memory.UseSystemCopy) { Assert.Inconclusive(); } // No exception is checked: values passed directly to the implementation int[] dstArray = { 1, 2, 3, 4 }; float[] srcArray = { 1.0f, 2.0f, 3.0f, 4.0f }; using (MemoryLock srcArrayLock = new MemoryLock(srcArray)) { Memory.Copy(dstArray, srcArrayLock.Address, 4 * 4); } for (int i = 0; i < srcArray.Length; i++) { srcArray[i] = 0.0f; } using (MemoryLock dstArrayLock = new MemoryLock(dstArray)) { Memory.Copy(srcArray, dstArrayLock.Address, 4 * 4); } for (int i = 0; i < srcArray.Length; i++) { Assert.AreEqual((float)(i + 1), srcArray[i]); } }
/// <summary> /// Upload the Vertices into Buffer Data. /// </summary> public void Update(Vertex[] vertices) { // Assign vertices information _vertices = vertices; _length = vertices.Length; // Pin the vertices, prevent GC wipe this pointer using (var memory = new MemoryLock(_vertices)) { var pointer = memory.Address; // Calculate the stride and upload the vertices var stride = Vertex.Stride; GL.VertexPointer(2, VertexPointerType.Float, stride, pointer.Increment(0)); GLChecker.CheckError(); GL.TexCoordPointer(2, TexCoordPointerType.Float, stride, pointer.Increment(8)); GLChecker.CheckError(); GL.ColorPointer(4, ColorPointerType.UnsignedByte, stride, pointer.Increment(16)); GLChecker.CheckError(); } }
private void RenderControl_Render_ES(object sender, GlControlEventArgs e) { Control control = (Control)sender; OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f); ModelMatrix modelMatrix = new ModelMatrix(); // Animate triangle modelMatrix.RotateZ(_Angle); Gl.Viewport(0, 0, control.Width, control.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4(_Es2_Program_Location_uMVP, false, (projectionMatrix * modelMatrix).ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void initOutputBuffer() { ParticleEditModel md = MainModel.ins.particleEditModel; //if(md == null || md.width == 0 || md.height == 0) { // return; //} if (bufWidth == md.width && bufHeight == md.height) { return; } int w = md.width; int h = md.height; bufWidth = w; bufHeight = h; bufferMVP = calcMVP(w, h); //Gl.Enable(EnableCap.FramebufferSrgb); if (mlBufferCache != null) { mlBufferCache.Dispose(); } if (mlBufferOutput != null) { mlBufferOutput.Dispose(); } int size = w * h * pxChannel; bufferCache = new byte[size]; mlBufferCache = new MemoryLock(bufferCache); bufferOutput = new byte[size]; mlBufferOutput = new MemoryLock(bufferOutput); //Gl.BufferData(BufferTarget.PixelPackBuffer, (uint)size, mlImageData.Address, BufferUsage.DynamicRead); Gl.BindTexture(TextureTarget.Texture2d, glbufTexCache); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToBorder); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToBorder); //Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureWrapR, (int)TextureWrapMode.Repeat); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Linear); Gl.TexParameter(TextureTarget.Texture2d, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear); //Gl.TexEnv(TextureEnvTarget.TextureEnv, TextureEnvParameter.TextureEnvMode, (int)LightEnvModeSGIX.Replace); Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, w, h, 0, OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, mlBufferCache.Address); Gl.BindTexture(TextureTarget.Texture2d, 0); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, glbufOutput); Gl.FramebufferTexture2D(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, TextureTarget.Texture2d, glbufTexCache, 0); Gl.BindFramebuffer(FramebufferTarget.Framebuffer, 0); }
public override void Render() { if (!Visible) { return; } if (NineSliceRendering) { float[] tempTex = new float[8]; float[] tempQuad = new float[12]; tempQuad[2] = quadPosition[2]; tempQuad[5] = quadPosition[5]; tempQuad[8] = quadPosition[8]; tempQuad[11] = quadPosition[11]; float boarder = NineSliceBoarder / (float)TextureSize; Gl.BindTexture(TextureTarget.Texture2d, texName.glID); //Iterate clockwise from bottom left corner as 0 for (int i = 0; i < 9; i++) { int row = i % 3; int col = i / 3; tempQuad[0] = (col == 0) ? (screenX - width3) : (col == 1) ? (screenX - width3 + NineSliceBoarder) : (screenX + width4 - NineSliceBoarder); tempQuad[3] = (col == 0) ? (screenX - width2) : (col == 1) ? (screenX - width2 + NineSliceBoarder) : (screenX + width1 - NineSliceBoarder); tempQuad[6] = (col == 0) ? (screenX - width2 + NineSliceBoarder) : (col == 1) ? (screenX + width1 - NineSliceBoarder) : (screenX + width1); tempQuad[9] = (col == 0) ? (screenX - width3 + NineSliceBoarder) : (col == 1) ? (screenX + width4 - NineSliceBoarder) : (screenX + width4); tempQuad[1] = (row == 0) ? (screenY - height3) : (row == 1) ? (screenY - height3 + NineSliceBoarder) : (screenY + height2 - NineSliceBoarder); tempQuad[10] = (row == 0) ? (screenY - height4) : (row == 1) ? (screenY - height4 + NineSliceBoarder) : (screenY + height1 - NineSliceBoarder); tempQuad[4] = (row == 0) ? (screenY - height3 + NineSliceBoarder) : (row == 1) ? (screenY + height2 - NineSliceBoarder) : (screenY + height2); tempQuad[7] = (row == 0) ? (screenY - height4 + NineSliceBoarder) : (row == 1) ? (screenY + height1 - NineSliceBoarder) : (screenY + height1); tempTex[0] = (col == 0) ? (0f) : (col == 1) ? (boarder) : (1f - boarder); tempTex[2] = tempTex[0]; tempTex[4] = (col == 0) ? (boarder) : (col == 1) ? (1f - boarder) : (1f); tempTex[6] = tempTex[4]; tempTex[1] = (row == 0) ? (0f) : (row == 1) ? (boarder) : (1f - boarder); tempTex[7] = tempTex[1]; tempTex[3] = (row == 0) ? (boarder) : (row == 1) ? (1f - boarder) : (1f); tempTex[5] = tempTex[3]; using (MemoryLock vertexArrayLock = new MemoryLock(tempQuad)) using (MemoryLock vertexTextureLock = new MemoryLock(tempTex)) { Gl.VertexPointer(3, VertexPointerType.Float, 0, vertexArrayLock.Address); //Use the vertex array for vertex information Gl.TexCoordPointer(2, TexCoordPointerType.Float, 0, vertexTextureLock.Address); //Use the texture array for texture coordinates Gl.DrawArrays(PrimitiveType.Quads, 0, 4); //Draw the quad } } } else { base.Render(); } }
public Activation(MapVertexBuffer source) { _positionLock = new MemoryLock(source._positions); _colorLock = new MemoryLock(source._colors); _textureLock = new MemoryLock(source._textures); Gl.VertexPointer(2, VertexPointerType.Float, 0, _positionLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(4, ColorPointerType.UnsignedByte, 0, _colorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.TexCoordPointer(2, TexCoordPointerType.Float, 0, _textureLock.Address); Gl.EnableClientState(EnableCap.TextureCoordArray); }
public virtual void Render() { using (MemoryLock vertexArrayLock = new MemoryLock(quadPosition)) using (MemoryLock vertexTextureLock = new MemoryLock(texturePosition)) { //Sets the texture used Gl.PushMatrix(); Gl.BindTexture(TextureTarget.Texture2d, texName.glID); Gl.VertexPointer(3, VertexPointerType.Float, 0, vertexArrayLock.Address); //Use the vertex array for vertex information Gl.TexCoordPointer(2, TexCoordPointerType.Float, 0, vertexTextureLock.Address); //Use the texture array for texture coordinates Gl.DrawArrays(PrimitiveType.Quads, 0, 4); //Draw the quad Gl.PopMatrix(); } }
public void render(float[] mMVP, int renderTime) { if (!isTextureExist) { return; } if (particleCount <= 0) { return; } Gl.UseProgram(ProgramName); using (MemoryLock lockIndex = new MemoryLock(_ArrayIndex)) using (MemoryLock lockCoord = new MemoryLock(_ArrayCoord)) { //index Gl.VertexAttribPointer((uint)LocationIndex, 2, VertexAttribType.Float, false, 0, lockIndex.Address); Gl.EnableVertexAttribArray((uint)LocationIndex); //cord Gl.VertexAttribPointer((uint)LocationCoord, 2, VertexAttribType.Float, false, 0, lockCoord.Address); Gl.EnableVertexAttribArray((uint)LocationCoord); //MVP Gl.UniformMatrix4(LocationMVP, false, mMVP); //attr Gl.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, attrBufferId); //Gl.UnmapBuffer(BufferTarget.ShaderStorageBuffer); Gl.Uniform1(LocationNowTime, (float)renderTime); Gl.Uniform1(LocationTotalLifeTime, (float)maxRenderTime); Gl.Uniform1(LocationParticleCount, particleCount); Gl.Uniform2(LocationStartPos, arrStartPos[0], arrStartPos[1]); //texture Gl.BindTexture(TextureTarget.Texture2d, texId); //Gl.GenerateMipmap(TextureTarget.Texture2d); Gl.Uniform1(LocationTex, 0); Gl.DrawArraysInstanced(PrimitiveType.Polygon, 0, _ArrayIndex.Length / 2, particleCount); //Gl.DrawArraysInstanced(PrimitiveType.Points, 0, _ArrayIndex.Length / 2, particleCount); } Gl.BindBufferBase(BufferTarget.ShaderStorageBuffer, 0, 0); Gl.BindTexture(TextureTarget.Texture2d, 0); Gl.UseProgram(0); }
private void RenderControl_Render_ES(object sender, GlControlEventArgs e) { Control control = (Control)sender; PerspectiveProjectionMatrix projectionMatrix = new PerspectiveProjectionMatrix(45.0f, (float)control.Width / (float)control.Height, 0.1f, 100.0f); ModelMatrix viewMatrix = new ModelMatrix(); ModelMatrix modelMatrix = new ModelMatrix(); // Move camera viewMatrix.Translate(new Vertex3f(0.0f, 0.0f, -2.0f)); // Animate triangle /*modelMatrix.LookAtDirection( * new Vertex3f(0.0f, 0.0f, 0.0f), * new Vertex3f( * (float)Math.Sin(angle_rad), * 0.0f, * (float)Math.Cos(angle_rad) * ), * new Vertex3f(0.0f, 1.0f, 0.0f) * );*/ //Quaternion Q = new Quaternion(new Vertex3f(0.0f, 1.0f, 0.0f), angle); modelMatrix.RotateZ(angle); modelMatrix.RotateY(angle); //modelMatrix.Translate(Math.Cos(theta), Math.Sin(theta)); //modelMatrix.RotateY(theta); Gl.UseProgram(Program_Shader); Gl.Viewport(0, 0, control.Width, control.Height); Gl.Enable(EnableCap.DepthTest); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)Program_Location_aPosition, 3, Gl.FLOAT, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aPosition); Gl.VertexAttribPointer((uint)Program_Location_aColor, 3, Gl.FLOAT, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)Program_Location_aColor); Gl.UniformMatrix4(Program_Location_uProjection, 1, false, projectionMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uView, 1, false, viewMatrix.ToArray()); Gl.UniformMatrix4(Program_Location_uModel, 1, false, modelMatrix.ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 36); } }
public async ValueTask <ILock> GetLock(string key) { MemoryLock l; lock (_locks) { if (!_locks.TryGetValue(key, out l)) { l = new MemoryLock(key, this); _locks.Add(key, l); return(l); } } return(await l.CreateTask()); }
public void Render() { //Gl.Rotate(angle, 0.0f, 0.0f, 1.0f); using (MemoryLock vertexArrayLock = new MemoryLock(quadPosition)) using (MemoryLock vertexColorLock = new MemoryLock(arrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void Es2_Render() { Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4f(_Es2_Program_Location_uMVP, 1, false, Matrix4x4f.Ortho(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
public static void Draw(PrimitiveType type) { using (var colorArrayLock = new MemoryLock(_colors.ToArray())) using (var vertexArrayLock = new MemoryLock(_verts.ToArray())) using (var normalArrayLock = new MemoryLock(_normals.ToArray())) { Gl.VertexPointer(3, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(4, ColorPointerType.Float, 0, colorArrayLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.NormalPointer(NormalPointerType.Float, 0, normalArrayLock.Address); Gl.EnableClientState(EnableCap.NormalArray); Gl.DrawArrays(type, 0, _verts.Count / 3); } }
private void Es1_Render() { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void GlControl_Render(object sender, GlControlEventArgs e) { Control senderControl = (Control)sender; Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.Rotate(_Angle, 0.0f, 0.0f, 1.0f); // This method is shared between the two GlControl // Uses the texture created with the GlControl1 on both controls Gl.BindTexture(TextureTarget.Texture2d, _SharedTexture); if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexTexCoordLock = new MemoryLock(_ArrayTexCoord)) { Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.TexCoordPointer(2, TexCoordPointerType.Float, 0, vertexTexCoordLock.Address); Gl.EnableClientState(EnableCap.TextureCoordArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } } else { // Old school OpenGL Gl.Begin(PrimitiveType.Triangles); for (int i = 0; i < _ArrayPosition.Length; i += 2) { Gl.TexCoord2(_ArrayTexCoord[i], _ArrayTexCoord[i + 1]); Gl.Vertex2(_ArrayPosition[i], _ArrayPosition[i + 1]); } Gl.End(); } _Angle += 1.0f; }
private void GlControl_Render(object sender, OpenGL.GlControlEventArgs e) { Control senderControl = (Control)sender; // FIXME I wonder why the viewport is affected when the GlControl is hosted in WPF windows. int vpx = -senderControl.ClientSize.Width; int vpy = -senderControl.ClientSize.Height; int vpw = senderControl.ClientSize.Width * 2; int vph = senderControl.ClientSize.Height * 2; Gl.Viewport(vpx, vpy, vpw, vph); Gl.Clear(ClearBufferMask.ColorBufferBit); if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } } else { // Old school OpenGL Gl.Begin(PrimitiveType.Triangles); Gl.Color3(1.0f, 0.0f, 0.0f); Gl.Vertex2(0.0f, 0.0f); Gl.Color3(0.0f, 1.0f, 0.0f); Gl.Vertex2(0.5f, 1.0f); Gl.Color3(0.0f, 0.0f, 1.0f); Gl.Vertex2(1.0f, 0.0f); Gl.End(); } }
private void GlControl_Render(object sender, OpenGL.GlControlEventArgs e) { var senderControl = sender as GlControl; senderControl.Animation = true; senderControl.AnimationTime = 1; int vpx = 0; int vpy = 0; int vpw = senderControl.ClientSize.Width; int vph = senderControl.ClientSize.Height; Gl.Viewport(vpx, vpy, vpw, vph); Gl.ClearColor(0.39f, 0.58f, 0.92f, 1); Gl.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); cycle(); if (chip.draw) { Draw(chip.display); } if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, _ArrayPosition.Length / 2); } } }
private void Es2_Render() { OrthoProjectionMatrix projectionMatrix = new OrthoProjectionMatrix(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f); Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, Gl.FLOAT, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, Gl.FLOAT, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4(_Es2_Program_Location_uMVP, 1, false, projectionMatrix.ToArray()); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void RenderControl_Render_GL(object sender, GlControlEventArgs e) { Control senderControl = (Control)sender; Gl.Viewport(0, 0, senderControl.ClientSize.Width, senderControl.ClientSize.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); // Animate triangle Gl.MatrixMode(MatrixMode.Modelview); Gl.LoadIdentity(); Gl.Rotate(_Angle, 0.0f, 0.0f, 1.0f); if (Gl.CurrentVersion >= Gl.Version_110) { // Old school OpenGL 1.1 // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); Gl.EnableClientState(EnableCap.VertexArray); Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); Gl.EnableClientState(EnableCap.ColorArray); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } } else { // Old school OpenGL Gl.Begin(PrimitiveType.Triangles); Gl.Color3(1.0f, 0.0f, 0.0f); Gl.Vertex2(0.0f, 0.0f); Gl.Color3(0.0f, 1.0f, 0.0f); Gl.Vertex2(0.5f, 1.0f); Gl.Color3(0.0f, 0.0f, 1.0f); Gl.Vertex2(1.0f, 0.0f); Gl.End(); } }
private void RenderControl_Render(object sender, GlControlEventArgs e) { lock (_drawLock) { Gl.Enable(EnableCap.Texture2d); Gl.ClearColor(Color.Gray.R / 255.0f, Color.Gray.G / 255.0f, Color.Gray.B / 255.0f, 0); Gl.Clear(ClearBufferMask.ColorBufferBit); if (_ui.gameStarted) { Gl.MatrixMode(MatrixMode.Projection); Gl.LoadIdentity(); Gl.Viewport(0, 0, ClientSize.Width, ClientSize.Height); Gl.Ortho(0d, 1d, 0d, 1d, -1d, 1d); Gl.MatrixMode(MatrixMode.Modelview); Gl.LoadIdentity(); Gl.BindTexture(TextureTarget.Texture2d, _textureId); using (MemoryLock locked = new MemoryLock(_ui.rawBitmap)) { Gl.TexImage2D(TextureTarget.Texture2d, 0, InternalFormat.Rgba, UI.GameWidth, UI.GameHeight, 0, PixelFormat.Bgra, PixelType.UnsignedByte, locked.Address); } Gl.TextureParameterEXT(_textureId, TextureTarget.Texture2d, TextureParameterName.TextureMagFilter, _ui._filterMode == UI.FilterMode.Linear ? Gl.LINEAR : Gl.NEAREST); Gl.Begin(PrimitiveType.Quads); Gl.TexCoord2(0, 1); Gl.Vertex2(0, 0); Gl.TexCoord2(0, 0); Gl.Vertex2(0, 1); Gl.TexCoord2(1, 0); Gl.Vertex2(1, 1); Gl.TexCoord2(1, 1); Gl.Vertex2(1, 0); Gl.End(); } } }
private void RenderControl_RenderGLES2() { Matrix4x4f projection = Matrix4x4f.Ortho2D(0.0f, 1.0f, 0.0f, 1.0f); Matrix4x4f modelview = Matrix4x4f.RotatedZ(_Angle); Gl.UseProgram(_Program.ProgramName); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Program.LocationPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Program.LocationPosition); Gl.VertexAttribPointer((uint)_Program.LocationColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Program.LocationColor); Gl.UniformMatrix4f(_Program.LocationMVP, 1, false, projection * modelview); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private static void Draw() { Gl.Viewport(0, 0, 1920, 1080); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4f(_Es2_Program_Location_uMVP, 1, false, Matrix4x4f.Ortho(0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f)); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
public void Render() { // Setup model-view matrix (as previously) // Model-view matrix selector Gl.MatrixMode(MatrixMode.Modelview); // Load (reset) to identity Gl.LoadIdentity(); // Multiply with rotation matrix (around Z axis) Gl.Rotate(_Angle, 0.0f, 0.0f, 1.0f); // Draw triangle using immediate mode // Setup & enable client states to specify vertex arrays, and use Gl.DrawArrays instead of Gl.Begin/End paradigm using (MemoryLock vertexArrayLock = new MemoryLock(_ArrayPosition)) using (MemoryLock vertexColorLock = new MemoryLock(_ArrayColor)) { // Note: the use of MemoryLock objects is necessary to pin vertex arrays since they can be reallocated by GC // at any time between the Gl.VertexPointer execution and the Gl.DrawArrays execution // Set current client memory pointer for position: a vertex of 2 floats Gl.VertexPointer(2, VertexPointerType.Float, 0, vertexArrayLock.Address); // Position is used for drawing Gl.EnableClientState(EnableCap.VertexArray); // Set current client memory pointer for color: a vertex of 3 floats Gl.ColorPointer(3, ColorPointerType.Float, 0, vertexColorLock.Address); // Color is used for drawing Gl.EnableClientState(EnableCap.ColorArray); // Note: enabled client state and client memory pointers are a GL state, and theorically they could be // set only at creation time. However, memory should be pinned for application lifetime. // Start drawing triangles (3 vertices -> 1 triangle) // Note: vertex attributes are streamed from client memory to GPU Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void RenderControl_Render(object sender, GlControlEventArgs e) { Control control = (Control)sender; Gl.Viewport(0, 0, control.Width, control.Height); Gl.Clear(ClearBufferMask.ColorBufferBit); Gl.UseProgram(_Es2_Program); using (MemoryLock arrayPosition = new MemoryLock(_ArrayPosition)) using (MemoryLock arrayColor = new MemoryLock(_ArrayColor)) { Gl.VertexAttribPointer((uint)_Es2_Program_Location_aPosition, 2, VertexAttribType.Float, false, 0, arrayPosition.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aPosition); Gl.VertexAttribPointer((uint)_Es2_Program_Location_aColor, 3, VertexAttribType.Float, false, 0, arrayColor.Address); Gl.EnableVertexAttribArray((uint)_Es2_Program_Location_aColor); Gl.UniformMatrix4f(_Es2_Program_Location_uMVP, 1, false, Matrix4x4f.Ortho2D(0.0f, 1.0f, 0.0f, 1.0f)); Gl.DrawArrays(PrimitiveType.Triangles, 0, 3); } }
private void glControl_ContextDestroying(object sender, GlControlEventArgs e) { clear(); if (mlBufferCache != null) { mlBufferCache.Dispose(); mlBufferCache = null; } if (mlBufferOutput != null) { mlBufferOutput.Dispose(); mlBufferOutput = null; } if (glbufTexCache > 0) { Gl.DeleteTextures(glbufTexCache); glbufTexCache = 0; } if (glbufOutput > 0) { Gl.DeleteFramebuffers(glbufOutput); glbufOutput = 0; } }
public static void MemoryLock_TestAddress() { float[] array = new float[16]; // Allow null argument Assert.DoesNotThrow(() => { using (new MemoryLock(null)) {} }); // Nominal case does not throw exceptions Assert.DoesNotThrow(() => { using (new MemoryLock(array)) {} }); // Null argument lead to IntPtr.Zero using (MemoryLock nullLock = new MemoryLock(null)) { Assert.AreEqual(IntPtr.Zero, nullLock.Address); } MemoryLock arrayLock = new MemoryLock(array); try { // Existing reference have non-null pointer Assert.AreNotEqual(IntPtr.Zero, arrayLock.Address); } finally { arrayLock.Dispose(); } // Address reset to IntPtr.Zero after disposition Assert.AreEqual(IntPtr.Zero, arrayLock.Address); // Allow multiple Dispose() Assert.DoesNotThrow(() => arrayLock.Dispose()); }
// Initializes all the buffer objects/arrays private void setupMesh() { using (MemoryLock verticesLock = new MemoryLock(this.vertices.ToArray())) using (MemoryLock indicesLock = new MemoryLock(this.indices.ToArray())) { // Create buffers/arrays this.VAO = Gl.GenVertexArray(); this.VBO = Gl.GenBuffer(); this.EBO = Gl.GenBuffer(); Gl.BindVertexArray(this.VAO); // Load data into vertex buffers Gl.BindBuffer(BufferTargetARB.ArrayBuffer, this.VBO); // A great thing about structs is that their memory layout is sequential for all its items. // The effect is that we can simply pass a pointer to the struct and it translates perfectly to a glm::vec3/2 array which // again translates to 3/2 floats which translates to a byte array. // Marshal.SizeOf(typeof(Vertex)) Gl.BufferData(BufferTargetARB.ArrayBuffer, (uint)(this.vertices.Count * Vertex.getSizeOf()), verticesLock.Address, BufferUsageARB.StaticDraw); Gl.BindBuffer(BufferTargetARB.ElementArrayBuffer, this.EBO); Gl.BufferData(BufferTargetARB.ElementArrayBuffer, (uint)(this.indices.Count * sizeof(uint)), indicesLock.Address, BufferUsageARB.StaticDraw); // Set the vertex attribute pointers // Vertex Positions Gl.EnableVertexAttribArray(0); Gl.VertexAttribPointer(0, 3, Gl.FLOAT, false, Vertex.getSizeOf(), (IntPtr)0); // Vertex Normals Gl.EnableVertexAttribArray(1); Gl.VertexAttribPointer(1, 3, Gl.FLOAT, false, Vertex.getSizeOf(), (IntPtr)Vertex.getNormalOffset()); // Vertex Texture Coords Gl.EnableVertexAttribArray(2); Gl.VertexAttribPointer(2, 2, Gl.FLOAT, false, Vertex.getSizeOf(), (IntPtr)Vertex.getTexCoordsOffset()); Gl.BindVertexArray(0); } }
public void updateAttr(int _maxRenderTime = -1) { particleCount = md.particleCount; if (particleCount > MainModel.ins.configModel.maxParticleCount) { particleCount = MainModel.ins.configModel.maxParticleCount; } if (md.particleCount <= 0) { return; } if (_maxRenderTime > 0) { maxRenderTime = _maxRenderTime; } var rootMd = MainModel.ins.particleEditModel; //md. Random rand = null; if (!rootMd.isSeedAuto) { nowSeed = rootMd.seed; } else if (nowSeed < 0) { nowSeed = new Random().Next(); } rand = new Random(nowSeed); //int count = _ArrayIndex.Length; //int idxCount = count / 2; const int attrCount = 14; float[] _arrParticleAttr = new float[particleCount * attrCount]; for (int i = 0; i < particleCount; ++i) { int x = i * 2; int y = x + 1; float ir0 = (float)rand.NextDouble(); float ir1 = (float)rand.NextDouble(); float ir2 = (float)rand.NextDouble(); float ir3 = (float)rand.NextDouble(); float ir4 = (float)rand.NextDouble(); float ir5 = (float)rand.NextDouble(); float ir6 = (float)rand.NextDouble(); float ir7 = (float)rand.NextDouble(); float ir8 = (float)rand.NextDouble(); //pos float px = md.xFloat * (ir0 - 0.5f) * 2; float py = md.yFloat * (ir1 - 0.5f) * 2; px = md.x + px; py = md.y + py; //speed float speed = md.startSpeed + md.startSpeedFloat * (ir2 - 0.5f) * 2; float sAngle = md.startSpeedAngle - 90 + md.startSpeedAngleFloat * (ir3 - 0.5f) * 2; float speedx = (float)(speed * Math.Cos(sAngle / 180 * Math.PI)); float speedy = (float)(speed * Math.Sin(sAngle / 180 * Math.PI)); //aspeed float aSpeed = md.gravityValue; float aSAngle = md.gravityAngle - 90; float aSpeedx = (float)(aSpeed * Math.Cos(aSAngle / 180 * Math.PI)); float aSpeedy = (float)(aSpeed * Math.Sin(aSAngle / 180 * Math.PI)); //size float startSize = md.particleStartSize + md.particleStartSizeFloat * (ir4 - 0.5f) * 2; startSize = Math.Max(0, startSize); float endSize = md.particleEndSize + md.particleEndSizeFloat * (ir5 - 0.5f) * 2; endSize = Math.Max(0, endSize); endSize = (endSize - startSize) / md.particleLife; //particle angle float startAngle = md.particleStartAngle + md.particleStartAngleFloat * (ir6 - 0.5f) * 2; float ParticleRotateSpeed = md.particleRotateSpeed + md.particleRotateSpeedFloat * (ir7 - 0.5f) * 2; startAngle = startAngle / 180 * (float)Math.PI; ParticleRotateSpeed = ParticleRotateSpeed / 180 * (float)Math.PI; //alpha float startAlpha = Math.Min(1, Math.Max(0, md.startAlpha)); float endAlpha = Math.Min(1, Math.Max(0, md.endAlpha)); endAlpha = (endAlpha - startAlpha) / md.particleLife; //lifeTime float lifeTime = md.particleLife - md.particleLifeFloat * ir8; lifeTime *= 1000; //startTime float startTime = (float)(particleCount - i) / particleCount * maxRenderTime; _arrParticleAttr[i * attrCount + 0] = px; _arrParticleAttr[i * attrCount + 1] = py; _arrParticleAttr[i * attrCount + 2] = speedx; _arrParticleAttr[i * attrCount + 3] = speedy; _arrParticleAttr[i * attrCount + 4] = aSpeedx; _arrParticleAttr[i * attrCount + 5] = aSpeedy; _arrParticleAttr[i * attrCount + 6] = startSize; _arrParticleAttr[i * attrCount + 7] = endSize; _arrParticleAttr[i * attrCount + 8] = startAngle; _arrParticleAttr[i * attrCount + 9] = ParticleRotateSpeed; _arrParticleAttr[i * attrCount + 10] = startAlpha; _arrParticleAttr[i * attrCount + 11] = endAlpha; _arrParticleAttr[i * attrCount + 12] = lifeTime; _arrParticleAttr[i * attrCount + 13] = startTime; } arrParticleAttr = _arrParticleAttr; if (lockBufferData != null) { lockBufferData.Dispose(); } lockBufferData = new MemoryLock(arrParticleAttr); Gl.BindBuffer(BufferTarget.ShaderStorageBuffer, attrBufferId); //Gl.BufferData(BufferTarget.ShaderStorageBuffer, (uint)arrPointAttr.Length * sizeof(float), lockBufferData.Address, BufferUsage.DynamicDraw); Gl.BufferData(BufferTarget.ShaderStorageBuffer, (uint)arrParticleAttr.Length * sizeof(float), lockBufferData.Address, BufferUsage.StaticDraw); }