Created
February 10, 2015 16:05
-
-
Save AntonNik0laev/55e2ddba436f229a533d to your computer and use it in GitHub Desktop.
opengl random chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Runtime.InteropServices; | |
using System.Text; | |
using System.Windows.Forms; | |
using SharpGL; | |
using SharpGL.SceneGraph.Shaders; | |
namespace SharpGLWinformsApplication1 | |
{ | |
public enum ShaderType : uint | |
{ | |
VertexShader = OpenGL.GL_VERTEX_SHADER, | |
FragmentShader = OpenGL.GL_FRAGMENT_SHADER | |
} | |
public class ShaderInfo | |
{ | |
public string FileName { get; set; } | |
public ShaderType ShaderType { get; set; } | |
} | |
struct Point | |
{ | |
public double X; | |
public double Y; | |
} | |
/// <summary> | |
/// The main form class. | |
/// </summary> | |
public partial class SharpGLForm : Form | |
{ | |
/// <summary> | |
/// Initializes a new instance of the <see cref="SharpGLForm"/> class. | |
/// </summary> | |
public SharpGLForm() | |
{ | |
InitializeComponent(); | |
} | |
private uint mainVertexArray; | |
private float[] mainArrayBuffer; | |
private double currentMinValue; | |
private double currentMaxValue; | |
private double[] generatedValues = new double[200000]; | |
private uint CompileShaders(OpenGL gl, IEnumerable<ShaderInfo> shaders) | |
{ | |
var program = gl.CreateProgram(); | |
foreach (var shaderInfo in shaders) | |
{ | |
var shaderSource = File.ReadAllText(shaderInfo.FileName); | |
var shader = gl.CreateShader((uint)shaderInfo.ShaderType); | |
gl.ShaderSource(shader, shaderSource); | |
gl.CompileShader(shader); | |
gl.AttachShader(program, shader); | |
} | |
gl.LinkProgram(program); | |
return program; | |
} | |
/// <summary> | |
/// Handles the OpenGLDraw event of the openGLControl control. | |
/// </summary> | |
/// <param name="sender">The source of the event.</param> | |
/// <param name="e">The <see cref="RenderEventArgs"/> instance containing the event data.</param> | |
private void openGLControl_OpenGLDraw(object sender, RenderEventArgs e) | |
{ | |
var gl = openGLControl.OpenGL; | |
gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT); | |
gl.BindVertexArray(mainVertexArray); | |
gl.DrawArrays(OpenGL.GL_POINTS, 0, mainArrayBuffer.Length / 2); | |
} | |
private void InitGl() | |
{ | |
var gl = openGLControl.OpenGL; | |
var arr = new uint[1]; | |
gl.GenVertexArrays(1, arr); | |
mainVertexArray = arr[0]; | |
gl.BindVertexArray(mainVertexArray); | |
var random = new Random(DateTime.Now.Millisecond); | |
generatedValues[0] = random.NextDouble() - 0.5; | |
currentMinValue = generatedValues[0]; | |
currentMaxValue = generatedValues[0]; | |
for (int i = 1; i < generatedValues.Length; i++) | |
{ | |
generatedValues[i] = generatedValues[i-1] + random.NextDouble() - 0.5; | |
if (generatedValues[i] > currentMaxValue) | |
currentMaxValue = generatedValues[i]; | |
if (generatedValues[i] < currentMinValue) | |
currentMinValue = generatedValues[i]; | |
} | |
var middle = (currentMinValue + currentMaxValue) /2; | |
var sum = (Math.Abs(currentMinValue) + Math.Abs(currentMaxValue)) / 2; | |
for (int i = 0; i < generatedValues.Length; i++) | |
{ | |
generatedValues[i] -= middle; | |
generatedValues[i] = generatedValues[i] / sum; | |
} | |
mainArrayBuffer = new float[generatedValues.Length * 2]; | |
for (int i = 0; i < mainArrayBuffer.Length; i+=2) | |
{ | |
float x = (float)((i - (mainArrayBuffer.Length / 2.0)) / (mainArrayBuffer.Length / 2.0)); | |
mainArrayBuffer[i] = x; | |
mainArrayBuffer[i+1] = (float)generatedValues[i /2 ]; | |
} | |
var buffers = new uint[1]; | |
gl.GenBuffers(1, buffers); | |
gl.BindBuffer(OpenGL.GL_ARRAY_BUFFER, buffers[0]); | |
gl.BufferData(OpenGL.GL_ARRAY_BUFFER, mainArrayBuffer, OpenGL.GL_STATIC_DRAW); | |
var program = CompileShaders(gl, new[] | |
{ | |
new ShaderInfo() | |
{ | |
FileName = @"C:\temp\test.vert", | |
ShaderType = ShaderType.VertexShader | |
}, | |
new ShaderInfo() | |
{ | |
FileName = @"c:\temp\test.frag", | |
ShaderType = ShaderType.FragmentShader | |
} | |
}); | |
gl.UseProgram(program); | |
gl.VertexAttribPointer(0, 2, OpenGL.GL_FLOAT, false, 0, IntPtr.Zero); | |
gl.EnableVertexAttribArray(0); | |
} | |
/// <summary> | |
/// Handles the OpenGLInitialized event of the openGLControl control. | |
/// </summary> | |
/// <param name="sender">The source of the event.</param> | |
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> | |
private void openGLControl_OpenGLInitialized(object sender, EventArgs e) | |
{ | |
// TODO: Initialise OpenGL here. | |
InitGl(); | |
} | |
/// <summary> | |
/// Handles the Resized event of the openGLControl control. | |
/// </summary> | |
/// <param name="sender">The source of the event.</param> | |
/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> | |
private void openGLControl_Resized(object sender, EventArgs e) | |
{ | |
// TODO: Set the projection matrix here. | |
// Get the OpenGL object. | |
OpenGL gl = openGLControl.OpenGL; | |
// Set the projection matrix. | |
//gl.MatrixMode(OpenGL.GL_PROJECTION); | |
// Load the identity. | |
//gl.LoadIdentity(); | |
// Create a perspective transformation. | |
//gl.Perspective(60.0f, (double)Width / (double)Height, 0.01, 100.0); | |
// Use the 'look at' helper function to position and aim the camera. | |
//gl.LookAt(-5, 5, -5, 0, 0, 0, 0, 1, 0); | |
// Set the modelview matrix. | |
//gl.MatrixMode(OpenGL.GL_MODELVIEW); | |
} | |
/// <summary> | |
/// The current rotation. | |
/// </summary> | |
private float rotation = 0.0f; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment