Näitä algoritmeja käytettäessä saadaan aikaan Lensflare imaget pelejä varten. Kuvat tulostuvat bittikarttaan, joka näytetään formilla.
Tästä oppii, kuinka luodaan matemaattisesta kaavasta kuva.
Koodi on suht yksinkertaista, joten jätin kommentoinnin mitättömälle tasolle. Olen käyttänyt tätä sivustoa apuna: http://www.blackpawn.com/texts/lensflare/default.html
Jos kaipaat lisäselityksiä vinkkiin, käy siellä.
Public Class frmFlare Inherits System.Windows.Forms.Form #Region " Windows Form Designer generated code " Public Sub New() MyBase.New() 'This call is required by the Windows Form Designer. InitializeComponent() 'Add any initialization after the InitializeComponent() call End Sub 'Form overrides dispose to clean up the component list. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub 'Required by the Windows Form Designer Private components As System.ComponentModel.IContainer 'NOTE: The following procedure is required by the Windows Form Designer 'It can be modified using the Windows Form Designer. 'Do not modify it using the code editor. Friend WithEvents picSurface As System.Windows.Forms.PictureBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.picSurface = New System.Windows.Forms.PictureBox Me.SuspendLayout() ' 'picSurface ' Me.picSurface.Location = New System.Drawing.Point(0, 0) Me.picSurface.Name = "picSurface" Me.picSurface.Size = New System.Drawing.Size(376, 320) Me.picSurface.TabIndex = 0 Me.picSurface.TabStop = False ' 'frmFlare ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(376, 318) Me.Controls.Add(Me.picSurface) Me.Name = "frmFlare" Me.Text = "LensFlare" Me.ResumeLayout(False) End Sub #End Region Private Sub frmFlare_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load ' pääbittikartta Dim b As New Bitmap(picSurface.Width, picSurface.Height) Dim g As Graphics = Graphics.FromImage(b) ' säde Dim rad As Double ' counttereita Dim x, y, i, a As Integer ' värit Dim c1, c2, c3, c4 As Double 'taulukko spark emitter kuviota varten Dim bi(b.Width, b.Height) As Double ' kuvio mapit Dim b1 As New Bitmap(111, 111) Dim b2 As New Bitmap(111, 111) Dim b3 As New Bitmap(111, 111) Dim b4 As New Bitmap(111, 111) ' partikkelien määrä For i = 0 To 100 Dim angle As Double = Rnd() * 2 * Math.PI Dim dx As Double = Math.Cos(angle) Dim dy As Double = Math.Sin(angle) Dim fx As Double = 50 Dim fy As Double = 50 ' trailien pituus For a = 0 To 40 For y = -1 To 1 For x = -1 To 1 Dim r2 As Double = x * x + y * y ' partikkelin kaava c1 = 1 - r2 / (2 * 2) c1 = c1 * c1 c1 = c1 * c1 ' Lisätään väri taulukkoon partikkelin väri(blendauksen takia) bi(x + fx, y + fy) += c1 / a Next Next fx += dx fy += dy Next Next ' renderöi spark emitteri For x = 1 To 110 For y = 1 To 110 If bi(x, y) > 1 Then bi(x, y) = 1 b1.SetPixel(x, y, Color.FromArgb(bi(x, y) * 255, bi(x, y) * 255, bi(x, y) * 255)) Next Next Dim R As Double = Math.Min(b2.Width, b2.Height) / 2 For x = 1 To 110 For y = 1 To 110 Dim dx As Integer = R - x Dim dy As Integer = R - y rad = Math.Sqrt(dx * dx + dy * dy) / R ' (1-r)^2 c2 = 1 - rad c2 = c2 * c2 c2 *= 1 - pehmennys(1 - 0.01, 1 + 0.01, rad) ' r c3 = rad c3 *= 1 - pehmennys(1 - 0.01, 1 + 0.01, rad) ' r^6 c4 = rad * rad c4 = c4 * c4 c4 = c4 * c4 * c4 c4 *= 1 - pehmennys(1 - 0.01, 1 + 0.01, rad) ' piirretään pikseli b2.SetPixel(x, y, Color.FromArgb(c2 * 255, c2 * 255, c2 * 255)) b3.SetPixel(x, y, Color.FromArgb(c3 * 255, c3 * 255, c3 * 255)) b4.SetPixel(x, y, Color.FromArgb(c4 * 255, c4 * 255, c4 * 255)) Next Next g.DrawImage(b1, 0, 0) g.DrawImage(b2, 120, 0) g.DrawImage(b3, 240, 0) g.DrawImage(b4, 0, 120) picSurface.BackgroundImage = b End Sub ' Kätevä pehmennys funktio Function pehmennys(ByVal a, ByVal b, ByVal x) If x < a Then Return 0 If x >= b Then Return 1 x = (x - a) / (b - a) Return (x * x) * (3 - 2 * x) End Function End Class
Prkl tätä .nettiä riittää, binäärii?
Mistä ihmeestä noi tarvittavat systeemit(framework vai mikä se oli) saa? Suora likki kiitos.
http://www.microsoft.com/downloads/details.aspx?
Paina download nappia.
Aihe on jo aika vanha, joten et voi enää vastata siihen.