How to check if device has been rotated on all axis in Unity - c#

I want to check in Unity if the device has been rotated on all of it's axis.
So, I am reading the rotation of all the axis.
What should I do in order to validate for example that the user has "flipped" his device over the X-axis? I need to check the value, and see that they contain 0, 90, 180 and 270 degrees in a loop.
Here is part of my code:
void Update () {
float X = Input.acceleration.x;
float Y = Input.acceleration.y;
float Z = Input.acceleration.z;
xText.text = ((Mathf.Atan2(Y, Z) * 180 / Mathf.PI)+180).ToString();
yText.text = ((Mathf.Atan2(X, Z) * 180 / Mathf.PI)+180).ToString();
zText.text = ((Mathf.Atan2(X, Y) * 180 / Mathf.PI)+180).ToString();
}

The accelerometer only tells you if the acceleration of the device changes. So you will have values if the device started moving, or stopped moving. You can't retrieve its orientation from that.
Instead you need to use the gyroscope of the device. Most device have one nowadays.
Fortunately, Unity supports the gyroscope through the Gyroscope class
Simply using
Input.gyro.attitude
Will give you the orientation of the device in space, in the form of a quaternion.
To check the angles, use the eulerAngles function, for instance, is the device flipped in the x axis:
Vector3 angles = Input.gyro.attitude.eulerAngles;
bool xFlipped = angles.x > 180;
Be careful, you might have to invert some values if you want to apply the rotation in Unity (because it depend which orientation the devices uses for positive values, left or right)
// The Gyroscope is right-handed. Unity is left handed.
// Make the necessary change to the camera.
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
Here is the full example from the doc (Unity version 2017.3), in case the link above is broken. It shows how to read value from the gyroscope, and apply them to an object in Unity.
// Create a cube with camera vector names on the faces.
// Allow the device to show named faces as it is oriented.
using UnityEngine;
public class ExampleScript : MonoBehaviour
{
// Faces for 6 sides of the cube
private GameObject[] quads = new GameObject[6];
// Textures for each quad, should be +X, +Y etc
// with appropriate colors, red, green, blue, etc
public Texture[] labels;
void Start()
{
// make camera solid colour and based at the origin
GetComponent<Camera>().backgroundColor = new Color(49.0f / 255.0f, 77.0f / 255.0f, 121.0f / 255.0f);
GetComponent<Camera>().transform.position = new Vector3(0, 0, 0);
GetComponent<Camera>().clearFlags = CameraClearFlags.SolidColor;
// create the six quads forming the sides of a cube
GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
quads[0] = createQuad(quad, new Vector3(1, 0, 0), new Vector3(0, 90, 0), "plus x",
new Color(0.90f, 0.10f, 0.10f, 1), labels[0]);
quads[1] = createQuad(quad, new Vector3(0, 1, 0), new Vector3(-90, 0, 0), "plus y",
new Color(0.10f, 0.90f, 0.10f, 1), labels[1]);
quads[2] = createQuad(quad, new Vector3(0, 0, 1), new Vector3(0, 0, 0), "plus z",
new Color(0.10f, 0.10f, 0.90f, 1), labels[2]);
quads[3] = createQuad(quad, new Vector3(-1, 0, 0), new Vector3(0, -90, 0), "neg x",
new Color(0.90f, 0.50f, 0.50f, 1), labels[3]);
quads[4] = createQuad(quad, new Vector3(0, -1, 0), new Vector3(90, 0, 0), "neg y",
new Color(0.50f, 0.90f, 0.50f, 1), labels[4]);
quads[5] = createQuad(quad, new Vector3(0, 0, -1), new Vector3(0, 180, 0), "neg z",
new Color(0.50f, 0.50f, 0.90f, 1), labels[5]);
GameObject.Destroy(quad);
}
// make a quad for one side of the cube
GameObject createQuad(GameObject quad, Vector3 pos, Vector3 rot, string name, Color col, Texture t)
{
Quaternion quat = Quaternion.Euler(rot);
GameObject GO = Instantiate(quad, pos, quat);
GO.name = name;
GO.GetComponent<Renderer>().material.color = col;
GO.GetComponent<Renderer>().material.mainTexture = t;
GO.transform.localScale += new Vector3(0.25f, 0.25f, 0.25f);
return GO;
}
protected void Update()
{
GyroModifyCamera();
}
protected void OnGUI()
{
GUI.skin.label.fontSize = Screen.width / 40;
GUILayout.Label("Orientation: " + Screen.orientation);
GUILayout.Label("input.gyro.attitude: " + Input.gyro.attitude);
GUILayout.Label("iphone width/font: " + Screen.width + " : " + GUI.skin.label.fontSize);
}
/********************************************/
// The Gyroscope is right-handed. Unity is left handed.
// Make the necessary change to the camera.
void GyroModifyCamera()
{
transform.rotation = GyroToUnity(Input.gyro.attitude);
}
private static Quaternion GyroToUnity(Quaternion q)
{
return new Quaternion(q.x, q.y, -q.z, -q.w);
}
}

Related

3D camera rotates the object, but not the camera in space. Implementation problem

I'm learning how to implement the 3D spaces. Now i want implement 3D camera for moving around my designed 3D space.
The problem is that camera rotate the 3D objects. But not the camera in space as need to be. I can not understand what i'm doing wrong.
Code of the Camera class:
public class Camera3D
{
public Vector3D Pos { get; set; }
public Vector3D Target { get; set; }
public Vector3D Up { get; set; }
public double CameraSpeed { get; set; }
public Camera3D(Vector3D pos, Vector3D target, Vector3D up)
{
Pos = pos;
Target = target;
Up = up;
Pos.Normalize();
Target.Normalize();
Up.Normalize();
}
public Camera3D()
{
Pos = new Vector3D(0, 0, 3);
Target = new Vector3D(0, 0, 0);
Up = new Vector3D(0, 1, 0);
}
private Vector3D GetCameraDirection(Vector3D pos, Vector3D target)
{
Vector3D dir = pos - target;
dir.Normalize();
return dir;
}
private Vector3D GetRight(Vector3D pos, Vector3D target, Vector3D up)
{
Vector3D right = Vector3D.CrossProduct(Up, GetCameraDirection(pos, up));
right.Normalize();
return right;
}
private Vector3D GetCameraUp(Vector3D pos, Vector3D target, Vector3D up)
{
Vector3D vup = Vector3D.CrossProduct(GetCameraDirection(pos, target), GetRight(pos, target, up));
return vup;
}
public Matrix3D LookAt(Vector3D pos, Vector3D target, Vector3D up)
{
// translation matrix for camera
Matrix3D translation = new Matrix3D
(
1, 0, 0, -Pos.X,
0, 1, 0, -Pos.Y,
0, 0, 1, -Pos.Z,
0, 0, 0, 1
);
Vector3D vRight = GetRight(pos, target, up);
Vector3D vUp = GetCameraUp(pos, target, up);
Vector3D vDir = GetCameraDirection(pos, target);
Matrix3D m = new Matrix3D
(
vRight.X, vRight.Y, vRight.Z, 0,
vUp.X, vUp.Y, vUp.Z, 0,
vDir.X, vDir.Y, vDir.Z, 0,
0, 0, 0, 1
);
return m * translation;
}
}
Some useful fields and constructor of main window
// just temporary field for test rotating Camera
double a = 0;
Vector3D Translate = new Vector3D(1, 1, 1);
Vector3D Rotation = new Vector3D(0, 0, 0);
Vector3D Scaling = new Vector3D(1, 1, 1);
CTransform3D cTransform; // class for transform operations
Camera3D c = new Camera3D();
public MainWindow()
{
InitializeComponent();
cTransform = new CTransform3D(Rotation, Translate, Scaling);
cTransform.Camera = c; // camera for transformations
// load the mesh
o.Mesh.LoadFromObjectFile("D:\\de\\test\\ship.obj");
}
The main method that draw objects(here is all transformations, projection, and camera transformations too):
public void Draw()
{
try
{
// util classes
MatrixUtil mu = new MatrixUtil();
cv.Children.Clear(); // clear canvas
foreach (var triangle in o.Mesh.Triangles)
{
Triangle tProjected = new Triangle(), tTransformed = new Triangle(), tViewed = new Triangle();
// just init for right compile
tProjected.Points = new Vector3D[3] { new Vector3D(0, 0, 0), new Vector3D(0, 0, 0), new Vector3D(0, 0, 0) };
tTransformed.Points = new Vector3D[3] { new Vector3D(0, 0, 0), new Vector3D(0, 0, 0), new Vector3D(0, 0, 0) };
tViewed.Points = new Vector3D[3] { new Vector3D(0, 0, 0), new Vector3D(0, 0, 0), new Vector3D(0, 0, 0) };
// project and transform matrices
Matrix3D ProjectionMatrix = cTransform.ProjectionMatrix(5, Width, Height, 0.1, 1000); // just projection matrix
Matrix3D TransformMatrix = RotationMatrices.RotatationYMatrix(Rotation.Y); // rotate y
Matrix3D CameraMatrix = cTransform.GetCameraTransform(); // camera matrix that contains only LookAt
c.Pos = new Vector3D(Math.Sin(a), 0, Math.Cos(a)); // change position of camera
// transform points
tTransformed.Points[0] = mu.Matrix_MulVector(TransformMatrix, triangle.Points[0]);
tTransformed.Points[1] = mu.Matrix_MulVector(TransformMatrix, triangle.Points[1]);
tTransformed.Points[2] = mu.Matrix_MulVector(TransformMatrix, triangle.Points[2]);
// multiple camera matrix to transformed points
tViewed.Points[0] = mu.Matrix_MulVector(CameraMatrix, tTransformed.Points[0]);
tViewed.Points[1] = mu.Matrix_MulVector(CameraMatrix, tTransformed.Points[1]);
tViewed.Points[2] = mu.Matrix_MulVector(CameraMatrix, tTransformed.Points[2]);
// project the object
tProjected.Points[0] = mu.Matrix_MulVector(ProjectionMatrix, tViewed.Points[0]);
tProjected.Points[1] = mu.Matrix_MulVector(ProjectionMatrix, tViewed.Points[1]);
tProjected.Points[2] = mu.Matrix_MulVector(ProjectionMatrix, tViewed.Points[2]);
// just draw the result
new CTriangle(
triangle.Points[0].X + o.Position.X, triangle.Points[0].Y + o.Position.Y ,
triangle.Points[1].X + o.Position.X, triangle.Points[1].Y + o.Position.Y ,
triangle.Points[2].X + o.Position.X, triangle.Points[2].Y + o.Position.Y ,
cv, Brushes.Gray, Brushes.Black);
}
a+= .1;
Rotation.X += 0.05f;
Rotation.Y += 0.05f;
Rotation.Z += 0.05f;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Projection matrix method from CTransform class:
public Matrix3D ProjectionMatrix(double fov, double width, double height, double near, double far)
{
int fAspectRatio = (int)(width / height);
double fFovRad = 1.0f / Math.Tan(fov * 0.5f / 180.0f * 3.14159f);
return new Matrix3D
(
fAspectRatio * fFovRad, 0, 0, 0,
0, fFovRad, 0, 0,
0, 0, far / (far - near), 1,
0, 0, (-far * near) / (far - near), 1
);
}
GetCameraTransform() Method from CTransform class:
public Matrix3D GetCameraTransform()
{
Matrix3D cameraLookAt = Camera.LookAt(camera.Pos, camera.Target, camera.Up);
return cameraLookAt;
}
Fields of camera from CTransform class:
private Camera3D camera;
public Camera3D Camera { get { return camera; } set { camera = value; } }
Multiple Matrix to Vector method from MatrixUtil class for transformations about points of triangle:
public Vector3D Matrix_MulVector(Matrix3D m, Vector3D v)
{
Vector3D res = new Vector3D();
// additional W component of vector usually will 1. So just add offsets
res.X = v.X * m.M11 + v.Y * m.M21 + v.Z * m.M31 + m.OffsetX;
res.Y = v.X * m.M12 + v.Y * m.M22 + v.Z * m.M32 + m.OffsetY;
res.Z = v.X * m.M13 + v.Y * m.M23 + v.Z * m.M33 + m.OffsetZ;
return res;
}
I know its hard to understand all that mess. But i need help in understanding how the 3D camera works in 3D graphic. And how to implement it in my designed 3D space.
Tried change the order of multiplication matrix and triangles in all pipeline. Got more troubles.
Change the translation camera matrix. Result: nothing.
Change the LookAt method. Result: nothing.
Checked and tried to change some other matrices about transform the object. Result: nothing.

Meshes created from code don't have a position unity

So, I tried to create a grid so that I can instantiate objects on it. I check for the position of said hit object (one of the squares I created) and then set the instantiated object to that position. Problem is, the squares I created with code don't have a position and are all set to 0, 0, 0.
{
GameObject tileObject = new GameObject(string.Format("{0}, {1}", x, y));
tileObject.transform.parent = transform;
Mesh mesh = new Mesh();
tileObject.AddComponent<MeshFilter>().mesh = mesh;
tileObject.AddComponent<MeshRenderer>().material = tileMaterial;
Vector3[] vertices = new Vector3[4];
vertices[0] = new Vector3(x * tileSize, 0, y * tileSize);
vertices[1] = new Vector3(x * tileSize, 0, (y +1) * tileSize);
vertices[2] = new Vector3((x +1) * tileSize, 0, y * tileSize);
vertices[3] = new Vector3((x +1) * tileSize, 0, (y +1) * tileSize);
int[] tris = new int[] { 0, 1, 2, 1, 3, 2 };
mesh.vertices = vertices;
mesh.triangles = tris;
mesh.RecalculateNormals();
tileObject.layer = LayerMask.NameToLayer("Tile");
tileObject.AddComponent<BoxCollider>();
//var xPos = Mathf.Round(x);
//var yPos = Mathf.Round(y);
//tileObject.gameObject.transform.position = new Vector3(xPos , 0f, yPos);
return tileObject;
}```
As said your issue is that you leave all tiles on the position 0,0,0 and only set their vertices to the desired world space positions.
You would rather want to keep your vertices local like e.g.
// I would use the offset of -0.5f so the mesh is centered at the transform pivot
// Also no need to recreate the arrays everytime, you can simply reference the same ones
private readonly Vector3[] vertices = new Vector3[4]
{
new Vector3(-0.5f, 0, -0.5f);
new Vector3(-0.5f, 0, 0.5f);
new Vector3(0.5f, 0, -0.5f);
new Vector3(0.5f, 0, 0.5f);
};
private readonly int[] tris = new int[] { 0, 1, 2, 1, 3, 2 };
and then in your method do
GameObject tileObject = new GameObject($"{x},{y}");
tileObject.transform.parent = transform;
tileObject.localScale = new Vector3 (tileSize, 1, tileSize);
tileObject.localPosition = new Vector3(x * tileSize, 0, y * tileSize);
The latter depends of course on your needs. Actually I would prefer to have the tiles also centered around the grid object so something like e.g.
// The "-0.5f" is for centering the tile itself correctly
// The "-gridWith/2f" makes the entire grid centered around the parent
tileObject.localPosition = new Vector3((x - 0.5f - gridWidth/2f) * tileSize, 0, (y - 0.5f - gridHeight/2f) * tileSize);
In order to later find out which tile you are standing on (e.g. via raycasts, collisions, etc) I would then rather use a dedicated component and simply tell it it's coordinates like e.g.
// Note that Tile is a built-in type so you would want to avoid confusion
public class MyTile : MonoBehaviour
{
public Vector2Int GridPosition;
}
and then while generating your grid you would simply add
var tile = tileObject.AddComponent<MyTile>();
tile.GridPosition = new Vector2Int(x,y);
while you can still also access its transform.position to get the actual world space center of the tiles

Unity: Rotate gizmo with an offset of 45

I am drawing two wireframe spheres that I would like to follow the player around. When the player moves the two gizmos follow, however, when I rotate only one of the gizmos rotates.
The broken gizmo code looks like this, it should have an offset of 45:
void OnDrawGizmosSelected() {
Gizmos.color = new Color(1, 0, 0);
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
For reference here is the whole block with both gizmos:
void OnDrawGizmosSelected() {
Gizmos.color = new Color(1, 0, 0);
// This one works
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
// This one does not work
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.x, transform.rotation.y + 45, transform.rotation.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
Default with no rotation (How I want it to stay when rotating)
Rotation around the Y Axis
Quaternion has 4 components, x,y,z and w.
Just putting x,y and z into Quaternion.Euler will not give you the expected results.
Instead, use transform.rotation.eulerAngles
void OnDrawGizmosSelected()
{
Gizmos.color = new Color(1, 0, 0);
// This one works
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
// This one works now :)
Gizmos.matrix = Matrix4x4.TRS(transform.position, Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 45, transform.rotation.eulerAngles.z), Vector3.one);
Gizmos.DrawWireSphere(Vector3.zero, 5f);
}
EDIT:
Okay, that fixes the Y value, but X and Z are still broken. They move but not in the proper direction.
Then try
// This works even better
Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one) * Matrix4x4.Rotate(Quaternion.Euler(0, 45, 0));

Helix toolkit, getting a new rotation vector

I have a 6 axis robot.
I am trying to get each axis to move independently. The robots axis all move correctly when axis one is at its origin angle, but when it has been modified all other axis move incorrectly. They retain the correct point of rotation just not the new vector.
The Skewed axis
I followed this tutorial http://www.barth-dev.de/getting-started-3d-wpf/
//moves axis_1
void move_axis_1(double angle)
{
//rotate the object by "angle", the vector describes the axis
RotateTransform3D axis_1_transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), angle));
//tells where the point of rotation is
axis_1_transform.CenterX = 77.5;
axis_1_transform.CenterY = 21.5;
axis_1_transform.CenterZ = -8;
//apply transformation
axis_1.Transform = axis_1_transform;
//also move the axis 2
move_axis_2(axis_2_angle);
}
//moves axis_2
void move_axis_2(double angle)
{
//new group of transformations, the group will "add" movements
var Group_3D = new Transform3DGroup();
Group_3D.Children.Add(axis_1.Transform);
//we need to find out where our old point is now
Point3D origin = Group_3D.Transform(new Point3D(84.6, 21, -2));
//create new transformation
RotateTransform3D axis_2_transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), angle));
axis_2_transform.CenterX = origin.X;
axis_2_transform.CenterY = origin.Y;
axis_2_transform.CenterZ = origin.Z;
//add it to the transformation group (and therefore to axis_1 movement
Group_3D.Children.Add(axis_2_transform);
//Apply the transform
axis_2.Transform = Group_3D;
//also move
move_axis_3(axis_3_angle);
}
//moves axis_3
void move_axis_3(double angle)
{
//new group of transformations, the group will "add" movements
var Group_3D = new Transform3DGroup();
Group_3D.Children.Add(axis_2.Transform);
//we need to find out where our old point is now
Point3D origin = Group_3D.Transform(new Point3D(84.6, 17, 16.2));
//create new transformation
RotateTransform3D axis_3_transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 1, 0), angle));
axis_3_transform.CenterX = origin.X;
axis_3_transform.CenterY = origin.Y;
axis_3_transform.CenterZ = origin.Z;
//add it to the transformation group
Group_3D.Children.Add(axis_3_transform);
//Apply the transform
axis_3.Transform = Group_3D;
//also move
move_axis_4(axis_4_angle);
}
U'll have to transform the Vector as well.
//we need to find out where our old point is now
Point3D origin = Group_3D.Transform(new Point3D(84.6, 21, -2));
//we also need to find out where our old vector is pointing now
Vector3D vector = Group_3D.Transform(new Vector3D(0, 1, 0));
//create new transformation
RotateTransform3D axis_2_transform = new RotateTransform3D(new AxisAngleRotation3D(vector, angle));
axis_2_transform.CenterX = origin.X;
axis_2_transform.CenterY = origin.Y;
axis_2_transform.CenterZ = origin.Z;
I also prefer the overload with the rotation center. Then you don't need to set the center afterwards. But thats just my opinion:
//create new transformation
RotateTransform3D axis_2_transform =
new RotateTransform3D(new AxisAngleRotation3D(vector, angle), origin);

Applying modeling matrix to view matrix = failure

I've got a problem with moving and rotating objects in OpenGL. I'm using C# and OpenTK (Mono), but I guess the problem is with me not understanding the OpenGL part, so you might be able to help me even if you don't know anything about C# / OpenTK.
I'm reading the OpenGL SuperBible (latest edition) and I tried to rewrite the GLFrame in C#. Here is the part I've already rewritten:
public class GameObject
{
protected Vector3 vLocation;
public Vector3 vUp;
protected Vector3 vForward;
public GameObject(float x, float y, float z)
{
vLocation = new Vector3(x, y, z);
vUp = Vector3.UnitY;
vForward = Vector3.UnitZ;
}
public Matrix4 GetMatrix(bool rotationOnly = false)
{
Matrix4 matrix;
Vector3 vXAxis;
Vector3.Cross(ref vUp, ref vForward, out vXAxis);
matrix = new Matrix4();
matrix.Row0 = new Vector4(vXAxis.X, vUp.X, vForward.X, vLocation.X);
matrix.Row1 = new Vector4(vXAxis.Y, vUp.Y, vForward.Y, vLocation.Y);
matrix.Row2 = new Vector4(vXAxis.Z, vUp.Z, vForward.Z, vLocation.Z);
matrix.Row3 = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
return matrix;
}
public void Move(float x, float y, float z)
{
vLocation = new Vector3(x, y, z);
}
public void RotateLocalZ(float angle)
{
Matrix4 rotMat;
// Just Rotate around the up vector
// Create a rotation matrix around my Up (Y) vector
rotMat = Matrix4.CreateFromAxisAngle(vForward, angle);
Vector3 newVect;
// Rotate forward pointing vector (inlined 3x3 transform)
newVect.X = rotMat.M11 * vUp.X + rotMat.M12 * vUp.Y + rotMat.M13 * vUp.Z;
newVect.Y = rotMat.M21 * vUp.X + rotMat.M22 * vUp.Y + rotMat.M23 * vUp.Z;
newVect.Z = rotMat.M31 * vUp.X + rotMat.M32 * vUp.Y + rotMat.M33 * vUp.Z;
vUp = newVect;
}
}
So I create a new GameObject (GLFrame) on some random coordinates: GameObject go = new GameObject(0, 0, 5); and rotate it a bit: go.RotateLocalZ(rotZ);. Then I get the matrix using Matrix4 matrix = go.GetMatrix(); and render frame (first, I set the viewing matrix and then I multiply it with modeling matrix)
protected override void OnRenderFrame(FrameEventArgs e)
{
base.OnRenderFrame(e);
this.Title = "FPS: " + (1 / e.Time).ToString("0.0");
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
Matrix4 modelmatrix = go.GetMatrix();
Matrix4 viewmatrix = Matrix4.LookAt(new Vector3(5, 5, -10), Vector3.Zero, Vector3.UnitY);
GL.LoadMatrix(ref viewmatrix);
GL.MultMatrix(ref modelmatrix);
DrawCube(new float[] { 0.5f, 0.4f, 0.5f, 0.8f });
SwapBuffers();
}
The DrawCube(float[] color) is my own method for drawing a cube.
Now the most important part: If I render the frame without the GL.MultMatrix(ref matrix); part, but using GL.Translate() and GL.Rotate(), it works (second screenshot). However, if I don't use these two methods and I pass the modeling matrix directly to OpenGL using GL.MultMatrix(), it draws something strange (first screenshot).
Can you help me and explain me where is the problem? Why does it work using translate and rotate methods, but not with multiplying the view matrix by the modeling matrix?
OpenGL transformation matrices are ordered column wise. You should use the transpose of the matrix you are using.

Categories

Resources