Вход

Моделирование движения небесных тел

Рекомендуемая категория для самостоятельной подготовки:
Курсовая работа*
Код 89383
Дата создания 2015
Страниц 49
Мы сможем обработать ваш заказ (!) 1 апреля в 12:00 [мск]
Файлы будут доступны для скачивания только после обработки заказа.
2 360руб.
КУПИТЬ

Содержание

Оглавление
Введение 2
Трехмерная графика 3
Описание работы программы 4
Процесс преобразования координат 5
Описание классов и методов в программе 7
Описание файлового формата моделей 11
Интерфейс программы 13
Список литературы 14
Исходный код 15
ObjLoader 16
Matrix 18
Vector 22
Model 26
Основная форма 39

Фрагмент работы для ознакомления

mesh == null)
return "No mesh";
int i;
for (i = 0; i < model.TrVertices.Count; i++)
if (model.TrVertices[i] == null)
return "TrVertices not filled (==null)";

for( i = 0; i < model.TrNormals.Count; i++ )
if (model.TrNormals[i] == null)
return "TrNornals not filled (==null)";
foreach (TriIndex tri in model.mesh.Tris)
{
for (i = 0; i < 3; i++)
{
if (tri.n[i] >= model.TrNormals.Count)
return "Normal index is out of TrNormals array";
if (tri.v[i] >= model.TrVertices.Count)
return "Vertex index is out of TrVertex array";
/* if (Math.Abs(model.TrNormals[tri.n[i]].Length - 1) > 0.01)
return "Normal length incorrect"; //
}
}
}
return "";
}
public void Draw()
{
int i, j;

foreach (Model model in scene)
{
if (!model.last_vis) continue;
if (model.TrVertices == null)
{
System.Windows.Forms.MessageBox.Show("TrVertices not filled.\nCall Transform before render");
return;
}
foreach (TriIndex tri in model.mesh.Tris)
{
// Prepare
for (i = 0; i < 3; i++)
{
vert[i].v = model.TrVertices[tri.v[i]];
vert[i].n = model.TrNormals[tri.n[i]];
}
Prepare();
DrawTri();
}
}
}
} // */
// Рисует простой зеленый треугольник
/* class TriRenderer : TriRenderHelper
{
public Graphics gr;
public override void DrawTri()
{
sort_y();
// Make first interpolation
double x1, x2, dx1, dx2;
int y;
x1 = x2 = vert[0].v.X;
// 1 - short
dx1 = (vert[1].v.X - vert[0].v.X) / clip1( vert[1].pt.Y - vert[0].pt.Y );
// 2 - long
dx2 = (vert[2].v.X - vert[0].v.X) / clip1( vert[2].pt.Y - vert[0].pt.Y );
for (y = vert[0].pt.Y; y <= vert[1].pt.Y; y++)
{
gr.DrawLine(Pens.Green, (int)x1, y, (int)x2, y);
x1 += dx1;
x2 += dx2;
}
// 1 - short (second part)
dx1 = (vert[2].v.X - vert[1].v.X) / clip1( vert[2].pt.Y - vert[1].pt.Y );
x1 = vert[1].v.X;
for (; y <= vert[2].pt.Y; y++)
{
gr.DrawLine(Pens.Green, (int)x1, y, (int)x2, y);
x1 += dx1;
x2 += dx2;
}
}
} // */
}
Основная форма
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;
namespace earth
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
v = new RenderPoint[3];
v[0] = new RenderPoint();
v[1] = new RenderPoint();
v[2] = new RenderPoint();
cpoint = 0;
}
Bitmap buf, texture;
Graphics gr;
RenderPoint[] v;
Renderer renderer;
Mesh planet_m, sat_m;
Bitmap planet_t, sat_t;
Model planet, sat;
int cpoint;
static void swap<T>(ref T lhs, ref T rhs)
{
T temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
void sort_y()
{
if (v[0].y > v[1].y) swap( ref v[0], ref v[1]);
if (v[0].y > v[2].y) swap( ref v[0], ref v[2]);
if (v[1].y > v[2].y) swap( ref v[1], ref v[2]);
}
float calc_interp(float v1, float v2, int steps)
{
if (steps == 0)
return 0;
return (v2 - v1) / steps;
}
void calc_interp_v(ref Vector r, Vector v1, Vector v2, int steps)
{
r.x = calc_interp(v1.x, v2.x, steps);
r.y = calc_interp(v1.y, v2.y, steps);
r.z = calc_interp(v1.z, v2.z, steps);
}
Pen pen_color;
void DrawLine(int x1, int x2, int y, int c)
{
gr.DrawLine(pen_color, x1, y, x2, y);
}
void DrawLine_t(int x1, int x2, Vector t1, Vector t2, int y, int c)
{
if ((y < 0) | (x1 >= pictureBox1.Height)) return;
if (x1 > x2)
{
swap(ref x1, ref x2);
swap(ref t1, ref t2);
}
Vector t_d = new Vector(), t = new Vector( t1 );
int tx, ty, w = pictureBox1.Width;
calc_interp_v(ref t_d, t1, t2, x2 - x1 + 1);
for (int x = x1; x <= x2; x++)
{
tx = (int) (t.x * texture.Width);
ty = (int) (t.y * texture.Height);
if ((0 <= x) & (x < w))
{
if (tx < 0) tx = 0;
if (tx >= texture.Width) tx = texture.Width - 1;
if (ty < 0) ty = 0;
if (ty >= texture.Height) ty = texture.Height - 1;
buf.SetPixel(x, y, texture.GetPixel(tx, ty));
}
t.Add(t_d);
}
}
int y0, y1, y2, y;
void prep()
{
sort_y();
y0 = (int)v[0].y;
y1 = (int)v[1].y;
y2 = (int)v[2].y;
}
void DrawTri()
{
float xl_d, xl2_d, xr_d, xl, xr;
int y;
prep();
y = v[0].color;
pen_color = new Pen(Color.FromArgb(y & 0xFF, (y >> 8) & 0xFF, (y >> 16) & 0xFF));
xl = xr = v[0].x;
xl_d = calc_interp( v[0].x, v[1].x, y1-y0+1 );
if (y0 == y1)
{
xl = v[1].x;
xl_d = 0;
}
xl2_d = calc_interp(v[1].x, v[2].x, y2 - y1 );
xr_d = calc_interp(v[0].x, v[2].x, y2 - y0+1 );
for (y = y0; y <= y1; y++)
{
DrawLine( (int) xl, (int) xr, y, 0);
xl += xl_d;
xr += xr_d;
}
for (y = y1+1; y <= y2; y++)
{
DrawLine((int)xl, (int)xr, y, 0);
xl += xl2_d;
xr += xr_d;
}
}
void DrawTri_t()
{
float xl_d, xl2_d, xr_d, xl, xr;
Vector tl, tr;
Vector tl_d = new Vector(), tl2_d = new Vector(), tr_d = new Vector();
int y;
prep();
xl = xr = v[0].x;
tl = new Vector(v[0].tex);
tr = new Vector(v[0].tex);
xl_d = calc_interp(v[0].x, v[1].x, y1 - y0 + 1);
calc_interp_v( ref tl_d, v[0].tex, v[1].tex, y1 - y0 + 1);
if (y0 == y1)
{
xl = v[1].x;
tl = new Vector(v[1].tex);
xl_d = 0;
tl_d = new Vector();
}
xl2_d = calc_interp(v[1].x, v[2].x, y2 - y1);
calc_interp_v(ref tl2_d, v[1].tex, v[2].tex, y2 - y1);
xr_d = calc_interp(v[0].x, v[2].x, y2 - y0 + 1);
calc_interp_v(ref tr_d, v[0].tex, v[2].tex, y2 - y0 + 1);
for (y = y0; y <= y1; y++)
{
DrawLine_t((int)xl, (int)xr, tl, tr, y, 0);
xl += xl_d;
xr += xr_d;
tl.Add(tl_d);
tr.Add(tr_d);
}
for (y = y1 + 1; y <= y2; y++)
{
DrawLine_t((int)xl, (int)xr, tl, tr, y, 0);
xl += xl2_d;
xr += xr_d;
tl.Add(tl2_d);
tr.Add(tr_d);
}
}
void Texture( Vector v )
{
Texture( v.x, v.y );
}
void Texture(float x, float y)
{
v[cpoint].tex.x = x;
v[cpoint].tex.y = y;
}
void SetColor(int c)
{
v[cpoint].color = c;
}
void Vector(Vector v)
{
Vector(v.x, v.y, v.z);
}
void Vector(double x, double y, double z)
{
Vector((float)x, (float)y, (float)z);
}
void Vector(float x, float y, float z)
{
v[cpoint].x = (int)x;
v[cpoint].y = (int)y;
v[cpoint].z = (int)z;
if ((++cpoint) == 3)
{
if (texture == null)
DrawTri();
else
DrawTri_t();
cpoint = 0;
}
}
float a = 0;
private void timer1_Tick(object sender, EventArgs e)
{
gr.FillRectangle(Brushes.LightGray, pictureBox1.ClientRectangle);
/* double t = Math.PI / 2;
Texture(0, 1);
Vector(Math.Cos(a) * 100 + 150, Math.Sin(a) * 100 + 150, 0);
Texture(1, 1);
Vector(Math.Cos(a+t) * 100 + 150, Math.Sin(a+t) * 100 + 150, 0);
Texture(1, 0);
Vector(Math.Cos(a-t) * 100 + 150, Math.Sin(a-t) * 100 + 150, 0); // */
a = a + 0.005f;
planet.ModelMatrix = Matrix4.Scale(10f) * Matrix4.RotateY(a) ;
sat.ModelMatrix = Matrix4.RotateY(a * 10) * Matrix4.Translate(12, 0, 0) * Matrix4.Scale(0.5f);
renderer.Transform();
Model m = planet;
texture = m.tex;
foreach (TriIndex tri in m.mesh.Tris)
{
Vector n;
n = (m.TrVertices[tri.v[1]] - m.TrVertices[tri.v[0]]).CrossProduct
(m.TrVertices[tri.v[2]] - m.TrVertices[tri.v[0]]);
if (n.z > 0) continue;
for( int i = 0; i < 3; i++ )
{
Texture( m.mesh.TexCoords[ tri.t[i] ] );
Vector( m.TrVertices[ tri.v[i] ] );
}
}
m = sat;
texture = null;
Vector light1 = new Vector(0,1,0);
// light1.Normalize();
foreach (TriIndex tri in m.mesh.Tris)
{
Vector n;
n = (m.TrVertices[tri.v[1]] - m.TrVertices[tri.v[0]]).CrossProduct
(m.TrVertices[tri.v[2]] - m.TrVertices[tri.v[0]]);
n.Normalize();
if (n.z > 0) continue;
SetColor((int) ( n.DotProduct( light1 ) * 64 + 127));
// SetColor(
for (int i = 0; i < 3; i++)
{
// Texture(m.mesh.TexCoords[tri.t[i]]);
Vector(m.TrVertices[tri.v[i]]);
}
}

pictureBox1.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
buf = new Bitmap(pictureBox1.Width, pictureBox1.Height);
gr = Graphics.FromImage(buf);
CreateRenderer();
planet_t = new Bitmap("Earth_D.bmp");
planet_m = ObjLoader.Load("Earth.obj");
sat_m = ObjLoader.Load("sat1.obj");
/* int t = planet_m.CheckIndices();
if (t != 0)
{
MessageBox.Show("Ошибка модели: " + t.ToString());
} // */

planet = new Model(planet_m);
planet.tex = planet_t;
planet.init(false);
planet.ModelMatrix = Matrix4.CreateIdentity();
renderer.scene.Add(planet);
sat = new Model(sat_m);
sat.init(false);
sat.ModelMatrix = Matrix4.CreateIdentity();
renderer.scene.Add(sat);
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (buf == null) return;
e.Graphics.DrawImage( buf, 0,0 );
}
void CreateRenderer()
{
renderer = new Renderer();
renderer.SetViewopt(pictureBox1.Width, pictureBox1.Height);
renderer.make_perspective = true;
renderer.ViewMatrix = Matrix4.Translate(new Vector(0, 0, -15)) * Matrix4.RotateX( (float) (-60 * 3.14 / 180) );
renderer.ProjectionMatrix = renderer.ProjectionMatrix * Matrix4.Scale(150, -150, 150) ;
}
}

public class RenderPoint : Vector
{
public int color;
public Vector tex;
public RenderPoint()
{
tex = new Vector();
}
} // */
}
3D (от англ. 3 Dimensions — «3 измерения») Graphics, Три измерения изображения
как к грани, так и к вершине, в зависимости от потребностей
к примеру в нем записан тор
14

Список литературы

фрагмент
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace earth
{
public class TriIndex
{
public int[]
v = new int[3],
n = new int[3],
t = new int[3];
public void SetV(int v0, int v1, int v2)
{
v[0] = v0;
v[1] = v1;
v[2] = v2;
}
public void SetV(int _v, int i)
{
v[i] = _v;
}
public void SetN(int n0, int n1, int n2)
{
n[0] = n0;
n[1] = n1;
n[2] = n2;
}
public void SetN( int _n, int i )
{
n[i] = _n;
}
public void SetT(int t0, int t1, int t2)
{
t[0] = t0;
t[1] = t1;
t[2] = t2;
}
public void SetT( int _t, int i )
{
t[i] = _t;
}
public void Copy(int to_i, TriIndex from, int from_i)
{
v[to_i] = from.v[from_i];
n[to_i] = from.n[from_i];
t[to_i] = from.t[from_i];
}
public TriIndex()
{
}
public TriIndex(int v1, int v2, int v3)
{
SetV(v1, v2, v3);
}
}
// Рисует треугольники к примеру
public class Mesh
{
public const int mNormals = 1;
public const int mTex = 2;
public const int mIndexed = 4;
public List<Vector> Vertices;
public List<Vector> Normals = null;
public List<Vector> TexCoords = null;
public int Flags = 0;
public List<TriIndex> Tris = null;
bool is_new(int fl, int m)
{
return ((Flags & m) == 0) & ((fl & m) != 0);
}
public void AddCap( int fl)
{
if (is_new( fl, mNormals ))
Normals = new List<Vector>();
if (is_new( fl, mTex))
TexCoords = new List<Vector>();
if (is_new( fl, mIndexed))
Tris = new List<TriIndex>();
Flags |= fl;
}
public Mesh(int fl)
{
Flags = 0;
Vertices = new List<Vector>();
AddCap(fl);
}
Vector GetNorm(int tr_i)
{
Vector n, v, v2;
TriIndex tri = Tris[tr_i];
Очень похожие работы
Пожалуйста, внимательно изучайте содержание и фрагменты работы. Деньги за приобретённые готовые работы по причине несоответствия данной работы вашим требованиям или её уникальности не возвращаются.
* Категория работы носит оценочный характер в соответствии с качественными и количественными параметрами предоставляемого материала. Данный материал ни целиком, ни любая из его частей не является готовым научным трудом, выпускной квалификационной работой, научным докладом или иной работой, предусмотренной государственной системой научной аттестации или необходимой для прохождения промежуточной или итоговой аттестации. Данный материал представляет собой субъективный результат обработки, структурирования и форматирования собранной его автором информации и предназначен, прежде всего, для использования в качестве источника для самостоятельной подготовки работы указанной тематики.
bmt: 0.00473
© Рефератбанк, 2002 - 2024