added bus

This commit is contained in:
Zyb3rWolfi 2025-11-13 17:13:54 +00:00
parent 4e7875bed3
commit cf8eb3f5fd
4 changed files with 104 additions and 21 deletions

View File

@ -31,6 +31,7 @@ namespace TheLabs
private RenderObject _cubeObject2;
private RenderObject _circleObject;
private RenderObject _cylinderObject;
private Bus _bus;
private Vector3 _cameraPosition = new Vector3(0.0f, 0.0f, -3.0f);
private Vector3 _cameraFront = new Vector3(0.0f, 0.0f, -1.0f);
@ -65,23 +66,16 @@ namespace TheLabs
GL.ClearColor(0.2f, 0.3f, 0.3f, 1.0f);
GL.Enable(EnableCap.DepthTest);
_camera = new Camera(_cameraPosition, _cameraFront, _cameraUp);
CursorState = CursorState.Grabbed;
_shader = new Shader("Shaders/shader.vert", "Shaders/shader.frag");
_cubeMesh = ShapeFactory.CreateTexturedCube();
_circleMesh = ShapeFactory.CreateTexturedCircle();
_cylinderMesh = ShapeFactory.CreateTexturedCylinder();
_cubeTexture = new Texture("Textures/stone.jpg");
_cubeTexture = new Texture("Textures/placeholder.png");
_cubeTexture2 = new Texture("Textures/placeholder.png");
_cubeObject = new RenderObject(_cubeMesh, _shader, _cubeTexture);
_cubeObject2 = new RenderObject(_cubeMesh, _shader, _cubeTexture2);
_circleObject = new RenderObject(_circleMesh, _shader, _cubeTexture);
_cylinderObject = new RenderObject(_cylinderMesh, _shader, _cubeTexture2);
_cylinderObject.Position = new Vector3(-2.5f, 0.0f, 0.0f);
_cubeObject.Position = new Vector3(-1.5f, 0.0f, 0.0f);
_cubeObject2.Position = new Vector3(1.5f, 0.0f, 0.0f);
_circleObject.Position = new Vector3(2.5f, 0.0f, 0.0f);
_cubeObject.Scale = new Vector3(0.5f, 0.5f, 0.5f);
_cubeObject2.Scale = new Vector3(0.5f, 0.5f, 0.5f);
_bus = new Bus(_cubeTexture, _shader);
}
@ -93,8 +87,8 @@ namespace TheLabs
base.OnRenderFrame(e);
// Clear the color buffer and the depth buffer
GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
_cubeObject.Rotation = Quaternion.FromEulerAngles(_rotation * 0.5f, _rotation, 0); //
_cubeObject2.Rotation = Quaternion.FromEulerAngles(-_rotation * 0.5f, -_rotation, 0);
//_cubeObject.Rotation = Quaternion.FromEulerAngles(_rotation * 0.5f, _rotation, 0); //
//_cubeObject2.Rotation = Quaternion.FromEulerAngles(-_rotation * 0.5f, -_rotation, 0);
// --- Set up Camera ---
Matrix4 view = _camera.LookAt();
@ -112,10 +106,8 @@ namespace TheLabs
100.0f
);
_cubeObject.Draw(view, projection);
_cubeObject2.Draw(view, projection);
_circleObject.Draw(view, projection);
_cylinderObject.Draw(view, projection);
// --- Draw Scene Objects ---
_bus.DrawBus(view, projection);
SwapBuffers();
}

View File

@ -10,7 +10,7 @@ namespace TheLabs
{
var nativeWindowSettings = new NativeWindowSettings
{
Size = new Vector2i(800, 600),
Size = new Vector2i(1920, 1080),
Title = "My OpenTK Example Program"
};
MyVector myVector = new MyVector(30, 40, 0);

View File

@ -11,6 +11,7 @@ public class RenderObject
public Texture Texture;
public Vector3 Position = Vector3.Zero;
public Matrix4 Transform = Matrix4.Identity;
public Quaternion Rotation = Quaternion.Identity;
public Vector3 Scale = Vector3.One;

View File

@ -0,0 +1,90 @@
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);
}
}