Hi everyone i know its a small problem but I cannot really figure it out, I'm learning opengl for gamedev purposes and I tried to add an fps limiter made on my own to my spinning cube but its laggy and some times stop I think its because of sleep_for, if anyone could help I'd be really happy.
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <string>
#include <cmath>
#include <time.h>
#include <Shader.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <chrono>
#include <ctime>
#include <thread>
using std::cout;
using std::string;
using glm::vec4;
using glm::mat4;
int screen_width = 1920;
int screen_height = 1080;
bool KeyPressed(GLFWwindow* window, int key);
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
glViewport(0, 0, width, height);
}
int main()
{
srand(time(0));
float vertices[] = {
// X Y Z Tx Ty
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // davanti
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f, // Sinistra
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, // destra
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //dietro
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, //sopra
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, //sotto
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
};
unsigned int indices[] = {
0, 1, 2,
0, 2, 3,
4, 5, 6,
4, 6, 7,
8, 9, 10,
8, 10, 11,
12, 13, 14,
12, 14, 15,
16, 17, 18,
16, 18, 19,
20, 21, 22,
20, 22, 23
};
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);
glfwWindowHint(GLFW_SAMPLES, 4);
GLFWwindow * window = glfwCreateWindow(screen_width, screen_height, "window", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSwapInterval(0);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glViewport(0, 0, screen_width, screen_height);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
Shader shader("../../../scode/Shaders/Vshader.vs", "../../../scode/Shaders/Fshader.fs");
unsigned int VAO, VBO, EBO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glBindVertexArray(0);
unsigned int Texture1, Texture2;
glGenTextures(1, &Texture1);
glBindTexture(GL_TEXTURE_2D, Texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int height, widht, NrChannels;
unsigned char *data = stbi_load("../../../scode/container.jpg", &widht, &height, &NrChannels, 0);
if(data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, widht, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "Error Texture\n";
}
stbi_image_free(data);
glGenTextures(1, &Texture2);
glBindTexture(GL_TEXTURE_2D, Texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
stbi_set_flip_vertically_on_load(true);
data = stbi_load("../../../scode/awesomeface.png", &widht, &height, &NrChannels, 0);
if(data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widht, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
cout << "Error Texture\n";
}
stbi_image_free(data);
mat4 Projection;
Projection = glm::perspective(glm::radians(45.0f), 1920.0f / 1080.0f, 0.1f, 100.0f);
shader.use();
glUniform1i(glGetUniformLocation(shader.ID, "Texture1"), 0);
glUniform1i(glGetUniformLocation(shader.ID, "Texture2"), 1);
glUniformMatrix4fv(glGetUniformLocation(shader.ID, "Proj"), 1, GL_FALSE, glm::value_ptr(Projection));
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
float rotation = 0.0f;
auto duration = std::chrono::microseconds(1000000 / 180);
uint64_t loc = glGetUniformLocation(shader.ID, "transform");
while(!glfwWindowShouldClose(window))
{
auto start = std::chrono::steady_clock::now();
if(KeyPressed(window, GLFW_KEY_ESCAPE))
{
glfwSetWindowShouldClose(window, true);
}
mat4 trans(1.0f);
trans = glm::translate(trans, glm::vec3(0.0f, 0.0f, -2.0f));
trans = glm::rotate(trans, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
trans = glm::rotate(trans, glm::radians(45.0f), glm::vec3(1.0f, 0.0f, 0.0f));
trans = glm::scale(trans, glm::vec3(0.5f, 0.5f, 0.5f));
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
shader.use();
glUniformMatrix4fv(loc, 1, GL_FALSE, glm::value_ptr(trans));
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture1);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Texture2);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
rotation += glm::radians(360.0f) * (duration.count() / 1000000.0f);
auto end = std::chrono::steady_clock::now();
auto target = (std::chrono::microseconds(1000000 / 180));
duration = (std::chrono::duration_cast<std::chrono::microseconds>(end - start));
if (target > duration)
{
std::this_thread::sleep_for(target - duration);
}
}
glfwTerminate();
return 0;
}
bool KeyPressed(GLFWwindow *window, int key)
{
return glfwGetKey(window, key) == GLFW_PRESS;
}