Anvil Engine
Loading...
Searching...
No Matches
MeshComponent.h
Go to the documentation of this file.
1#pragma once
2#include <glad/glad.h>
3#include "AEntity.h"
4#include "AMesh.h"
5#include "AShader.h"
6#include "IComponent.h"
7#include "AMath.h"
8
10{
11 public:
12 MeshComponent(AMesh* mesh) : m_mesh(mesh)
13 {
14 }
15
19 void OnInit(AEntity* owner) override
20 {
21 m_owner = owner; // Store the owner entity pointer
22 }
23
27 void OnUpdate(float dt) override
28 {
29 // This is a placeholder implementation
30 // The actual update logic should be implemented in derived classes
31 }
32
36 void OnRender(AShader* shader) override
37 {
38 // Early return if mesh or owner is not valid
39 if (!m_mesh || !m_owner)
40 return;
41 // Initialize model matrix as identity matrix
42 glm::mat4 model = glm::mat4(1.0f);
43
44
45 // Apply transformations in order: translation, rotation, scaling
46 model = glm::translate(model, m_owner->position); // Move to owner's position
47 model = glm::rotate(model, glm::radians(m_owner->rotation.y), {0, 1, 0}); // Rotate around Y-axis
48 model = glm::scale(model, m_owner->scale); // Apply scaling
49
50 // Set the model matrix uniform in the shader
51 glUniformMatrix4fv(glGetUniformLocation(shader->GetID(), "model"), 1, GL_FALSE,
52 // Draw the mesh
53 glm::value_ptr(model));
54 m_mesh->Draw();
55 }
56
57 private:
58 AMesh* m_mesh;
59};
float dt
Definition Main.cpp:10
#define ANVIL_API
Definition ACore.h:6
Represents an entity in the game engine with position, rotation, scale and components.
Definition AEntity.h:16
Represents a mesh object in the ANVIL engine with vertices, indices, and texture support.
Definition AMesh.h:19
Definition AShader.h:8
uint32_t GetID() const
Definition AShader.h:15
The IComponent class is an abstract base class that defines the interface for all components in the A...
Definition IComponent.h:12
AEntity * m_owner
Pointer to the entity that owns this component. This allows the component to access its parent entity...
Definition IComponent.h:40
void OnRender(AShader *shader) override
Definition MeshComponent.h:36
void OnInit(AEntity *owner) override
Definition MeshComponent.h:19
void OnUpdate(float dt) override
Definition MeshComponent.h:27
MeshComponent(AMesh *mesh)
Definition MeshComponent.h:12