-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
104 lines (87 loc) · 3.71 KB
/
Copy pathmain.cpp
File metadata and controls
104 lines (87 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//
// Main Entry Point
// Keep this simple - just setup scene and run engine
//
#include "GraphicsEngine.h"
#include "Assets/Scripts/rotator.h"
#include "Assets/Scripts/cameraController.h"
#include <cmath>
int main()
{
// Create scene
Scene scene;
scene.name = "MainScene";
scene.backgroundColor = color(0.1f, 0.1f, 0.15f);
// Create camera
auto camera = scene.createGameObject("MainCamera");
camera->transform.setPosition(vec3(0, 5, 15));
camera->transform.setRotation(vec3(0, M_PI, 0)); // Rotate 180° to look at -Z
camera->addComponent<CameraComponent>();
camera->addComponent<CameraController>();
// Create ground
auto ground = scene.createGameObject("Ground");
ground->addComponent<MeshFilter>()->setMesh(Mesh::createPlane(20, 20));
auto groundRenderer = ground->addComponent<MeshRenderer>();
// Set vertex colors for ground
for (auto& v : ground->getComponent<MeshFilter>()->getMesh()->vertices) {
v.vertexColor = color(0.3f, 0.5f, 0.3f);
}
// Create some objects
for (int x = -3; x <= 3; x++)
{
for (int z = -3; z <= 3; z++)
{
float hue = (float)(x + 3) / 6.0f;
color objColor(
std::abs(std::sin(hue * M_PI)),
std::abs(std::cos(hue * M_PI)),
std::abs(std::sin((hue + 0.5f) * M_PI))
);
auto obj = scene.createGameObject("Cube_" + std::to_string(x) + "_" + std::to_string(z));
obj->transform.setPosition(vec3(x * 3.0f, 1.5f, z * 3.0f));
if ((x + z) % 2 == 0) {
auto meshFilter = obj->addComponent<MeshFilter>();
meshFilter->setMesh(Mesh::createCube());
// Apply tint to cube's existing per-face colors for variety
float tintStrength = 0.6f; // How much of the tint to apply
for (auto& v : meshFilter->getMesh()->vertices) {
// Blend original face color with tint color
v.vertexColor = color(
v.vertexColor.x * (1.0f - tintStrength) + objColor.x * tintStrength,
v.vertexColor.y * (1.0f - tintStrength) + objColor.y * tintStrength,
v.vertexColor.z * (1.0f - tintStrength) + objColor.z * tintStrength
);
}
} else {
auto meshFilter = obj->addComponent<MeshFilter>();
meshFilter->setMesh(Mesh::createSphere(1.0f, 2));
// Spheres get solid color
for (auto& v : meshFilter->getMesh()->vertices) {
v.vertexColor = objColor;
}
}
// Add MeshRenderer component
obj->addComponent<MeshRenderer>();
// Add rotator to some objects
if (x % 2 == 0) {
obj->addComponent<Rotator>();
}
}
}
// Optional: Save scene
// SceneSerializer::saveSceneToAssets(scene, "MainScene");
// Optional: Load scene
// Scene loadedScene = SceneSerializer::loadSceneFromAssets("MainScene");
std::cout << "=== CPP Graphics Engine ===" << std::endl;
std::cout << "Scene: " << scene.name << std::endl;
std::cout << "Objects: " << scene.getAllGameObjects().size() << std::endl;
std::cout << "\nControls:" << std::endl;
std::cout << " WASD - Move" << std::endl;
std::cout << " Space/Shift - Up/Down" << std::endl;
std::cout << " Right Mouse - Look" << std::endl;
std::cout << " Left Ctrl - Sprint" << std::endl;
std::cout << " ESC - Exit\n" << std::endl;
// Run engine
Engine::runOpenGL(scene, 1280, 720, "CPP Graphics Engine");
return 0;
}