/// <summary> /// 把坐标轴画上 绿X 红Y 蓝Z /// </summary> private void drawCoorAxis() { if (axisGlProgram != null) { axisGlProgram.Bind(gl); axisGlProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); var vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, index_in_vPosition, axisData, false, 3); var colorDataBuffer = new VertexBuffer(); colorDataBuffer.Create(gl); colorDataBuffer.Bind(gl); colorDataBuffer.SetData(gl, index_in_vColor, axisColor, false, 3); // Draw the square. gl.DrawArrays(OpenGL.GL_LINES, 0, 6); // Unbind our vertex array and shader. vertexDataBuffer.Unbind(gl); colorDataBuffer.Unbind(gl); vertexBufferArray.Unbind(gl); axisGlProgram.Unbind(gl); } }
public void GenerateGeometry(GLScene scene) { my_OpenGL = scene.GL; my_Texture = new Texture() { Name = "" }; my_VertexBufferArray = new VertexBufferArray(); my_VertexBufferArray.Create(my_OpenGL); my_VertexBufferArray.Bind(my_OpenGL); CreateVertexBuffer(GLScene.positionAttribute); CreateNormalBuffer(GLScene.normalAttribute); CreateColorBuffer(GLScene.colorAttribute); CreateTextureCoordBuffer(GLScene.textureAttribute); CreateIndexBuffer(); my_VertexBufferArray.Unbind(my_OpenGL); if (my_OpenGL != null & my_UseTexture & my_TextureFile != "") { try { my_Texture.Create(my_OpenGL, my_TextureFile); my_Texture.Name = my_TextureFile; } catch (System.Exception) { MessageBox.Show("Unable to create Texture from File:" + my_TextureFile, "GeometryGL error", MessageBoxButton.OK, MessageBoxImage.Error); my_Texture = new Texture(); } } }
private void drawPoints() { if (mainGlProgram != null) { mainGlProgram.Bind(gl); mainGlProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); // 同样数组对象不用调用glVertexAttribPointer,可以用它封装好的方式 var vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, index_in_vPosition, pointData, false, 3); // Draw the square. gl.DrawArrays(OpenGL.GL_POINTS, 0, SumPoints.Count); // Unbind our vertex array and shader. vertexDataBuffer.Unbind(gl); vertexBufferArray.Unbind(gl); mainGlProgram.Unbind(gl); } }
/// <summary> /// 初始化Vbo /// </summary> private void InitVbo() { var gl = openGLControl.OpenGL; vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, vertices, false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, colors, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }
/// <summary> /// Creates the geometry for the square, also creating the vertex buffer array. /// </summary> /// <param name="gl">The OpenGL instance.</param> private void CreateVertices(OpenGL gl) { //GeneratePoints(out vertices, out colors); GenerateModel(out positions, out colors); // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, positions, false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, colors, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }
private VertexBufferArray CreateVertexBufferArray(OpenGL gl, PrimordialObject primordialObject) { vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); // for(int i = 0; i<100; i++) { // var sendDataTime = _stopwatch.ElapsedMilliseconds; gl.BufferData(OpenGL.GL_ARRAY_BUFFER, primordialObject.VertexData, OpenGL.GL_STATIC_DRAW); // File.AppendAllText(fileTitle + "Data.txt", (_stopwatch.ElapsedMilliseconds - sendDataTime).ToString() + "\n"); // } gl.VertexAttribPointer(attributeIndexPosition, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Zero); gl.EnableVertexAttribArray(0); gl.VertexAttribPointer(attributeIndexColour, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Add(IntPtr.Zero, 4 * sizeof(float))); gl.EnableVertexAttribArray(1); gl.VertexAttribPointer(attributeIndexNormal, 4, OpenGL.GL_FLOAT, false, 3 * 4 * sizeof(float), IntPtr.Add(IntPtr.Zero, 8 * sizeof(float))); gl.EnableVertexAttribArray(2); vertexDataBuffer.Unbind(gl); vertexBufferArray.Unbind(gl); return(vertexBufferArray); }
/// <summary> /// Отрисовка OpenGL-графики в форме. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OpenGLControl_OpenGLDraw(object sender, OpenGLEventArgs args) { // Get the OpenGL instance that's been passed to us. OpenGL gl = openGLControl.OpenGL; gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); if (Vertlist.Count > 0 || kei == true) { // Bind the shader, set the matrices. shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); viewMatrix = glm.translate(new mat4(1.0f), new vec3(LfRt, UpDn, Dstnc)); shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); viewMatrix = glm.translate(new mat4(1.0f), new vec3(LfRt, UpDn, Dstnc)); // Create a model matrix to make the model a little bigger. //modelMatrix = glm.rotate(mat4.identity(), rotateX, new vec3(0.1f, 0, 0)); //modelMatrix = glm.rotate(mat4.identity(), rotateY, new vec3(0, 0.1f, 0)); // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, vrcount); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shaderProgram.Unbind(gl); } }
void CreateAndPlotData(OpenGL GL, float[] raw_data, int figureID) { if (_dataTail[figureID] + raw_data.Length >= C_SAMPLES_PER_FRAME) { _dataTail[figureID] = 0; // Clear the buffer } AddVerticesToFigure(figureID, raw_data, _dataTail[figureID]); // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(GL); vertexBufferArray.Bind(GL); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(GL); vertexDataBuffer.Bind(GL); vertexDataBuffer.SetData(GL, attribute_vpos, _data[figureID], false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(GL); colourDataBuffer.Bind(GL); colourDataBuffer.SetData(GL, attribute_vcol, _dataColor[figureID], false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(GL); // Bind the shader, set the matrices. shaderProgram.Bind(GL); //shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); //shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(GL, "modelMatrix", mviewdata.to_array()); // Bind the out vertex array. vertexBufferArray.Bind(GL); // Draw the square. GL.DrawArrays(OpenGL.GL_LINE_STRIP, 0, _dataTail[figureID]); // Unbind our vertex array and shader. vertexBufferArray.Unbind(GL); shaderProgram.Unbind(GL); }
private void CreateVerticesForSquare2(OpenGL gl, float[] vert, float[] col, float[] nrm) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_VideoController"); string graphicsCard = string.Empty; foreach (ManagementObject obj in searcher.Get()) { if (obj["CurrentBitsPerPixel"] != null && obj["CurrentHorizontalResolution"] != null) { graphicsCard = obj["Name"].ToString(); } } string[] checkname1 = graphicsCard.Split(' '); List <string> checkname2 = new List <string>(checkname1); string match = checkname2.FirstOrDefault(element => element.Equals("nvidia", StringComparison.CurrentCultureIgnoreCase)); var vertices = nrm; var colors = vert; // Colors for our vertices var normals = col; if (match != null) { vertices = vert; colors = col; // Colors for our vertices normals = nrm; } // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, vertices, false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, colors, false, 3); var normalDataBuffer = new VertexBuffer(); normalDataBuffer.Create(gl); normalDataBuffer.Bind(gl); normalDataBuffer.SetData(gl, 2, normals, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }
public void Dispose() { vertexBufferArray.Bind(gl); gl.DeleteBuffers(1, new uint[] { vertexDataBuffer.VertexBufferObject }); vertexBufferArray.Unbind(gl); vertexBufferArray.Delete(gl); _openGLRenderingForm?.Dispose(); }
/// <summary> /// Creates the geometry for the square, also creating the vertex buffer array. /// </summary> /// <param name="gl">The OpenGL instance.</param> private void CreateVerticesForSquare(OpenGL gl) { var vertices = new float[18]; vertices[0] = -1.0f; vertices[1] = -1.0f; vertices[2] = 0.0f; // Bottom left corner vertices[3] = -1.0f; vertices[4] = 1.0f; vertices[5] = 0.0f; // Top left corner vertices[6] = 1.0f; vertices[7] = 1.0f; vertices[8] = 0.0f; // Top Right corner vertices[9] = 1.0f; vertices[10] = -1.0f; vertices[11] = 0.0f; // Bottom right corner vertices[12] = -1.0f; vertices[13] = -1.0f; vertices[14] = 0.0f; // Bottom left corner vertices[15] = 1.0f; vertices[16] = 1.0f; vertices[17] = 0.0f; // Top Right corner var texcoords = new float[12]; texcoords[0] = 0.0f; texcoords[1] = 0.0f; texcoords[2] = 0.0f; texcoords[3] = 1.0f; texcoords[4] = 1.0f; texcoords[5] = 1.0f; texcoords[6] = 1.0f; texcoords[7] = 0.0f; texcoords[8] = 0.0f; texcoords[9] = 0.0f; texcoords[10] = 1.0f; texcoords[11] = 1.0f; // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); texCoordsBufferArray = new VertexBufferArray(); texCoordsBufferArray.Create(gl); texCoordsBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, vertices, false, 3); var texCoordsBuffer = new VertexBuffer(); texCoordsBuffer.Create(gl); texCoordsBuffer.Bind(gl); texCoordsBuffer.SetData(gl, 1, texcoords, false, 2); // Now do the same for the colour data. /*var colourDataBuffer = new VertexBuffer(); * colourDataBuffer.Create(gl); * colourDataBuffer.Bind(gl); * colourDataBuffer.SetData(gl, 1, colors, false, 3);*/ // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); texCoordsBufferArray.Unbind(gl); }
private void OpenGL_Draw(object sender, OpenGLEventArgs args) { if (DebugHandler.Debugger.CurrentMode == dbgDebugMode.dbgBreakMode) { OpenGL gl = openGLControl.OpenGL; gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT); //modelMatrix = glm.rotate(modelMatrix, glm.radians(5f), new vec3(0f, 1f, 0f)); mat4 m = modelMatrix * coordinateSystemMatrix; mat4 mvp = projectionMatrix * viewMatrix * m; shader.Bind(gl); shader.SetUniformMatrix4(gl, "mvp", mvp.to_array()); shader.SetUniformMatrix4(gl, "m", m.to_array()); vao.Bind(gl); gl.DrawArrays(OpenGL.GL_POINTS, 0, numVertices); } }
/// <summary> /// Draws the scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Draw(OpenGL gl, float inputWidth, float inputHeight) { if (_needsRefresh) { Initialise(gl); } if (!isValid) { return; } //todo : multiscaling resolutionX = inputWidth; resolutionY = inputHeight; gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); // Bind the shader shaderProgram.Bind(gl); //pass uniforms shaderProgram.SetUniform3(gl, "iResolution", resolutionX, resolutionY, 0.0f); shaderProgram.SetUniform1(gl, "iGlobalTime", time); time += 0.1f; // Bind the out vertex array. vertexBufferArray.Bind(gl); texCoordsBufferArray.Bind(gl); //Bind Textures //var channel0Location = shaderProgram.GetUniformLocation(gl, "iChannel0"); //gl.ActiveTexture(OpenGL.GL_TEXTURE0); //gl.BindTexture(OpenGL.GL_TEXTURE_2D, _glTextureArray[0]); //gl.Uniform1(channel0Location, 0); //var channel1Location = shaderProgram.GetUniformLocation(gl, "iChannel1"); //gl.ActiveTexture(OpenGL.GL_TEXTURE1); //gl.BindTexture(OpenGL.GL_TEXTURE_2D, _glTextureArray[1]); //gl.Uniform1(channel1Location, 1); // Draw the square. gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); texCoordsBufferArray.Unbind(gl); shaderProgram.Unbind(gl); }
private void CreateVerticesForSquare(OpenGL gl) { var vertices = new float[36]; var colors = new float[36]; // Colors for our vertices colors[0] = 1.0f; colors[1] = 0.0f; colors[2] = 0.0f; vertices[0] = 0.0f; vertices[1] = 1.0f; vertices[2] = 0.0f; colors[3] = 0.0f; colors[4] = 1.0f; colors[5] = 0.0f; vertices[3] = -1.0f; vertices[4] = -1.0f; vertices[5] = 1.0f; colors[6] = 0.0f; colors[7] = 0.3f; colors[8] = 1.0f; vertices[6] = 1.0f; vertices[7] = -1.0f; vertices[8] = 1.0f; colors[9] = 1.0f; colors[10] = 0.3f; colors[11] = 0.5f; vertices[9] = 0.0f; vertices[10] = 1.0f; vertices[11] = 0.0f; colors[12] = 0.0f; colors[13] = 0.0f; colors[14] = 1.0f; vertices[12] = 1.0f; vertices[13] = -1.0f; vertices[14] = 1.0f; colors[15] = 0.3f; colors[16] = 1.0f; colors[17] = 1.0f; vertices[15] = 1.0f; vertices[16] = -1.0f; vertices[17] = -1.0f; colors[18] = 1.0f; colors[19] = 0.0f; colors[20] = 0.4f; vertices[18] = 0.0f; vertices[19] = 1.0f; vertices[20] = 0.0f; colors[21] = 0.0f; colors[22] = 1.0f; colors[23] = 0.0f; vertices[21] = 1.0f; vertices[22] = -1.0f; vertices[23] = -1.0f; colors[24] = 0.0f; colors[25] = 0.5f; colors[26] = 1.0f; vertices[24] = -1.0f; vertices[25] = -1.0f; vertices[26] = -1.0f; colors[27] = 1.0f; colors[28] = 0.0f; colors[29] = 0.0f; vertices[27] = 0.0f; vertices[28] = 1.0f; vertices[29] = 0.0f; colors[30] = 0.0f; colors[31] = 0.0f; colors[32] = 1.0f; vertices[30] = -1.0f; vertices[31] = -1.0f; vertices[32] = -1.0f; colors[33] = 0.0f; colors[34] = 1.0f; colors[35] = 0.0f; vertices[33] = -1.0f; vertices[34] = -1.0f; vertices[35] = 1.0f; // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, colors, false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, vertices, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }
//private void GeneratePoints(out float[] vertices, out float[] colors) //{ // const int length = 256 * 3; // vertices = new float[length]; colors = new float[length]; // Random random = new Random(); // int direction = this.positiveGrowth ? 1 : -1; // // points // for (int i = 0; i < length; i++) // { // vertices[i] = direction * (float)i / (float)length; // colors[i] = (float)((random.NextDouble() * 2 - 1) * 1); // //colors[i] = (float)i / (float)length; // } // //// triangles // //for (int i = 0; i < length / 9; i++) // //{ // // var x = random.NextDouble(); var y = random.NextDouble(); var z = random.NextDouble(); // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3] = (float)(x + random.NextDouble() / 5 - 1); // // } // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3 + 1] = (float)(y + random.NextDouble() / 5 - 1); // // } // // for (int j = 0; j < 3; j++) // // { // // vertices[i * 9 + j * 3 + 2] = (float)(z + random.NextDouble() / 5 - 1); // // } // //} //} #region IRenderable 成员 void IRenderable.Render(OpenGL gl, RenderMode renderMode) { // Update matrices. IScientificCamera camera = this.Camera; if (camera != null) { if (camera.CameraType == CameraTypes.Perspecitive) { IPerspectiveViewCamera perspective = camera; this.projectionMatrix = perspective.GetProjectionMat4(); this.viewMatrix = perspective.GetViewMat4(); } else if (camera.CameraType == CameraTypes.Ortho) { IOrthoViewCamera ortho = camera; this.projectionMatrix = ortho.GetProjectionMat4(); this.viewMatrix = ortho.GetViewMat4(); } else { throw new NotImplementedException(); } } modelMatrix = mat4.identity(); gl.PointSize(3); var shader = (renderMode == RenderMode.HitTest) ? pickingShaderProgram : shaderProgram; // Bind the shader, set the matrices. shader.Bind(gl); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); if (renderMode == RenderMode.HitTest) { shader.SetUniform1(gl, "pickingBaseID", ((IColorCodedPicking)this).PickingBaseID); } // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. gl.DrawArrays((uint)this.mode, 0, positions.Length / 3); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shader.Unbind(gl); }
public void Draw(ShaderProgram shader) { my_OpenGL.LineWidth(my_LineWidth); my_OpenGL.PointSize(my_PointSize); if (my_UseMaterial) { shader.SetUniform3(my_OpenGL, "material.diffuse", my_DiffuseMaterial.ScR, my_DiffuseMaterial.ScG, my_DiffuseMaterial.ScB); shader.SetUniform3(my_OpenGL, "material.ambient", my_AmbientMaterial.ScR, my_AmbientMaterial.ScG, my_AmbientMaterial.ScB); } else { shader.SetUniform3(my_OpenGL, "material.diffuse", 0.0F, 0.0F, 0.0F); shader.SetUniform3(my_OpenGL, "material.ambient", 0.0F, 0.0F, 0.0F); } shader.SetUniform3(my_OpenGL, "material.specular", my_SpecularMaterial.ScR, my_SpecularMaterial.ScG, my_SpecularMaterial.ScB); shader.SetUniform1(my_OpenGL, "material.shininess", my_Shininess); shader.SetUniformMatrix4(my_OpenGL, "Model", GetModelMatrix().to_array()); shader.SetUniformMatrix3(my_OpenGL, "NormalMatrix", GetNormalMatrix().to_array()); my_Texture.Bind(my_OpenGL); my_VertexBufferArray.Bind(my_OpenGL); switch (my_DrawMode) { case DrawMode.Fill: my_OpenGL.PolygonMode(FaceMode.FrontAndBack, PolygonMode.Filled); break; case DrawMode.Lines: my_OpenGL.PolygonMode(FaceMode.FrontAndBack, PolygonMode.Lines); break; case DrawMode.Points: my_OpenGL.PolygonMode(FaceMode.FrontAndBack, PolygonMode.Points); break; } switch (GLBeginMode) { case BeginMode.Triangles: my_OpenGL.DrawElements(OpenGL.GL_TRIANGLES, my_Indices.Length, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); break; case BeginMode.Lines: my_OpenGL.DrawElements(OpenGL.GL_LINES, my_Indices.Length, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); break; case BeginMode.Points: my_OpenGL.DrawElements(OpenGL.GL_POINTS, my_Indices.Length, OpenGL.GL_UNSIGNED_INT, IntPtr.Zero); break; } my_VertexBufferArray.Unbind(my_OpenGL); }
public void Create(OpenGL GL, string texPath, GShaderProgram shader) { vec3[] points = new vec3[] { /*new vec3(-C_END, -C_END, 0), new vec3(-C_END, C_END, 0), new vec3(C_END, C_END, 0), new vec3(C_END, C_END, 0) */ new vec3(C_BOX_END, C_BOX_END, 0), new vec3(C_BOX_END, -C_BOX_END, 0), new vec3(-C_BOX_END, C_BOX_END, 0), new vec3(-C_BOX_END, -C_BOX_END, 0) }; vec3[] normals = new vec3[points.Length]; for (int i = 0; i < normals.Length; i++) { normals[i] = new vec3(0.0f, 0.0f, -1.0f); } vec2[] texCord = new vec2[] { new vec2(0.0f, 1.0f), new vec2(0.0f, 0.0f), new vec2(1.0f, 1.0f), new vec2(1.0f, 0.0f) }; if (texPath == null) { _textureName = C_DUMMY_TEXTURE; TextureManager.Instance.CreateTexture1x1(C_DUMMY_TEXTURE, GL, false, Color.Blue); } else { _textureName = texPath; TextureManager.Instance.CreateTexture(texPath, GL, false); } arrowVAO = new VertexBufferArray(); arrowVAO.Create(GL); arrowVAO.Bind(GL); VertexBuffer[] cVBO = new VertexBuffer[3]; cVBO[0] = new VertexBuffer(); cVBO[0].Create(GL); cVBO[0].Bind(GL); cVBO[0].SetData(GL, (uint)shader.GetAttributeID(GL, "vPosition"), points, false, 3); // Texture cVBO[1] = new VertexBuffer(); cVBO[1].Create(GL); cVBO[1].Bind(GL); cVBO[1].SetData(GL, (uint)shader.GetAttributeID(GL, "vTextureCoord"), texCord, false, 2); // Normals cVBO[2] = new VertexBuffer(); cVBO[2].Create(GL); cVBO[2].Bind(GL); cVBO[2].SetData(GL, (uint)shader.GetAttributeID(GL, "vSurfaceNormal"), normals, false, 3); arrowVAO.Unbind(GL); }
public void Create(OpenGL GL, string texPath, GShaderProgram shader) { CircleData c = DrawCircle(0f, 0f, 1f, _numSegments); vec3[] points = c.points; vec3[] normals = new vec3[points.Length]; vec2[] texCord = c.texture; for (int i = 0; i < normals.Length; i++) { normals[i] = new vec3(0.0f, 0.0f, -1.0f); } if (texPath == null || texPath == "") { _textureName = C_DUMMY_TEXTURE; TextureManager.Instance.CreateTexture1x1(C_DUMMY_TEXTURE, GL, false, Color.Blue); } else { _textureName = texPath; TextureManager.Instance.CreateTexture(texPath, GL, false); } compassVAO = new VertexBufferArray(); compassVAO.Create(GL); compassVAO.Bind(GL); VertexBuffer[] cVBO = new VertexBuffer[3]; cVBO[0] = new VertexBuffer(); cVBO[0].Create(GL); cVBO[0].Bind(GL); cVBO[0].SetData(GL, (uint)shader.GetAttributeID(GL, "vPosition"), points, false, 3); // Texture cVBO[1] = new VertexBuffer(); cVBO[1].Create(GL); cVBO[1].Bind(GL); cVBO[1].SetData(GL, (uint)shader.GetAttributeID(GL, "vTextureCoord"), texCord, false, 2); // Normals cVBO[2] = new VertexBuffer(); cVBO[2].Create(GL); cVBO[2].Bind(GL); cVBO[2].SetData(GL, (uint)shader.GetAttributeID(GL, "vSurfaceNormal"), normals, false, 3); compassVAO.Unbind(GL); }
/// <summary> /// Draws the scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Draw(OpenGL gl, float width, float height) { if (needsRefresh) { Initialise(gl); } resolutionX = width; resolutionY = height; // Clear the scene. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); // Bind the shader, set the matrices. shaderProgram.Bind(gl); shaderProgram.SetUniform3(gl, "iResolution", resolutionX, resolutionY, 0.0f); shaderProgram.SetUniform1(gl, "iGlobalTime", time); time += 0.1f; // Bind the out vertex array. vertexBufferArray.Bind(gl); texCoordsBufferArray.Bind(gl); //Bind Textures var ch0loc = shaderProgram.GetUniformLocation(gl, "iChannel0"); gl.ActiveTexture(OpenGL.GL_TEXTURE0); gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[0]); gl.Uniform1(ch0loc, 0); var ch1loc = shaderProgram.GetUniformLocation(gl, "iChannel1"); gl.ActiveTexture(OpenGL.GL_TEXTURE1); gl.BindTexture(OpenGL.GL_TEXTURE_2D, glTextureArray[1]); gl.Uniform1(ch1loc, 1); // Draw the square. gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); texCoordsBufferArray.Unbind(gl); shaderProgram.Unbind(gl); }
private void ogl_OpenGLInitialized(object sender, OpenGLRoutedEventArgs args) { var gl = args.OpenGL; gl.GenTextures(3, _textures); _fbos[0] = _fbos[1] = _fbos[2] = _fbos[3] = _fbos[4] = 0; _textures[3] = _textures[4] = _textures[5] = _textures[6] = _textures[7] = 0; try { _blurShader1 = new ShaderProgram(); var frag = ReadResource(@"LibDmd.Output.Virtual.Dmd.Blur.frag") + "void main() { FragColor = vec4(blur_level_2(texture, uv, direction).rgb, 1.0); }"; _blurShader1.Create(gl, ReadResource(@"LibDmd.Output.Virtual.Dmd.Blur.vert"), frag, _attributeLocations); _bs1Texture = _blurShader1.GetUniformLocation(gl, "texture"); _bs1Direction = _blurShader1.GetUniformLocation(gl, "direction"); } catch (ShaderCompilationException e) { Logger.Error(e, "Blur Shader 1 compilation failed"); Logger.Error(e.CompilerOutput); } try { _blurShader2 = new ShaderProgram(); var frag = ReadResource(@"LibDmd.Output.Virtual.Dmd.Blur.frag") + "void main() { FragColor = vec4(blur_level_12(texture, uv, direction).rgb, 1.0); }"; _blurShader2.Create(gl, ReadResource(@"LibDmd.Output.Virtual.Dmd.Blur.vert"), frag, _attributeLocations); _bs2Texture = _blurShader2.GetUniformLocation(gl, "texture"); _bs2Direction = _blurShader2.GetUniformLocation(gl, "direction"); } catch (ShaderCompilationException e) { Logger.Error(e, "Blur Shader 2 compilation failed"); Logger.Error(e.CompilerOutput); } _quadVbo = new VertexBufferArray(); _quadVbo.Create(gl); _quadVbo.Bind(gl); var posVBO = new VertexBuffer(); posVBO.Create(gl); posVBO.Bind(gl); posVBO.SetData(gl, PositionAttribute, new float[] { -1f, -1f, -1f, 1f, 1f, 1f, 1f, -1f }, false, 2); var texVBO = new VertexBuffer(); texVBO.Create(gl); texVBO.Bind(gl); texVBO.SetData(gl, TexCoordAttribute, new float[] { 0f, 1f, 0f, 0f, 1f, 0f, 1f, 1f }, false, 2); _quadVbo.Unbind(gl); }
public void GenerateGeometry(OpenGL gl, uint vertexAttributeLocation, uint normalAttributeLocation) { // Create the vertex array object. This will hold the state of all of the // vertex buffer operations that follow it's binding, meaning instead of setting the // vertex buffer data and binding it each time, we can just bind the array and call // DrawElements - much easier. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Generate the vertices and normals. CreateVertexNormalBuffer(gl, vertexAttributeLocation, normalAttributeLocation); CreateIndexBuffer(gl); vertexBufferArray.Unbind(gl); }
public void Render(OpenGL GL, GShaderProgram shader) { compassVAO.Bind(GL); TexContainer tc; tc = TextureManager.Instance.GetElement(_textureName); tc.Tex.Bind(GL); // Bind to the current texture on texture unit 0 GL.ActiveTexture(OpenGL.GL_TEXTURE0 + (uint)tc.ID); shader.SetUniform(GL, "uSampler", tc.ID); // Turn ON aplha blending for the arrow so the empty background is ignored GL.Enable(OpenGL.GL_BLEND); GL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); GL.DrawArrays(OpenGL.GL_TRIANGLE_FAN, 0, _numSegments); GL.Disable(OpenGL.GL_BLEND); compassVAO.Unbind(GL); }
public void Render(OpenGL GL, GShaderProgram shader) { pointVAO.Bind(GL); GL.PointSize(Size); // todo: modify to use gl_PointSize = 10.0; in the vertex shader instead GL.Enable(OpenGL.GL_POINT_SPRITE_ARB); // MUST be enabled for gl_PointCord to be available in the frag shader GL.Enable(OpenGL.GL_POINT_SIZE); GL.Enable(OpenGL.GL_BLEND); GL.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA); LoadData(GL, shader, Pos, Color); GL.DrawArrays(OpenGL.GL_POINTS, 0, Pos.Length); GL.Disable(OpenGL.GL_POINT_SIZE); GL.Disable(OpenGL.GL_POINT_SPRITE_ARB); GL.PointSize(1); pointVAO.Unbind(GL); }
/// <summary> /// Draws the scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Draw(OpenGL gl, RenderMode renderMode = RenderMode.Render) { var shader = (renderMode == RenderMode.HitTest) ? pickingShaderProgram : shaderProgram; if (renderMode == RenderMode.HitTest) { gl.ClearColor(1, 1, 1, 1); } else { // Set a blue clear colour. gl.ClearColor(0.4f, 0.6f, 0.9f, 0.5f); } // Clear the scene. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); gl.PointSize(3); // Update matrices. IPerspectiveCamera camera = this.cameraRotation.Camera; projectionMatrix = camera.GetProjectionMat4(); viewMatrix = this.cameraRotation.Camera.GetViewMat4(); modelMatrix = mat4.identity(); // Bind the shader, set the matrices. shader.Bind(gl); shader.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shader.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shader.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. //gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, vertices.Length); gl.DrawArrays(OpenGL.GL_POINTS, 0, vertices.Length); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shader.Unbind(gl); gl.Flush(); }
public void GenerateGeometry(OpenGL gl) { vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); VertexBuffer vb = new VertexBuffer(); vb.Create(gl); vb.Bind(gl); vb.SetData(gl, 0, vertices.SelectMany(v => ((vec3)v).ToArray()).ToArray(), false, 3); IndexBuffer ib = new IndexBuffer(); ib.Create(gl); ib.Bind(gl); ib.SetData(gl, indices); }
public void Create(OpenGL GL, GShaderProgram shader, vec3[] vertex, vec4[] color, float size) { Pos = vertex; Color = color; Size = size; pointVAO = new VertexBufferArray(); pointVAO.Create(GL); pointVAO.Bind(GL); pVBO = new VertexBuffer[2]; pVBO[0] = new VertexBuffer(); pVBO[0].Create(GL); pVBO[1] = new VertexBuffer(); pVBO[1].Create(GL); LoadData(GL, shader, Pos, Color); pointVAO.Unbind(GL); }
public virtual void CreateVerticesForSquare(ref DataForDraw Data) { // Create the vertex array object. vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, Data.vertices_arrayed, false, 3); // Now do the same for the colour data. colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, Data.colours_arrayed, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); vertexDataBuffer.Unbind(gl); colourDataBuffer.Unbind(gl); }
/// <summary> /// Creates the geometry for the square, also creating the vertex buffer array. /// </summary> /// <param name="gl">The OpenGL instance.</param> private void CreateVerticesForSquare(OpenGL gl) { var vertices = new float[18]; var colors = new float[18]; // Colors for our vertices vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = 0.0f; // Bottom left corner colors[0] = 1.0f; colors[1] = 1.0f; colors[2] = 1.0f; // Bottom left corner vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = 0.0f; // Top left corner colors[3] = 1.0f; colors[4] = 0.0f; colors[5] = 0.0f; // Top left corner vertices[6] = 0.5f; vertices[7] = 0.5f; vertices[8] = 0.0f; // Top Right corner colors[6] = 0.0f; colors[7] = 1.0f; colors[8] = 0.0f; // Top Right corner vertices[9] = 0.5f; vertices[10] = -0.5f; vertices[11] = 0.0f; // Bottom right corner colors[9] = 0.0f; colors[10] = 0.0f; colors[11] = 1.0f; // Bottom right corner vertices[12] = -0.5f; vertices[13] = -0.5f; vertices[14] = 0.0f; // Bottom left corner colors[12] = 1.0f; colors[13] = 1.0f; colors[14] = 1.0f; // Bottom left corner vertices[15] = 0.5f; vertices[16] = 0.5f; vertices[17] = 0.0f; // Top Right corner colors[15] = 0.0f; colors[16] = 1.0f; colors[17] = 0.0f; // Top Right corner // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, vertices); vertexDataBuffer.SetAttributeData(gl, 0, 3, false); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, colors); colourDataBuffer.SetAttributeData(gl, 1, 3, false); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }
/// <summary> /// Draws the scene. /// </summary> /// <param name="gl">The OpenGL instance.</param> public void Draw(OpenGL gl) { // Clear the scene. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT | OpenGL.GL_STENCIL_BUFFER_BIT); // Bind the shader, set the matrices. shaderProgram.Bind(gl); shaderProgram.SetUniformMatrix4(gl, "projectionMatrix", projectionMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "viewMatrix", viewMatrix.to_array()); shaderProgram.SetUniformMatrix4(gl, "modelMatrix", modelMatrix.to_array()); // Bind the out vertex array. vertexBufferArray.Bind(gl); // Draw the square. gl.DrawArrays(OpenGL.GL_TRIANGLES, 0, 6); // Unbind our vertex array and shader. vertexBufferArray.Unbind(gl); shaderProgram.Unbind(gl); }
private void CreateVertexBufferArray(OpenGL gl, Mesh mesh) { // Create and bind a vertex buffer array. var vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertices. var verticesVertexBuffer = new VertexBuffer(); verticesVertexBuffer.Create(gl); verticesVertexBuffer.Bind(gl); verticesVertexBuffer.SetData(gl, VertexAttributes.Position, mesh.vertices.SelectMany(v => v.to_array()).ToArray(), false, 3); if (mesh.normals != null) { var normalsVertexBuffer = new VertexBuffer(); normalsVertexBuffer.Create(gl); normalsVertexBuffer.Bind(gl); normalsVertexBuffer.SetData(gl, VertexAttributes.Normal, mesh.normals.SelectMany(v => v.to_array()).ToArray(), false, 3); } if (mesh.uvs != null) { var texCoordsVertexBuffer = new VertexBuffer(); texCoordsVertexBuffer.Create(gl); texCoordsVertexBuffer.Bind(gl); texCoordsVertexBuffer.SetData(gl, VertexAttributes.TexCoord, mesh.uvs.SelectMany(v => v.to_array()).ToArray(), false, 2); } // We're done creating the vertex buffer array - unbind it and add it to the dictionary. verticesVertexBuffer.Unbind(gl); meshVertexBufferArrays[mesh] = vertexBufferArray; }
/// <summary> /// Creates the geometry for the square, also creating the vertex buffer array. /// </summary> /// <param name="gl">The OpenGL instance.</param> private void CreateVerticesForSquare(OpenGL gl) { var vertices = new float[18]; var colors = new float[18]; // Colors for our vertices vertices[0] = -0.5f; vertices[1] = -0.5f; vertices[2] = 0.0f; // Bottom left corner colors[0] = 1.0f; colors[1] = 1.0f; colors[2] = 1.0f; // Bottom left corner vertices[3] = -0.5f; vertices[4] = 0.5f; vertices[5] = 0.0f; // Top left corner colors[3] = 1.0f; colors[4] = 0.0f; colors[5] = 0.0f; // Top left corner vertices[6] = 0.5f; vertices[7] = 0.5f; vertices[8] = 0.0f; // Top Right corner colors[6] = 0.0f; colors[7] = 1.0f; colors[8] = 0.0f; // Top Right corner vertices[9] = 0.5f; vertices[10] = -0.5f; vertices[11] = 0.0f; // Bottom right corner colors[9] = 0.0f; colors[10] = 0.0f; colors[11] = 1.0f; // Bottom right corner vertices[12] = -0.5f; vertices[13] = -0.5f; vertices[14] = 0.0f; // Bottom left corner colors[12] = 1.0f; colors[13] = 1.0f; colors[14] = 1.0f; // Bottom left corner vertices[15] = 0.5f; vertices[16] = 0.5f; vertices[17] = 0.0f; // Top Right corner colors[15] = 0.0f; colors[16] = 1.0f; colors[17] = 0.0f; // Top Right corner // Create the vertex array object. vertexBufferArray = new VertexBufferArray(); vertexBufferArray.Create(gl); vertexBufferArray.Bind(gl); // Create a vertex buffer for the vertex data. var vertexDataBuffer = new VertexBuffer(); vertexDataBuffer.Create(gl); vertexDataBuffer.Bind(gl); vertexDataBuffer.SetData(gl, 0, vertices, false, 3); // Now do the same for the colour data. var colourDataBuffer = new VertexBuffer(); colourDataBuffer.Create(gl); colourDataBuffer.Bind(gl); colourDataBuffer.SetData(gl, 1, colors, false, 3); // Unbind the vertex array, we've finished specifying data for it. vertexBufferArray.Unbind(gl); }