Feat: Better noise overall and new FPS counter

This commit is contained in:
2025-09-29 20:20:44 +01:00
parent fcb28671c2
commit a96657e40c
6 changed files with 165 additions and 58 deletions
+65 -20
View File
@@ -106,9 +106,9 @@ int main() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // REQUIRED on macOS
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 16); // 16x MSAA
glfwWindowHint(GLFW_SAMPLES, 8); // 8x MSAA
GLFWwindow* window = glfwCreateWindow((int)SCR_WIDTH, (int)SCR_HEIGHT, "OpenGL Template", nullptr, nullptr);
GLFWwindow* window = glfwCreateWindow((int)SCR_WIDTH, (int)SCR_HEIGHT, "RASTER", nullptr, nullptr);
if (!window) {
glfwTerminate();
return -1;
@@ -196,44 +196,89 @@ int main() {
glUniform2f(resLoc, SCR_WIDTH, SCR_HEIGHT); // hardcode for now
GLint timeLoc = glGetUniformLocation(shaderProgram, "u_time");
GLint directionLoc = glGetUniformLocation(shaderProgram, "u_direction");
GLint oscilationLoc = glGetUniformLocation(shaderProgram, "u_oscilator");
GLint scaleLoc = glGetUniformLocation(shaderProgram, "u_scale");
GLint mouseLoc = glGetUniformLocation(shaderProgram, "u_mouse");
glEnable(GL_MULTISAMPLE);
float oscillation = 5.0f;
float scale = 1.0f;
int frameCount = 0;
float mousePos[2] = {0.0f, 0.0f};
// FPS tracking
double lastTime = glfwGetTime(); // when we last printed FPS
double fps = 0.0;
// Render loop
while (!glfwWindowShouldClose(window)) {
static float direction = 1.0f;
static int frameCount = 0;
static unsigned char invert = 0;
float timeValue = (float)glfwGetTime();
double xpos, ypos;
// Get mouse positions
glfwGetCursorPos(window, &xpos, &ypos);
mousePos[0] = (float)xpos;
mousePos[1] = (float)(SCR_HEIGHT - ypos); // Invert y-axis for OpenGL coordinates
// Input
glfwPollEvents();
if (frameCount++ % 24 == 0) {
if (oscillation > 40) invert = 1;
if (oscillation < 5) invert = 0;
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
glDeleteProgram(shaderProgram);
shaderProgram = createShaderProgram(VERTEX_FILE, FRAGMENT_FILE);
modelLoc = (unsigned int) glGetUniformLocation(shaderProgram, "model");
viewLoc = (unsigned int) glGetUniformLocation(shaderProgram, "view");
projLoc = (unsigned int) glGetUniformLocation(shaderProgram, "projection");
if (invert) oscillation -= 0.01f;
else oscillation += 0.001f;
glUseProgram(shaderProgram);
glUniformMatrix4fv((int) modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniformMatrix4fv((int) viewLoc, 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv((int) projLoc, 1, GL_FALSE, glm::value_ptr(projection));
// Debug
// std::cout << "Oscillation: " << oscillation << "\t";
// std::cout << "Invert: " << invert << std::endl;
resLoc = glGetUniformLocation(shaderProgram, "u_resolution");
glUniform2f(resLoc, SCR_WIDTH, SCR_HEIGHT); // hardcode for now
direction = rand() % 50; // change direction every 300 frames
timeLoc = glGetUniformLocation(shaderProgram, "u_time");
scaleLoc = glGetUniformLocation(shaderProgram, "u_scale");
std::cout << "Shaders recompiled!\n";
}
// Scroll to zoom in/out
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
scale += 0.01f;
if (scale > 10.0f) scale = 10.0f; // Max zoom
}
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
scale -= 0.01f;
if (scale < 0.1f) scale = 0.1f; // Min zoom
}
// --- FPS Counter ---
double currentTime = glfwGetTime();
double delta = currentTime - lastTime;
if (delta >= 1.0) { // print every ~1 second
fps = double(frameCount) / delta;
std::cout << "FPS: " << fps
<< " Time: " << timeValue
<< " Scale: " << scale
<< " Mouse: (" << mousePos[0] << ", " << mousePos[1] << ")"
<< std::endl;
frameCount = 0;
lastTime = currentTime;
}
glUniform1f(timeLoc, timeValue);
glUniform1f(directionLoc, direction);
glUniform1f(oscilationLoc, oscillation);
glUniform1f(scaleLoc, scale);
glUniform2f(mouseLoc, mousePos[0], mousePos[1]);
frameCount++;
glClearColor(0.01f, 0.01f, 0.01f, 1.0f);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);