-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
228 lines (178 loc) · 6.6 KB
/
main.cpp
File metadata and controls
228 lines (178 loc) · 6.6 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <windows.h>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <imgui.h>
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include "menu.h"
#include "camera.h"
#include "keyboard-overlay.h"
const char* vertexShaderSource = R"(
#version 330 core
layout (location = 0) in vec3 aPos;
uniform mat4 MVP;
void main()
{
gl_Position = MVP * vec4(aPos, 1.0);
}
)";
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.2, 0.7, 0.9, 1.0);
}
)";
float cubeVertices[] = {
-0.5f,-0.5f,-0.5f, 0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f,
0.5f, 0.5f,-0.5f, -0.5f, 0.5f,-0.5f, -0.5f,-0.5f,-0.5f,
-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f,-0.5f, 0.5f,
-0.5f, 0.5f, 0.5f, -0.5f, 0.5f,-0.5f, -0.5f,-0.5f,-0.5f,
-0.5f,-0.5f,-0.5f, -0.5f,-0.5f, 0.5f, -0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f,-0.5f,
0.5f,-0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f,
-0.5f,-0.5f,-0.5f, 0.5f,-0.5f,-0.5f, 0.5f,-0.5f, 0.5f,
0.5f,-0.5f, 0.5f, -0.5f,-0.5f, 0.5f, -0.5f,-0.5f,-0.5f,
-0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f, -0.5f, 0.5f, 0.5f, -0.5f, 0.5f,-0.5f
};
const char* windowTitle = "3D Cube with ImGui Controls";
GLuint compileShader(GLenum type, const char* src)
{
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
return shader;
}
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4); // Enable 4x MSAA
GLFWwindow* window = glfwCreateWindow(1280, 720, windowTitle, nullptr, nullptr);
glfwMakeContextCurrent(window);
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE); // Enable multisampling for anti-aliasing
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 330");
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
GLuint shader = glCreateProgram();
glAttachShader(shader, vs);
glAttachShader(shader, fs);
glLinkProgram(shader);
glDeleteShader(vs);
glDeleteShader(fs);
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
float cubeScale = 1.0f;
// FPS counter variables
double lastTime = glfwGetTime();
double lastFrameTime = lastTime;
int frameCount = 0;
float fps = 0.0f;
// FPS limiter variables
const char* fpsOptions[] = { "30", "60", "90", "120", "244" };
int fpsLimits[] = { 30, 60, 90, 120, 244 };
int currentFpsIndex = 1; // Default to 60 FPS
double targetFrameTime = 1.0 / fpsLimits[currentFpsIndex];
// Create camera
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
// Mouse tracking variables
double lastMouseX = 0.0, lastMouseY = 0.0;
bool firstMouse = true;
while (!glfwWindowShouldClose(window))
{
double frameStartTime = glfwGetTime();
float deltaTime = static_cast<float>(frameStartTime - lastFrameTime);
lastFrameTime = frameStartTime;
glfwPollEvents();
// Process camera input
camera.processInput(window, deltaTime);
// Process mouse look (only when left mouse button is held and ImGui is not being used)
ImGuiIO& io = ImGui::GetIO();
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS && !io.WantCaptureMouse)
{
double mouseX, mouseY;
glfwGetCursorPos(window, &mouseX, &mouseY);
if (firstMouse)
{
lastMouseX = mouseX;
lastMouseY = mouseY;
firstMouse = false;
}
float xoffset = static_cast<float>(mouseX - lastMouseX);
float yoffset = static_cast<float>(lastMouseY - mouseY); // Reversed: y-coordinates go from bottom to top
lastMouseX = mouseX;
lastMouseY = mouseY;
camera.processMouseMovement(xoffset, yoffset);
}
else
{
firstMouse = true;
}
// Calculate FPS
frameCount++;
double currentTime = glfwGetTime();
if (currentTime - lastTime >= 1.0)
{
fps = frameCount / (currentTime - lastTime);
frameCount = 0;
lastTime = currentTime;
}
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Render menu
renderMenu(cubeScale, fps, currentFpsIndex, fpsOptions, fpsLimits, targetFrameTime);
// Render keyboard overlay
renderKeyboardOverlay(window);
glClearColor(0.1f, 0.1f, 0.15f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader);
glm::mat4 model = glm::scale(glm::mat4(1.0f), glm::vec3(cubeScale));
glm::mat4 view = camera.getViewMatrix();
glm::mat4 proj = glm::perspective(glm::radians(45.0f), 1280.0f / 720.0f, 0.1f, 100.0f);
glm::mat4 mvp = proj * view * model;
glUniformMatrix4fv(glGetUniformLocation(shader, "MVP"),
1, GL_FALSE, glm::value_ptr(mvp));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 36);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glfwSwapBuffers(window);
// FPS limiting
double frameEndTime = glfwGetTime();
double frameTime = frameEndTime - frameStartTime;
if (frameTime < targetFrameTime)
{
double sleepTime = targetFrameTime - frameTime;
// Use busy-wait for more precise timing
while (glfwGetTime() - frameEndTime < sleepTime) {}
}
}
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
glfwTerminate();
return 0;
}