2020-03-04 03:29:15 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2020 faddenSoft
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
|
|
using PluginCommon;
|
|
|
|
|
|
|
|
|
|
namespace SourceGen {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Renders a wireframe visualization, generating a collection of line segments in clip space.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class WireframeObject {
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Line segment.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class LineSeg {
|
|
|
|
|
public double X0 { get; private set; }
|
|
|
|
|
public double Y0 { get; private set; }
|
|
|
|
|
public double X1 { get; private set; }
|
|
|
|
|
public double Y1 { get; private set; }
|
|
|
|
|
|
|
|
|
|
public LineSeg(double x0, double y0, double x1, double y1) {
|
2021-11-01 22:07:29 +00:00
|
|
|
|
if (double.IsNaN(x0) || double.IsNaN(y0) || double.IsNaN(x1) || double.IsNaN(y1)) {
|
|
|
|
|
throw new Exception("Invalid LineSeg: x0=" + x0 + " y0=" + y0 +
|
|
|
|
|
" x1=" + x1 + " y1=" + y1);
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
X0 = x0;
|
|
|
|
|
Y0 = y0;
|
|
|
|
|
X1 = x1;
|
|
|
|
|
Y1 = y1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Vertex {
|
|
|
|
|
public Vector3 Vec { get; private set; }
|
2020-03-13 17:53:53 +00:00
|
|
|
|
public List<Face> Faces { get; private set; }
|
|
|
|
|
public bool IsExcluded { get; private set; }
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-03-13 17:53:53 +00:00
|
|
|
|
public Vertex(double x, double y, double z, bool isExcluded) {
|
2020-03-04 03:29:15 +00:00
|
|
|
|
Vec = new Vector3(x, y, z);
|
|
|
|
|
Faces = new List<Face>();
|
2020-03-13 17:53:53 +00:00
|
|
|
|
IsExcluded = isExcluded;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
2020-03-11 00:18:46 +00:00
|
|
|
|
|
|
|
|
|
public override string ToString() {
|
|
|
|
|
return Vec.ToString() + " + " + Faces.Count + " faces";
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Edge {
|
|
|
|
|
public Vertex Vertex0 { get; private set; }
|
|
|
|
|
public Vertex Vertex1 { get; private set; }
|
|
|
|
|
public List<Face> Faces { get; private set; }
|
2020-03-13 17:53:53 +00:00
|
|
|
|
public bool IsExcluded { get; private set; }
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-03-13 17:53:53 +00:00
|
|
|
|
public Edge(Vertex v0, Vertex v1, bool isExcluded) {
|
2020-03-04 03:29:15 +00:00
|
|
|
|
Vertex0 = v0;
|
|
|
|
|
Vertex1 = v1;
|
|
|
|
|
Faces = new List<Face>();
|
2020-03-13 17:53:53 +00:00
|
|
|
|
IsExcluded = isExcluded;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Face {
|
2020-03-07 00:51:47 +00:00
|
|
|
|
// Surface normal.
|
2020-03-04 03:29:15 +00:00
|
|
|
|
public Vector3 Normal { get; private set; }
|
2020-03-07 00:51:47 +00:00
|
|
|
|
// One vertex on the face, for BFC.
|
|
|
|
|
public Vertex Vert { get; set; }
|
|
|
|
|
// Flag set during BFC calculation.
|
|
|
|
|
public bool IsVisible { get; set; }
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
|
|
|
|
public Face(double x, double y, double z) {
|
|
|
|
|
Normal = new Vector3(x, y, z);
|
2020-03-07 00:51:47 +00:00
|
|
|
|
Normal.Normalize(); // not necessary, but easier to read in debug output
|
|
|
|
|
IsVisible = true;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-14 00:33:34 +00:00
|
|
|
|
public bool VerboseDebug { get; set; }
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
private bool mIs2d = false;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
private List<Vertex> mVertices = new List<Vertex>();
|
2020-04-11 18:23:16 +00:00
|
|
|
|
private List<Vertex> mPoints = new List<Vertex>();
|
2020-03-04 03:29:15 +00:00
|
|
|
|
private List<Edge> mEdges = new List<Edge>();
|
|
|
|
|
private List<Face> mFaces = new List<Face>();
|
|
|
|
|
private double mBigMag = -1.0;
|
2020-04-12 00:24:21 +00:00
|
|
|
|
private double mBigMagRc = -1.0;
|
|
|
|
|
private double mCenterAdjX, mCenterAdjY;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// private constructor; use Create()
|
|
|
|
|
private WireframeObject() { }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new object from a wireframe visualization.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="visWire">Visualization object.</param>
|
2020-04-23 18:25:45 +00:00
|
|
|
|
/// <returns>New object, or null if visualization data fails validation.</returns>
|
2020-03-04 03:29:15 +00:00
|
|
|
|
public static WireframeObject Create(IVisualizationWireframe visWire) {
|
2020-04-23 18:25:45 +00:00
|
|
|
|
if (!visWire.Validate(out string msg)) {
|
|
|
|
|
// Should not be here -- visualizer should have checked validation and
|
|
|
|
|
// reported an error.
|
|
|
|
|
Debug.WriteLine("Wireframe validation failed: " + msg);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
WireframeObject wireObj = new WireframeObject();
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
wireObj.mIs2d = visWire.Is2d;
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
//
|
|
|
|
|
// Start by extracting data from the visualization object. Everything stored
|
2020-04-11 18:23:16 +00:00
|
|
|
|
// there is loaded into this object. The VisWireframe validator will have
|
|
|
|
|
// ensured that all the indices are in range.
|
2020-03-04 03:29:15 +00:00
|
|
|
|
//
|
2020-04-12 00:24:21 +00:00
|
|
|
|
// IMPORTANT: do not retain "visWire", as it may be a proxy for an object with a
|
|
|
|
|
// limited lifespan.
|
|
|
|
|
//
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
|
|
|
|
float[] normalsX = visWire.GetNormalsX();
|
|
|
|
|
if (normalsX.Length > 0) {
|
|
|
|
|
float[] normalsY = visWire.GetNormalsY();
|
|
|
|
|
float[] normalsZ = visWire.GetNormalsZ();
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < normalsX.Length; i++) {
|
|
|
|
|
wireObj.mFaces.Add(new Face(normalsX[i], normalsY[i], normalsZ[i]));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float[] verticesX = visWire.GetVerticesX();
|
|
|
|
|
float[] verticesY = visWire.GetVerticesY();
|
|
|
|
|
float[] verticesZ = visWire.GetVerticesZ();
|
2020-03-13 17:53:53 +00:00
|
|
|
|
int[] excludedVertices = visWire.GetExcludedVertices();
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
// Compute min/max for X/Y for 2d re-centering. The trick is that we only want
|
|
|
|
|
// to use vertices that are visible. If the shape starts with a huge move off to
|
|
|
|
|
// the left, we don't want to include (0,0).
|
|
|
|
|
double xmin, xmax, ymin, ymax;
|
|
|
|
|
xmin = ymin = 10e9;
|
|
|
|
|
xmax = ymax = -10e9;
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
for (int i = 0; i < verticesX.Length; i++) {
|
2020-03-13 17:53:53 +00:00
|
|
|
|
wireObj.mVertices.Add(new Vertex(verticesX[i], verticesY[i], verticesZ[i],
|
|
|
|
|
HasIndex(excludedVertices, i)));
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 18:23:16 +00:00
|
|
|
|
int[] points = visWire.GetPoints();
|
|
|
|
|
for (int i = 0; i < points.Length; i++) {
|
2020-04-12 00:24:21 +00:00
|
|
|
|
Vertex vert = wireObj.mVertices[points[i]];
|
|
|
|
|
wireObj.mPoints.Add(vert);
|
|
|
|
|
UpdateMinMax(vert, ref xmin, ref xmax, ref ymin, ref ymax);
|
2020-04-11 18:23:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
IntPair[] edges = visWire.GetEdges();
|
2020-03-13 17:53:53 +00:00
|
|
|
|
int[] excludedEdges = visWire.GetExcludedEdges();
|
2020-03-04 03:29:15 +00:00
|
|
|
|
for (int i = 0; i < edges.Length; i++) {
|
|
|
|
|
int v0index = edges[i].Val0;
|
|
|
|
|
int v1index = edges[i].Val1;
|
|
|
|
|
|
2020-04-23 18:25:45 +00:00
|
|
|
|
//if (v0index < 0 || v0index >= wireObj.mVertices.Count ||
|
|
|
|
|
// v1index < 0 || v1index >= wireObj.mVertices.Count) {
|
|
|
|
|
// Debug.Assert(false);
|
|
|
|
|
// return null;
|
|
|
|
|
//}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
Vertex vert0 = wireObj.mVertices[v0index];
|
|
|
|
|
Vertex vert1 = wireObj.mVertices[v1index];
|
|
|
|
|
wireObj.mEdges.Add(new Edge(vert0, vert1, HasIndex(excludedEdges, i)));
|
|
|
|
|
|
|
|
|
|
UpdateMinMax(vert0, ref xmin, ref xmax, ref ymin, ref ymax);
|
|
|
|
|
UpdateMinMax(vert1, ref xmin, ref xmax, ref ymin, ref ymax);
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPair[] vfaces = visWire.GetVertexFaces();
|
|
|
|
|
for (int i = 0; i < vfaces.Length; i++) {
|
|
|
|
|
int vindex = vfaces[i].Val0;
|
|
|
|
|
int findex = vfaces[i].Val1;
|
|
|
|
|
|
2020-04-23 18:25:45 +00:00
|
|
|
|
//if (vindex < 0 || vindex >= wireObj.mVertices.Count ||
|
|
|
|
|
// findex < 0 || findex >= wireObj.mFaces.Count) {
|
|
|
|
|
// Debug.Assert(false);
|
|
|
|
|
// return null;
|
|
|
|
|
//}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-03-07 00:51:47 +00:00
|
|
|
|
Face face = wireObj.mFaces[findex];
|
|
|
|
|
wireObj.mVertices[vindex].Faces.Add(face);
|
|
|
|
|
if (face.Vert == null) {
|
|
|
|
|
face.Vert = wireObj.mVertices[vindex];
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IntPair[] efaces = visWire.GetEdgeFaces();
|
|
|
|
|
for (int i = 0; i < efaces.Length; i++) {
|
|
|
|
|
int eindex = efaces[i].Val0;
|
|
|
|
|
int findex = efaces[i].Val1;
|
|
|
|
|
|
2020-04-23 18:25:45 +00:00
|
|
|
|
//if (eindex < 0 || eindex >= wireObj.mEdges.Count ||
|
|
|
|
|
// findex < 0 || findex >= wireObj.mFaces.Count) {
|
|
|
|
|
// Debug.Assert(false);
|
|
|
|
|
// return null;
|
|
|
|
|
//}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
2020-03-07 00:51:47 +00:00
|
|
|
|
Face face = wireObj.mFaces[findex];
|
|
|
|
|
wireObj.mEdges[eindex].Faces.Add(face);
|
|
|
|
|
if (face.Vert == null) {
|
|
|
|
|
face.Vert = wireObj.mEdges[eindex].Vertex0;
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// All data has been loaded into friendly classes.
|
|
|
|
|
//
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
// Compute center of visible vertices.
|
|
|
|
|
wireObj.mCenterAdjX = -(xmin + xmax) / 2;
|
|
|
|
|
wireObj.mCenterAdjY = -(ymin + ymax / 2);
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
// Compute the magnitude of the largest vertex, for scaling.
|
|
|
|
|
double bigMag = -1.0;
|
2020-04-12 00:24:21 +00:00
|
|
|
|
double bigMagRc = -1.0;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
for (int i = 0; i < wireObj.mVertices.Count; i++) {
|
2020-04-12 00:24:21 +00:00
|
|
|
|
Vector3 vec = wireObj.mVertices[i].Vec;
|
|
|
|
|
double mag = vec.Magnitude();
|
2020-03-04 03:29:15 +00:00
|
|
|
|
if (bigMag < mag) {
|
|
|
|
|
bigMag = mag;
|
|
|
|
|
}
|
2020-04-12 00:24:21 +00:00
|
|
|
|
|
|
|
|
|
// Repeat the operation with recentering. This isn't quite right as we're
|
|
|
|
|
// including all vertices, not just the visible ones.
|
|
|
|
|
mag = new Vector3(vec.X + wireObj.mCenterAdjX,
|
|
|
|
|
vec.Y + wireObj.mCenterAdjY, vec.Z).Magnitude();
|
|
|
|
|
if (bigMagRc < mag) {
|
|
|
|
|
bigMagRc = mag;
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
2021-11-01 22:07:29 +00:00
|
|
|
|
|
|
|
|
|
// Avoid divide-by-zero.
|
|
|
|
|
if (bigMag == 0) {
|
|
|
|
|
Debug.WriteLine("NOTE: wireframe magnitude was zero");
|
|
|
|
|
bigMag = 1;
|
|
|
|
|
}
|
|
|
|
|
if (bigMagRc == 0) {
|
|
|
|
|
bigMagRc = 1;
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
wireObj.mBigMag = bigMag;
|
2020-04-12 00:24:21 +00:00
|
|
|
|
wireObj.mBigMagRc = bigMagRc;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
|
|
|
|
|
return wireObj;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
private static void UpdateMinMax(Vertex vert, ref double xmin, ref double xmax,
|
|
|
|
|
ref double ymin, ref double ymax) {
|
|
|
|
|
if (vert.Vec.X < xmin) {
|
|
|
|
|
xmin = vert.Vec.X;
|
2020-04-14 00:33:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (vert.Vec.X > xmax) {
|
2020-04-12 00:24:21 +00:00
|
|
|
|
xmax = vert.Vec.X;
|
|
|
|
|
}
|
|
|
|
|
if (vert.Vec.Y < ymin) {
|
|
|
|
|
ymin = vert.Vec.Y;
|
2020-04-14 00:33:34 +00:00
|
|
|
|
}
|
|
|
|
|
if (vert.Vec.Y > ymax) {
|
2020-04-12 00:24:21 +00:00
|
|
|
|
ymax = vert.Vec.Y;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-13 17:53:53 +00:00
|
|
|
|
private static bool HasIndex(int[] arr, int val) {
|
|
|
|
|
for (int i = 0; i < arr.Length; i++) {
|
|
|
|
|
if (arr[i] == val) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Generates a list of line segments for the wireframe data and the specified
|
|
|
|
|
/// parameters.
|
|
|
|
|
/// </summary>
|
2020-03-09 20:48:30 +00:00
|
|
|
|
/// <param name="eulerX">Rotation about X axis.</params>
|
|
|
|
|
/// <param name="eulerY">Rotation about Y axis.</params>
|
|
|
|
|
/// <param name="eulerZ">Rotation about Z axis.</params>
|
|
|
|
|
/// <param name="doPersp">Perspective or othographic projection?</param>
|
|
|
|
|
/// <param name="doBfc">Perform backface culling?</param>
|
2020-04-12 00:24:21 +00:00
|
|
|
|
/// <param name="doRecenter">Re-center 2D renderings?</param>
|
2020-03-09 20:48:30 +00:00
|
|
|
|
/// <returns>List a of line segments, which could be empty if backface culling
|
2020-04-11 18:23:16 +00:00
|
|
|
|
/// was especially successful. All segment coordinates are in the range
|
|
|
|
|
/// [-1,1].</returns>
|
2020-03-09 20:48:30 +00:00
|
|
|
|
public List<LineSeg> Generate(int eulerX, int eulerY, int eulerZ,
|
2020-04-12 00:24:21 +00:00
|
|
|
|
bool doPersp, bool doBfc, bool doRecenter) {
|
2020-04-14 00:33:34 +00:00
|
|
|
|
if (VerboseDebug) {
|
|
|
|
|
Debug.WriteLine("Found center=" + mCenterAdjX + "," + mCenterAdjY);
|
|
|
|
|
Debug.WriteLine(" bigMag=" + mBigMag + " / " + mBigMagRc);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
// overrule flags that don't make sense
|
|
|
|
|
if (mIs2d) {
|
|
|
|
|
doPersp = doBfc = false;
|
|
|
|
|
} else {
|
|
|
|
|
doRecenter = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
List<LineSeg> segs = new List<LineSeg>(mEdges.Count);
|
|
|
|
|
|
2020-03-07 00:51:47 +00:00
|
|
|
|
// Camera Z coordinate adjustment, used to control how perspective projections
|
|
|
|
|
// appear. The larger the value, the farther the object appears to be. Very
|
|
|
|
|
// large values approximate an orthographic projection.
|
2020-03-04 03:29:15 +00:00
|
|
|
|
const double zadj = 3.0;
|
|
|
|
|
|
2020-03-07 00:51:47 +00:00
|
|
|
|
// Scale coordinate values to [-1,1].
|
2020-04-12 00:24:21 +00:00
|
|
|
|
double scale;
|
|
|
|
|
if (doRecenter) {
|
|
|
|
|
scale = 1.0 / mBigMagRc;
|
|
|
|
|
} else {
|
|
|
|
|
scale = 1.0 / mBigMag;
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
if (doPersp) {
|
2020-03-07 00:51:47 +00:00
|
|
|
|
// objects closer to camera are bigger; reduce scale slightly
|
2020-03-11 00:18:46 +00:00
|
|
|
|
scale = (scale * zadj) / (zadj + 0.3);
|
2020-03-07 00:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
// Configure X/Y translation for 2D wireframes.
|
|
|
|
|
double transX = 0;
|
|
|
|
|
double transY = 0;
|
|
|
|
|
if (doRecenter) {
|
|
|
|
|
transX = mCenterAdjX;
|
|
|
|
|
transY = mCenterAdjY;
|
|
|
|
|
}
|
|
|
|
|
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
// In a left-handed coordinate system, +Z is away from the viewer. The
|
|
|
|
|
// visualizer expects a left-handed system with the "nose" aimed toward +Z,
|
|
|
|
|
// which leaves us looking at the back end of things. We can add a 180 degree
|
|
|
|
|
// rotation about Y so we're looking at the front instead, though this
|
|
|
|
|
// effectively reverses the direction of rotation about X. We can compensate
|
|
|
|
|
// for it by reversing the handedness of the X rotation.
|
|
|
|
|
//eulerY = (eulerY + 180) % 360;
|
|
|
|
|
|
|
|
|
|
// Form rotation matrix.
|
|
|
|
|
Matrix33 rotMat = new Matrix33();
|
|
|
|
|
rotMat.SetRotationEuler(eulerX, eulerY, eulerZ, Matrix33.RotMode.ZYX_LLL);
|
|
|
|
|
//Debug.WriteLine("ROT: " + rotMat);
|
2020-03-07 00:51:47 +00:00
|
|
|
|
|
|
|
|
|
if (doBfc) {
|
|
|
|
|
// Mark faces as visible or not. This is determined with the surface normal,
|
|
|
|
|
// rather than by checking whether a transformed triangle is clockwise.
|
|
|
|
|
foreach (Face face in mFaces) {
|
|
|
|
|
// Transform the surface normal.
|
|
|
|
|
Vector3 rotNorm = rotMat.Multiply(face.Normal);
|
|
|
|
|
if (doPersp) {
|
|
|
|
|
// Transform one vertex to get a vector from the camera to the
|
|
|
|
|
// surface. We want (V0 - C), where C is the camera; since we're
|
|
|
|
|
// at the origin, we just need -C.
|
|
|
|
|
if (face.Vert == null) {
|
|
|
|
|
Debug.WriteLine("GLITCH: no vertex for face");
|
|
|
|
|
face.IsVisible = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
Vector3 camVec = rotMat.Multiply(face.Vert.Vec); // transform
|
2020-03-11 03:59:45 +00:00
|
|
|
|
camVec = camVec.Multiply(-scale); // scale to [-1,1] and negate to get -C
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
camVec = camVec.Add(new Vector3(0, 0, -zadj)); // translate
|
2020-03-07 00:51:47 +00:00
|
|
|
|
|
|
|
|
|
// Now compute the dot product of the camera vector.
|
|
|
|
|
double dot = Vector3.Dot(camVec, rotNorm);
|
|
|
|
|
face.IsVisible = (dot >= 0);
|
|
|
|
|
//Debug.WriteLine(string.Format(
|
|
|
|
|
// "Face {0} vis={1,-5} dot={2,-8:N2}: camVec={3} rotNorm={4}",
|
|
|
|
|
// index++, face.IsVisible, dot, camVec, rotNorm));
|
|
|
|
|
} else {
|
|
|
|
|
// For orthographic projection, the camera is essentially looking
|
|
|
|
|
// down the Z axis at every X,Y, so we can trivially check the
|
|
|
|
|
// value of Z in the transformed normal.
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
face.IsVisible = (rotNorm.Z <= 0);
|
2020-03-07 00:51:47 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-11 18:23:16 +00:00
|
|
|
|
foreach (Vertex point in mPoints) {
|
|
|
|
|
// There are no "point faces" at the moment, so no BFC is applied.
|
2020-04-12 00:24:21 +00:00
|
|
|
|
Vector3 vec = point.Vec;
|
|
|
|
|
if (doRecenter) {
|
|
|
|
|
vec = new Vector3(vec.X + transX, vec.Y + transY, vec.Z);
|
|
|
|
|
}
|
|
|
|
|
Vector3 trv = rotMat.Multiply(vec);
|
|
|
|
|
|
2020-04-11 18:23:16 +00:00
|
|
|
|
double xc, yc;
|
|
|
|
|
if (doPersp) {
|
|
|
|
|
double zc = trv.Z * scale;
|
|
|
|
|
xc = (trv.X * scale * zadj) / (zadj + zc);
|
|
|
|
|
yc = (trv.Y * scale * zadj) / (zadj + zc);
|
|
|
|
|
} else {
|
|
|
|
|
xc = trv.X * scale;
|
|
|
|
|
yc = trv.Y * scale;
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
//Debug.WriteLine("POINT " + xc + "," + yc);
|
|
|
|
|
|
2020-04-11 18:23:16 +00:00
|
|
|
|
// Zero-length line segments don't do anything. Try a '+'.
|
|
|
|
|
const double dist = 1 / 64.0;
|
|
|
|
|
double x0 = Math.Max(-1.0, xc - dist);
|
|
|
|
|
double x1 = Math.Min(xc + dist, 1.0);
|
|
|
|
|
segs.Add(new LineSeg(x0, yc, x1, yc));
|
|
|
|
|
double y0 = Math.Max(-1.0, yc - dist);
|
|
|
|
|
double y1 = Math.Min(yc + dist, 1.0);
|
|
|
|
|
segs.Add(new LineSeg(xc, y0, xc, y1));
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-04 03:29:15 +00:00
|
|
|
|
foreach (Edge edge in mEdges) {
|
2020-03-07 00:51:47 +00:00
|
|
|
|
if (doBfc) {
|
|
|
|
|
// To be visible, vertices and edges must either not specify any
|
2020-03-13 17:53:53 +00:00
|
|
|
|
// faces, or must specify a visible face. They can also be hidden
|
|
|
|
|
// by the level-of-detail exclusion mechanism.
|
|
|
|
|
if (!IsVertexVisible(edge.Vertex0) || edge.Vertex0.IsExcluded ||
|
|
|
|
|
!IsVertexVisible(edge.Vertex1) || edge.Vertex1.IsExcluded ||
|
|
|
|
|
!IsEdgeVisible(edge) || edge.IsExcluded) {
|
2020-03-07 00:51:47 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-12 00:24:21 +00:00
|
|
|
|
Vector3 vec0 = edge.Vertex0.Vec;
|
|
|
|
|
Vector3 vec1 = edge.Vertex1.Vec;
|
|
|
|
|
if (doRecenter) {
|
|
|
|
|
vec0 = new Vector3(vec0.X + transX, vec0.Y + transY, vec0.Z);
|
|
|
|
|
vec1 = new Vector3(vec1.X + transX, vec1.Y + transY, vec1.Z);
|
|
|
|
|
}
|
|
|
|
|
Vector3 trv0 = rotMat.Multiply(vec0);
|
|
|
|
|
Vector3 trv1 = rotMat.Multiply(vec1);
|
2020-03-04 03:29:15 +00:00
|
|
|
|
double x0, y0, x1, y1;
|
|
|
|
|
|
|
|
|
|
if (doPersp) {
|
Switch to left-handed coordinate system
There's no "standard" coordinate system, so the choice is arbitrary.
However, an examination of the Transporter mesh in Elite revealed
that the mesh was designed for a left-handed coordinate system. We
can compensate for that trivially in the Elite visualizer, but we
might as well match what they're doing. (The only change required
in the code is a couple of sign changes on the Z coordinate, and an
update to the rotation matrix.)
This also downsizes Matrix44 to Matrix33, exposes the rotation mode
enum, and adds a left-handed ZYX rotation mode.
This does mean that meshes that put the front at +Z will show their
backsides initially, since we're now oriented as if we're flying
the ships rather than facing them. I considered adding a 180-degree
Y rotation (with a tweak to the rotation matrix handedness to correct
the first rotation axis) to have them facing by default, but figured
that might be confusing since +Z is supposed to be away.
Anybody who really wants it to be the other way can trivially flip
the coordinates in their visualizer (negate xc/zc).
The Z coordinates in the visualization test project were flipped so
that the design is still facing the viewer at rotation (0,0,0).
2020-03-14 17:25:17 +00:00
|
|
|
|
// Left-handed system, so +Z is away from viewer.
|
|
|
|
|
double z0 = trv0.Z * scale;
|
|
|
|
|
double z1 = trv1.Z * scale;
|
2020-03-07 00:51:47 +00:00
|
|
|
|
x0 = (trv0.X * scale * zadj) / (zadj + z0);
|
|
|
|
|
y0 = (trv0.Y * scale * zadj) / (zadj + z0);
|
|
|
|
|
x1 = (trv1.X * scale * zadj) / (zadj + z1);
|
|
|
|
|
y1 = (trv1.Y * scale * zadj) / (zadj + z1);
|
2020-03-04 03:29:15 +00:00
|
|
|
|
} else {
|
2020-03-07 00:51:47 +00:00
|
|
|
|
x0 = trv0.X * scale;
|
|
|
|
|
y0 = trv0.Y * scale;
|
|
|
|
|
x1 = trv1.X * scale;
|
|
|
|
|
y1 = trv1.Y * scale;
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
segs.Add(new LineSeg(x0, y0, x1, y1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return segs;
|
|
|
|
|
}
|
2020-03-07 00:51:47 +00:00
|
|
|
|
|
|
|
|
|
private bool IsVertexVisible(Vertex vert) {
|
|
|
|
|
if (vert.Faces.Count == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
foreach (Face face in vert.Faces) {
|
|
|
|
|
if (face.IsVisible) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsEdgeVisible(Edge edg) {
|
|
|
|
|
if (edg.Faces.Count == 0) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
foreach (Face face in edg.Faces) {
|
|
|
|
|
if (face.IsVisible) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2020-03-04 03:29:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|