یکی دو سال پیش تو یه پروژه نیاز بود که بتونم دادهها رو نمایش بدم و البته بر اساس مقدار رنگآمیزی کنم. همون موقع تونستم با استفاده از OpenTK و راهنمایی این پست این کار رو انجام بدم. بعد هم کدی که تو C# نوشته بودم رو اینجا پست کردم. امروز دنبال اون کد میگشتم و فکر کردم بهتره تو آرشیو وبلاگ هم داشته باشمش. بنابر این کد رو اینجا هم میذارم. توضیحاتش رو هم تو پست اصلی بخونین.
[csharp]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
bool loaded = false;
int texture;
void CreateTextureObject()
{
byte[,] Texture8 = new byte[,]
{
{ ۰۰۰, ۰۰۰, ۲۵۵ },
{ ۰۰۰, ۲۵۵, ۲۵۵ },
{ ۰۰۰, ۲۵۵, ۰۰۰ },
{ ۲۵۵, ۲۵۵, ۰۰۰ },
{ ۲۵۵, ۰۰۰, ۰۰۰ }
};
GL.Enable(EnableCap.Texture1D);
// Set pixel storage mode
GL.PixelStore(PixelStoreParameter.UnpackAlignment, 1);
// Generate a texture name
texture = GL.GenTexture();
// Create a texture object
GL.BindTexture(TextureTarget.ProxyTexture1D, texture);
GL.TexParameter(TextureTarget.Texture1D,
TextureParameterName.TextureMagFilter,
(int)All.Nearest);
GL.TexParameter(TextureTarget.Texture1D,
TextureParameterName.TextureMinFilter,
(int)All.Nearest);
GL.TexImage1D(TextureTarget.Texture1D, 0,
PixelInternalFormat.Three, /*with*/5, 0,
PixelFormat.Rgb,
PixelType.UnsignedByte, Texture8);
}
public Form1()
{
InitializeComponent();
}
private void glControl1_Load(object sender, EventArgs e)
{
loaded = true;
GL.ClearColor(Color.SkyBlue);
SetupViewport();
CreateTextureObject();
}
private void SetupViewport()
{
int w = glControl1.Width;
int h = glControl1.Height;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
// Bottom-left corner pixel has coordinate (0, 0)
GL.Ortho(0, w, 0, h, -1, 1);
// Use all of the glControl painting area
GL.Viewport(0, 0, w, h);
}
private void glControl1_Paint(object sender, PaintEventArgs e)
{
if (!loaded)
return;
GL.Clear(ClearBufferMask.ColorBufferBit |
ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.BindTexture(TextureTarget.ProxyTexture1D, texture);
GL.Begin(BeginMode.Triangles);
GL.TexCoord1(0.0);
GL.Vertex2(10, 10);
GL.TexCoord1(0.5);
GL.Vertex2(200, 10);
GL.TexCoord1(1.0);
GL.Vertex2(10, 200);
GL.End();
glControl1.SwapBuffers();
}
}
}
[/csharp]