2025-11-13 17:13:54 +00:00

90 lines
3.0 KiB
C#

using LearnOpenTK.Common;
using OpenTK.Mathematics;
namespace TheLabs.Shapes;
public class Bus
{
private Mesh _wheelMesh;
private RenderObject _wheelObject;
private Texture _wheelTexture;
private Shader _wheelShader;
private Texture _bodyTexture;
public Bus(Texture texture, Shader shader)
{
_wheelTexture = new Texture("Textures/rubber.jpg");
_wheelShader = shader;
_bodyTexture = new Texture("Textures/rust.png");
}
public void DrawWheel(Matrix4 view, Matrix4 projection, Vector3 position)
{
Matrix4 localTransform = Matrix4.Identity;
//Matrix4 comboTransform = localTransform * _wheelObject.Transform;
_wheelMesh = ShapeFactory.CreateTexturedCylinder();
_wheelObject = new RenderObject(_wheelMesh, _wheelShader, _wheelTexture);
// Front Left Wheel
_wheelObject.Position = new Vector3(position);
_wheelObject.Scale = new Vector3(0.3f, 0.1f, 0.2f);
_wheelObject.Rotation = Quaternion.FromAxisAngle(Vector3.UnitX, MathHelper.DegreesToRadians(90.0f));
_wheelObject.Draw(view, projection);
}
public void drawBody(Matrix4 view, Matrix4 projection)
{
Mesh bodyMesh = ShapeFactory.CreateTexturedCube();
RenderObject bodyObject = new RenderObject(bodyMesh, _wheelShader, _bodyTexture);
bodyObject.Position = new Vector3(0.0f, 0.0f, 0.0f);
bodyObject.Scale = new Vector3(1.5f, 0.5f, 0.5f);
bodyObject.Draw(view, projection);
}
private void drawRoof(Matrix4 view, Matrix4 projection)
{
Mesh roofMesh = ShapeFactory.CreateTexturedCylinder();
RenderObject roofObject = new RenderObject(roofMesh, _wheelShader, _bodyTexture);
roofObject.Position = new Vector3(0.0f, 0.25f, 0.0f);
roofObject.Scale = new Vector3(0.5f, 1.5f, .25f);
roofObject.Rotation = Quaternion.FromAxisAngle(Vector3.UnitY, MathHelper.DegreesToRadians(90.0f));
roofObject.Rotation *= Quaternion.FromAxisAngle(Vector3.UnitX, MathHelper.DegreesToRadians(90.0f));
roofObject.Draw(view, projection);
}
public void DrawBus(Matrix4 view, Matrix4 projection)
{
// Drawing 4 wheels
float xOffset = 0.4f; // Reduced to bring wheels closer to the body
float zOffset = 0.28f; // Reduced to bring front and back wheels closer
float yOffset = -0.3f; // Adjusted to ensure wheels touch the body
// Drawing 4 wheels
for (int i = 0; i < 4; i++)
{
float xPosition = (i % 2 == 0) ? -xOffset : xOffset; // Left or Right
float zPosition = (i < 2) ? zOffset : -zOffset; // Front or Back
DrawWheel(view, projection, new Vector3(xPosition, yOffset, zPosition));
}
// Drawing the body
drawBody(view, projection);
// Drawing the roof
drawRoof(view, projection);
}
}