Нарезка текстуры для отображения псевдо 3D -самолетов в xna/monogameC#

Место общения программистов C#
Ответить Пред. темаСлед. тема
Anonymous
 Нарезка текстуры для отображения псевдо 3D -самолетов в xna/monogame

Сообщение Anonymous »

В настоящее время я рисую псевдо 3D -сегменты дороги для достижения фальшивой 3D -дороги с помощью линий рисунка. Вот мой код: < /p>
public void Draw()
{
//define the clipping bottom line to render only segments above it
int clipBottomLine = Basic.height;

//get the base segment
Segment baseSegment = GetSegment(GameScreen.camera.Z);
var baseIndex = baseSegment.Index;

for (int n = 0; n < visibleSegments; n++)
{
//get the current segment
int currIndex = (baseIndex + n) % totalSegments;
Segment currSegment = segments[currIndex];

//get the camera offset-Z to loop back the road
int offsetZ = (currIndex < baseIndex) ? roadLength : 0;

//project the segment to the screen space
Project3D(currSegment, GameScreen.camera.X, GameScreen.camera.Y, GameScreen.camera.Z - offsetZ, GameScreen.camera.DistToPlane);

//draw this segment only if it is above the clipping bottom line
var currbottomLine = currSegment.Screen.Y;

if (n > 0 && currbottomLine < clipBottomLine)
{
int prevIndex = (currIndex > 0) ? currIndex - 1 : totalSegments - 1;
Segment prevSegment = segments[prevIndex];

Rectangle p1 = prevSegment.Screen;
Rectangle p2 = currSegment.Screen;

DrawSegment(p1.X, p1.Y, p1.Width, p2.X, p2.Y, p2.Width, currSegment.ColorRoad, currSegment.Index); //p1.x, p1.y, p1.w, p2.x, p2.y, p2.w, currSegment.Color

//move the clipping bottom line up
clipBottomLine = currbottomLine;
}
}
}

//projects a point from his world coordinates to camera coordinates
private void Project3D(Segment segment, float cameraX, float cameraY, float cameraZ, float cameraDepth)
{
//translating world coordinates to camera coordinates
float transX = segment.World.X - cameraX;
float transY = segment.World.Y - cameraY;
float transZ = segment.World.Z - cameraZ;

//scaling facor based on the law of similar triangles
segment.Scale = cameraDepth / transZ;

//projecting camera coordinates onto a normalized projection plane
float projectedX = segment.Scale * transX;
float projectedY = segment.Scale * transY;
float projectedW = segment.Scale * roadWidth;

//scaling projected coordinates to the screen coordinates
segment.Screen = new Rectangle(
(int)(Math.Round((1 + projectedX) * screenCX)),
(int)(Math.Round((1 - projectedY) * screenCY)),
(int)(Math.Round(projectedW * screenCX)),
segment.Screen.Height
);
}

//draws a segment
private void DrawSegment(int x1, int y1, int w1, int x2, int y2, int w2, Color color, int index)
{
//draw road
DrawPolygon(x1 - w1, y1, x1 + w1, y1, x2 + w2, y2, x2 - w2, y2, color);
}

private void DrawPolygon(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Color color)
{
Vector2 p1 = new Vector2(x1, y1);
Vector2 p2 = new Vector2(x2, y2);
Vector2 p3 = new Vector2(x3, y3);
Vector2 p4 = new Vector2(x4, y4);

int thickness = 2;

Canvas.DrawLine(p1, p2, thickness, color); //bottom line
Canvas.DrawLine(p2, p3, thickness, color); //right line
Canvas.DrawLine(p3, p4, thickness, color); //top line
Canvas.DrawLine(p4, p1, thickness, color); //left line
}
< /code>
мой класс камеры действует так: < /p>
class Camera
{
public float X { get; set; }
public float Y { get; set; }
public float Z { get; set; }
public float DistToPlayer { get; set; }
public float DistToPlane { get; set; }

public Camera()
{
//camera world coordinates
X = 0;
Y = 1000;
Z = 0;

//Z-distance between camera and player
DistToPlayer = 200;

//Z-distance between camera and normalized projection plane
DistToPlane = 1 / (Y / DistToPlayer);
}

//updates camera position to follow the player
public void Update()
{
//since player X is normalized within [-1, 1]
X = GameScreen.car.X * GameScreen.circuit.roadWidth;

//place the camera behind the player at the desired distance
Z = GameScreen.car.Z - DistToPlayer;

//don't let camZ go to negative
if (Z < 0)
{
Z += GameScreen.circuit.roadLength;
}
}
}
< /code>
Теперь я стараюсь не рисовать линии для отображения дорожных сегментов, но срезы текстуры, так что один сегмент имеет ломтики моей текстуры на основе ее высоты. < /p>
Я попробовал это здесь: < /p>
int segLength = y2 - y3;

for (int i = 0; i < segLength; i++)
{
var y = Textures.roadSeg.Height - 1 - i;
float scaleX = ((segLength - i) / (float)segLength);

Rectangle slice = new Rectangle(0, y % Textures.roadSeg.Height, Textures.roadSeg.Width, 1);

Basic.spriteBatch.Draw(Textures.roadSeg, new Rectangle(x1, y4 - segLength + i, x2 - x1, y2 - y3), Color.White);
//Basic.spriteBatch.Draw(Textures.roadSeg, new Rectangle(x1, y4, x2 - x1, y2 - y3), Color.White); //-200, 501, 1206, 101
}
< /code>
Но теперь я застрял и достигаю странных эффектов при попытке использовать переменную Scalex или когда я пытаюсь разместить срез текстуры. Я думаю, что мне также нужно использовать камеру, но я не уверен, как это достичь. Мне нужно повторить мою текстуру для каждого сегмента, и там нарезать и масштабировать ее, чтобы соответствовать многоугольникам. сегмент, но это не работает. Я уже отдаю всю сцену в целевой мишени. Это заканчивается, чтобы быть очень медленным. Так что как -то мне нужно было бы получить ломтик на основе высоты сегмента дороги.

Подробнее здесь: https://stackoverflow.com/questions/795 ... a-monogame
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение
  • Реконструкция самолетов МРТ
    Гость » » в форуме Python
    0 Ответы
    23 Просмотры
    Последнее сообщение Гость
  • Текстура OpenGL показывает сплошной цвет текстуры, а не всей текстуры.
    Anonymous » » в форуме C++
    0 Ответы
    42 Просмотры
    Последнее сообщение Anonymous
  • Почему мое видео не соответствует размеру текстуры, когда я меняю размер текстуры
    Anonymous » » в форуме Android
    0 Ответы
    32 Просмотры
    Последнее сообщение Anonymous
  • Как использовать контроллер Play Station 2, подключенный через USB-порт, в XNA
    Anonymous » » в форуме C#
    0 Ответы
    40 Просмотры
    Последнее сообщение Anonymous
  • C# XNA 4.0: как получить тангенс и бинормаль модели?
    Anonymous » » в форуме C#
    0 Ответы
    8 Просмотры
    Последнее сообщение Anonymous

Вернуться в «C#»